I am struggling with the fetch API and the implementation of flowjs inside a react-native application.
Using Nuclide, I can see that the fetch function I use to get data from a remote API is not covered by Flow...
Yes, it's no big deal if I get 99% of my code monitored but still. I want to understand what's going on.
I have installed flow-typed. I have create a stub for my fetch, I can see it in flow-typed/npm/fbjs_vx.x.x.x and here it is :
declare module 'fbjs/lib/fetch' {
declare module.exports: any;
}
I also try to download alternative ponyfill or polyfill in order to get that line covered without any success.
If no libdef already exist, how can I say "fetch take a string as argument and return a promise" to flow ?
Thanks for help
UPDATE
Here is the package.json
{
"name": "BeerOmatic",
"version": "0.0.1",
"private": true,
"scripts": {
"start": "node node_modules/react-native/local-cli/cli.js start",
"test": "jest",
"flow": "flow",
"android": "ENVFILE=.env.dev react-native run-android"
},
"dependencies": {
"eslint-config-airbnb-flow": "^1.0.2",
"eslint-plugin-flowtype": "^2.35.0",
"fetch-ponyfill": "^4.1.0",
"flow-typed": "^2.1.2",
"react": "16.0.0-alpha.12",
"react-native": "0.46.3",
"react-native-config": "^0.5.0",
"react-native-fetch-polyfill": "^1.1.2",
"react-native-navigation": "^1.1.136",
"react-redux": "^5.0.5",
"redux": "^3.7.2",
"redux-logger": "^3.0.6",
"redux-thunk": "^2.2.0",
"whatwg-fetch": "^2.0.3"
},
"devDependencies": {
"babel-cli": "^6.24.1",
"babel-eslint": "^7.2.3",
"babel-jest": "20.0.3",
"babel-preset-flow": "^6.23.0",
"babel-preset-react-native": "2.1.0",
"eslint": "^3.19.0",
"eslint-config-airbnb": "^15.0.2",
"eslint-plugin-import": "^2.7.0",
"eslint-plugin-jsx-a11y": "^5.1.1",
"eslint-plugin-react": "^7.1.0",
"eslint-plugin-react-native": "^2.3.2",
"flow-bin": "^0.50.0",
"jest": "20.0.4",
"react-test-renderer": "16.0.0-alpha.12"
},
"jest": {
"preset": "react-native"
}
}
Well... as most of the time, the answer was in the doc...
To declare a global function that should be accessible throughout your project, use the declare function syntax in a libdef file:
flow-typed/myLibDef.js
declare function foo(a: number): string; This tells Flow that any code within the project can reference the foo global function, and that the function takes one argument (a number) and it returns a string.
So I added that
declare function fetch (a: string): Promise;
to a brand new myLibDef.js file at the root of my flow-typed folder. Then surprisingly, I had to add it to my .flowConfig (in the libs section). I was pretty sure it was automatic but... well... Anyway now fetch is covered ! \0/