Skip to content

Commit

Permalink
fix(fetch): provide better @vercel/fetch encapsulation through the us…
Browse files Browse the repository at this point in the history
…e of a static method on IsomorphicFetchHttpLibrary
  • Loading branch information
gfortaine committed Jan 30, 2023
1 parent 4899fef commit 7d1c02f
Showing 1 changed file with 7 additions and 5 deletions.
12 changes: 7 additions & 5 deletions sdk-template-overrides/typescript/http/isomorphic-fetch.mustache
Original file line number Diff line number Diff line change
@@ -1,22 +1,24 @@
import {HttpLibrary, RequestContext, ResponseContext} from './http{{extensionForDeno}}';
import { from, Observable } from {{#useRxJS}}'rxjs'{{/useRxJS}}{{^useRxJS}}'../rxjsStub{{extensionForDeno}}'{{/useRxJS}};
import createFetch, { FetchModule } from '@fortaine/fetch';
import createFetch, { RequestInfo, RequestInit } from '@fortaine/fetch';
{{#platforms}}
{{#browser}}
import "whatwg-fetch";
{{/browser}}
{{/platforms}}

{{! https://github.com/ajaishankar/openapi-typescript-fetch#server-side-usage }}
globalThis.fetch = createFetch(fetch as unknown as FetchModule) as any;

export class IsomorphicFetchHttpLibrary implements HttpLibrary {
static fetch(...args: [RequestInfo, RequestInit]) {
const fetcher = {{#platforms}}{{#node}}(createFetch as any)(){{/node}}{{^node}}fetch{{/node}}{{/platforms}};

return fetcher(...args);
}

public send(request: RequestContext): Observable<ResponseContext> {
let method = request.getHttpMethod().toString();
let body = request.getBody();
const resultPromise = fetch(request.getUrl(), {
const resultPromise = IsomorphicFetchHttpLibrary.fetch(request.getUrl(), {
method: method,
body: body as any,
headers: request.getHeaders(),
Expand Down

0 comments on commit 7d1c02f

Please sign in to comment.