[typescript] add abort signal to requestcontext (#21323)

* add abort signal to requestcontext

* regenerate

* rebuild and regenerate

* abort signal import

* refresh samples

* clean csharp samples

* csharp sample cleanup

* add missing cs samples from master

* abort testing
This commit is contained in:
David Gamero
2025-06-17 04:45:43 -04:00
committed by GitHub
parent dbc5d09da6
commit d2b8a1eeac
34 changed files with 366 additions and 104 deletions

View File

@@ -5,6 +5,7 @@ import {{^supportsES6}}* as{{/supportsES6}} FormData from "form-data";
import { URL, URLSearchParams } from 'url'; import { URL, URLSearchParams } from 'url';
import * as http from 'http'; import * as http from 'http';
import * as https from 'https'; import * as https from 'https';
import { AbortSignal } from "node-fetch/externals";
{{/node}} {{/node}}
{{/platforms}} {{/platforms}}
import { Observable, from } from {{#useRxJS}}'rxjs'{{/useRxJS}}{{^useRxJS}}'../rxjsStub{{importFileExtension}}'{{/useRxJS}}; import { Observable, from } from {{#useRxJS}}'rxjs'{{/useRxJS}}{{^useRxJS}}'../rxjsStub{{importFileExtension}}'{{/useRxJS}};
@@ -86,6 +87,7 @@ export class RequestContext {
private headers: Headers = {}; private headers: Headers = {};
private body: RequestBody = undefined; private body: RequestBody = undefined;
private url: URL; private url: URL;
private signal: AbortSignal | undefined = undefined;
{{#platforms}} {{#platforms}}
{{#node}} {{#node}}
private agent: http.Agent | https.Agent | undefined = undefined; private agent: http.Agent | https.Agent | undefined = undefined;
@@ -167,6 +169,15 @@ export class RequestContext {
public setHeaderParam(key: string, value: string): void { public setHeaderParam(key: string, value: string): void {
this.headers[key] = value; this.headers[key] = value;
} }
public setSignal(signal: AbortSignal): void {
this.signal = signal;
}
public getSignal(): AbortSignal | undefined {
return this.signal;
}
{{#platforms}} {{#platforms}}
{{#node}} {{#node}}

View File

@@ -19,6 +19,7 @@ export class IsomorphicFetchHttpLibrary implements HttpLibrary {
method: method, method: method,
body: body as any, body: body as any,
headers: request.getHeaders(), headers: request.getHeaders(),
signal: request.getSignal(),
{{#platforms}} {{#platforms}}
{{#node}} {{#node}}
agent: request.getAgent(), agent: request.getAgent(),

View File

@@ -3,6 +3,7 @@ import * as FormData from "form-data";
import { URL, URLSearchParams } from 'url'; import { URL, URLSearchParams } from 'url';
import * as http from 'http'; import * as http from 'http';
import * as https from 'https'; import * as https from 'https';
import { AbortSignal } from "node-fetch/externals";
import { Observable, from } from '../rxjsStub'; import { Observable, from } from '../rxjsStub';
export * from './isomorphic-fetch'; export * from './isomorphic-fetch';
@@ -57,6 +58,7 @@ export class RequestContext {
private headers: Headers = {}; private headers: Headers = {};
private body: RequestBody = undefined; private body: RequestBody = undefined;
private url: URL; private url: URL;
private signal: AbortSignal | undefined = undefined;
private agent: http.Agent | https.Agent | undefined = undefined; private agent: http.Agent | https.Agent | undefined = undefined;
/** /**
@@ -135,6 +137,15 @@ export class RequestContext {
this.headers[key] = value; this.headers[key] = value;
} }
public setSignal(signal: AbortSignal): void {
this.signal = signal;
}
public getSignal(): AbortSignal | undefined {
return this.signal;
}
public setAgent(agent: http.Agent | https.Agent) { public setAgent(agent: http.Agent | https.Agent) {
this.agent = agent; this.agent = agent;
} }

View File

@@ -12,6 +12,7 @@ export class IsomorphicFetchHttpLibrary implements HttpLibrary {
method: method, method: method,
body: body as any, body: body as any,
headers: request.getHeaders(), headers: request.getHeaders(),
signal: request.getSignal(),
agent: request.getAgent(), agent: request.getAgent(),
}).then((resp: any) => { }).then((resp: any) => {
const headers: { [name: string]: string } = {}; const headers: { [name: string]: string } = {};

View File

@@ -49,6 +49,7 @@ export class RequestContext {
private headers: Headers = {}; private headers: Headers = {};
private body: RequestBody = undefined; private body: RequestBody = undefined;
private url: URL; private url: URL;
private signal: AbortSignal | undefined = undefined;
/** /**
* Creates the request context using a http method and request resource url * Creates the request context using a http method and request resource url
@@ -125,6 +126,15 @@ export class RequestContext {
public setHeaderParam(key: string, value: string): void { public setHeaderParam(key: string, value: string): void {
this.headers[key] = value; this.headers[key] = value;
} }
public setSignal(signal: AbortSignal): void {
this.signal = signal;
}
public getSignal(): AbortSignal | undefined {
return this.signal;
}
} }
export interface ResponseBody { export interface ResponseBody {

View File

@@ -12,6 +12,7 @@ export class IsomorphicFetchHttpLibrary implements HttpLibrary {
method: method, method: method,
body: body as any, body: body as any,
headers: request.getHeaders(), headers: request.getHeaders(),
signal: request.getSignal(),
credentials: "same-origin" credentials: "same-origin"
}).then((resp: any) => { }).then((resp: any) => {
const headers: { [name: string]: string } = {}; const headers: { [name: string]: string } = {};

View File

@@ -49,6 +49,7 @@ export class RequestContext {
private headers: Headers = {}; private headers: Headers = {};
private body: RequestBody = undefined; private body: RequestBody = undefined;
private url: URL; private url: URL;
private signal: AbortSignal | undefined = undefined;
/** /**
* Creates the request context using a http method and request resource url * Creates the request context using a http method and request resource url
@@ -125,6 +126,15 @@ export class RequestContext {
public setHeaderParam(key: string, value: string): void { public setHeaderParam(key: string, value: string): void {
this.headers[key] = value; this.headers[key] = value;
} }
public setSignal(signal: AbortSignal): void {
this.signal = signal;
}
public getSignal(): AbortSignal | undefined {
return this.signal;
}
} }
export interface ResponseBody { export interface ResponseBody {

View File

@@ -12,6 +12,7 @@ export class IsomorphicFetchHttpLibrary implements HttpLibrary {
method: method, method: method,
body: body as any, body: body as any,
headers: request.getHeaders(), headers: request.getHeaders(),
signal: request.getSignal(),
credentials: "same-origin" credentials: "same-origin"
}).then((resp: any) => { }).then((resp: any) => {
const headers: { [name: string]: string } = {}; const headers: { [name: string]: string } = {};

View File

@@ -49,6 +49,7 @@ export class RequestContext {
private headers: Headers = {}; private headers: Headers = {};
private body: RequestBody = undefined; private body: RequestBody = undefined;
private url: URL; private url: URL;
private signal: AbortSignal | undefined = undefined;
/** /**
* Creates the request context using a http method and request resource url * Creates the request context using a http method and request resource url
@@ -125,6 +126,15 @@ export class RequestContext {
public setHeaderParam(key: string, value: string): void { public setHeaderParam(key: string, value: string): void {
this.headers[key] = value; this.headers[key] = value;
} }
public setSignal(signal: AbortSignal): void {
this.signal = signal;
}
public getSignal(): AbortSignal | undefined {
return this.signal;
}
} }
export interface ResponseBody { export interface ResponseBody {

View File

@@ -12,6 +12,7 @@ export class IsomorphicFetchHttpLibrary implements HttpLibrary {
method: method, method: method,
body: body as any, body: body as any,
headers: request.getHeaders(), headers: request.getHeaders(),
signal: request.getSignal(),
credentials: "same-origin" credentials: "same-origin"
}).then((resp: any) => { }).then((resp: any) => {
const headers: { [name: string]: string } = {}; const headers: { [name: string]: string } = {};

View File

@@ -49,6 +49,7 @@ export class RequestContext {
private headers: Headers = {}; private headers: Headers = {};
private body: RequestBody = undefined; private body: RequestBody = undefined;
private url: URL; private url: URL;
private signal: AbortSignal | undefined = undefined;
/** /**
* Creates the request context using a http method and request resource url * Creates the request context using a http method and request resource url
@@ -125,6 +126,15 @@ export class RequestContext {
public setHeaderParam(key: string, value: string): void { public setHeaderParam(key: string, value: string): void {
this.headers[key] = value; this.headers[key] = value;
} }
public setSignal(signal: AbortSignal): void {
this.signal = signal;
}
public getSignal(): AbortSignal | undefined {
return this.signal;
}
} }
export interface ResponseBody { export interface ResponseBody {

View File

@@ -12,6 +12,7 @@ export class IsomorphicFetchHttpLibrary implements HttpLibrary {
method: method, method: method,
body: body as any, body: body as any,
headers: request.getHeaders(), headers: request.getHeaders(),
signal: request.getSignal(),
credentials: "same-origin" credentials: "same-origin"
}).then((resp: any) => { }).then((resp: any) => {
const headers: { [name: string]: string } = {}; const headers: { [name: string]: string } = {};

View File

@@ -3,6 +3,7 @@ import * as FormData from "form-data";
import { URL, URLSearchParams } from 'url'; import { URL, URLSearchParams } from 'url';
import * as http from 'http'; import * as http from 'http';
import * as https from 'https'; import * as https from 'https';
import { AbortSignal } from "node-fetch/externals";
import { Observable, from } from '../rxjsStub'; import { Observable, from } from '../rxjsStub';
export * from './isomorphic-fetch'; export * from './isomorphic-fetch';
@@ -57,6 +58,7 @@ export class RequestContext {
private headers: Headers = {}; private headers: Headers = {};
private body: RequestBody = undefined; private body: RequestBody = undefined;
private url: URL; private url: URL;
private signal: AbortSignal | undefined = undefined;
private agent: http.Agent | https.Agent | undefined = undefined; private agent: http.Agent | https.Agent | undefined = undefined;
/** /**
@@ -135,6 +137,15 @@ export class RequestContext {
this.headers[key] = value; this.headers[key] = value;
} }
public setSignal(signal: AbortSignal): void {
this.signal = signal;
}
public getSignal(): AbortSignal | undefined {
return this.signal;
}
public setAgent(agent: http.Agent | https.Agent) { public setAgent(agent: http.Agent | https.Agent) {
this.agent = agent; this.agent = agent;
} }

View File

@@ -12,6 +12,7 @@ export class IsomorphicFetchHttpLibrary implements HttpLibrary {
method: method, method: method,
body: body as any, body: body as any,
headers: request.getHeaders(), headers: request.getHeaders(),
signal: request.getSignal(),
agent: request.getAgent(), agent: request.getAgent(),
}).then((resp: any) => { }).then((resp: any) => {
const headers: { [name: string]: string } = {}; const headers: { [name: string]: string } = {};

View File

@@ -49,6 +49,7 @@ export class RequestContext {
private headers: Headers = {}; private headers: Headers = {};
private body: RequestBody = undefined; private body: RequestBody = undefined;
private url: URL; private url: URL;
private signal: AbortSignal | undefined = undefined;
/** /**
* Creates the request context using a http method and request resource url * Creates the request context using a http method and request resource url
@@ -125,6 +126,15 @@ export class RequestContext {
public setHeaderParam(key: string, value: string): void { public setHeaderParam(key: string, value: string): void {
this.headers[key] = value; this.headers[key] = value;
} }
public setSignal(signal: AbortSignal): void {
this.signal = signal;
}
public getSignal(): AbortSignal | undefined {
return this.signal;
}
} }
export interface ResponseBody { export interface ResponseBody {

View File

@@ -12,6 +12,7 @@ export class IsomorphicFetchHttpLibrary implements HttpLibrary {
method: method, method: method,
body: body as any, body: body as any,
headers: request.getHeaders(), headers: request.getHeaders(),
signal: request.getSignal(),
credentials: "same-origin" credentials: "same-origin"
}).then((resp: any) => { }).then((resp: any) => {
const headers: { [name: string]: string } = {}; const headers: { [name: string]: string } = {};

View File

@@ -49,6 +49,7 @@ export class RequestContext {
private headers: Headers = {}; private headers: Headers = {};
private body: RequestBody = undefined; private body: RequestBody = undefined;
private url: URL; private url: URL;
private signal: AbortSignal | undefined = undefined;
/** /**
* Creates the request context using a http method and request resource url * Creates the request context using a http method and request resource url
@@ -125,6 +126,15 @@ export class RequestContext {
public setHeaderParam(key: string, value: string): void { public setHeaderParam(key: string, value: string): void {
this.headers[key] = value; this.headers[key] = value;
} }
public setSignal(signal: AbortSignal): void {
this.signal = signal;
}
public getSignal(): AbortSignal | undefined {
return this.signal;
}
} }
export interface ResponseBody { export interface ResponseBody {

View File

@@ -12,6 +12,7 @@ export class IsomorphicFetchHttpLibrary implements HttpLibrary {
method: method, method: method,
body: body as any, body: body as any,
headers: request.getHeaders(), headers: request.getHeaders(),
signal: request.getSignal(),
credentials: "same-origin" credentials: "same-origin"
}).then((resp: any) => { }).then((resp: any) => {
const headers: { [name: string]: string } = {}; const headers: { [name: string]: string } = {};

View File

@@ -3,6 +3,7 @@ import * as FormData from "form-data";
import { URL, URLSearchParams } from 'url'; import { URL, URLSearchParams } from 'url';
import * as http from 'http'; import * as http from 'http';
import * as https from 'https'; import * as https from 'https';
import { AbortSignal } from "node-fetch/externals";
import { Observable, from } from '../rxjsStub'; import { Observable, from } from '../rxjsStub';
export * from './isomorphic-fetch'; export * from './isomorphic-fetch';
@@ -57,6 +58,7 @@ export class RequestContext {
private headers: Headers = {}; private headers: Headers = {};
private body: RequestBody = undefined; private body: RequestBody = undefined;
private url: URL; private url: URL;
private signal: AbortSignal | undefined = undefined;
private agent: http.Agent | https.Agent | undefined = undefined; private agent: http.Agent | https.Agent | undefined = undefined;
/** /**
@@ -135,6 +137,15 @@ export class RequestContext {
this.headers[key] = value; this.headers[key] = value;
} }
public setSignal(signal: AbortSignal): void {
this.signal = signal;
}
public getSignal(): AbortSignal | undefined {
return this.signal;
}
public setAgent(agent: http.Agent | https.Agent) { public setAgent(agent: http.Agent | https.Agent) {
this.agent = agent; this.agent = agent;
} }

View File

@@ -12,6 +12,7 @@ export class IsomorphicFetchHttpLibrary implements HttpLibrary {
method: method, method: method,
body: body as any, body: body as any,
headers: request.getHeaders(), headers: request.getHeaders(),
signal: request.getSignal(),
agent: request.getAgent(), agent: request.getAgent(),
}).then((resp: any) => { }).then((resp: any) => {
const headers: { [name: string]: string } = {}; const headers: { [name: string]: string } = {};

View File

@@ -48,6 +48,7 @@ export class RequestContext {
private headers: Headers = {}; private headers: Headers = {};
private body: RequestBody = undefined; private body: RequestBody = undefined;
private url: URL; private url: URL;
private signal: AbortSignal | undefined = undefined;
/** /**
* Creates the request context using a http method and request resource url * Creates the request context using a http method and request resource url
@@ -124,6 +125,15 @@ export class RequestContext {
public setHeaderParam(key: string, value: string): void { public setHeaderParam(key: string, value: string): void {
this.headers[key] = value; this.headers[key] = value;
} }
public setSignal(signal: AbortSignal): void {
this.signal = signal;
}
public getSignal(): AbortSignal | undefined {
return this.signal;
}
} }
export interface ResponseBody { export interface ResponseBody {

View File

@@ -11,6 +11,7 @@ export class IsomorphicFetchHttpLibrary implements HttpLibrary {
method: method, method: method,
body: body as any, body: body as any,
headers: request.getHeaders(), headers: request.getHeaders(),
signal: request.getSignal(),
}).then((resp: any) => { }).then((resp: any) => {
const headers: { [name: string]: string } = {}; const headers: { [name: string]: string } = {};
resp.headers.forEach((value: string, name: string) => { resp.headers.forEach((value: string, name: string) => {

View File

@@ -48,6 +48,7 @@ export class RequestContext {
private headers: Headers = {}; private headers: Headers = {};
private body: RequestBody = undefined; private body: RequestBody = undefined;
private url: URL; private url: URL;
private signal: AbortSignal | undefined = undefined;
/** /**
* Creates the request context using a http method and request resource url * Creates the request context using a http method and request resource url
@@ -124,6 +125,15 @@ export class RequestContext {
public setHeaderParam(key: string, value: string): void { public setHeaderParam(key: string, value: string): void {
this.headers[key] = value; this.headers[key] = value;
} }
public setSignal(signal: AbortSignal): void {
this.signal = signal;
}
public getSignal(): AbortSignal | undefined {
return this.signal;
}
} }
export interface ResponseBody { export interface ResponseBody {

View File

@@ -11,6 +11,7 @@ export class IsomorphicFetchHttpLibrary implements HttpLibrary {
method: method, method: method,
body: body as any, body: body as any,
headers: request.getHeaders(), headers: request.getHeaders(),
signal: request.getSignal(),
}).then((resp: any) => { }).then((resp: any) => {
const headers: { [name: string]: string } = {}; const headers: { [name: string]: string } = {};
resp.headers.forEach((value: string, name: string) => { resp.headers.forEach((value: string, name: string) => {

View File

@@ -3,6 +3,7 @@ import * as FormData from "form-data";
import { URL, URLSearchParams } from 'url'; import { URL, URLSearchParams } from 'url';
import * as http from 'http'; import * as http from 'http';
import * as https from 'https'; import * as https from 'https';
import { AbortSignal } from "node-fetch/externals";
import { Observable, from } from '../rxjsStub'; import { Observable, from } from '../rxjsStub';
export * from './isomorphic-fetch'; export * from './isomorphic-fetch';
@@ -57,6 +58,7 @@ export class RequestContext {
private headers: Headers = {}; private headers: Headers = {};
private body: RequestBody = undefined; private body: RequestBody = undefined;
private url: URL; private url: URL;
private signal: AbortSignal | undefined = undefined;
private agent: http.Agent | https.Agent | undefined = undefined; private agent: http.Agent | https.Agent | undefined = undefined;
/** /**
@@ -135,6 +137,15 @@ export class RequestContext {
this.headers[key] = value; this.headers[key] = value;
} }
public setSignal(signal: AbortSignal): void {
this.signal = signal;
}
public getSignal(): AbortSignal | undefined {
return this.signal;
}
public setAgent(agent: http.Agent | https.Agent) { public setAgent(agent: http.Agent | https.Agent) {
this.agent = agent; this.agent = agent;
} }

View File

@@ -12,6 +12,7 @@ export class IsomorphicFetchHttpLibrary implements HttpLibrary {
method: method, method: method,
body: body as any, body: body as any,
headers: request.getHeaders(), headers: request.getHeaders(),
signal: request.getSignal(),
agent: request.getAgent(), agent: request.getAgent(),
}).then((resp: any) => { }).then((resp: any) => {
const headers: { [name: string]: string } = {}; const headers: { [name: string]: string } = {};

View File

@@ -3,6 +3,7 @@ import FormData from "form-data";
import { URL, URLSearchParams } from 'url'; import { URL, URLSearchParams } from 'url';
import * as http from 'http'; import * as http from 'http';
import * as https from 'https'; import * as https from 'https';
import { AbortSignal } from "node-fetch/externals";
import { Observable, from } from '../rxjsStub'; import { Observable, from } from '../rxjsStub';
export * from './isomorphic-fetch'; export * from './isomorphic-fetch';
@@ -57,6 +58,7 @@ export class RequestContext {
private headers: Headers = {}; private headers: Headers = {};
private body: RequestBody = undefined; private body: RequestBody = undefined;
private url: URL; private url: URL;
private signal: AbortSignal | undefined = undefined;
private agent: http.Agent | https.Agent | undefined = undefined; private agent: http.Agent | https.Agent | undefined = undefined;
/** /**
@@ -135,6 +137,15 @@ export class RequestContext {
this.headers[key] = value; this.headers[key] = value;
} }
public setSignal(signal: AbortSignal): void {
this.signal = signal;
}
public getSignal(): AbortSignal | undefined {
return this.signal;
}
public setAgent(agent: http.Agent | https.Agent) { public setAgent(agent: http.Agent | https.Agent) {
this.agent = agent; this.agent = agent;
} }

View File

@@ -12,6 +12,7 @@ export class IsomorphicFetchHttpLibrary implements HttpLibrary {
method: method, method: method,
body: body as any, body: body as any,
headers: request.getHeaders(), headers: request.getHeaders(),
signal: request.getSignal(),
agent: request.getAgent(), agent: request.getAgent(),
}).then((resp: any) => { }).then((resp: any) => {
const headers: { [name: string]: string } = {}; const headers: { [name: string]: string } = {};

View File

@@ -49,6 +49,7 @@ export class RequestContext {
private headers: Headers = {}; private headers: Headers = {};
private body: RequestBody = undefined; private body: RequestBody = undefined;
private url: URL; private url: URL;
private signal: AbortSignal | undefined = undefined;
/** /**
* Creates the request context using a http method and request resource url * Creates the request context using a http method and request resource url
@@ -125,6 +126,15 @@ export class RequestContext {
public setHeaderParam(key: string, value: string): void { public setHeaderParam(key: string, value: string): void {
this.headers[key] = value; this.headers[key] = value;
} }
public setSignal(signal: AbortSignal): void {
this.signal = signal;
}
public getSignal(): AbortSignal | undefined {
return this.signal;
}
} }
export interface ResponseBody { export interface ResponseBody {

View File

@@ -49,6 +49,7 @@ export class RequestContext {
private headers: Headers = {}; private headers: Headers = {};
private body: RequestBody = undefined; private body: RequestBody = undefined;
private url: URL; private url: URL;
private signal: AbortSignal | undefined = undefined;
/** /**
* Creates the request context using a http method and request resource url * Creates the request context using a http method and request resource url
@@ -125,6 +126,15 @@ export class RequestContext {
public setHeaderParam(key: string, value: string): void { public setHeaderParam(key: string, value: string): void {
this.headers[key] = value; this.headers[key] = value;
} }
public setSignal(signal: AbortSignal): void {
this.signal = signal;
}
public getSignal(): AbortSignal | undefined {
return this.signal;
}
} }
export interface ResponseBody { export interface ResponseBody {

View File

@@ -12,6 +12,7 @@ export class IsomorphicFetchHttpLibrary implements HttpLibrary {
method: method, method: method,
body: body as any, body: body as any,
headers: request.getHeaders(), headers: request.getHeaders(),
signal: request.getSignal(),
credentials: "same-origin" credentials: "same-origin"
}).then((resp: any) => { }).then((resp: any) => {
const headers: { [name: string]: string } = {}; const headers: { [name: string]: string } = {};

View File

@@ -3,6 +3,7 @@ import * as FormData from "form-data";
import { URL, URLSearchParams } from 'url'; import { URL, URLSearchParams } from 'url';
import * as http from 'http'; import * as http from 'http';
import * as https from 'https'; import * as https from 'https';
import { AbortSignal } from "node-fetch/externals";
import { Observable, from } from '../rxjsStub'; import { Observable, from } from '../rxjsStub';
export * from './isomorphic-fetch'; export * from './isomorphic-fetch';
@@ -57,6 +58,7 @@ export class RequestContext {
private headers: Headers = {}; private headers: Headers = {};
private body: RequestBody = undefined; private body: RequestBody = undefined;
private url: URL; private url: URL;
private signal: AbortSignal | undefined = undefined;
private agent: http.Agent | https.Agent | undefined = undefined; private agent: http.Agent | https.Agent | undefined = undefined;
/** /**
@@ -135,6 +137,15 @@ export class RequestContext {
this.headers[key] = value; this.headers[key] = value;
} }
public setSignal(signal: AbortSignal): void {
this.signal = signal;
}
public getSignal(): AbortSignal | undefined {
return this.signal;
}
public setAgent(agent: http.Agent | https.Agent) { public setAgent(agent: http.Agent | https.Agent) {
this.agent = agent; this.agent = agent;
} }

View File

@@ -12,6 +12,7 @@ export class IsomorphicFetchHttpLibrary implements HttpLibrary {
method: method, method: method,
body: body as any, body: body as any,
headers: request.getHeaders(), headers: request.getHeaders(),
signal: request.getSignal(),
agent: request.getAgent(), agent: request.getAgent(),
}).then((resp: any) => { }).then((resp: any) => {
const headers: { [name: string]: string } = {}; const headers: { [name: string]: string } = {};

View File

@@ -14,36 +14,66 @@ tag.id = Math.floor(Math.random() * 100000)
let pet: petstore.Pet; let pet: petstore.Pet;
function overridePetIDMiddleware(id: number): Middleware { function overridePetIDMiddleware(id: number): Middleware {
return { return {
pre: (c: RequestContext) => { pre: async (c: RequestContext) => {
return new Promise((resolve) => { const segments = c.getUrl().split('/')
const segments = c.getUrl().split('/') segments[segments.length - 1] = id.toString()
segments[segments.length - 1] = id.toString() const newURL = segments.join('/')
const newURL = segments.join('/') c.setUrl(newURL)
c.setUrl(newURL) return c
resolve(c)
})
}, },
post: (c: ResponseContext) => { post: async (c: ResponseContext) => {
return new Promise<ResponseContext>((resolve) => { return c
resolve(c)
})
}, },
} }
} }
function NoopMiddleware(onPre: () => void, onPost: () => void): Middleware { function noopMiddleware(onPre: () => void, onPost: () => void): Middleware {
return { return {
pre: (c: RequestContext) => { pre: async (c: RequestContext) => {
return new Promise((resolve) => { onPre()
onPre() return c
resolve(c)
})
}, },
post: (c: ResponseContext) => { post: async (c: ResponseContext) => {
onPost()
return c
},
}
}
/**
* Middleware that adds an abort signal to the request context.
* This can be used to abort requests using an AbortController.
* @param signal AbortSignal to use for the request
* @returns Middleware that sets the signal in the request context
*/
function abortSignalMiddleware(signal: AbortSignal): Middleware {
return {
pre: async (c: RequestContext) => {
c.setSignal(signal)
return c
},
post: async (c: ResponseContext) => {
return c
},
}
}
/**
* Middleware that delays the request/response by a specified amount of time.
* @param delay in milliseconds
* @returns Middleware that delays the request/response
*/
function delayMiddleware(delay: number): Middleware {
return {
pre: async (c: RequestContext) => {
return new Promise<RequestContext>((resolve) => {
setTimeout(() => resolve(c), delay);
});
},
post: async (c: ResponseContext) => {
return new Promise<ResponseContext>((resolve) => { return new Promise<ResponseContext>((resolve) => {
onPost() setTimeout(() => resolve(c), delay);
resolve(c) });
})
}, },
} }
} }
@@ -52,8 +82,8 @@ function MiddlewareCallTracker() {
let CallOrder = [] as string[] let CallOrder = [] as string[]
return { return {
CallOrder, CallOrder,
BaseMiddleware: NoopMiddleware(() => CallOrder.push('base-pre'), () => CallOrder.push('base-post')), BaseMiddleware: noopMiddleware(() => CallOrder.push('base-pre'), () => CallOrder.push('base-post')),
CalltimeMiddleware: NoopMiddleware(() => CallOrder.push('call-pre'), () => CallOrder.push('call-post')) CalltimeMiddleware: noopMiddleware(() => CallOrder.push('call-pre'), () => CallOrder.push('call-post'))
} }
} }
@@ -74,88 +104,127 @@ describe("PetApi", () => {
expect(createdPet).to.deep.equal(pet); expect(createdPet).to.deep.equal(pet);
}) })
it("addPetViaMiddleware", async () => { describe("Middeware", () => {
const wrongId = pet.id + 1
const createdPet = await petApi.getPetById(wrongId, { middleware: [overridePetIDMiddleware(pet.id)] }) it("addPetViaMiddleware", async () => {
expect(createdPet).to.deep.equal(pet); const wrongId = pet.id + 1
const createdPet = await petApi.getPetById(wrongId, { middleware: [overridePetIDMiddleware(pet.id)] })
expect(createdPet).to.deep.equal(pet);
})
it("appendMiddleware petid", async () => {
const wrongId = pet.id + 100
const configuration = petstore.createConfiguration({ promiseMiddleware: [overridePetIDMiddleware(wrongId)] })
const petApi = new petstore.PetApi(configuration)
try {
void await petApi.getPetById(pet.id)
} catch (err) {
expect(err.code).to.equal(404);
expect(err.message).to.include("Pet not found");
}
const callTimeAppendedRightPet = await petApi.getPetById(wrongId, { middleware: [overridePetIDMiddleware(pet.id)], middlewareMergeStrategy: 'append' })
expect(callTimeAppendedRightPet).to.deep.equal(pet);
})
it("should keep middleware when no options are given", async () => {
let { CallOrder, BaseMiddleware, CalltimeMiddleware } = MiddlewareCallTracker()
const configuration = petstore.createConfiguration({ promiseMiddleware: [BaseMiddleware] });
const petApi = new petstore.PetApi(configuration);
const callTimeAppendedRightPet = await petApi.getPetById(pet.id);
expect(callTimeAppendedRightPet).to.deep.equal(pet);
expect(CallOrder).deep.equal(['base-pre', 'base-post'])
})
it("should keep middleware when options are given without middleware", async () => {
let { CallOrder, BaseMiddleware, CalltimeMiddleware } = MiddlewareCallTracker()
const configuration = petstore.createConfiguration({ promiseMiddleware: [BaseMiddleware] });
const petApi = new petstore.PetApi(configuration);
const callTimeAppendedRightPet = await petApi.getPetById(pet.id, {});
expect(callTimeAppendedRightPet).to.deep.equal(pet);
expect(CallOrder).deep.equal(['base-pre', 'base-post'])
})
it("should replace middleware when options contain an empty array of middlewares", async () => {
let { CallOrder, BaseMiddleware, CalltimeMiddleware } = MiddlewareCallTracker()
const configuration = petstore.createConfiguration({ promiseMiddleware: [BaseMiddleware] });
const petApi = new petstore.PetApi(configuration);
const callTimeAppendedRightPet = await petApi.getPetById(pet.id, { middleware: [] });
expect(callTimeAppendedRightPet).to.deep.equal(pet);
expect(CallOrder).deep.equal([])
})
it("replace Middleware call order", async () => {
let { CallOrder, BaseMiddleware, CalltimeMiddleware } = MiddlewareCallTracker()
const configuration = petstore.createConfiguration({ promiseMiddleware: [BaseMiddleware] })
const petApi = new petstore.PetApi(configuration)
const callTimeAppendedRightPet = await petApi.getPetById(pet.id, { middleware: [CalltimeMiddleware] })
expect(callTimeAppendedRightPet).to.deep.equal(pet);
expect(CallOrder).deep.equal(['call-pre', 'call-post'])
})
it("prepend Middleware call order", async () => {
let { CallOrder, BaseMiddleware, CalltimeMiddleware } = MiddlewareCallTracker()
const configuration = petstore.createConfiguration({ promiseMiddleware: [BaseMiddleware] })
const petApi = new petstore.PetApi(configuration)
const callTimeAppendedRightPet = await petApi.getPetById(pet.id, { middleware: [CalltimeMiddleware], middlewareMergeStrategy: 'prepend' })
expect(callTimeAppendedRightPet).to.deep.equal(pet);
expect(CallOrder).deep.equal(['call-pre', 'base-pre', 'base-post', 'call-post'])
})
it("append Middleware call order", async () => {
let { CallOrder, BaseMiddleware, CalltimeMiddleware } = MiddlewareCallTracker()
const configuration = petstore.createConfiguration({ promiseMiddleware: [BaseMiddleware] })
const petApi = new petstore.PetApi(configuration)
const callTimeAppendedRightPet = await petApi.getPetById(pet.id, { middleware: [CalltimeMiddleware], middlewareMergeStrategy: 'append' })
expect(callTimeAppendedRightPet).to.deep.equal(pet);
expect(CallOrder).deep.equal(['base-pre', 'call-pre', 'call-post', 'base-post'])
})
it("prependMiddleware pet id", async () => {
const wrongId = pet.id + 100
const configuration = petstore.createConfiguration({ promiseMiddleware: [overridePetIDMiddleware(pet.id)] })
const petApi = new petstore.PetApi(configuration)
const callTimeAppendedRightPet = await petApi.getPetById(wrongId, { middleware: [overridePetIDMiddleware(wrongId)], middlewareMergeStrategy: 'prepend' })
expect(callTimeAppendedRightPet).to.deep.equal(pet);
})
}) })
it("appendMiddleware petid", async () => { describe("AbortController", () => {
const wrongId = pet.id + 100 it("fails on invalid requests", async () => {
const configuration = petstore.createConfiguration({ promiseMiddleware: [overridePetIDMiddleware(wrongId)] }) const controller = new AbortController();
const petApi = new petstore.PetApi(configuration) const abortMiddleware = abortSignalMiddleware(controller.signal);
try { const wrongId = pet.id + 1
void await petApi.getPetById(pet.id) try {
} catch (err) { await petApi.getPetById(wrongId, { middleware: [abortMiddleware] })
expect(err.code).to.equal(404); } catch (err) {
expect(err.message).to.include("Pet not found"); expect(err.code).to.equal(404);
} }
const callTimeAppendedRightPet = await petApi.getPetById(wrongId, { middleware: [overridePetIDMiddleware(pet.id)], middlewareMergeStrategy: 'append' }) })
expect(callTimeAppendedRightPet).to.deep.equal(pet); it("succeeds on valid requests", async () => {
}) const controller = new AbortController();
const abortMiddleware = abortSignalMiddleware(controller.signal);
it("should keep middleware when no options are given", async () => { const createdPet = await petApi.getPetById(pet.id, { middleware: [abortMiddleware] })
let { CallOrder, BaseMiddleware, CalltimeMiddleware } = MiddlewareCallTracker() expect(createdPet).to.deep.equal(pet);
const configuration = petstore.createConfiguration({ promiseMiddleware: [BaseMiddleware] }); })
const petApi = new petstore.PetApi(configuration); it("aborts request", async () => {
const callTimeAppendedRightPet = await petApi.getPetById(pet.id); const signal = AbortSignal.timeout(10); // Set a timeout to ensure the request is aborted
expect(callTimeAppendedRightPet).to.deep.equal(pet); const abortMiddleware = abortSignalMiddleware(signal);
expect(CallOrder).deep.equal(['base-pre', 'base-post']) try {
}) await petApi.getPetById(pet.id, { middleware: [abortMiddleware, delayMiddleware(20)] })
} catch (err) {
it("should keep middleware when options are given without middleware", async () => { expect(err.name).to.equal("AbortError");
let { CallOrder, BaseMiddleware, CalltimeMiddleware } = MiddlewareCallTracker() return;
const configuration = petstore.createConfiguration({ promiseMiddleware: [BaseMiddleware] }); }
const petApi = new petstore.PetApi(configuration); throw new Error("Request was not aborted!");
const callTimeAppendedRightPet = await petApi.getPetById(pet.id, {}); })
expect(callTimeAppendedRightPet).to.deep.equal(pet); it("ignores abort after response", async () => {
expect(CallOrder).deep.equal(['base-pre', 'base-post']) const signal = AbortSignal.timeout(20); // Set a timeout to ensure the request is aborted
}) const abortMiddleware = abortSignalMiddleware(signal);
const createdPet = await petApi.getPetById(pet.id, { middleware: [abortMiddleware, delayMiddleware(10)] })
it("should replace middleware when options contain an empty array of middlewares", async () => { expect(createdPet).to.deep.equal(pet);
let { CallOrder, BaseMiddleware, CalltimeMiddleware } = MiddlewareCallTracker() })
const configuration = petstore.createConfiguration({ promiseMiddleware: [BaseMiddleware] });
const petApi = new petstore.PetApi(configuration);
const callTimeAppendedRightPet = await petApi.getPetById(pet.id, { middleware: [] });
expect(callTimeAppendedRightPet).to.deep.equal(pet);
expect(CallOrder).deep.equal([])
})
it("replace Middleware call order", async () => {
let { CallOrder, BaseMiddleware, CalltimeMiddleware } = MiddlewareCallTracker()
const configuration = petstore.createConfiguration({ promiseMiddleware: [BaseMiddleware] })
const petApi = new petstore.PetApi(configuration)
const callTimeAppendedRightPet = await petApi.getPetById(pet.id, { middleware: [CalltimeMiddleware] })
expect(callTimeAppendedRightPet).to.deep.equal(pet);
expect(CallOrder).deep.equal(['call-pre', 'call-post'])
})
it("prepend Middleware call order", async () => {
let { CallOrder, BaseMiddleware, CalltimeMiddleware } = MiddlewareCallTracker()
const configuration = petstore.createConfiguration({ promiseMiddleware: [BaseMiddleware] })
const petApi = new petstore.PetApi(configuration)
const callTimeAppendedRightPet = await petApi.getPetById(pet.id, { middleware: [CalltimeMiddleware], middlewareMergeStrategy: 'prepend' })
expect(callTimeAppendedRightPet).to.deep.equal(pet);
expect(CallOrder).deep.equal(['call-pre', 'base-pre', 'base-post','call-post'])
})
it("append Middleware call order", async () => {
let { CallOrder, BaseMiddleware, CalltimeMiddleware } = MiddlewareCallTracker()
const configuration = petstore.createConfiguration({ promiseMiddleware: [BaseMiddleware] })
const petApi = new petstore.PetApi(configuration)
const callTimeAppendedRightPet = await petApi.getPetById(pet.id, { middleware: [CalltimeMiddleware],middlewareMergeStrategy: 'append' })
expect(callTimeAppendedRightPet).to.deep.equal(pet);
expect(CallOrder).deep.equal(['base-pre','call-pre','call-post','base-post'])
})
it("prependMiddleware pet id", async () => {
const wrongId = pet.id + 100
const configuration = petstore.createConfiguration({ promiseMiddleware: [overridePetIDMiddleware(pet.id)] })
const petApi = new petstore.PetApi(configuration)
const callTimeAppendedRightPet = await petApi.getPetById(wrongId, { middleware: [overridePetIDMiddleware(wrongId)], middlewareMergeStrategy: 'prepend' })
expect(callTimeAppendedRightPet).to.deep.equal(pet);
}) })
it("should override http api from option", async () => { it("should override http api from option", async () => {