@zcatalyst/transport 0.0.2 → 0.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENCE +55 -1
- package/README.md +266 -0
- package/dist-cjs/fetch-handler.js +67 -64
- package/dist-cjs/http-handler.js +25 -23
- package/dist-cjs/index.browser.js +8 -3
- package/dist-cjs/index.js +9 -7
- package/dist-cjs/utils/clonable-stream.js +1 -1
- package/dist-cjs/utils/form-data.js +11 -5
- package/dist-cjs/utils/request-agent.js +1 -1
- package/dist-es/fetch-handler.js +64 -64
- package/dist-es/http-handler.js +25 -24
- package/dist-es/index.browser.js +3 -2
- package/dist-es/index.js +3 -5
- package/dist-es/utils/clonable-stream.js +0 -1
- package/dist-es/utils/form-data.js +10 -5
- package/dist-es/utils/request-agent.js +0 -1
- package/dist-types/fetch-handler.d.ts +115 -6
- package/dist-types/http-handler.d.ts +48 -8
- package/dist-types/index.browser.d.ts +17 -1
- package/dist-types/index.d.ts +22 -2
- package/dist-types/ts3.4/fetch-handler.d.ts +115 -6
- package/dist-types/ts3.4/http-handler.d.ts +48 -8
- package/dist-types/ts3.4/index.browser.d.ts +17 -1
- package/dist-types/ts3.4/index.d.ts +22 -2
- package/dist-types/ts3.4/utils/clonable-stream.d.ts +54 -0
- package/dist-types/ts3.4/utils/errors.d.ts +7 -0
- package/dist-types/ts3.4/utils/form-data.d.ts +242 -1
- package/dist-types/ts3.4/utils/helpers.d.ts +12 -0
- package/dist-types/ts3.4/utils/interfaces.d.ts +5 -1
- package/dist-types/ts3.4/utils/request-agent.d.ts +6 -0
- package/dist-types/ts3.4/utils/request-timeout.d.ts +12 -0
- package/dist-types/utils/clonable-stream.d.ts +54 -0
- package/dist-types/utils/errors.d.ts +7 -0
- package/dist-types/utils/form-data.d.ts +242 -1
- package/dist-types/utils/helpers.d.ts +12 -0
- package/dist-types/utils/interfaces.d.ts +5 -1
- package/dist-types/utils/request-agent.d.ts +6 -0
- package/dist-types/utils/request-timeout.d.ts +12 -0
- package/package.json +31 -6
package/dist-cjs/http-handler.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
|
|
1
|
+
"use strict";
|
|
2
2
|
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
3
|
if (k2 === undefined) k2 = k;
|
|
4
4
|
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
@@ -37,6 +37,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
37
37
|
};
|
|
38
38
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
39
39
|
exports.AuthorizedHttpClient = exports.HttpClient = exports.DefaultHttpResponse = void 0;
|
|
40
|
+
const auth_admin_1 = require("@zcatalyst/auth-admin");
|
|
40
41
|
const utils_1 = require("@zcatalyst/utils");
|
|
41
42
|
const http_1 = __importDefault(require("http"));
|
|
42
43
|
const https_1 = __importDefault(require("https"));
|
|
@@ -45,12 +46,13 @@ const querystring_1 = require("querystring");
|
|
|
45
46
|
const stream_1 = require("stream");
|
|
46
47
|
const url_1 = require("url");
|
|
47
48
|
const util_1 = require("util");
|
|
48
|
-
const package_json_1 = require("../package.json");
|
|
49
|
+
const package_json_1 = __importDefault(require("../package.json"));
|
|
50
|
+
const { version } = package_json_1.default;
|
|
49
51
|
const errors_1 = require("./utils/errors");
|
|
50
52
|
const form_data_1 = __importDefault(require("./utils/form-data"));
|
|
51
53
|
const helpers_1 = require("./utils/helpers");
|
|
52
54
|
const request_agent_1 = __importDefault(require("./utils/request-agent"));
|
|
53
|
-
const {
|
|
55
|
+
const { IS_LOCAL, USER_KEY_NAME, CREDENTIAL_USER, CATALYST_ORIGIN, USER_AGENT, APM_INSIGHT, ACCEPT_HEADER, REQ_RETRY_THRESHOLD, IS_APM } = utils_1.CONSTANTS;
|
|
54
56
|
class DefaultHttpResponse {
|
|
55
57
|
constructor(resp) {
|
|
56
58
|
this.statusCode = resp.statusCode;
|
|
@@ -96,7 +98,7 @@ function rejectWithContext(reject, statusCode, data) {
|
|
|
96
98
|
});
|
|
97
99
|
return;
|
|
98
100
|
}
|
|
99
|
-
catch
|
|
101
|
+
catch {
|
|
100
102
|
reject({
|
|
101
103
|
statusCode,
|
|
102
104
|
message: (0, util_1.inspect)(data)
|
|
@@ -246,6 +248,11 @@ async function _request(transport, options, config, data, retryCount = 0) {
|
|
|
246
248
|
if (data instanceof web_1.ReadableStream) {
|
|
247
249
|
data = webStreamToNodeStream(data);
|
|
248
250
|
}
|
|
251
|
+
if (typeof data === 'string' || Buffer.isBuffer(data)) {
|
|
252
|
+
req.write(data);
|
|
253
|
+
req.end();
|
|
254
|
+
return;
|
|
255
|
+
}
|
|
249
256
|
data.on('error', (er) => {
|
|
250
257
|
reject(er);
|
|
251
258
|
req.end();
|
|
@@ -267,10 +274,14 @@ function webStreamToNodeStream(webStream) {
|
|
|
267
274
|
}
|
|
268
275
|
});
|
|
269
276
|
}
|
|
270
|
-
async function sendRequest(config) {
|
|
277
|
+
async function sendRequest(config, componentName, componentVersion) {
|
|
271
278
|
let data;
|
|
279
|
+
let userAgent = USER_AGENT.PREFIX + version;
|
|
280
|
+
if (componentName) {
|
|
281
|
+
userAgent += ` ${componentName}/${componentVersion || 'unknown'}`;
|
|
282
|
+
}
|
|
272
283
|
let headers = Object.assign({
|
|
273
|
-
[USER_AGENT.KEY]:
|
|
284
|
+
[USER_AGENT.KEY]: userAgent
|
|
274
285
|
}, config.headers);
|
|
275
286
|
if (config.data !== undefined) {
|
|
276
287
|
switch (config.type) {
|
|
@@ -320,27 +331,17 @@ async function sendRequest(config) {
|
|
|
320
331
|
class HttpClient {
|
|
321
332
|
constructor(app) {
|
|
322
333
|
this.app = app;
|
|
323
|
-
this.user = CREDENTIAL_USER.admin;
|
|
324
334
|
}
|
|
325
|
-
async send(req, apmTrackerName) {
|
|
335
|
+
async send(req, apmTrackerName, componentVersion) {
|
|
326
336
|
req.headers = Object.assign({}, req.headers);
|
|
327
337
|
req.qs = Object.assign({}, req.qs);
|
|
328
338
|
req.retry = req.retry || true;
|
|
329
339
|
if (this.app !== undefined && req.service !== "external") {
|
|
330
|
-
|
|
331
|
-
req.headers
|
|
332
|
-
req.headers[ENVIRONMENT_KEY_NAME] = this.app.config.environment;
|
|
333
|
-
req.headers[ENVIRONMENT] = this.app.config.environment;
|
|
334
|
-
if ((0, utils_1.isNonEmptyString)(process.env.X_ZOHO_CATALYST_ORG_ID)) {
|
|
335
|
-
req.headers[X_ZOHO_CATALYST_ORG_ID] = process.env.X_ZOHO_CATALYST_ORG_ID;
|
|
336
|
-
}
|
|
337
|
-
if ((0, utils_1.isNonEmptyString)(this.app.config.projectSecretKey)) {
|
|
338
|
-
req.headers[PROJECT_HEADER.projectSecretKey] = this.app.config
|
|
339
|
-
.projectSecretKey;
|
|
340
|
-
}
|
|
340
|
+
const user = this.app.credential.getCurrentUser();
|
|
341
|
+
req.headers = (0, auth_admin_1.addDefaultAppHeaders)(req.headers, this.app.config);
|
|
341
342
|
req.headers[USER_KEY_NAME] = this.app.credential.getCurrentUserType();
|
|
342
343
|
if (IS_LOCAL === 'true') {
|
|
343
|
-
switch (
|
|
344
|
+
switch (user) {
|
|
344
345
|
case CREDENTIAL_USER.admin:
|
|
345
346
|
req.origin =
|
|
346
347
|
'https://' +
|
|
@@ -363,14 +364,14 @@ class HttpClient {
|
|
|
363
364
|
if (req.track && apmTrackerName && IS_APM === 'true') {
|
|
364
365
|
try {
|
|
365
366
|
const apminsight = await Promise.resolve().then(() => __importStar(require('apminsight')));
|
|
366
|
-
resp = await apminsight.startTracker(APM_INSIGHT.tracker_name, apmTrackerName, () => sendRequest(req));
|
|
367
|
+
resp = await apminsight.startTracker(APM_INSIGHT.tracker_name, apmTrackerName, () => sendRequest(req, apmTrackerName, componentVersion));
|
|
367
368
|
}
|
|
368
369
|
catch (err) {
|
|
369
370
|
throw new errors_1.CatalystAPIError('APM_TRACKER_ERROR', 'To enable APM tracking locally, please download the apminsight package from the UI and place it in the node_modules directory of your project.', err, 400);
|
|
370
371
|
}
|
|
371
372
|
}
|
|
372
373
|
else {
|
|
373
|
-
resp = await sendRequest(req);
|
|
374
|
+
resp = await sendRequest(req, apmTrackerName, componentVersion);
|
|
374
375
|
}
|
|
375
376
|
return new DefaultHttpResponse(resp);
|
|
376
377
|
}
|
|
@@ -388,6 +389,7 @@ class AuthorizedHttpClient extends HttpClient {
|
|
|
388
389
|
super(app);
|
|
389
390
|
if (component) {
|
|
390
391
|
this.componentName = component.getComponentName();
|
|
392
|
+
this.componentVersion = component.getComponentVersion?.();
|
|
391
393
|
}
|
|
392
394
|
}
|
|
393
395
|
async send(request) {
|
|
@@ -396,7 +398,7 @@ class AuthorizedHttpClient extends HttpClient {
|
|
|
396
398
|
if (request.auth !== false) {
|
|
397
399
|
await this.app?.authenticateRequest(requestCopy);
|
|
398
400
|
}
|
|
399
|
-
return await super.send(requestCopy, this.componentName);
|
|
401
|
+
return await super.send(requestCopy, this.componentName, this.componentVersion);
|
|
400
402
|
}
|
|
401
403
|
}
|
|
402
404
|
exports.AuthorizedHttpClient = AuthorizedHttpClient;
|
|
@@ -1,13 +1,18 @@
|
|
|
1
|
-
|
|
1
|
+
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.CatalystAPIError = exports.ResponseType = exports.RequestType = exports.Handler = void 0;
|
|
3
|
+
exports.CatalystAPIError = exports.ResponseType = exports.RequestType = exports.Handler = exports.PrefixedCatalystError = exports.CONSTANTS = exports.CatalystService = void 0;
|
|
4
4
|
const fetch_handler_1 = require("./fetch-handler");
|
|
5
|
+
var utils_1 = require("@zcatalyst/utils");
|
|
6
|
+
Object.defineProperty(exports, "CatalystService", { enumerable: true, get: function () { return utils_1.CatalystService; } });
|
|
7
|
+
Object.defineProperty(exports, "CONSTANTS", { enumerable: true, get: function () { return utils_1.CONSTANTS; } });
|
|
8
|
+
var utils_2 = require("@zcatalyst/utils");
|
|
9
|
+
Object.defineProperty(exports, "PrefixedCatalystError", { enumerable: true, get: function () { return utils_2.PrefixedCatalystError; } });
|
|
5
10
|
class Handler {
|
|
6
11
|
constructor(app, component) {
|
|
7
12
|
this.component = component;
|
|
8
13
|
}
|
|
9
14
|
async send(options) {
|
|
10
|
-
return (await fetch_handler_1.ResponseHandler.send(options));
|
|
15
|
+
return (await fetch_handler_1.ResponseHandler.send(options, this.component?.getComponentName(), this.component?.getComponentVersion?.()));
|
|
11
16
|
}
|
|
12
17
|
}
|
|
13
18
|
exports.Handler = Handler;
|
package/dist-cjs/index.js
CHANGED
|
@@ -1,20 +1,22 @@
|
|
|
1
|
-
|
|
1
|
+
"use strict";
|
|
2
2
|
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
3
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
4
|
};
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
exports.FormData = exports.CatalystAPIError = exports.ResponseType = exports.RequestType = exports.Handler = void 0;
|
|
7
|
-
const
|
|
6
|
+
exports.FormData = exports.CatalystAPIError = exports.ResponseType = exports.RequestType = exports.Handler = exports.PrefixedCatalystError = exports.CONSTANTS = exports.CatalystService = void 0;
|
|
7
|
+
const auth_admin_1 = require("@zcatalyst/auth-admin");
|
|
8
8
|
const http_handler_1 = require("./http-handler");
|
|
9
9
|
const form_data_1 = __importDefault(require("./utils/form-data"));
|
|
10
10
|
exports.FormData = form_data_1.default;
|
|
11
|
+
var utils_1 = require("@zcatalyst/utils");
|
|
12
|
+
Object.defineProperty(exports, "CatalystService", { enumerable: true, get: function () { return utils_1.CatalystService; } });
|
|
13
|
+
Object.defineProperty(exports, "CONSTANTS", { enumerable: true, get: function () { return utils_1.CONSTANTS; } });
|
|
14
|
+
var utils_2 = require("@zcatalyst/utils");
|
|
15
|
+
Object.defineProperty(exports, "PrefixedCatalystError", { enumerable: true, get: function () { return utils_2.PrefixedCatalystError; } });
|
|
11
16
|
class Handler {
|
|
12
17
|
constructor(app, component) {
|
|
13
18
|
if (!app) {
|
|
14
|
-
app = new
|
|
15
|
-
}
|
|
16
|
-
if (!(app instanceof auth_1.CatalystApp)) {
|
|
17
|
-
throw new auth_1.CatalystAppError('INVALID_PROJECT_CREDENTIALS', 'Unable to process the project credentials. Please verify that the initialization is configured correctly.');
|
|
19
|
+
app = new auth_admin_1.ZCAuth().getDefaultCredentials();
|
|
18
20
|
}
|
|
19
21
|
this.app = app;
|
|
20
22
|
this.component = component;
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
|
|
1
|
+
"use strict";
|
|
2
2
|
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
3
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
4
|
};
|
|
@@ -45,11 +45,17 @@ class FormData extends stream_1.Stream {
|
|
|
45
45
|
_getContentDisposition(value) {
|
|
46
46
|
let filename = '';
|
|
47
47
|
let contentDisposition = '';
|
|
48
|
-
if (value
|
|
49
|
-
|
|
48
|
+
if (typeof value === 'object' && value !== null && ('name' in value || 'path' in value)) {
|
|
49
|
+
const val = value;
|
|
50
|
+
filename = (0, path_1.basename)(val.name || val.path || '');
|
|
50
51
|
}
|
|
51
|
-
else if (value
|
|
52
|
-
|
|
52
|
+
else if (typeof value === 'object' &&
|
|
53
|
+
value !== null &&
|
|
54
|
+
'readable' in value &&
|
|
55
|
+
'httpVersion' in value &&
|
|
56
|
+
'client' in value) {
|
|
57
|
+
const httpValue = value;
|
|
58
|
+
filename = (0, path_1.basename)(httpValue.client._httpMessage.path || '');
|
|
53
59
|
}
|
|
54
60
|
if (filename) {
|
|
55
61
|
contentDisposition = 'filename="' + filename + '"';
|
package/dist-es/fetch-handler.js
CHANGED
|
@@ -4,15 +4,14 @@ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (
|
|
|
4
4
|
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
|
|
5
5
|
};
|
|
6
6
|
var _a, _ResponseHandler_attachAppSpecificHeaders, _ResponseHandler_followZcrfTokenProtocol, _ResponseHandler_followJwtZCAuthProtocol, _ResponseHandler_sendRequest;
|
|
7
|
-
import { Auth_Protocol,
|
|
8
|
-
import { CONSTANTS, getServicePath
|
|
7
|
+
import { addDefaultAppHeaders, Auth_Protocol, collectZCRFToken, ConfigStore, getToken, JWT_COOKIE_PREFIX, PROJECT_ID } from '@zcatalyst/auth-client';
|
|
8
|
+
import { CONSTANTS, getServicePath } from '@zcatalyst/utils';
|
|
9
|
+
import pkg from '../package.json';
|
|
10
|
+
const { version } = pkg;
|
|
9
11
|
import { HTTP_HEADER_MAP as HEADER_MAP, HTTP_HEADER_MAP, X_ZCSRF_TOKEN, ZD_CSRPARAM } from './utils/constants';
|
|
10
12
|
import { CatalystAPIError } from './utils/errors';
|
|
11
13
|
import { requestTimeout } from './utils/request-timeout';
|
|
12
14
|
const { REQ_METHOD } = CONSTANTS;
|
|
13
|
-
export const keepAliveSupport = {
|
|
14
|
-
supported: undefined
|
|
15
|
-
};
|
|
16
15
|
export class DefaultHttpResponse {
|
|
17
16
|
constructor(resp) {
|
|
18
17
|
this.statusCode = resp.statusCode;
|
|
@@ -33,13 +32,16 @@ export class ResponseHandler {
|
|
|
33
32
|
static async fireGeneralRequest({ requestCore, url }, requestOptions = {}) {
|
|
34
33
|
try {
|
|
35
34
|
const headers = requestCore.headers || {};
|
|
35
|
+
const method = requestCore.method;
|
|
36
|
+
const methodAllowsBody = method !== REQ_METHOD.get && method !== REQ_METHOD.head;
|
|
36
37
|
const options = {
|
|
37
|
-
method
|
|
38
|
+
method,
|
|
38
39
|
headers,
|
|
39
40
|
credentials: requestOptions.auth ? 'include' : 'omit',
|
|
40
|
-
body:
|
|
41
|
+
body: methodAllowsBody ? requestCore.body : undefined
|
|
41
42
|
};
|
|
42
|
-
|
|
43
|
+
const isExternal = requestOptions.service === "external";
|
|
44
|
+
if (requestOptions.auth && !isExternal) {
|
|
43
45
|
options.headers = await _a.attachZCAuthHeaders(headers);
|
|
44
46
|
options.headers = __classPrivateFieldGet(this, _a, "m", _ResponseHandler_attachAppSpecificHeaders).call(this, headers);
|
|
45
47
|
}
|
|
@@ -63,7 +65,7 @@ export class ResponseHandler {
|
|
|
63
65
|
request: requestCore
|
|
64
66
|
});
|
|
65
67
|
}
|
|
66
|
-
catch
|
|
68
|
+
catch {
|
|
67
69
|
requestOptions.retry--;
|
|
68
70
|
return this.fireGeneralRequest({ requestCore, url }, requestOptions);
|
|
69
71
|
}
|
|
@@ -88,22 +90,29 @@ export class ResponseHandler {
|
|
|
88
90
|
}
|
|
89
91
|
static async wrapResponse(response, options) {
|
|
90
92
|
try {
|
|
93
|
+
const method = options?.request?.method?.toUpperCase();
|
|
94
|
+
const hasNoBody = method === REQ_METHOD.head || response.status === 204 || response.status === 304;
|
|
91
95
|
let data;
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
96
|
+
if (hasNoBody) {
|
|
97
|
+
data = '';
|
|
98
|
+
}
|
|
99
|
+
else {
|
|
100
|
+
switch (options?.expecting || "json") {
|
|
101
|
+
case "buffer":
|
|
102
|
+
data = await response.arrayBuffer();
|
|
103
|
+
break;
|
|
104
|
+
case "raw":
|
|
105
|
+
data = await response.blob();
|
|
106
|
+
break;
|
|
107
|
+
case "json":
|
|
108
|
+
data = await response.json();
|
|
109
|
+
break;
|
|
110
|
+
case "string":
|
|
111
|
+
data = await response.text();
|
|
112
|
+
break;
|
|
113
|
+
default:
|
|
114
|
+
throw new CatalystAPIError('UNSUPPORTED_RESPONSE_TYPE', `Unsupported response type: ${options?.expecting}`);
|
|
115
|
+
}
|
|
107
116
|
}
|
|
108
117
|
return new DefaultHttpResponse({
|
|
109
118
|
headers: response.headers,
|
|
@@ -130,23 +139,22 @@ export class ResponseHandler {
|
|
|
130
139
|
};
|
|
131
140
|
if (requestCore.method !== REQ_METHOD.get && requestCore.body !== null)
|
|
132
141
|
options.body = requestCore.body;
|
|
133
|
-
const url = this.
|
|
134
|
-
? `${this.configManager.APIDomain}${requestCore.url}`
|
|
135
|
-
: requestCore.url;
|
|
142
|
+
const url = this.apiDomain ? `${this.apiDomain}${requestCore.url}` : requestCore.url;
|
|
136
143
|
return await this.wrapResponse(await fetch(url, options));
|
|
137
144
|
}
|
|
138
|
-
static attachZCAuthHeaders(headers) {
|
|
139
|
-
|
|
145
|
+
static async attachZCAuthHeaders(headers) {
|
|
146
|
+
const authProtocol = ConfigStore.get('AUTH_PROTOCOL');
|
|
147
|
+
switch (authProtocol) {
|
|
140
148
|
case Auth_Protocol.ZcrfTokenProtocol:
|
|
141
|
-
return __classPrivateFieldGet(_a, _a, "m", _ResponseHandler_followZcrfTokenProtocol).call(_a, headers);
|
|
149
|
+
return await __classPrivateFieldGet(_a, _a, "m", _ResponseHandler_followZcrfTokenProtocol).call(_a, headers);
|
|
142
150
|
case Auth_Protocol.JwtTokenProtocol:
|
|
143
|
-
return __classPrivateFieldGet(_a, _a, "m", _ResponseHandler_followJwtZCAuthProtocol).call(_a, headers);
|
|
151
|
+
return await __classPrivateFieldGet(_a, _a, "m", _ResponseHandler_followJwtZCAuthProtocol).call(_a, headers);
|
|
144
152
|
default:
|
|
145
153
|
return Promise.resolve(headers);
|
|
146
154
|
}
|
|
147
155
|
}
|
|
148
156
|
static getJWTZCAuthToken() {
|
|
149
|
-
const
|
|
157
|
+
const jwtPrefix = ConfigStore.get(JWT_COOKIE_PREFIX);
|
|
150
158
|
return new Promise((resolve, reject) => {
|
|
151
159
|
const jwtZCAuthToken = getToken();
|
|
152
160
|
if (jwtZCAuthToken === '') {
|
|
@@ -154,7 +162,7 @@ export class ResponseHandler {
|
|
|
154
162
|
}
|
|
155
163
|
else {
|
|
156
164
|
resolve({
|
|
157
|
-
access_token: `${
|
|
165
|
+
access_token: `${jwtPrefix} ${jwtZCAuthToken}`
|
|
158
166
|
});
|
|
159
167
|
}
|
|
160
168
|
});
|
|
@@ -171,8 +179,16 @@ export class ResponseHandler {
|
|
|
171
179
|
const existingParams = url.split('?')[1] || '';
|
|
172
180
|
return `${baseUrl}?${existingParams ? `${existingParams}&` : ''}${searchParams.toString()}`;
|
|
173
181
|
}
|
|
174
|
-
static async send(options) {
|
|
182
|
+
static async send(options, componentName, componentVersion) {
|
|
175
183
|
const headers = options.headers || {};
|
|
184
|
+
const isExternal = options.service === "external";
|
|
185
|
+
if (!isExternal) {
|
|
186
|
+
let userAgent = CONSTANTS.USER_AGENT.PREFIX + version;
|
|
187
|
+
if (componentName) {
|
|
188
|
+
userAgent += ` ${componentName}/${componentVersion || 'unknown'}`;
|
|
189
|
+
}
|
|
190
|
+
headers['X-Catalyst-User-Agent'] = userAgent;
|
|
191
|
+
}
|
|
176
192
|
let data = options.data;
|
|
177
193
|
if (data !== undefined) {
|
|
178
194
|
switch (options.type) {
|
|
@@ -189,25 +205,24 @@ export class ResponseHandler {
|
|
|
189
205
|
data = formData;
|
|
190
206
|
break;
|
|
191
207
|
case "raw":
|
|
192
|
-
data = JSON.stringify(data);
|
|
193
208
|
if (headers['Content-Type'] === undefined) {
|
|
194
209
|
headers['Content-Type'] = 'application/octet-stream';
|
|
195
210
|
}
|
|
211
|
+
data = new Blob([data], { type: headers['Content-Type'] });
|
|
196
212
|
break;
|
|
197
213
|
default:
|
|
198
214
|
data = JSON.stringify(data);
|
|
199
215
|
headers['Content-Type'] = 'application/x-www-form-urlencoded';
|
|
200
|
-
headers['Content-Length'] = Buffer.byteLength(data) + '';
|
|
201
216
|
}
|
|
202
217
|
}
|
|
203
|
-
if (this.
|
|
218
|
+
if (this.apiDomain === null && !options.origin) {
|
|
204
219
|
throw new CatalystAPIError('API_REQUEST_ERROR', 'Unable to get the base url');
|
|
205
220
|
}
|
|
206
|
-
if (
|
|
207
|
-
options.path = `${getServicePath(options.service)}/project/${
|
|
221
|
+
if (!isExternal && options.path) {
|
|
222
|
+
options.path = `${getServicePath(options.service)}/project/${ConfigStore.get(PROJECT_ID)}${options.path}`;
|
|
208
223
|
}
|
|
209
224
|
const request = {
|
|
210
|
-
url: options.url ?? `${options.origin ?? this.
|
|
225
|
+
url: options.url ?? `${options.origin ?? this.apiDomain}${options.path}`,
|
|
211
226
|
method: options.method,
|
|
212
227
|
...(data ? { body: data } : {}),
|
|
213
228
|
headers
|
|
@@ -215,35 +230,20 @@ export class ResponseHandler {
|
|
|
215
230
|
request.url = this.appendQueryString(request.url, options.qs);
|
|
216
231
|
return await __classPrivateFieldGet(this, _a, "m", _ResponseHandler_sendRequest).call(this, request, {
|
|
217
232
|
expecting: options.expecting,
|
|
218
|
-
auth: options.auth ?? true
|
|
233
|
+
auth: options.auth ?? true,
|
|
234
|
+
service: options.service
|
|
219
235
|
});
|
|
220
236
|
}
|
|
221
237
|
}
|
|
222
238
|
_a = ResponseHandler, _ResponseHandler_attachAppSpecificHeaders = function _ResponseHandler_attachAppSpecificHeaders(headers) {
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
if (!currentAccept) {
|
|
226
|
-
normalizedHeaders['Accept'] = 'application/vnd.catalyst.v2+json';
|
|
227
|
-
}
|
|
228
|
-
else {
|
|
229
|
-
normalizedHeaders['Accept'] = `application/vnd.catalyst.v2+json, ${currentAccept}`;
|
|
230
|
-
}
|
|
231
|
-
if (typeof this.configManager?.OrgId === 'string') {
|
|
232
|
-
normalizedHeaders['CATALYST-ORG'] = this.configManager.OrgId;
|
|
233
|
-
}
|
|
234
|
-
normalizedHeaders['CATALYST-COMPONENT'] = 'true';
|
|
239
|
+
let normalizedHeaders = headers;
|
|
240
|
+
normalizedHeaders = addDefaultAppHeaders(normalizedHeaders);
|
|
235
241
|
return normalizedHeaders;
|
|
236
242
|
}, _ResponseHandler_followZcrfTokenProtocol = async function _ResponseHandler_followZcrfTokenProtocol(headers) {
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
`${ZD_CSRPARAM}=${this.configManager.CsrfToken}`;
|
|
242
|
-
return headers;
|
|
243
|
-
})
|
|
244
|
-
.catch((err) => {
|
|
245
|
-
throw new CatalystAPIError('API_ERROR', err.message, err.status);
|
|
246
|
-
});
|
|
243
|
+
await collectZCRFToken();
|
|
244
|
+
const csrfToken = ConfigStore.get('CSRF_TOKEN');
|
|
245
|
+
headers[X_ZCSRF_TOKEN] = `${ZD_CSRPARAM}=${csrfToken}`;
|
|
246
|
+
return headers;
|
|
247
247
|
}, _ResponseHandler_followJwtZCAuthProtocol = async function _ResponseHandler_followJwtZCAuthProtocol(headers) {
|
|
248
248
|
return this.getJWTZCAuthToken()
|
|
249
249
|
.then((resp) => {
|
|
@@ -258,4 +258,4 @@ _a = ResponseHandler, _ResponseHandler_attachAppSpecificHeaders = function _Resp
|
|
|
258
258
|
const { url, ...requestCore } = request;
|
|
259
259
|
return _a.fireGeneralRequest({ url, requestCore }, options);
|
|
260
260
|
};
|
|
261
|
-
ResponseHandler.
|
|
261
|
+
ResponseHandler.apiDomain = typeof document !== 'undefined' ? document.location.origin : '';
|
package/dist-es/http-handler.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
|
|
2
|
-
import { CONSTANTS, getServicePath,
|
|
1
|
+
import { addDefaultAppHeaders } from '@zcatalyst/auth-admin';
|
|
2
|
+
import { CONSTANTS, getServicePath, LOGGER } from '@zcatalyst/utils';
|
|
3
3
|
import http from 'http';
|
|
4
4
|
import https from 'https';
|
|
5
5
|
import { ReadableStream } from 'node:stream/web';
|
|
@@ -7,12 +7,13 @@ import { stringify } from 'querystring';
|
|
|
7
7
|
import { Readable } from 'stream';
|
|
8
8
|
import { URL } from 'url';
|
|
9
9
|
import { inspect } from 'util';
|
|
10
|
-
import
|
|
10
|
+
import pkg from '../package.json';
|
|
11
|
+
const { version } = pkg;
|
|
11
12
|
import { CatalystAPIError } from './utils/errors';
|
|
12
13
|
import FORM from './utils/form-data';
|
|
13
14
|
import { isHttps } from './utils/helpers';
|
|
14
15
|
import RequestAgent from './utils/request-agent';
|
|
15
|
-
const {
|
|
16
|
+
const { IS_LOCAL, USER_KEY_NAME, CREDENTIAL_USER, CATALYST_ORIGIN, USER_AGENT, APM_INSIGHT, ACCEPT_HEADER, REQ_RETRY_THRESHOLD, IS_APM } = CONSTANTS;
|
|
16
17
|
export class DefaultHttpResponse {
|
|
17
18
|
constructor(resp) {
|
|
18
19
|
this.statusCode = resp.statusCode;
|
|
@@ -57,7 +58,7 @@ function rejectWithContext(reject, statusCode, data) {
|
|
|
57
58
|
});
|
|
58
59
|
return;
|
|
59
60
|
}
|
|
60
|
-
catch
|
|
61
|
+
catch {
|
|
61
62
|
reject({
|
|
62
63
|
statusCode,
|
|
63
64
|
message: inspect(data)
|
|
@@ -207,6 +208,11 @@ async function _request(transport, options, config, data, retryCount = 0) {
|
|
|
207
208
|
if (data instanceof ReadableStream) {
|
|
208
209
|
data = webStreamToNodeStream(data);
|
|
209
210
|
}
|
|
211
|
+
if (typeof data === 'string' || Buffer.isBuffer(data)) {
|
|
212
|
+
req.write(data);
|
|
213
|
+
req.end();
|
|
214
|
+
return;
|
|
215
|
+
}
|
|
210
216
|
data.on('error', (er) => {
|
|
211
217
|
reject(er);
|
|
212
218
|
req.end();
|
|
@@ -228,10 +234,14 @@ function webStreamToNodeStream(webStream) {
|
|
|
228
234
|
}
|
|
229
235
|
});
|
|
230
236
|
}
|
|
231
|
-
async function sendRequest(config) {
|
|
237
|
+
async function sendRequest(config, componentName, componentVersion) {
|
|
232
238
|
let data;
|
|
239
|
+
let userAgent = USER_AGENT.PREFIX + version;
|
|
240
|
+
if (componentName) {
|
|
241
|
+
userAgent += ` ${componentName}/${componentVersion || 'unknown'}`;
|
|
242
|
+
}
|
|
233
243
|
let headers = Object.assign({
|
|
234
|
-
[USER_AGENT.KEY]:
|
|
244
|
+
[USER_AGENT.KEY]: userAgent
|
|
235
245
|
}, config.headers);
|
|
236
246
|
if (config.data !== undefined) {
|
|
237
247
|
switch (config.type) {
|
|
@@ -281,27 +291,17 @@ async function sendRequest(config) {
|
|
|
281
291
|
export class HttpClient {
|
|
282
292
|
constructor(app) {
|
|
283
293
|
this.app = app;
|
|
284
|
-
this.user = CREDENTIAL_USER.admin;
|
|
285
294
|
}
|
|
286
|
-
async send(req, apmTrackerName) {
|
|
295
|
+
async send(req, apmTrackerName, componentVersion) {
|
|
287
296
|
req.headers = Object.assign({}, req.headers);
|
|
288
297
|
req.qs = Object.assign({}, req.qs);
|
|
289
298
|
req.retry = req.retry || true;
|
|
290
299
|
if (this.app !== undefined && req.service !== "external") {
|
|
291
|
-
|
|
292
|
-
req.headers
|
|
293
|
-
req.headers[ENVIRONMENT_KEY_NAME] = this.app.config.environment;
|
|
294
|
-
req.headers[ENVIRONMENT] = this.app.config.environment;
|
|
295
|
-
if (isNonEmptyString(process.env.X_ZOHO_CATALYST_ORG_ID)) {
|
|
296
|
-
req.headers[X_ZOHO_CATALYST_ORG_ID] = process.env.X_ZOHO_CATALYST_ORG_ID;
|
|
297
|
-
}
|
|
298
|
-
if (isNonEmptyString(this.app.config.projectSecretKey)) {
|
|
299
|
-
req.headers[PROJECT_HEADER.projectSecretKey] = this.app.config
|
|
300
|
-
.projectSecretKey;
|
|
301
|
-
}
|
|
300
|
+
const user = this.app.credential.getCurrentUser();
|
|
301
|
+
req.headers = addDefaultAppHeaders(req.headers, this.app.config);
|
|
302
302
|
req.headers[USER_KEY_NAME] = this.app.credential.getCurrentUserType();
|
|
303
303
|
if (IS_LOCAL === 'true') {
|
|
304
|
-
switch (
|
|
304
|
+
switch (user) {
|
|
305
305
|
case CREDENTIAL_USER.admin:
|
|
306
306
|
req.origin =
|
|
307
307
|
'https://' +
|
|
@@ -324,14 +324,14 @@ export class HttpClient {
|
|
|
324
324
|
if (req.track && apmTrackerName && IS_APM === 'true') {
|
|
325
325
|
try {
|
|
326
326
|
const apminsight = await import('apminsight');
|
|
327
|
-
resp = await apminsight.startTracker(APM_INSIGHT.tracker_name, apmTrackerName, () => sendRequest(req));
|
|
327
|
+
resp = await apminsight.startTracker(APM_INSIGHT.tracker_name, apmTrackerName, () => sendRequest(req, apmTrackerName, componentVersion));
|
|
328
328
|
}
|
|
329
329
|
catch (err) {
|
|
330
330
|
throw new CatalystAPIError('APM_TRACKER_ERROR', 'To enable APM tracking locally, please download the apminsight package from the UI and place it in the node_modules directory of your project.', err, 400);
|
|
331
331
|
}
|
|
332
332
|
}
|
|
333
333
|
else {
|
|
334
|
-
resp = await sendRequest(req);
|
|
334
|
+
resp = await sendRequest(req, apmTrackerName, componentVersion);
|
|
335
335
|
}
|
|
336
336
|
return new DefaultHttpResponse(resp);
|
|
337
337
|
}
|
|
@@ -348,6 +348,7 @@ export class AuthorizedHttpClient extends HttpClient {
|
|
|
348
348
|
super(app);
|
|
349
349
|
if (component) {
|
|
350
350
|
this.componentName = component.getComponentName();
|
|
351
|
+
this.componentVersion = component.getComponentVersion?.();
|
|
351
352
|
}
|
|
352
353
|
}
|
|
353
354
|
async send(request) {
|
|
@@ -356,6 +357,6 @@ export class AuthorizedHttpClient extends HttpClient {
|
|
|
356
357
|
if (request.auth !== false) {
|
|
357
358
|
await this.app?.authenticateRequest(requestCopy);
|
|
358
359
|
}
|
|
359
|
-
return await super.send(requestCopy, this.componentName);
|
|
360
|
+
return await super.send(requestCopy, this.componentName, this.componentVersion);
|
|
360
361
|
}
|
|
361
362
|
}
|
package/dist-es/index.browser.js
CHANGED
|
@@ -1,11 +1,12 @@
|
|
|
1
|
-
'use strict';
|
|
2
1
|
import { ResponseHandler } from './fetch-handler';
|
|
2
|
+
export { CatalystService, CONSTANTS } from '@zcatalyst/utils';
|
|
3
|
+
export { PrefixedCatalystError } from '@zcatalyst/utils';
|
|
3
4
|
export class Handler {
|
|
4
5
|
constructor(app, component) {
|
|
5
6
|
this.component = component;
|
|
6
7
|
}
|
|
7
8
|
async send(options) {
|
|
8
|
-
return (await ResponseHandler.send(options));
|
|
9
|
+
return (await ResponseHandler.send(options, this.component?.getComponentName(), this.component?.getComponentVersion?.()));
|
|
9
10
|
}
|
|
10
11
|
}
|
|
11
12
|
export { RequestType, ResponseType } from './utils/enums';
|
package/dist-es/index.js
CHANGED
|
@@ -1,15 +1,13 @@
|
|
|
1
|
-
|
|
2
|
-
import { CatalystApp, CatalystAppError, ZCAuth } from '@zcatalyst/auth';
|
|
1
|
+
import { ZCAuth } from '@zcatalyst/auth-admin';
|
|
3
2
|
import { AuthorizedHttpClient } from './http-handler';
|
|
4
3
|
import FormData from './utils/form-data';
|
|
4
|
+
export { CatalystService, CONSTANTS } from '@zcatalyst/utils';
|
|
5
|
+
export { PrefixedCatalystError } from '@zcatalyst/utils';
|
|
5
6
|
export class Handler {
|
|
6
7
|
constructor(app, component) {
|
|
7
8
|
if (!app) {
|
|
8
9
|
app = new ZCAuth().getDefaultCredentials();
|
|
9
10
|
}
|
|
10
|
-
if (!(app instanceof CatalystApp)) {
|
|
11
|
-
throw new CatalystAppError('INVALID_PROJECT_CREDENTIALS', 'Unable to process the project credentials. Please verify that the initialization is configured correctly.');
|
|
12
|
-
}
|
|
13
11
|
this.app = app;
|
|
14
12
|
this.component = component;
|
|
15
13
|
}
|
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
'use strict';
|
|
2
1
|
import { basename } from 'path';
|
|
3
2
|
import { Readable, Stream } from 'stream';
|
|
4
3
|
import { inspect } from 'util';
|
|
@@ -41,11 +40,17 @@ class FormData extends Stream {
|
|
|
41
40
|
_getContentDisposition(value) {
|
|
42
41
|
let filename = '';
|
|
43
42
|
let contentDisposition = '';
|
|
44
|
-
if (value
|
|
45
|
-
|
|
43
|
+
if (typeof value === 'object' && value !== null && ('name' in value || 'path' in value)) {
|
|
44
|
+
const val = value;
|
|
45
|
+
filename = basename(val.name || val.path || '');
|
|
46
46
|
}
|
|
47
|
-
else if (value
|
|
48
|
-
|
|
47
|
+
else if (typeof value === 'object' &&
|
|
48
|
+
value !== null &&
|
|
49
|
+
'readable' in value &&
|
|
50
|
+
'httpVersion' in value &&
|
|
51
|
+
'client' in value) {
|
|
52
|
+
const httpValue = value;
|
|
53
|
+
filename = basename(httpValue.client._httpMessage.path || '');
|
|
49
54
|
}
|
|
50
55
|
if (filename) {
|
|
51
56
|
contentDisposition = 'filename="' + filename + '"';
|