Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🚧 node-fetch v3 #171

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
47 changes: 23 additions & 24 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 4 additions & 2 deletions package.json
Expand Up @@ -28,13 +28,16 @@
"url": "https://github.com/octokit/request.js/issues"
},
"homepage": "https://github.com/octokit/request.js#readme",
"browser": {
"node-fetch": "./pkg/dist-src/node-fetch-browser.js"
},
"dependencies": {
"@octokit/endpoint": "^6.0.1",
"@octokit/request-error": "^2.0.0",
"@octokit/types": "^5.0.0",
"deprecation": "^2.0.0",
"is-plain-object": "^3.0.0",
"node-fetch": "^2.3.0",
"node-fetch": "^3.0.0-beta.7",
"once": "^1.4.0",
"universal-user-agent": "^5.0.0"
},
Expand All @@ -48,7 +51,6 @@
"@types/jest": "^26.0.0",
"@types/lolex": "^5.1.0",
"@types/node": "^14.0.0",
"@types/node-fetch": "^2.3.3",
"@types/once": "^1.4.0",
"fetch-mock": "^9.3.1",
"jest": "^26.0.1",
Expand Down
24 changes: 24 additions & 0 deletions src/node-fetch-browser.ts
@@ -0,0 +1,24 @@
// ref: https://github.com/tc39/proposal-global
function getGlobal() {
// the only reliable means to get the global object is
// `Function('return this')()`
// However, this causes CSP violations in Chrome apps.
if (typeof self !== "undefined") {
return self;
}
if (typeof window !== "undefined") {
return window;
}
if (typeof global !== "undefined") {
return global;
}
throw new Error("unable to locate global object");
}

const globalScope = getGlobal();

export default globalScope.fetch.bind(globalScope);

export const Headers = globalScope.Headers;
export const Request = globalScope.Request;
export const Response = globalScope.Response;
4 changes: 2 additions & 2 deletions test/request.test.ts
Expand Up @@ -447,7 +447,7 @@ x//0u+zd/R/QRUzLOw4N72/Hu+UG6MNt5iDZFCtapRaKt6OvSBwy8w==
it("passes node-fetch options to fetch only", () => {
const mock = (url: string, options: RequestInit) => {
expect(url).toEqual("https://api.github.com/");
expect(options.timeout).toEqual(100);
expect(options.follow).toEqual(100);
return Promise.reject(new Error("ok"));
};

Expand All @@ -456,7 +456,7 @@ x//0u+zd/R/QRUzLOw4N72/Hu+UG6MNt5iDZFCtapRaKt6OvSBwy8w==
"user-agent": "funky boom boom pow",
},
request: {
timeout: 100,
follow: 100,
fetch: mock,
},
}).catch((error) => {
Expand Down