Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ export class RequestContext {
private body: RequestBody = undefined;
private url: URL;
private signal: AbortSignal | undefined = undefined;
private timeout?: number;
{{#platforms}}
{{#node}}
private dispatcher: Dispatcher | undefined = undefined;
Expand Down Expand Up @@ -170,6 +171,13 @@ export class RequestContext {
this.url.searchParams.append(name, value);
}

public setTimeout(timeout: number) {
this.timeout = timeout;
}

public getTimeout(): number | undefined {
return this.timeout;
}
/**
* Sets a cookie with the name and value. NO check for duplicate cookies is performed
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,42 @@ import "whatwg-fetch";
{{/browser}}
{{/platforms}}

function withTimeout(fetchFn: any, timeout?: number) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks for the PR

what about adding a docstring explaining what this function does?

please also follow step 3 in the PR checklist to update the samples so that CI can verify the change

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please specify what unit the number is, also in the modules/openapi-generator/src/main/resources/typescript/http/http.mustache

return (url: string, options: any = {}) => {
const controller = new AbortController();
const userSignal = options.signal as AbortSignal | undefined;
if (userSignal) {
if (userSignal.aborted) {
controller.abort();
} else {
userSignal.addEventListener("abort", () => controller.abort());
}
}

let timeoutId: NodeJS.Timeout | undefined;

if (timeout && timeout > 0) {
timeoutId = setTimeout(() => controller.abort(), timeout);
}

return fetchFn(url, { ...options, signal: controller.signal }).finally(
() => {
if (timeoutId) clearTimeout(timeoutId);
}
);
};
}


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