Skip to content

Commit

Permalink
[typescript] Adding Custom Agent Support for fetch call (#11400)
Browse files Browse the repository at this point in the history
* add custom agent support

* samples

* more samples

* merge master files

* only enable custom agent on nodejs not browser

* samples again

* samples

* samples once more
  • Loading branch information
davidgamero committed Jan 28, 2022
1 parent 24366be commit b2bb5d0
Show file tree
Hide file tree
Showing 8 changed files with 58 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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}}
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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}}
Expand Down
Original file line number Diff line number Diff line change
@@ -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";
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down
Original file line number Diff line number Diff line change
@@ -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";
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down
Original file line number Diff line number Diff line change
@@ -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";
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down

0 comments on commit b2bb5d0

Please sign in to comment.