@zcatalyst/transport 0.0.1 → 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 +269 -0
- package/dist-cjs/http-handler.js +404 -0
- package/dist-cjs/index.browser.js +23 -0
- package/dist-cjs/index.js +34 -0
- package/dist-cjs/utils/clonable-stream.js +107 -0
- package/dist-cjs/utils/constants.js +55 -0
- package/dist-cjs/utils/enums.js +22 -0
- package/dist-cjs/utils/errors.js +10 -0
- package/dist-cjs/utils/form-data.js +223 -0
- package/dist-cjs/utils/helpers.js +10 -0
- package/dist-cjs/utils/interfaces.js +2 -0
- package/dist-cjs/utils/request-agent.js +22 -0
- package/dist-cjs/utils/request-timeout.js +14 -0
- package/dist-es/fetch-handler.js +261 -0
- package/dist-es/http-handler.js +362 -0
- package/dist-es/index.browser.js +13 -0
- package/dist-es/index.js +21 -0
- package/dist-es/utils/clonable-stream.js +104 -0
- package/dist-es/utils/constants.js +52 -0
- package/dist-es/utils/enums.js +19 -0
- package/dist-es/utils/errors.js +6 -0
- package/dist-es/utils/form-data.js +218 -0
- package/dist-es/utils/helpers.js +7 -0
- package/dist-es/utils/interfaces.js +1 -0
- package/dist-es/utils/request-agent.js +19 -0
- package/dist-es/utils/request-timeout.js +11 -0
- package/dist-types/fetch-handler.d.ts +149 -0
- package/dist-types/http-handler.d.ts +79 -0
- package/dist-types/index.browser.d.ts +29 -0
- package/dist-types/index.d.ts +36 -0
- package/dist-types/ts3.4/fetch-handler.d.ts +149 -0
- package/dist-types/ts3.4/http-handler.d.ts +79 -0
- package/dist-types/ts3.4/index.browser.d.ts +29 -0
- package/dist-types/ts3.4/index.d.ts +36 -0
- package/dist-types/ts3.4/utils/clonable-stream.d.ts +75 -0
- package/dist-types/ts3.4/utils/constants.d.ts +11 -0
- package/dist-types/ts3.4/utils/enums.d.ts +16 -0
- package/dist-types/ts3.4/utils/errors.d.ts +11 -0
- package/dist-types/ts3.4/utils/form-data.d.ts +285 -0
- package/dist-types/ts3.4/utils/helpers.d.ts +13 -0
- package/dist-types/ts3.4/utils/interfaces.d.ts +68 -0
- package/dist-types/ts3.4/utils/request-agent.d.ts +12 -0
- package/dist-types/ts3.4/utils/request-timeout.d.ts +13 -0
- package/dist-types/utils/clonable-stream.d.ts +75 -0
- package/dist-types/utils/constants.d.ts +11 -0
- package/dist-types/utils/enums.d.ts +16 -0
- package/dist-types/utils/errors.d.ts +11 -0
- package/dist-types/utils/form-data.d.ts +285 -0
- package/dist-types/utils/helpers.d.ts +13 -0
- package/dist-types/utils/interfaces.d.ts +68 -0
- package/dist-types/utils/request-agent.d.ts +12 -0
- package/dist-types/utils/request-timeout.d.ts +13 -0
- package/package.json +37 -7
package/LICENCE
CHANGED
|
@@ -1 +1,55 @@
|
|
|
1
|
-
|
|
1
|
+
Zoho Catalyst SDK
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 Zoho Corporation Private Limited
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
Apache License Version 2.0
|
|
7
|
+
http://www.apache.org/licenses/
|
|
8
|
+
|
|
9
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
10
|
+
|
|
11
|
+
1. Definitions.
|
|
12
|
+
|
|
13
|
+
"License" shall mean the terms and conditions for use, reproduction, and distribution as defined by Sections 1 through 9 of this document.
|
|
14
|
+
|
|
15
|
+
"Licensor" shall mean the copyright owner or entity authorized by the copyright owner that is granting the License.
|
|
16
|
+
|
|
17
|
+
"Legal Entity" shall mean the union of the acting entity and all other entities that control, are controlled by, or are under common control with that entity. For the purposes of this definition, "control" means (i) the power, direct or indirect, to cause the direction or management of such entity, whether by contract or otherwise, or (ii) ownership of fifty percent (50%) or more of the outstanding shares, or (iii) beneficial ownership of such entity.
|
|
18
|
+
|
|
19
|
+
"You" (or "Your") shall mean an individual or Legal Entity exercising permissions granted by this License.
|
|
20
|
+
|
|
21
|
+
"Source" form shall mean the preferred form for making modifications, including but not limited to software source code, documentation source, and configuration files.
|
|
22
|
+
|
|
23
|
+
"Object" form shall mean any form resulting from mechanical transformation or translation of a Source form, including but not limited to compiled object code, generated documentation, and conversions to other media types.
|
|
24
|
+
|
|
25
|
+
"Work" shall mean the work of authorship, whether in Source or Object form, made available under the License, as indicated by a copyright notice that is included in or attached to the work (an example is provided in the Appendix below).
|
|
26
|
+
|
|
27
|
+
"Derivative Works" shall mean any work, whether in Source or Object form, that is based on (or derived from) the Work and for which the editorial revisions, annotations, elaborations, or other modifications represent, as a whole, an original work of authorship. For the purposes of this License, Derivative Works shall not include works that remain separable from, or merely link (or bind by name) to the interfaces of, the Work and Derivative Works thereof.
|
|
28
|
+
|
|
29
|
+
"Contribution" shall mean any work of authorship, including the original version of the Work and any modifications or additions to that Work or Derivative Works thereof, that is intentionally submitted to Licensor for inclusion in the Work by the copyright owner or by an individual or Legal Entity authorized to submit on behalf of the copyright owner. For the purposes of this definition, "submitted" means any form of electronic, verbal, or written communication sent to the Licensor or its representatives, including but not limited to communication on electronic mailing lists, source code control systems, and issue tracking systems that are managed by, or on behalf of, the Licensor for the purpose of discussing and improving the Work, but excluding communication that is conspicuously marked or otherwise designated in writing by the copyright owner as "Not a Contribution."
|
|
30
|
+
|
|
31
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity on behalf of whom a Contribution has been received by Licensor and subsequently incorporated within the Work.
|
|
32
|
+
|
|
33
|
+
2. Grant of Copyright License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable copyright license to reproduce, prepare Derivative Works of, publicly display, publicly perform, sublicense, and distribute the Work and such Derivative Works in Source or Object form.
|
|
34
|
+
|
|
35
|
+
3. Grant of Patent License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable (except as stated in this section) patent license to make, have made, use, offer to sell, sell, import, and otherwise transfer the Work, where such license applies only to those patent claims licensable by such Contributor that are necessarily infringed by their Contribution(s) alone or by combination of their Contribution(s) with the Work to which such Contribution(s) was submitted. If You institute patent litigation against any entity (including a cross-claim or counterclaim in a lawsuit) alleging that the Work or a Contribution incorporated within the Work constitutes direct or contributory patent infringement, then any patent licenses granted to You under this License for that Work shall terminate as of the date such litigation is filed.
|
|
36
|
+
|
|
37
|
+
4. Redistribution. You may reproduce and distribute copies of the Work or Derivative Works thereof in any medium, with or without modifications, and in Source or Object form, provided that You meet the following conditions:
|
|
38
|
+
|
|
39
|
+
You must give any other recipients of the Work or Derivative Works a copy of this License; and
|
|
40
|
+
You must cause any modified files to carry prominent notices stating that You changed the files; and
|
|
41
|
+
You must retain, in the Source form of any Derivative Works that You distribute, all copyright, patent, trademark, and attribution notices from the Source form of the Work, excluding those notices that do not pertain to any part of the Derivative Works; and
|
|
42
|
+
If the Work includes a "NOTICE" text file as part of its distribution, then any Derivative Works that You distribute must include a readable copy of the attribution notices contained within such NOTICE file, excluding those notices that do not pertain to any part of the Derivative Works, in at least one of the following places: within a NOTICE text file distributed as part of the Derivative Works; within the Source form or documentation, if provided along with the Derivative Works; or, within a display generated by the Derivative Works, if and wherever such third-party notices normally appear. The contents of the NOTICE file are for informational purposes only and do not modify the License. You may add Your own attribution notices within Derivative Works that You distribute, alongside or as an addendum to the NOTICE text from the Work, provided that such additional attribution notices cannot be construed as modifying the License.
|
|
43
|
+
You may add Your own copyright statement to Your modifications and may provide additional or different license terms and conditions for use, reproduction, or distribution of Your modifications, or for any such Derivative Works as a whole, provided Your use, reproduction, and distribution of the Work otherwise complies with the conditions stated in this License.
|
|
44
|
+
|
|
45
|
+
5. Submission of Contributions. Unless You explicitly state otherwise, any Contribution intentionally submitted for inclusion in the Work by You to the Licensor shall be under the terms and conditions of this License, without any additional terms or conditions. Notwithstanding the above, nothing herein shall supersede or modify the terms of any separate license agreement you may have executed with Licensor regarding such Contributions.
|
|
46
|
+
|
|
47
|
+
6. Trademarks. This License does not grant permission to use the trade names, trademarks, service marks, or product names of the Licensor, except as required for reasonable and customary use in describing the origin of the Work and reproducing the content of the NOTICE file.
|
|
48
|
+
|
|
49
|
+
7. Disclaimer of Warranty. Unless required by applicable law or agreed to in writing, Licensor provides the Work (and each Contributor provides its Contributions) on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied, including, without limitation, any warranties or conditions of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A PARTICULAR PURPOSE. You are solely responsible for determining the appropriateness of using or redistributing the Work and assume any risks associated with Your exercise of permissions under this License.
|
|
50
|
+
|
|
51
|
+
8. Limitation of Liability. In no event and under no legal theory, whether in tort (including negligence), contract, or otherwise, unless required by applicable law (such as deliberate and grossly negligent acts) or agreed to in writing, shall any Contributor be liable to You for damages, including any direct, indirect, special, incidental, or consequential damages of any character arising as a result of this License or out of the use or inability to use the Work (including but not limited to damages for loss of goodwill, work stoppage, computer failure or malfunction, or any and all other commercial damages or losses), even if such Contributor has been advised of the possibility of such damages.
|
|
52
|
+
|
|
53
|
+
9. Accepting Warranty or Additional Liability. While redistributing the Work or Derivative Works thereof, You may choose to offer, and charge a fee for, acceptance of support, warranty, indemnity, or other liability obligations and/or rights consistent with this License. However, in accepting such obligations, You may act only on Your own behalf and on Your sole responsibility, not on behalf of any other Contributor, and only if You agree to indemnify, defend, and hold each Contributor harmless for any liability incurred by, or claims asserted against, such Contributor by reason of your accepting any such warranty or additional liability.
|
|
54
|
+
|
|
55
|
+
END OF TERMS AND CONDITIONS
|
package/README.md
CHANGED
|
@@ -0,0 +1,266 @@
|
|
|
1
|
+
# @zcatalyst/transport
|
|
2
|
+
|
|
3
|
+
HTTP Transport Layer for Catalyst SDK - Internal Communication Module
|
|
4
|
+
|
|
5
|
+
## Overview
|
|
6
|
+
|
|
7
|
+
The `@zcatalyst/transport` package provides the `Handler` class and request/response types used by Catalyst SDK packages to send HTTP requests. It has Node.js and browser entry points.
|
|
8
|
+
|
|
9
|
+
### Prerequisites
|
|
10
|
+
|
|
11
|
+
- Typically installed as a dependency of other Catalyst packages
|
|
12
|
+
- Not required to be installed separately unless building custom integrations
|
|
13
|
+
|
|
14
|
+
## Installation
|
|
15
|
+
|
|
16
|
+
To install this package, simply type add or install @zcatalyst/transport
|
|
17
|
+
using your favorite package manager:
|
|
18
|
+
|
|
19
|
+
- `npm install @zcatalyst/transport`
|
|
20
|
+
- `yarn add @zcatalyst/transport`
|
|
21
|
+
- `pnpm add @zcatalyst/transport`
|
|
22
|
+
|
|
23
|
+
## Getting Started
|
|
24
|
+
|
|
25
|
+
### Import
|
|
26
|
+
|
|
27
|
+
To use the transport handler, import `Handler`:
|
|
28
|
+
|
|
29
|
+
```js
|
|
30
|
+
// ES5 example
|
|
31
|
+
const { Handler } = require("@zcatalyst/transport");
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
```ts
|
|
35
|
+
// ES6+ example
|
|
36
|
+
import { Handler } from "@zcatalyst/transport";
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
### Usage
|
|
40
|
+
|
|
41
|
+
#### Node.js Environment
|
|
42
|
+
|
|
43
|
+
For server-side HTTP requests using Node.js built-in modules:
|
|
44
|
+
|
|
45
|
+
```js
|
|
46
|
+
const handler = new Handler(app, component);
|
|
47
|
+
|
|
48
|
+
// Send HTTP request
|
|
49
|
+
const response = await handler.send({
|
|
50
|
+
method: 'GET',
|
|
51
|
+
url: '/api/data',
|
|
52
|
+
headers: {
|
|
53
|
+
'Content-Type': 'application/json'
|
|
54
|
+
}
|
|
55
|
+
});
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
#### Browser Environment
|
|
59
|
+
|
|
60
|
+
For client-side HTTP requests using Fetch API:
|
|
61
|
+
|
|
62
|
+
```js
|
|
63
|
+
const handler = new Handler(app, component);
|
|
64
|
+
|
|
65
|
+
// Send HTTP request
|
|
66
|
+
const response = await handler.send({
|
|
67
|
+
method: 'POST',
|
|
68
|
+
url: '/api/data',
|
|
69
|
+
headers: {
|
|
70
|
+
'Content-Type': 'application/json'
|
|
71
|
+
},
|
|
72
|
+
data: {
|
|
73
|
+
name: 'example',
|
|
74
|
+
value: 123
|
|
75
|
+
}
|
|
76
|
+
});
|
|
77
|
+
```
|
|
78
|
+
|
|
79
|
+
### Request Configuration
|
|
80
|
+
|
|
81
|
+
The `send` method accepts an `IRequestConfig` object:
|
|
82
|
+
|
|
83
|
+
```js
|
|
84
|
+
const config = {
|
|
85
|
+
method: 'POST', // HTTP method: GET, POST, PUT, DELETE, etc.
|
|
86
|
+
url: '/api/endpoint', // API endpoint
|
|
87
|
+
headers: { // HTTP headers
|
|
88
|
+
'Content-Type': 'application/json',
|
|
89
|
+
'Authorization': 'Bearer token'
|
|
90
|
+
},
|
|
91
|
+
data: { // Request body (JSON)
|
|
92
|
+
key: 'value'
|
|
93
|
+
},
|
|
94
|
+
qs: { // URL query parameters
|
|
95
|
+
page: 1,
|
|
96
|
+
limit: 10
|
|
97
|
+
}
|
|
98
|
+
};
|
|
99
|
+
```
|
|
100
|
+
|
|
101
|
+
### Async/await
|
|
102
|
+
|
|
103
|
+
We recommend using [await](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/await)
|
|
104
|
+
operator to wait for the promise returned by HTTP requests:
|
|
105
|
+
|
|
106
|
+
```js
|
|
107
|
+
// async/await.
|
|
108
|
+
try {
|
|
109
|
+
const response = await handler.send(config);
|
|
110
|
+
// process response.
|
|
111
|
+
} catch (error) {
|
|
112
|
+
// error handling.
|
|
113
|
+
} finally {
|
|
114
|
+
// finally.
|
|
115
|
+
}
|
|
116
|
+
```
|
|
117
|
+
|
|
118
|
+
### Error Handling
|
|
119
|
+
|
|
120
|
+
|
|
121
|
+
```js
|
|
122
|
+
try {
|
|
123
|
+
const response = await handler.send(config);
|
|
124
|
+
// process response.
|
|
125
|
+
} catch (error) {
|
|
126
|
+
const message = error.message;
|
|
127
|
+
const status = error.statusCode;
|
|
128
|
+
console.log({ message, status });
|
|
129
|
+
}
|
|
130
|
+
```
|
|
131
|
+
|
|
132
|
+
## Method Details
|
|
133
|
+
|
|
134
|
+
### Transport Operations
|
|
135
|
+
|
|
136
|
+
<details>
|
|
137
|
+
<summary>
|
|
138
|
+
Create Handler Instance
|
|
139
|
+
</summary>
|
|
140
|
+
|
|
141
|
+
```js
|
|
142
|
+
const handler = new Handler(app, component);
|
|
143
|
+
```
|
|
144
|
+
|
|
145
|
+
- `app`: Catalyst app instance (optional)
|
|
146
|
+
- `component`: Component instance for request context (optional)
|
|
147
|
+
|
|
148
|
+
</details>
|
|
149
|
+
|
|
150
|
+
<details>
|
|
151
|
+
<summary>
|
|
152
|
+
Send GET Request
|
|
153
|
+
</summary>
|
|
154
|
+
|
|
155
|
+
```js
|
|
156
|
+
const response = await handler.send({
|
|
157
|
+
method: 'GET',
|
|
158
|
+
url: '/api/users',
|
|
159
|
+
params: {
|
|
160
|
+
page: 1,
|
|
161
|
+
limit: 10
|
|
162
|
+
}
|
|
163
|
+
});
|
|
164
|
+
```
|
|
165
|
+
|
|
166
|
+
</details>
|
|
167
|
+
|
|
168
|
+
<details>
|
|
169
|
+
<summary>
|
|
170
|
+
Send POST Request
|
|
171
|
+
</summary>
|
|
172
|
+
|
|
173
|
+
```js
|
|
174
|
+
const response = await handler.send({
|
|
175
|
+
method: 'POST',
|
|
176
|
+
url: '/api/users',
|
|
177
|
+
headers: {
|
|
178
|
+
'Content-Type': 'application/json'
|
|
179
|
+
},
|
|
180
|
+
data: {
|
|
181
|
+
name: 'John Doe',
|
|
182
|
+
email: 'john@example.com'
|
|
183
|
+
}
|
|
184
|
+
});
|
|
185
|
+
```
|
|
186
|
+
|
|
187
|
+
</details>
|
|
188
|
+
|
|
189
|
+
<details>
|
|
190
|
+
<summary>
|
|
191
|
+
Send PUT Request
|
|
192
|
+
</summary>
|
|
193
|
+
|
|
194
|
+
```js
|
|
195
|
+
const response = await handler.send({
|
|
196
|
+
method: 'PUT',
|
|
197
|
+
url: '/api/users/123',
|
|
198
|
+
headers: {
|
|
199
|
+
'Content-Type': 'application/json'
|
|
200
|
+
},
|
|
201
|
+
data: {
|
|
202
|
+
name: 'Jane Doe',
|
|
203
|
+
email: 'jane@example.com'
|
|
204
|
+
}
|
|
205
|
+
});
|
|
206
|
+
```
|
|
207
|
+
|
|
208
|
+
</details>
|
|
209
|
+
|
|
210
|
+
<details>
|
|
211
|
+
<summary>
|
|
212
|
+
Send DELETE Request
|
|
213
|
+
</summary>
|
|
214
|
+
|
|
215
|
+
```js
|
|
216
|
+
const response = await handler.send({
|
|
217
|
+
method: 'DELETE',
|
|
218
|
+
url: '/api/users/123'
|
|
219
|
+
});
|
|
220
|
+
```
|
|
221
|
+
|
|
222
|
+
</details>
|
|
223
|
+
|
|
224
|
+
<details>
|
|
225
|
+
<summary>
|
|
226
|
+
Handle File Upload
|
|
227
|
+
</summary>
|
|
228
|
+
|
|
229
|
+
```js
|
|
230
|
+
const formData = new FormData();
|
|
231
|
+
formData.append('file', fileInput.files[0]);
|
|
232
|
+
formData.append('description', 'Profile picture');
|
|
233
|
+
|
|
234
|
+
const response = await handler.send({
|
|
235
|
+
method: 'POST',
|
|
236
|
+
url: '/api/upload',
|
|
237
|
+
headers: {
|
|
238
|
+
'Content-Type': 'multipart/form-data'
|
|
239
|
+
},
|
|
240
|
+
data: formData
|
|
241
|
+
});
|
|
242
|
+
```
|
|
243
|
+
|
|
244
|
+
</details>
|
|
245
|
+
|
|
246
|
+
### Environment Support
|
|
247
|
+
|
|
248
|
+
This package has separate Node.js and browser entry points:
|
|
249
|
+
|
|
250
|
+
- **Node.js**: Uses built-in `http` and `https` modules
|
|
251
|
+
- **Browser**: Uses Fetch API
|
|
252
|
+
|
|
253
|
+
## Resources
|
|
254
|
+
|
|
255
|
+
- [Catalyst SDK Documentation](https://docs.catalyst.zoho.com/en/sdk/)
|
|
256
|
+
- [Catalyst API Reference](https://docs.catalyst.zoho.com/en/sdk/)
|
|
257
|
+
- [Node.js HTTP Module](https://nodejs.org/api/http.html)
|
|
258
|
+
- [Fetch API](https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API)
|
|
259
|
+
|
|
260
|
+
## Contributing
|
|
261
|
+
|
|
262
|
+
See [CONTRIBUTING](../../CONTRIBUTING.md) for more information on how to get started.
|
|
263
|
+
|
|
264
|
+
## License
|
|
265
|
+
|
|
266
|
+
This SDK is distributed under the Apache License 2.0. See [LICENSE](../../LICENCE) file for more information.
|
|
@@ -0,0 +1,269 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
|
|
3
|
+
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
|
|
4
|
+
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
|
|
5
|
+
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
|
|
6
|
+
};
|
|
7
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
8
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
9
|
+
};
|
|
10
|
+
var _a, _ResponseHandler_attachAppSpecificHeaders, _ResponseHandler_followZcrfTokenProtocol, _ResponseHandler_followJwtZCAuthProtocol, _ResponseHandler_sendRequest;
|
|
11
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
+
exports.ResponseHandler = exports.DefaultHttpResponse = void 0;
|
|
13
|
+
const auth_client_1 = require("@zcatalyst/auth-client");
|
|
14
|
+
const utils_1 = require("@zcatalyst/utils");
|
|
15
|
+
const package_json_1 = __importDefault(require("../package.json"));
|
|
16
|
+
const { version } = package_json_1.default;
|
|
17
|
+
const constants_1 = require("./utils/constants");
|
|
18
|
+
const errors_1 = require("./utils/errors");
|
|
19
|
+
const request_timeout_1 = require("./utils/request-timeout");
|
|
20
|
+
const { REQ_METHOD } = utils_1.CONSTANTS;
|
|
21
|
+
class DefaultHttpResponse {
|
|
22
|
+
constructor(resp) {
|
|
23
|
+
this.statusCode = resp.statusCode;
|
|
24
|
+
this.headers = resp.headers;
|
|
25
|
+
this.config = resp.config;
|
|
26
|
+
this.resp = resp;
|
|
27
|
+
}
|
|
28
|
+
get data() {
|
|
29
|
+
if (this.resp.data === undefined) {
|
|
30
|
+
throw new errors_1.CatalystAPIError('UNPARSABLE_RESPONSE', `Error while processing response data. Raw server ` +
|
|
31
|
+
`response: "${this.resp.data}". `, '', this.statusCode);
|
|
32
|
+
}
|
|
33
|
+
return this.resp.data;
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
exports.DefaultHttpResponse = DefaultHttpResponse;
|
|
37
|
+
class ResponseHandler {
|
|
38
|
+
constructor() { }
|
|
39
|
+
static async fireGeneralRequest({ requestCore, url }, requestOptions = {}) {
|
|
40
|
+
try {
|
|
41
|
+
const headers = requestCore.headers || {};
|
|
42
|
+
const method = requestCore.method;
|
|
43
|
+
const methodAllowsBody = method !== REQ_METHOD.get && method !== REQ_METHOD.head;
|
|
44
|
+
const options = {
|
|
45
|
+
method,
|
|
46
|
+
headers,
|
|
47
|
+
credentials: requestOptions.auth ? 'include' : 'omit',
|
|
48
|
+
body: methodAllowsBody ? requestCore.body : undefined
|
|
49
|
+
};
|
|
50
|
+
const isExternal = requestOptions.service === "external";
|
|
51
|
+
if (requestOptions.auth && !isExternal) {
|
|
52
|
+
options.headers = await _a.attachZCAuthHeaders(headers);
|
|
53
|
+
options.headers = __classPrivateFieldGet(this, _a, "m", _ResponseHandler_attachAppSpecificHeaders).call(this, headers);
|
|
54
|
+
}
|
|
55
|
+
const controller = new AbortController();
|
|
56
|
+
if (requestOptions.abortSignal) {
|
|
57
|
+
requestOptions.abortSignal.addEventListener('abort', () => controller.abort());
|
|
58
|
+
}
|
|
59
|
+
options.signal = controller.signal;
|
|
60
|
+
const response = await Promise.race([
|
|
61
|
+
fetch(url, options),
|
|
62
|
+
(0, request_timeout_1.requestTimeout)(requestOptions?.requestTimeout)
|
|
63
|
+
]);
|
|
64
|
+
if (!response.ok) {
|
|
65
|
+
const errorData = await response.json().catch(() => null);
|
|
66
|
+
throw new errors_1.CatalystAPIError(`HTTP_ERROR_${response.status}`, errorData?.message || response.statusText, errorData, response.status);
|
|
67
|
+
}
|
|
68
|
+
if (requestOptions.retry && requestOptions.retry > 0) {
|
|
69
|
+
try {
|
|
70
|
+
return await _a.wrapResponse(response, {
|
|
71
|
+
...requestOptions,
|
|
72
|
+
request: requestCore
|
|
73
|
+
});
|
|
74
|
+
}
|
|
75
|
+
catch {
|
|
76
|
+
requestOptions.retry--;
|
|
77
|
+
return this.fireGeneralRequest({ requestCore, url }, requestOptions);
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
return await _a.wrapResponse(response, {
|
|
81
|
+
...requestOptions,
|
|
82
|
+
request: requestCore
|
|
83
|
+
});
|
|
84
|
+
}
|
|
85
|
+
catch (error) {
|
|
86
|
+
if (error instanceof errors_1.CatalystAPIError) {
|
|
87
|
+
throw error;
|
|
88
|
+
}
|
|
89
|
+
if (error.name === 'AbortError') {
|
|
90
|
+
throw new errors_1.CatalystAPIError('REQUEST_ABORTED', 'The request was aborted', error);
|
|
91
|
+
}
|
|
92
|
+
if (error.name === 'TypeError' && error.message.includes('Failed to fetch')) {
|
|
93
|
+
throw new errors_1.CatalystAPIError('NETWORK_ERROR', 'Network error occurred while making the request', error);
|
|
94
|
+
}
|
|
95
|
+
throw new errors_1.CatalystAPIError('REQUEST_FAILED', error.message || 'Request failed', error, error.statusCode);
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
static async wrapResponse(response, options) {
|
|
99
|
+
try {
|
|
100
|
+
const method = options?.request?.method?.toUpperCase();
|
|
101
|
+
const hasNoBody = method === REQ_METHOD.head || response.status === 204 || response.status === 304;
|
|
102
|
+
let data;
|
|
103
|
+
if (hasNoBody) {
|
|
104
|
+
data = '';
|
|
105
|
+
}
|
|
106
|
+
else {
|
|
107
|
+
switch (options?.expecting || "json") {
|
|
108
|
+
case "buffer":
|
|
109
|
+
data = await response.arrayBuffer();
|
|
110
|
+
break;
|
|
111
|
+
case "raw":
|
|
112
|
+
data = await response.blob();
|
|
113
|
+
break;
|
|
114
|
+
case "json":
|
|
115
|
+
data = await response.json();
|
|
116
|
+
break;
|
|
117
|
+
case "string":
|
|
118
|
+
data = await response.text();
|
|
119
|
+
break;
|
|
120
|
+
default:
|
|
121
|
+
throw new errors_1.CatalystAPIError('UNSUPPORTED_RESPONSE_TYPE', `Unsupported response type: ${options?.expecting}`);
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
return new DefaultHttpResponse({
|
|
125
|
+
headers: response.headers,
|
|
126
|
+
statusCode: response.status,
|
|
127
|
+
data,
|
|
128
|
+
request: options?.request || {},
|
|
129
|
+
config: options || {}
|
|
130
|
+
});
|
|
131
|
+
}
|
|
132
|
+
catch (error) {
|
|
133
|
+
throw new errors_1.CatalystAPIError('RESPONSE_PARSE_ERROR', 'Failed to parse response data', error);
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
static async fireRawRequest(requestCore, reqOptions) {
|
|
137
|
+
let headers = requestCore.headers;
|
|
138
|
+
if (reqOptions.auth) {
|
|
139
|
+
headers = await _a.attachZCAuthHeaders(headers);
|
|
140
|
+
headers = __classPrivateFieldGet(this, _a, "m", _ResponseHandler_attachAppSpecificHeaders).call(this, headers);
|
|
141
|
+
headers = { ...headers, credentials: 'include' };
|
|
142
|
+
}
|
|
143
|
+
const options = {
|
|
144
|
+
method: requestCore.method,
|
|
145
|
+
headers
|
|
146
|
+
};
|
|
147
|
+
if (requestCore.method !== REQ_METHOD.get && requestCore.body !== null)
|
|
148
|
+
options.body = requestCore.body;
|
|
149
|
+
const url = this.apiDomain ? `${this.apiDomain}${requestCore.url}` : requestCore.url;
|
|
150
|
+
return await this.wrapResponse(await fetch(url, options));
|
|
151
|
+
}
|
|
152
|
+
static async attachZCAuthHeaders(headers) {
|
|
153
|
+
const authProtocol = auth_client_1.ConfigStore.get('AUTH_PROTOCOL');
|
|
154
|
+
switch (authProtocol) {
|
|
155
|
+
case auth_client_1.Auth_Protocol.ZcrfTokenProtocol:
|
|
156
|
+
return await __classPrivateFieldGet(_a, _a, "m", _ResponseHandler_followZcrfTokenProtocol).call(_a, headers);
|
|
157
|
+
case auth_client_1.Auth_Protocol.JwtTokenProtocol:
|
|
158
|
+
return await __classPrivateFieldGet(_a, _a, "m", _ResponseHandler_followJwtZCAuthProtocol).call(_a, headers);
|
|
159
|
+
default:
|
|
160
|
+
return Promise.resolve(headers);
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
static getJWTZCAuthToken() {
|
|
164
|
+
const jwtPrefix = auth_client_1.ConfigStore.get(auth_client_1.JWT_COOKIE_PREFIX);
|
|
165
|
+
return new Promise((resolve, reject) => {
|
|
166
|
+
const jwtZCAuthToken = (0, auth_client_1.getToken)();
|
|
167
|
+
if (jwtZCAuthToken === '') {
|
|
168
|
+
reject('Unable to get the JWT Access Token.');
|
|
169
|
+
}
|
|
170
|
+
else {
|
|
171
|
+
resolve({
|
|
172
|
+
access_token: `${jwtPrefix} ${jwtZCAuthToken}`
|
|
173
|
+
});
|
|
174
|
+
}
|
|
175
|
+
});
|
|
176
|
+
}
|
|
177
|
+
static appendQueryString(url, qs) {
|
|
178
|
+
if (!qs || Object.keys(qs).length === 0) {
|
|
179
|
+
return url;
|
|
180
|
+
}
|
|
181
|
+
const searchParams = new URLSearchParams();
|
|
182
|
+
Object.entries(qs)
|
|
183
|
+
.filter(([_, value]) => value != null)
|
|
184
|
+
.forEach(([key, value]) => searchParams.append(key, String(value)));
|
|
185
|
+
const baseUrl = url.split('?')[0];
|
|
186
|
+
const existingParams = url.split('?')[1] || '';
|
|
187
|
+
return `${baseUrl}?${existingParams ? `${existingParams}&` : ''}${searchParams.toString()}`;
|
|
188
|
+
}
|
|
189
|
+
static async send(options, componentName, componentVersion) {
|
|
190
|
+
const headers = options.headers || {};
|
|
191
|
+
const isExternal = options.service === "external";
|
|
192
|
+
if (!isExternal) {
|
|
193
|
+
let userAgent = utils_1.CONSTANTS.USER_AGENT.PREFIX + version;
|
|
194
|
+
if (componentName) {
|
|
195
|
+
userAgent += ` ${componentName}/${componentVersion || 'unknown'}`;
|
|
196
|
+
}
|
|
197
|
+
headers['X-Catalyst-User-Agent'] = userAgent;
|
|
198
|
+
}
|
|
199
|
+
let data = options.data;
|
|
200
|
+
if (data !== undefined) {
|
|
201
|
+
switch (options.type) {
|
|
202
|
+
case "json":
|
|
203
|
+
data = JSON.stringify(data);
|
|
204
|
+
headers['Content-Type'] = constants_1.HTTP_HEADER_MAP.CONTENT_JSON;
|
|
205
|
+
break;
|
|
206
|
+
case "file":
|
|
207
|
+
const formData = new FormData();
|
|
208
|
+
const keyData = Object.keys(data);
|
|
209
|
+
keyData.forEach((key) => {
|
|
210
|
+
formData.append(key, data[key]);
|
|
211
|
+
});
|
|
212
|
+
data = formData;
|
|
213
|
+
break;
|
|
214
|
+
case "raw":
|
|
215
|
+
if (headers['Content-Type'] === undefined) {
|
|
216
|
+
headers['Content-Type'] = 'application/octet-stream';
|
|
217
|
+
}
|
|
218
|
+
data = new Blob([data], { type: headers['Content-Type'] });
|
|
219
|
+
break;
|
|
220
|
+
default:
|
|
221
|
+
data = JSON.stringify(data);
|
|
222
|
+
headers['Content-Type'] = 'application/x-www-form-urlencoded';
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
if (this.apiDomain === null && !options.origin) {
|
|
226
|
+
throw new errors_1.CatalystAPIError('API_REQUEST_ERROR', 'Unable to get the base url');
|
|
227
|
+
}
|
|
228
|
+
if (!isExternal && options.path) {
|
|
229
|
+
options.path = `${(0, utils_1.getServicePath)(options.service)}/project/${auth_client_1.ConfigStore.get(auth_client_1.PROJECT_ID)}${options.path}`;
|
|
230
|
+
}
|
|
231
|
+
const request = {
|
|
232
|
+
url: options.url ?? `${options.origin ?? this.apiDomain}${options.path}`,
|
|
233
|
+
method: options.method,
|
|
234
|
+
...(data ? { body: data } : {}),
|
|
235
|
+
headers
|
|
236
|
+
};
|
|
237
|
+
request.url = this.appendQueryString(request.url, options.qs);
|
|
238
|
+
return await __classPrivateFieldGet(this, _a, "m", _ResponseHandler_sendRequest).call(this, request, {
|
|
239
|
+
expecting: options.expecting,
|
|
240
|
+
auth: options.auth ?? true,
|
|
241
|
+
service: options.service
|
|
242
|
+
});
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
exports.ResponseHandler = ResponseHandler;
|
|
246
|
+
_a = ResponseHandler, _ResponseHandler_attachAppSpecificHeaders = function _ResponseHandler_attachAppSpecificHeaders(headers) {
|
|
247
|
+
let normalizedHeaders = headers;
|
|
248
|
+
normalizedHeaders = (0, auth_client_1.addDefaultAppHeaders)(normalizedHeaders);
|
|
249
|
+
return normalizedHeaders;
|
|
250
|
+
}, _ResponseHandler_followZcrfTokenProtocol = async function _ResponseHandler_followZcrfTokenProtocol(headers) {
|
|
251
|
+
await (0, auth_client_1.collectZCRFToken)();
|
|
252
|
+
const csrfToken = auth_client_1.ConfigStore.get('CSRF_TOKEN');
|
|
253
|
+
headers[constants_1.X_ZCSRF_TOKEN] = `${constants_1.ZD_CSRPARAM}=${csrfToken}`;
|
|
254
|
+
return headers;
|
|
255
|
+
}, _ResponseHandler_followJwtZCAuthProtocol = async function _ResponseHandler_followJwtZCAuthProtocol(headers) {
|
|
256
|
+
return this.getJWTZCAuthToken()
|
|
257
|
+
.then((resp) => {
|
|
258
|
+
headers[constants_1.HTTP_HEADER_MAP.AUTHORIZATION_KEY] =
|
|
259
|
+
resp.access_token;
|
|
260
|
+
return headers;
|
|
261
|
+
})
|
|
262
|
+
.catch((err) => {
|
|
263
|
+
throw new errors_1.CatalystAPIError('API_ERROR', err.message, err.status);
|
|
264
|
+
});
|
|
265
|
+
}, _ResponseHandler_sendRequest = async function _ResponseHandler_sendRequest(request, options) {
|
|
266
|
+
const { url, ...requestCore } = request;
|
|
267
|
+
return _a.fireGeneralRequest({ url, requestCore }, options);
|
|
268
|
+
};
|
|
269
|
+
ResponseHandler.apiDomain = typeof document !== 'undefined' ? document.location.origin : '';
|