From b2bb5d071e296adff31dca8a72fd2f558538b502 Mon Sep 17 00:00:00 2001 From: David Gamero Date: Fri, 28 Jan 2022 06:01:32 -0500 Subject: [PATCH] [typescript] Adding Custom Agent Support for fetch call (#11400) * add custom agent support * samples * more samples * merge master files * only enable custom agent on nodejs not browser * samples again * samples * samples once more --- .../resources/typescript/http/http.mustache | 19 +++++++++++++++++++ .../typescript/http/isomorphic-fetch.mustache | 3 +++ .../typescript/builds/default/http/http.ts | 11 +++++++++++ .../builds/default/http/isomorphic-fetch.ts | 1 + .../typescript/builds/inversify/http/http.ts | 11 +++++++++++ .../builds/inversify/http/isomorphic-fetch.ts | 1 + .../builds/object_params/http/http.ts | 11 +++++++++++ .../object_params/http/isomorphic-fetch.ts | 1 + 8 files changed, 58 insertions(+) diff --git a/modules/openapi-generator/src/main/resources/typescript/http/http.mustache b/modules/openapi-generator/src/main/resources/typescript/http/http.mustache index 2f71d86109f2..2f66d4b8996e 100644 --- a/modules/openapi-generator/src/main/resources/typescript/http/http.mustache +++ b/modules/openapi-generator/src/main/resources/typescript/http/http.mustache @@ -3,6 +3,8 @@ // TODO: evaluate if we can easily get rid of this library import * as FormData from "form-data"; import { URLSearchParams } from 'url'; +import * as http from 'http'; +import * as https from 'https'; {{/node}} {{/platforms}} {{#platforms}} @@ -113,6 +115,11 @@ export class RequestContext { private headers: { [key: string]: string } = {}; private body: RequestBody = undefined; private url: URLParse; + {{#platforms}} + {{#node}} + private agent: http.Agent | https.Agent | undefined = undefined; + {{/node}} + {{/platforms}} /** * Creates the request context using a http method and request resource url @@ -185,6 +192,18 @@ export class RequestContext { public setHeaderParam(key: string, value: string): void { this.headers[key] = value; } + {{#platforms}} + {{#node}} + + public setAgent(agent: http.Agent | https.Agent) { + this.agent = agent; + } + + public getAgent(): http.Agent | https.Agent | undefined { + return this.agent; + } + {{/node}} + {{/platforms}} } export interface ResponseBody { diff --git a/modules/openapi-generator/src/main/resources/typescript/http/isomorphic-fetch.mustache b/modules/openapi-generator/src/main/resources/typescript/http/isomorphic-fetch.mustache index 7646836694b3..57b1ae594f6f 100644 --- a/modules/openapi-generator/src/main/resources/typescript/http/isomorphic-fetch.mustache +++ b/modules/openapi-generator/src/main/resources/typescript/http/isomorphic-fetch.mustache @@ -20,6 +20,9 @@ export class IsomorphicFetchHttpLibrary implements HttpLibrary { body: body as any, headers: request.getHeaders(), {{#platforms}} + {{#node}} + agent: request.getAgent(), + {{/node}} {{#browser}} credentials: "same-origin" {{/browser}} diff --git a/samples/openapi3/client/petstore/typescript/builds/default/http/http.ts b/samples/openapi3/client/petstore/typescript/builds/default/http/http.ts index a623aef12789..3b6e1461d924 100644 --- a/samples/openapi3/client/petstore/typescript/builds/default/http/http.ts +++ b/samples/openapi3/client/petstore/typescript/builds/default/http/http.ts @@ -1,6 +1,8 @@ // TODO: evaluate if we can easily get rid of this library import * as FormData from "form-data"; import { URLSearchParams } from 'url'; +import * as http from 'http'; +import * as https from 'https'; // typings of url-parse are incorrect... // @ts-ignore import * as URLParse from "url-parse"; @@ -50,6 +52,7 @@ export class RequestContext { private headers: { [key: string]: string } = {}; private body: RequestBody = undefined; private url: URLParse; + private agent: http.Agent | https.Agent | undefined = undefined; /** * Creates the request context using a http method and request resource url @@ -122,6 +125,14 @@ export class RequestContext { public setHeaderParam(key: string, value: string): void { this.headers[key] = value; } + + public setAgent(agent: http.Agent | https.Agent) { + this.agent = agent; + } + + public getAgent(): http.Agent | https.Agent | undefined { + return this.agent; + } } export interface ResponseBody { diff --git a/samples/openapi3/client/petstore/typescript/builds/default/http/isomorphic-fetch.ts b/samples/openapi3/client/petstore/typescript/builds/default/http/isomorphic-fetch.ts index ed390df7ce10..26d267cfc063 100644 --- a/samples/openapi3/client/petstore/typescript/builds/default/http/isomorphic-fetch.ts +++ b/samples/openapi3/client/petstore/typescript/builds/default/http/isomorphic-fetch.ts @@ -12,6 +12,7 @@ export class IsomorphicFetchHttpLibrary implements HttpLibrary { method: method, body: body as any, headers: request.getHeaders(), + agent: request.getAgent(), }).then((resp: any) => { const headers: { [name: string]: string } = {}; resp.headers.forEach((value: string, name: string) => { diff --git a/samples/openapi3/client/petstore/typescript/builds/inversify/http/http.ts b/samples/openapi3/client/petstore/typescript/builds/inversify/http/http.ts index a623aef12789..3b6e1461d924 100644 --- a/samples/openapi3/client/petstore/typescript/builds/inversify/http/http.ts +++ b/samples/openapi3/client/petstore/typescript/builds/inversify/http/http.ts @@ -1,6 +1,8 @@ // TODO: evaluate if we can easily get rid of this library import * as FormData from "form-data"; import { URLSearchParams } from 'url'; +import * as http from 'http'; +import * as https from 'https'; // typings of url-parse are incorrect... // @ts-ignore import * as URLParse from "url-parse"; @@ -50,6 +52,7 @@ export class RequestContext { private headers: { [key: string]: string } = {}; private body: RequestBody = undefined; private url: URLParse; + private agent: http.Agent | https.Agent | undefined = undefined; /** * Creates the request context using a http method and request resource url @@ -122,6 +125,14 @@ export class RequestContext { public setHeaderParam(key: string, value: string): void { this.headers[key] = value; } + + public setAgent(agent: http.Agent | https.Agent) { + this.agent = agent; + } + + public getAgent(): http.Agent | https.Agent | undefined { + return this.agent; + } } export interface ResponseBody { diff --git a/samples/openapi3/client/petstore/typescript/builds/inversify/http/isomorphic-fetch.ts b/samples/openapi3/client/petstore/typescript/builds/inversify/http/isomorphic-fetch.ts index ed390df7ce10..26d267cfc063 100644 --- a/samples/openapi3/client/petstore/typescript/builds/inversify/http/isomorphic-fetch.ts +++ b/samples/openapi3/client/petstore/typescript/builds/inversify/http/isomorphic-fetch.ts @@ -12,6 +12,7 @@ export class IsomorphicFetchHttpLibrary implements HttpLibrary { method: method, body: body as any, headers: request.getHeaders(), + agent: request.getAgent(), }).then((resp: any) => { const headers: { [name: string]: string } = {}; resp.headers.forEach((value: string, name: string) => { diff --git a/samples/openapi3/client/petstore/typescript/builds/object_params/http/http.ts b/samples/openapi3/client/petstore/typescript/builds/object_params/http/http.ts index a623aef12789..3b6e1461d924 100644 --- a/samples/openapi3/client/petstore/typescript/builds/object_params/http/http.ts +++ b/samples/openapi3/client/petstore/typescript/builds/object_params/http/http.ts @@ -1,6 +1,8 @@ // TODO: evaluate if we can easily get rid of this library import * as FormData from "form-data"; import { URLSearchParams } from 'url'; +import * as http from 'http'; +import * as https from 'https'; // typings of url-parse are incorrect... // @ts-ignore import * as URLParse from "url-parse"; @@ -50,6 +52,7 @@ export class RequestContext { private headers: { [key: string]: string } = {}; private body: RequestBody = undefined; private url: URLParse; + private agent: http.Agent | https.Agent | undefined = undefined; /** * Creates the request context using a http method and request resource url @@ -122,6 +125,14 @@ export class RequestContext { public setHeaderParam(key: string, value: string): void { this.headers[key] = value; } + + public setAgent(agent: http.Agent | https.Agent) { + this.agent = agent; + } + + public getAgent(): http.Agent | https.Agent | undefined { + return this.agent; + } } export interface ResponseBody { diff --git a/samples/openapi3/client/petstore/typescript/builds/object_params/http/isomorphic-fetch.ts b/samples/openapi3/client/petstore/typescript/builds/object_params/http/isomorphic-fetch.ts index ed390df7ce10..26d267cfc063 100644 --- a/samples/openapi3/client/petstore/typescript/builds/object_params/http/isomorphic-fetch.ts +++ b/samples/openapi3/client/petstore/typescript/builds/object_params/http/isomorphic-fetch.ts @@ -12,6 +12,7 @@ export class IsomorphicFetchHttpLibrary implements HttpLibrary { method: method, body: body as any, headers: request.getHeaders(), + agent: request.getAgent(), }).then((resp: any) => { const headers: { [name: string]: string } = {}; resp.headers.forEach((value: string, name: string) => {