Skip to content

Commit

Permalink
js: Reformat isomorphic-fetch.ts
Browse files Browse the repository at this point in the history
- Consistently use 2-space indentation like other files
- Other minor space adjustments
  • Loading branch information
svix-jplatte committed Jul 8, 2024
1 parent e5dbc75 commit 5b02312
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 44 deletions.
4 changes: 2 additions & 2 deletions javascript/src/openapi/http/http.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<Blob>) {}
constructor(private dataSource: Promise<Blob>) { }

binary(): Promise<Blob> {
return this.dataSource;
Expand Down Expand Up @@ -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"`
Expand Down
83 changes: 41 additions & 42 deletions javascript/src/openapi/http/isomorphic-fetch.ts
Original file line number Diff line number Diff line change
@@ -1,54 +1,53 @@
import {HttpLibrary, RequestContext, ResponseContext} from './http';
import { HttpLibrary, RequestContext, ResponseContext } from './http';
import { from, Observable } from '../rxjsStub';
import "svix-fetch";

const numRetries = 2;
const sleep = (interval: number) => new Promise(resolve => setTimeout(resolve, interval));

export class IsomorphicFetchHttpLibrary implements HttpLibrary {
public send(request: RequestContext): Observable<ResponseContext> {
const resultPromise = this.sendWithRetry(request, numRetries, 50, 1);
return from<Promise<ResponseContext>>(resultPromise);
}

public send(request: RequestContext): Observable<ResponseContext> {
const resultPromise = this.sendWithRetry(request, numRetries, 50, 1);
return from<Promise<ResponseContext>>(resultPromise);
}
private async sendWithRetry(request: RequestContext, triesLeft: number, nextInterval: number, retryCount: number): Promise<ResponseContext> {
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<ResponseContext> {
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<ResponseContext> {
let method = request.getHttpMethod().toString();
let body = request.getBody();
private sendOnce(request: RequestContext): Promise<ResponseContext> {
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);
});
}
}

0 comments on commit 5b02312

Please sign in to comment.