Skip to content

Commit 981c8b9

Browse files
committed
feat: nodejs support doc and in tests using cross-fetch
1 parent 00982aa commit 981c8b9

File tree

4 files changed

+20
-21
lines changed

4 files changed

+20
-21
lines changed

README.md

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ Tiny wrapper around DOM fetch for common API wrappings.
44
[![](https://shields.servallapps.com/npm/v/@lcdev/fetch.svg?registry_uri=https%3A%2F%2Fnpm.servalldatasystems.com)](https://npm.servalldatasystems.com/#/detail/@lcdev/fetch)
55

66
```bash
7-
# replace VERSION below with the most recent major version above (eg. 0.5.1 -> 0.5, 1.2.3 -> 1)
87
yarn add @lcdev/fetch@VERSION
98
```
109

@@ -13,6 +12,9 @@ Use:
1312
```typescript
1413
import { HttpMethod, api, apiCall } from '@lcdev/fetch';
1514

15+
// one-off requests are easy
16+
await apiCall('https://base-url.com/endpoint').json<MyReturnType>();
17+
1618
// re-use this any time you want to make a call to this api
1719
const myCoreApi = api('https://base-url.com');
1820

@@ -23,9 +25,6 @@ await myCoreApi.get('/endpoint')
2325
.withQuery({ baz: 'bat' })
2426
// chain .json if you know the response is json
2527
.json<MyReturnType>();
26-
27-
// don't need to define a base URL up front for one-offs
28-
await apiCall('https://base-url.com/endpoint').json<MyReturnType>();
2928
```
3029

3130
Requests start on await/then. Chain to add data to the request. This is just a thin way to make `fetch` calls.
@@ -63,3 +62,10 @@ Chainable methods for API calls:
6362

6463
A base API itself can be called with `.get`, `.post`, etc. You can change the base URL if required
6564
with `changeBaseURL(path)`, though be warned that every request from then on will then be based on that.
65+
66+
## NodeJS Support
67+
Just polyfill `fetch`, and this package will work. Install `cross-fetch` package and add the following to your main file.
68+
69+
```typescript
70+
import 'cross-fetch/polyfill';
71+
```

package.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,10 @@
3232
"@lcdev/tsconfig": "0.1",
3333
"@types/jest": "24",
3434
"@types/koa": "2",
35-
"@types/node-fetch": "2",
35+
"cross-fetch": "3",
3636
"eslint": "6",
3737
"husky": "3",
3838
"jest": "24",
39-
"node-fetch": "2",
4039
"prettier": "1",
4140
"typescript": "3"
4241
},

src/index.test.ts

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,7 @@
11
import * as http from 'http';
2-
import fetch, { Headers, Request, Response } from 'node-fetch';
32
import { HttpMethod, buildPath, api, apiCall } from './index';
43

5-
Object.assign(global, {
6-
fetch,
7-
Headers,
8-
Request,
9-
Response,
10-
});
4+
import 'cross-fetch/polyfill';
115

126
describe('buildings paths', () => {
137
test('basic additions', () => {

yarn.lock

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -639,13 +639,6 @@
639639
resolved "https://registry.yarnpkg.com/@types/mime/-/mime-2.0.1.tgz#dc488842312a7f075149312905b5e3c0b054c79d"
640640
integrity sha512-FwI9gX75FgVBJ7ywgnq/P7tw+/o1GUbtP0KzbtusLigAOgIgNISRK0ZPl4qertvXSIE8YbsVJueQ90cDt9YYyw==
641641

642-
"@types/node-fetch@2":
643-
version "2.5.4"
644-
resolved "https://registry.yarnpkg.com/@types/node-fetch/-/node-fetch-2.5.4.tgz#5245b6d8841fc3a6208b82291119bc11c4e0ce44"
645-
integrity sha512-Oz6id++2qAOFuOlE1j0ouk1dzl3mmI1+qINPNBhi9nt/gVOz0G+13Ao6qjhdF0Ys+eOkhu6JnFmt38bR3H0POQ==
646-
dependencies:
647-
"@types/node" "*"
648-
649642
"@types/node@*", "@types/node@^12.0.2":
650643
version "12.12.21"
651644
resolved "https://registry.yarnpkg.com/@types/node/-/node-12.12.21.tgz#aa44a6363291c7037111c47e4661ad210aded23f"
@@ -1360,6 +1353,13 @@ cosmiconfig@^5.2.0, cosmiconfig@^5.2.1:
13601353
js-yaml "^3.13.1"
13611354
parse-json "^4.0.0"
13621355

1356+
cross-fetch@3:
1357+
version "3.0.5"
1358+
resolved "https://registry.yarnpkg.com/cross-fetch/-/cross-fetch-3.0.5.tgz#2739d2981892e7ab488a7ad03b92df2816e03f4c"
1359+
integrity sha512-FFLcLtraisj5eteosnX1gf01qYDCOc4fDy0+euOt8Kn9YBY2NtXL/pCoYPavw24NIQkQqm5ZOLsGD5Zzj0gyew==
1360+
dependencies:
1361+
node-fetch "2.6.0"
1362+
13631363
cross-spawn@^6.0.0, cross-spawn@^6.0.5:
13641364
version "6.0.5"
13651365
resolved "https://registry.yarnpkg.com/cross-spawn/-/cross-spawn-6.0.5.tgz#4a5ec7c64dfae22c3a14124dbacdee846d80cbc4"
@@ -3451,7 +3451,7 @@ nice-try@^1.0.4:
34513451
resolved "https://registry.yarnpkg.com/nice-try/-/nice-try-1.0.5.tgz#a3378a7696ce7d223e88fc9b764bd7ef1089e366"
34523452
integrity sha512-1nh45deeb5olNY7eX82BkPO7SSxR5SSYJiPTrTdFUVYwAl8CKMA5N9PjTYkHiRjisVcxcQ1HXdLhx2qxxJzLNQ==
34533453

3454-
node-fetch@2:
3454+
node-fetch@2.6.0:
34553455
version "2.6.0"
34563456
resolved "https://registry.yarnpkg.com/node-fetch/-/node-fetch-2.6.0.tgz#e633456386d4aa55863f676a7ab0daa8fdecb0fd"
34573457
integrity sha512-8dG4H5ujfvFiqDmVu9fQ5bOHUC15JMjMY/Zumv26oOvvVJjM67KF8koCWIabKQ1GJIa9r2mMZscBq/TbdOcmNA==

0 commit comments

Comments
 (0)