[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
This commit is contained in:
David Gamero 2022-01-28 06:01:32 -05:00 committed by GitHub
parent 24366be0db
commit b2bb5d071e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 58 additions and 0 deletions

View File

@ -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 {

View File

@ -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}}

View File

@ -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 {

View File

@ -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) => {

View File

@ -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 {

View File

@ -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) => {

View File

@ -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 {

View File

@ -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) => {