Skip to content

Commit 7d1c02f

Browse files
committed
fix(fetch): provide better @vercel/fetch encapsulation through the use of a static method on IsomorphicFetchHttpLibrary
1 parent 4899fef commit 7d1c02f

File tree

1 file changed

+7
-5
lines changed

1 file changed

+7
-5
lines changed

sdk-template-overrides/typescript/http/isomorphic-fetch.mustache

+7-5
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,24 @@
11
import {HttpLibrary, RequestContext, ResponseContext} from './http{{extensionForDeno}}';
22
import { from, Observable } from {{#useRxJS}}'rxjs'{{/useRxJS}}{{^useRxJS}}'../rxjsStub{{extensionForDeno}}'{{/useRxJS}};
3-
import createFetch, { FetchModule } from '@fortaine/fetch';
3+
import createFetch, { RequestInfo, RequestInit } from '@fortaine/fetch';
44
{{#platforms}}
55
{{#browser}}
66
import "whatwg-fetch";
77
{{/browser}}
88
{{/platforms}}
99

10-
{{! https://github.com/ajaishankar/openapi-typescript-fetch#server-side-usage }}
11-
globalThis.fetch = createFetch(fetch as unknown as FetchModule) as any;
12-
1310
export class IsomorphicFetchHttpLibrary implements HttpLibrary {
11+
static fetch(...args: [RequestInfo, RequestInit]) {
12+
const fetcher = {{#platforms}}{{#node}}(createFetch as any)(){{/node}}{{^node}}fetch{{/node}}{{/platforms}};
13+
14+
return fetcher(...args);
15+
}
1416

1517
public send(request: RequestContext): Observable<ResponseContext> {
1618
let method = request.getHttpMethod().toString();
1719
let body = request.getBody();
1820
19-
const resultPromise = fetch(request.getUrl(), {
21+
const resultPromise = IsomorphicFetchHttpLibrary.fetch(request.getUrl(), {
2022
method: method,
2123
body: body as any,
2224
headers: request.getHeaders(),

0 commit comments

Comments
 (0)