diff --git a/javascript/src/openapi/http/http.ts b/javascript/src/openapi/http/http.ts index 3298153dc2..5c08f00066 100644 --- a/javascript/src/openapi/http/http.ts +++ b/javascript/src/openapi/http/http.ts @@ -142,7 +142,7 @@ export interface ResponseBody { * Helper class to generate a `ResponseBody` from binary data */ export class SelfDecodingBody implements ResponseBody { - constructor(private dataSource: Promise) {} + constructor(private dataSource: Promise) { } binary(): Promise { return this.dataSource; @@ -170,7 +170,7 @@ export class ResponseContext { public httpStatusCode: number, public headers: { [key: string]: string }, public body: ResponseBody - ) {} + ) { } /** * Parse header value in the form `value; param1="value1"` diff --git a/javascript/src/openapi/http/isomorphic-fetch.ts b/javascript/src/openapi/http/isomorphic-fetch.ts index 9a35f2ab15..1ad779f30b 100644 --- a/javascript/src/openapi/http/isomorphic-fetch.ts +++ b/javascript/src/openapi/http/isomorphic-fetch.ts @@ -1,4 +1,4 @@ -import {HttpLibrary, RequestContext, ResponseContext} from './http'; +import { HttpLibrary, RequestContext, ResponseContext } from './http'; import { from, Observable } from '../rxjsStub'; import "svix-fetch"; @@ -6,49 +6,48 @@ const numRetries = 2; const sleep = (interval: number) => new Promise(resolve => setTimeout(resolve, interval)); export class IsomorphicFetchHttpLibrary implements HttpLibrary { + public send(request: RequestContext): Observable { + const resultPromise = this.sendWithRetry(request, numRetries, 50, 1); + return from>(resultPromise); + } - public send(request: RequestContext): Observable { - const resultPromise = this.sendWithRetry(request, numRetries, 50, 1); - return from>(resultPromise); - } + private async sendWithRetry(request: RequestContext, triesLeft: number, nextInterval: number, retryCount: number): Promise { + try { + const response = await this.sendOnce(request); + if (triesLeft <= 0 || response.httpStatusCode < 500) { + return response; + } + } catch (e) { + if (triesLeft <= 0) { + throw e; + } + }; + await sleep(nextInterval); + const headers = request.getHeaders(); + headers['svix-retry-count'] = retryCount.toString() + return await this.sendWithRetry(request, --triesLeft, nextInterval * 2, ++retryCount); + } - private async sendWithRetry(request: RequestContext, triesLeft: number, nextInterval: number, retryCount: number): Promise { - try { - const response = await this.sendOnce(request); - if (triesLeft <= 0 || response.httpStatusCode < 500) { - return response; - } - } catch (e) { - if (triesLeft <= 0) { - throw e; - } - }; - await sleep(nextInterval); - const headers = request.getHeaders(); - headers['svix-retry-count'] = retryCount.toString() - return await this.sendWithRetry(request, --triesLeft, nextInterval * 2, ++retryCount); - } - - private sendOnce(request: RequestContext): Promise { - let method = request.getHttpMethod().toString(); - let body = request.getBody(); + private sendOnce(request: RequestContext): Promise { + let method = request.getHttpMethod().toString(); + let body = request.getBody(); - return fetch(request.getUrl(), { - method: method, - body: body as any, - headers: request.getHeaders(), - credentials: "same-origin" - }).then((resp: any) => { - const headers: { [name: string]: string } = {}; - resp.headers.forEach((value: string, name: string) => { - headers[name] = value; - }); + return fetch(request.getUrl(), { + method: method, + body: body as any, + headers: request.getHeaders(), + credentials: "same-origin" + }).then((resp: any) => { + const headers: { [name: string]: string } = {}; + resp.headers.forEach((value: string, name: string) => { + headers[name] = value; + }); - const body = { - text: () => resp.text(), - binary: () => resp.blob() - }; - return new ResponseContext(resp.status, headers, body); - }); - } + const body = { + text: () => resp.text(), + binary: () => resp.blob() + }; + return new ResponseContext(resp.status, headers, body); + }); + } }