/
requestService.ts
147 lines (127 loc) · 4.11 KB
/
requestService.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
/*
* Copyright (c) 2017, salesforce.com, inc.
* All rights reserved.
* Licensed under the BSD 3-Clause license.
* For full license text, see LICENSE.txt file in the repo root or https://opensource.org/licenses/BSD-3-Clause
*/
import { configure, xhr, XHROptions, XHRResponse } from 'request-light';
import {
CLIENT_ID,
DEFAULT_CONNECTION_TIMEOUT_MS,
ENV_SFDX_DEFAULTUSERNAME,
ENV_SFDX_INSTANCE_URL
} from '../constants';
import { BaseCommand } from './baseCommand';
// Right now have POST and DELETE (out of Query, GET, POST, PATCH, DELETE),
// add any new ones needed as they are encountered. Note: when adding those
// it'll be the responsiblity of whomever added them to verify or change
// anything in the arguments for the call to deal with them.
export enum RestHttpMethodEnum {
Delete = 'DELETE',
Get = 'GET',
Post = 'POST'
}
export class RequestService {
private _instanceUrl!: string;
private _accessToken!: string;
private _proxyUrl!: string;
private _proxyStrictSSL: boolean = false;
private _proxyAuthorization!: string;
private _connectionTimeoutMs: number = DEFAULT_CONNECTION_TIMEOUT_MS;
public getEnvVars(): any {
const envVars = Object.assign({}, process.env);
const proxyUrl = this.proxyUrl;
if (proxyUrl) {
envVars['HTTP_PROXY'] = proxyUrl;
envVars['HTTPS_PROXY'] = proxyUrl;
}
const instanceUrl = this.instanceUrl;
if (instanceUrl) {
envVars[ENV_SFDX_INSTANCE_URL] = instanceUrl;
}
const sid = this.accessToken;
if (sid) {
envVars[ENV_SFDX_DEFAULTUSERNAME] = sid;
}
return envVars;
}
public get instanceUrl(): string {
return this._instanceUrl;
}
public set instanceUrl(instanceUrl: string) {
this._instanceUrl = instanceUrl;
}
public get accessToken(): string {
return this._accessToken;
}
public set accessToken(accessToken: string) {
this._accessToken = accessToken;
}
public get proxyUrl(): string {
return this._proxyUrl;
}
public set proxyUrl(proxyUrl: string) {
this._proxyUrl = proxyUrl;
}
public get proxyStrictSSL(): boolean {
return this._proxyStrictSSL;
}
public set proxyStrictSSL(proxyStrictSSL: boolean) {
this._proxyStrictSSL = proxyStrictSSL;
}
public get proxyAuthorization(): string {
return this._proxyAuthorization;
}
public set proxyAuthorization(proxyAuthorization: string) {
this._proxyAuthorization = proxyAuthorization;
}
public get connectionTimeoutMs(): number {
return this._connectionTimeoutMs || DEFAULT_CONNECTION_TIMEOUT_MS;
}
public set connectionTimeoutMs(connectionTimeoutMs: number) {
this._connectionTimeoutMs = connectionTimeoutMs;
}
// Execute defaults to POST
public async execute(
command: BaseCommand,
restHttpMethodEnum: RestHttpMethodEnum = RestHttpMethodEnum.Post
): Promise<string> {
if (this.proxyUrl) {
configure(this._proxyUrl, this._proxyStrictSSL);
}
const urlElements = [this.instanceUrl, command.getCommandUrl()];
const requestUrl =
command.getQueryString() == null
? urlElements.join('/')
: urlElements.join('/').concat('?', command.getQueryString()!);
const requestBody = command.getRequest();
const options: XHROptions = {
type: restHttpMethodEnum,
url: requestUrl,
timeout: this.connectionTimeoutMs,
headers: {
'Content-Type': 'application/json;charset=utf-8',
Accept: 'application/json',
Authorization: `OAuth ${this.accessToken}`,
'Content-Length': requestBody
? Buffer.byteLength(requestBody, 'utf-8')
: 0,
'Sforce-Call-Options': `client=${CLIENT_ID}`
},
data: requestBody
};
if (this.proxyAuthorization) {
options.headers['Proxy-Authorization'] = this.proxyAuthorization;
}
try {
const response = await this.sendRequest(options);
return Promise.resolve(response.responseText);
} catch (error) {
const xhrResponse: XHRResponse = error;
return Promise.reject(xhrResponse.responseText);
}
}
public async sendRequest(options: XHROptions): Promise<XHRResponse> {
return xhr(options);
}
}