@zcatalyst/transport 0.0.1 → 0.0.2

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.
Files changed (53) hide show
  1. package/dist-cjs/fetch-handler.js +266 -0
  2. package/dist-cjs/http-handler.js +402 -0
  3. package/dist-cjs/index.browser.js +18 -0
  4. package/dist-cjs/index.js +32 -0
  5. package/dist-cjs/utils/clonable-stream.js +107 -0
  6. package/dist-cjs/utils/constants.js +55 -0
  7. package/dist-cjs/utils/enums.js +22 -0
  8. package/dist-cjs/utils/errors.js +10 -0
  9. package/dist-cjs/utils/form-data.js +217 -0
  10. package/dist-cjs/utils/helpers.js +10 -0
  11. package/dist-cjs/utils/interfaces.js +2 -0
  12. package/dist-cjs/utils/request-agent.js +22 -0
  13. package/dist-cjs/utils/request-timeout.js +14 -0
  14. package/dist-es/fetch-handler.js +261 -0
  15. package/dist-es/http-handler.js +361 -0
  16. package/dist-es/index.browser.js +12 -0
  17. package/dist-es/index.js +23 -0
  18. package/dist-es/utils/clonable-stream.js +105 -0
  19. package/dist-es/utils/constants.js +52 -0
  20. package/dist-es/utils/enums.js +19 -0
  21. package/dist-es/utils/errors.js +6 -0
  22. package/dist-es/utils/form-data.js +213 -0
  23. package/dist-es/utils/helpers.js +7 -0
  24. package/dist-es/utils/interfaces.js +1 -0
  25. package/dist-es/utils/request-agent.js +20 -0
  26. package/dist-es/utils/request-timeout.js +11 -0
  27. package/dist-types/fetch-handler.d.ts +40 -0
  28. package/dist-types/http-handler.d.ts +39 -0
  29. package/dist-types/index.browser.d.ts +13 -0
  30. package/dist-types/index.d.ts +16 -0
  31. package/dist-types/ts3.4/fetch-handler.d.ts +40 -0
  32. package/dist-types/ts3.4/http-handler.d.ts +39 -0
  33. package/dist-types/ts3.4/index.browser.d.ts +13 -0
  34. package/dist-types/ts3.4/index.d.ts +16 -0
  35. package/dist-types/ts3.4/utils/clonable-stream.d.ts +21 -0
  36. package/dist-types/ts3.4/utils/constants.d.ts +11 -0
  37. package/dist-types/ts3.4/utils/enums.d.ts +16 -0
  38. package/dist-types/ts3.4/utils/errors.d.ts +4 -0
  39. package/dist-types/ts3.4/utils/form-data.d.ts +44 -0
  40. package/dist-types/ts3.4/utils/helpers.d.ts +1 -0
  41. package/dist-types/ts3.4/utils/interfaces.d.ts +64 -0
  42. package/dist-types/ts3.4/utils/request-agent.d.ts +6 -0
  43. package/dist-types/ts3.4/utils/request-timeout.d.ts +1 -0
  44. package/dist-types/utils/clonable-stream.d.ts +21 -0
  45. package/dist-types/utils/constants.d.ts +11 -0
  46. package/dist-types/utils/enums.d.ts +16 -0
  47. package/dist-types/utils/errors.d.ts +4 -0
  48. package/dist-types/utils/form-data.d.ts +44 -0
  49. package/dist-types/utils/helpers.d.ts +1 -0
  50. package/dist-types/utils/interfaces.d.ts +64 -0
  51. package/dist-types/utils/request-agent.d.ts +6 -0
  52. package/dist-types/utils/request-timeout.d.ts +1 -0
  53. package/package.json +10 -5
@@ -0,0 +1,105 @@
1
+ 'use strict';
2
+ import { nextTick } from 'process';
3
+ import { PassThrough } from 'stream';
4
+ function clonePiped(that) {
5
+ if (--that._clonesCount === 0 && !that.destroyed) {
6
+ that._original?.pipe(that);
7
+ that._original = undefined;
8
+ }
9
+ }
10
+ function _destroy(error, callback) {
11
+ if (!error) {
12
+ this.push(null);
13
+ this.end();
14
+ }
15
+ nextTick(callback, error);
16
+ }
17
+ function forwardDestroy(src, dest) {
18
+ function destroy(err) {
19
+ src.removeListener('close', onClose);
20
+ dest.destroy(err);
21
+ }
22
+ function onClose() {
23
+ dest.end();
24
+ }
25
+ src.on('error', destroy);
26
+ src.on('close', onClose);
27
+ }
28
+ class StreamClone extends PassThrough {
29
+ constructor(parent) {
30
+ super({ objectMode: parent.readableObjectMode });
31
+ this.parent = parent;
32
+ forwardDestroy(parent, this);
33
+ parent._internalPipe = true;
34
+ parent.pipe(this);
35
+ parent._internalPipe = false;
36
+ this.on('newListener', this.onDataClone);
37
+ this.once('resume', this.onResumeClone);
38
+ }
39
+ onDataClone(event, _listener) {
40
+ if (event === 'data' || event === 'readable' || event === 'close') {
41
+ nextTick(clonePiped, this.parent);
42
+ this.removeListener('newListener', this.onDataClone);
43
+ this.removeListener('resume', this.onResumeClone);
44
+ }
45
+ }
46
+ onResumeClone() {
47
+ this.removeListener('newListener', this.onDataClone);
48
+ this.removeListener('resume', this.onResumeClone);
49
+ nextTick(clonePiped, this.parent);
50
+ }
51
+ clone() {
52
+ return this.parent.clone();
53
+ }
54
+ isCloneable(stream) {
55
+ return stream instanceof CloneableStream || stream instanceof StreamClone;
56
+ }
57
+ }
58
+ export default class CloneableStream extends PassThrough {
59
+ constructor(stream) {
60
+ super({ objectMode: stream.readableObjectMode });
61
+ this._original = stream;
62
+ this._clonesCount = 1;
63
+ this._internalPipe = false;
64
+ forwardDestroy(stream, this);
65
+ this.on('newListener', this.onData);
66
+ this.on('resume', this.onResume);
67
+ this._hasListener = true;
68
+ this._destroy = _destroy;
69
+ }
70
+ onData(event, _listener) {
71
+ if (event === 'data' || event === 'readable') {
72
+ this._hasListener = false;
73
+ this.removeListener('newListener', this.onData);
74
+ this.removeListener('resume', this.onResume);
75
+ nextTick(clonePiped, this);
76
+ }
77
+ }
78
+ onResume() {
79
+ this._hasListener = false;
80
+ this.removeListener('newListener', this.onData);
81
+ this.removeListener('resume', this.onResume);
82
+ nextTick(clonePiped, this);
83
+ }
84
+ resume() {
85
+ if (this._internalPipe) {
86
+ return this;
87
+ }
88
+ PassThrough.prototype.resume.call(this);
89
+ return this;
90
+ }
91
+ clone() {
92
+ if (!this._original) {
93
+ throw new Error('already started');
94
+ }
95
+ this._clonesCount++;
96
+ this.removeListener('newListener', this.onData);
97
+ this.removeListener('resume', this.onResume);
98
+ const clone = new StreamClone(this);
99
+ if (this._hasListener) {
100
+ this.on('newListener', this.onData);
101
+ this.on('resume', this.onResume);
102
+ }
103
+ return clone;
104
+ }
105
+ }
@@ -0,0 +1,52 @@
1
+ export const HTTP_CODE_MAP = {
2
+ OK: {
3
+ CODE: 200,
4
+ TEXT: 'OK'
5
+ },
6
+ NO_CONTENT: {
7
+ CODE: 204,
8
+ TEXT: 'NO CONTENT'
9
+ },
10
+ INTERNAL_SERVER_ERROR: {
11
+ CODE: 500,
12
+ TEXT: 'INTERNAL SERVER ERROR'
13
+ },
14
+ RESOURCE_NOT_FOUND: {
15
+ CODE: 404,
16
+ TEXT: 'RESOURCE NOT FOUND'
17
+ },
18
+ BAD_REQUEST: {
19
+ CODE: 400,
20
+ TEXT: 'BAD REQUEST'
21
+ },
22
+ UNAUTHORIZED: {
23
+ CODE: 401,
24
+ TEXT: 'UNAUTHORIZED'
25
+ },
26
+ CONFLICT: {
27
+ CODE: 409,
28
+ TEXT: 'CONFLICT, MAY BE YOU ARE TRYING TO CREATE A RESOURCE THAT ALREADY EXIST!'
29
+ },
30
+ FORBIDDEN: {
31
+ CODE: 403,
32
+ TEXT: 'FORBIDDEN, MAY BE YOUR USAGE REACHED MAX ALLOWED LIMIT!'
33
+ },
34
+ UNEXPECTED: {
35
+ CODE: 101,
36
+ TEXT: 'UNEXPECTED RESPONSE FOUND'
37
+ }
38
+ };
39
+ export const HTTP_HEADER_MAP = {
40
+ CONTENT_JSON: 'application/json; charset=utf-8',
41
+ AUTHORIZATION_KEY: 'Authorization'
42
+ };
43
+ export const REQUEST_PROPERTY = {
44
+ BLOB: 'blob'
45
+ };
46
+ export const X_ZCSRF_TOKEN = 'X-ZCSRF-TOKEN';
47
+ export const ZD_CSRPARAM = 'zd_csrparam';
48
+ const HTTP_CODE_REV_MAP = {};
49
+ const HTTP_CODE_MAP_KEYS = Object.keys(HTTP_CODE_MAP);
50
+ for (const KEY of HTTP_CODE_MAP_KEYS)
51
+ HTTP_CODE_REV_MAP[HTTP_CODE_MAP[KEY].CODE] = HTTP_CODE_MAP[KEY];
52
+ export { HTTP_CODE_REV_MAP };
@@ -0,0 +1,19 @@
1
+ export var ResponseType;
2
+ (function (ResponseType) {
3
+ ResponseType["RAW"] = "raw";
4
+ ResponseType["JSON"] = "json";
5
+ ResponseType["STRING"] = "string";
6
+ ResponseType["BUFFER"] = "buffer";
7
+ })(ResponseType || (ResponseType = {}));
8
+ export var RequestType;
9
+ (function (RequestType) {
10
+ RequestType["FILE"] = "file";
11
+ RequestType["JSON"] = "json";
12
+ RequestType["URL_ENCODED"] = "url_encoded";
13
+ RequestType["RAW"] = "raw";
14
+ })(RequestType || (RequestType = {}));
15
+ export var FormDataKeys;
16
+ (function (FormDataKeys) {
17
+ FormDataKeys["CODE"] = "code";
18
+ FormDataKeys["FILE_NAME"] = "file_name";
19
+ })(FormDataKeys || (FormDataKeys = {}));
@@ -0,0 +1,6 @@
1
+ import { PrefixedCatalystError } from '@zcatalyst/utils';
2
+ export class CatalystAPIError extends PrefixedCatalystError {
3
+ constructor(code, message, value, statusCode) {
4
+ super('app', code, message, value, statusCode);
5
+ }
6
+ }
@@ -0,0 +1,213 @@
1
+ 'use strict';
2
+ import { basename } from 'path';
3
+ import { Readable, Stream } from 'stream';
4
+ import { inspect } from 'util';
5
+ import CloneableStream from './clonable-stream';
6
+ class FormData extends Stream {
7
+ constructor(streams) {
8
+ super();
9
+ this.writable = false;
10
+ this.readable = true;
11
+ this.released = false;
12
+ this.streams = streams || [];
13
+ this.insideLoop = false;
14
+ this.pendingNext = false;
15
+ }
16
+ isStream(value) {
17
+ return (value !== undefined &&
18
+ value !== null &&
19
+ ((typeof value.on === 'function' &&
20
+ typeof value.pipe === 'function') ||
21
+ value instanceof Readable));
22
+ }
23
+ _multiPartHeader(field, value) {
24
+ const contentDisposition = this._getContentDisposition(value);
25
+ const contentType = this._getContentType(value);
26
+ let contents = '';
27
+ const headers = {
28
+ 'Content-Disposition': ['form-data', 'name="' + field + '"'].concat(contentDisposition || []),
29
+ 'Content-Type': [contentType]
30
+ };
31
+ for (const prop in headers) {
32
+ if (headers[prop]) {
33
+ const header = headers[prop];
34
+ if (header.length > 0) {
35
+ contents += prop + ': ' + header.join('; ') + FormData.LINE_BREAK;
36
+ }
37
+ }
38
+ }
39
+ return '--' + this.getBoundary() + FormData.LINE_BREAK + contents + FormData.LINE_BREAK;
40
+ }
41
+ _getContentDisposition(value) {
42
+ let filename = '';
43
+ let contentDisposition = '';
44
+ if (value['name'] || value['path']) {
45
+ filename = basename(value['name'] || value['path']);
46
+ }
47
+ else if (value['readable'] && value.hasOwnProperty('httpVersion')) {
48
+ filename = basename(value['client']._httpMessage.path || '');
49
+ }
50
+ if (filename) {
51
+ contentDisposition = 'filename="' + filename + '"';
52
+ }
53
+ return contentDisposition;
54
+ }
55
+ _getContentType(value) {
56
+ let contentType = '';
57
+ if (value.readable && value.hasOwnProperty('httpVersion')) {
58
+ contentType = value.headers['content-type'];
59
+ }
60
+ if (!contentType) {
61
+ contentType = FormData.CONTENT_TYPE;
62
+ }
63
+ return contentType;
64
+ }
65
+ _lastBoundary() {
66
+ return '--' + this.getBoundary() + '--' + FormData.LINE_BREAK;
67
+ }
68
+ _generateBoundary() {
69
+ let boundary = '--------------------------';
70
+ for (let i = 0; i < 24; i++) {
71
+ boundary += Math.floor(Math.random() * 10).toString(16);
72
+ }
73
+ this.boundary = boundary;
74
+ return boundary;
75
+ }
76
+ _error(err) {
77
+ this._reset();
78
+ this.emit('error', err);
79
+ }
80
+ _handleStreamErrors(stream) {
81
+ stream.on('error', (err) => {
82
+ this._error(err);
83
+ });
84
+ }
85
+ _pipeNext(stream) {
86
+ this.currentStream = stream;
87
+ if (this.isStream(stream)) {
88
+ stream.on('end', this._getNext.bind(this));
89
+ stream.pipe(this, {
90
+ end: false
91
+ });
92
+ return;
93
+ }
94
+ const value = stream;
95
+ this.write(value);
96
+ this._getNext();
97
+ }
98
+ _getNext() {
99
+ this.currentStream = undefined;
100
+ if (this.insideLoop) {
101
+ this.pendingNext = true;
102
+ return;
103
+ }
104
+ this.insideLoop = true;
105
+ try {
106
+ do {
107
+ this.pendingNext = false;
108
+ const stream = this.streams.shift();
109
+ if (typeof stream === 'undefined') {
110
+ this.end();
111
+ }
112
+ else {
113
+ this._pipeNext(stream);
114
+ }
115
+ } while (this.pendingNext);
116
+ }
117
+ finally {
118
+ this.insideLoop = false;
119
+ }
120
+ }
121
+ _reset() {
122
+ this.writable = false;
123
+ this.streams = [];
124
+ this.currentStream = undefined;
125
+ }
126
+ createClone() {
127
+ const newStreams = [];
128
+ this.streams.forEach((stream) => {
129
+ const clone = this.isStream(stream)
130
+ ? new CloneableStream(stream).clone()
131
+ : stream;
132
+ newStreams.push(clone);
133
+ });
134
+ return new FormData(newStreams);
135
+ }
136
+ append(field, value) {
137
+ if (Array.isArray(value)) {
138
+ this._error(new Error('Arrays are not supported.'));
139
+ return this;
140
+ }
141
+ if (typeof value !== 'string' && !Buffer.isBuffer(value) && !this.isStream(value)) {
142
+ value = inspect(value);
143
+ }
144
+ if (this.isStream(value)) {
145
+ this._handleStreamErrors(value);
146
+ }
147
+ this.streams.push(this._multiPartHeader(field, value));
148
+ this.streams.push(value);
149
+ this.streams.push(FormData.LINE_BREAK);
150
+ return this;
151
+ }
152
+ getHeaders(userHeaders) {
153
+ const formHeaders = {};
154
+ for (const header in userHeaders) {
155
+ if (userHeaders[header]) {
156
+ formHeaders[header.toLowerCase()] = userHeaders[header];
157
+ }
158
+ }
159
+ formHeaders['content-type'] = 'multipart/form-data; boundary=' + this.getBoundary();
160
+ return formHeaders;
161
+ }
162
+ getBoundary() {
163
+ if (this.boundary === undefined) {
164
+ return this._generateBoundary();
165
+ }
166
+ return this.boundary;
167
+ }
168
+ pipe(dest, options) {
169
+ Stream.prototype.pipe.call(this, dest, options);
170
+ this.resume();
171
+ return dest;
172
+ }
173
+ write(data) {
174
+ const lastPart = this.streams.length === 0;
175
+ this.emit('data', data);
176
+ if (lastPart) {
177
+ this.emit('data', this._lastBoundary());
178
+ }
179
+ return true;
180
+ }
181
+ pause() {
182
+ if (this.currentStream !== undefined &&
183
+ typeof this.currentStream.pause === 'function') {
184
+ this.currentStream.pause();
185
+ }
186
+ this.emit('pause');
187
+ }
188
+ resume() {
189
+ if (!this.released) {
190
+ this.released = true;
191
+ this.writable = true;
192
+ this._getNext();
193
+ }
194
+ if (this.currentStream && typeof this.currentStream.resume === 'function') {
195
+ this.currentStream.resume();
196
+ }
197
+ this.emit('resume');
198
+ }
199
+ end() {
200
+ this._reset();
201
+ this.emit('end');
202
+ }
203
+ destroy() {
204
+ this._reset();
205
+ this.emit('close');
206
+ }
207
+ toString() {
208
+ return '[object FormData]';
209
+ }
210
+ }
211
+ FormData.LINE_BREAK = '\r\n';
212
+ FormData.CONTENT_TYPE = 'application/octet-stream';
213
+ export default FormData;
@@ -0,0 +1,7 @@
1
+ export function isHttps(url) {
2
+ if (url === undefined) {
3
+ return false;
4
+ }
5
+ const parsedUrl = url instanceof URL ? url : new URL(url);
6
+ return parsedUrl.protocol === 'https:';
7
+ }
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,20 @@
1
+ 'use strict';
2
+ import { CONSTANTS } from '@zcatalyst/utils';
3
+ import { Agent as httpAgent } from 'http';
4
+ import { Agent as httpsAgent } from 'https';
5
+ const agentMap = {};
6
+ export default class RequestAgent {
7
+ constructor(isHttps, host, replaceAgent) {
8
+ const protocol = isHttps ? CONSTANTS.PROTOCOL.HTTPS : CONSTANTS.PROTOCOL.HTTP;
9
+ if (agentMap[protocol] === undefined) {
10
+ agentMap[protocol] = {};
11
+ }
12
+ const protocolMap = agentMap[protocol];
13
+ if (protocolMap[host] === undefined || replaceAgent) {
14
+ protocolMap[host] = isHttps
15
+ ? new httpsAgent({ keepAlive: true })
16
+ : new httpAgent({ keepAlive: true });
17
+ }
18
+ this.agent = protocolMap[host];
19
+ }
20
+ }
@@ -0,0 +1,11 @@
1
+ export function requestTimeout(timeoutInMs = 0) {
2
+ return new Promise((resolve, reject) => {
3
+ if (timeoutInMs) {
4
+ setTimeout(() => {
5
+ const timeoutError = new Error(`Request did not complete within ${timeoutInMs} ms`);
6
+ timeoutError.name = 'TimeoutError';
7
+ reject(timeoutError);
8
+ }, timeoutInMs);
9
+ }
10
+ });
11
+ }
@@ -0,0 +1,40 @@
1
+ import { ConfigManager } from '@zcatalyst/auth-client';
2
+ import { CoreType, IRequestConfig, jwtAccessTokenResponse, RequestHandlerOptions } from './utils/interfaces';
3
+ export declare const keepAliveSupport: {
4
+ supported: undefined | boolean;
5
+ };
6
+ type ICatalystDataRes = Record<string, string> | string | Blob | ArrayBuffer | ReadableStreamDefaultReader<Uint8Array> | FormData;
7
+ export interface ICatalystClientRes {
8
+ request: RequestInit;
9
+ statusCode?: number;
10
+ headers: HeadersInit;
11
+ data?: ICatalystDataRes;
12
+ buffer?: Buffer;
13
+ blob?: Blob;
14
+ config: RequestHandlerOptions;
15
+ stream?: ArrayBuffer;
16
+ }
17
+ export declare class DefaultHttpResponse {
18
+ statusCode: number;
19
+ headers: HeadersInit;
20
+ config: RequestHandlerOptions;
21
+ resp: ICatalystClientRes;
22
+ constructor(resp: ICatalystClientRes);
23
+ get data(): ICatalystDataRes;
24
+ }
25
+ export declare class ResponseHandler {
26
+ #private;
27
+ static configManager: ConfigManager;
28
+ constructor();
29
+ static fireGeneralRequest({ requestCore, url }: {
30
+ requestCore: RequestInit;
31
+ url: string;
32
+ }, requestOptions?: RequestHandlerOptions): Promise<DefaultHttpResponse | void>;
33
+ static wrapResponse(response: Response, options?: RequestHandlerOptions): Promise<DefaultHttpResponse>;
34
+ static fireRawRequest(requestCore: CoreType, reqOptions: RequestHandlerOptions): Promise<DefaultHttpResponse>;
35
+ static attachZCAuthHeaders(headers: HeadersInit): Promise<HeadersInit>;
36
+ static getJWTZCAuthToken(): Promise<jwtAccessTokenResponse>;
37
+ static appendQueryString(url: string, qs?: Record<string, string | number | boolean | undefined>): string;
38
+ static send(options: IRequestConfig): Promise<DefaultHttpResponse | void>;
39
+ }
40
+ export {};
@@ -0,0 +1,39 @@
1
+ import { CatalystApp } from '@zcatalyst/auth';
2
+ import { ClientRequest, IncomingHttpHeaders, IncomingMessage } from 'http';
3
+ import { Component, IRequestConfig } from './utils/interfaces';
4
+ export interface IAPIResponse {
5
+ request: ClientRequest;
6
+ statusCode?: number;
7
+ headers: IncomingHttpHeaders;
8
+ data?: string;
9
+ buffer?: Buffer;
10
+ config: IRequestConfig;
11
+ stream?: IncomingMessage;
12
+ }
13
+ export declare class DefaultHttpResponse {
14
+ statusCode: number;
15
+ headers: IncomingHttpHeaders;
16
+ config: IRequestConfig;
17
+ resp: IAPIResponse;
18
+ constructor(resp: IAPIResponse);
19
+ get data(): any;
20
+ }
21
+ export declare class HttpClient {
22
+ app?: CatalystApp;
23
+ private user;
24
+ /**
25
+ * @param {CatalystApp} app The app used to fetch access tokens to sign API requests.
26
+ * @constructor
27
+ */
28
+ constructor(app?: CatalystApp);
29
+ send(req: IRequestConfig, apmTrackerName?: string): Promise<DefaultHttpResponse>;
30
+ }
31
+ export declare class AuthorizedHttpClient extends HttpClient {
32
+ readonly componentName?: string;
33
+ /**
34
+ * @param {unknown} app The app used to fetch access tokens to sign API requests.
35
+ * @constructor
36
+ */
37
+ constructor(app?: CatalystApp, component?: Component);
38
+ send(request: IRequestConfig): Promise<DefaultHttpResponse>;
39
+ }
@@ -0,0 +1,13 @@
1
+ import { ICatalystClientRes } from './fetch-handler';
2
+ import { Component, IRequestConfig } from './utils/interfaces';
3
+ export declare class Handler {
4
+ component?: Component;
5
+ /**
6
+ * @constructor
7
+ */
8
+ constructor(app?: unknown, component?: Component);
9
+ send(options: IRequestConfig): Promise<ICatalystClientRes>;
10
+ }
11
+ export { RequestType, ResponseType } from './utils/enums';
12
+ export { CatalystAPIError } from './utils/errors';
13
+ export { IRequestConfig };
@@ -0,0 +1,16 @@
1
+ import { DefaultHttpResponse } from './http-handler';
2
+ import FormData from './utils/form-data';
3
+ import { Component, IRequestConfig } from './utils/interfaces';
4
+ export declare class Handler {
5
+ component?: Component;
6
+ app?: any;
7
+ /**
8
+ * @param {unknown} app The app used to fetch access tokens to sign API requests.
9
+ * @constructor
10
+ */
11
+ constructor(app?: unknown, component?: Component);
12
+ send(options: IRequestConfig): Promise<DefaultHttpResponse>;
13
+ }
14
+ export { RequestType, ResponseType } from './utils/enums';
15
+ export { CatalystAPIError } from './utils/errors';
16
+ export { FormData, IRequestConfig };
@@ -0,0 +1,40 @@
1
+ import { ConfigManager } from '@zcatalyst/auth-client';
2
+ import { CoreType, IRequestConfig, jwtAccessTokenResponse, RequestHandlerOptions } from './utils/interfaces';
3
+ export declare const keepAliveSupport: {
4
+ supported: undefined | boolean;
5
+ };
6
+ type ICatalystDataRes = Record<string, string> | string | Blob | ArrayBuffer | ReadableStreamDefaultReader<Uint8Array> | FormData;
7
+ export interface ICatalystClientRes {
8
+ request: RequestInit;
9
+ statusCode?: number;
10
+ headers: HeadersInit;
11
+ data?: ICatalystDataRes;
12
+ buffer?: Buffer;
13
+ blob?: Blob;
14
+ config: RequestHandlerOptions;
15
+ stream?: ArrayBuffer;
16
+ }
17
+ export declare class DefaultHttpResponse {
18
+ statusCode: number;
19
+ headers: HeadersInit;
20
+ config: RequestHandlerOptions;
21
+ resp: ICatalystClientRes;
22
+ constructor(resp: ICatalystClientRes);
23
+ readonly data: ICatalystDataRes;
24
+ }
25
+ export declare class ResponseHandler {
26
+ private "ResponseHandler.#private";
27
+ static configManager: ConfigManager;
28
+ constructor();
29
+ static fireGeneralRequest({ requestCore, url }: {
30
+ requestCore: RequestInit;
31
+ url: string;
32
+ }, requestOptions?: RequestHandlerOptions): Promise<DefaultHttpResponse | void>;
33
+ static wrapResponse(response: Response, options?: RequestHandlerOptions): Promise<DefaultHttpResponse>;
34
+ static fireRawRequest(requestCore: CoreType, reqOptions: RequestHandlerOptions): Promise<DefaultHttpResponse>;
35
+ static attachZCAuthHeaders(headers: HeadersInit): Promise<HeadersInit>;
36
+ static getJWTZCAuthToken(): Promise<jwtAccessTokenResponse>;
37
+ static appendQueryString(url: string, qs?: Record<string, string | number | boolean | undefined>): string;
38
+ static send(options: IRequestConfig): Promise<DefaultHttpResponse | void>;
39
+ }
40
+ export {};
@@ -0,0 +1,39 @@
1
+ import { CatalystApp } from '@zcatalyst/auth';
2
+ import { ClientRequest, IncomingHttpHeaders, IncomingMessage } from 'http';
3
+ import { Component, IRequestConfig } from './utils/interfaces';
4
+ export interface IAPIResponse {
5
+ request: ClientRequest;
6
+ statusCode?: number;
7
+ headers: IncomingHttpHeaders;
8
+ data?: string;
9
+ buffer?: Buffer;
10
+ config: IRequestConfig;
11
+ stream?: IncomingMessage;
12
+ }
13
+ export declare class DefaultHttpResponse {
14
+ statusCode: number;
15
+ headers: IncomingHttpHeaders;
16
+ config: IRequestConfig;
17
+ resp: IAPIResponse;
18
+ constructor(resp: IAPIResponse);
19
+ readonly data: any;
20
+ }
21
+ export declare class HttpClient {
22
+ app?: CatalystApp;
23
+ private user;
24
+ /**
25
+ * @param {CatalystApp} app The app used to fetch access tokens to sign API requests.
26
+ * @constructor
27
+ */
28
+ constructor(app?: CatalystApp);
29
+ send(req: IRequestConfig, apmTrackerName?: string): Promise<DefaultHttpResponse>;
30
+ }
31
+ export declare class AuthorizedHttpClient extends HttpClient {
32
+ readonly componentName?: string;
33
+ /**
34
+ * @param {unknown} app The app used to fetch access tokens to sign API requests.
35
+ * @constructor
36
+ */
37
+ constructor(app?: CatalystApp, component?: Component);
38
+ send(request: IRequestConfig): Promise<DefaultHttpResponse>;
39
+ }
@@ -0,0 +1,13 @@
1
+ import { ICatalystClientRes } from './fetch-handler';
2
+ import { Component, IRequestConfig } from './utils/interfaces';
3
+ export declare class Handler {
4
+ component?: Component;
5
+ /**
6
+ * @constructor
7
+ */
8
+ constructor(app?: unknown, component?: Component);
9
+ send(options: IRequestConfig): Promise<ICatalystClientRes>;
10
+ }
11
+ export { RequestType, ResponseType } from './utils/enums';
12
+ export { CatalystAPIError } from './utils/errors';
13
+ export { IRequestConfig };
@@ -0,0 +1,16 @@
1
+ import { DefaultHttpResponse } from './http-handler';
2
+ import FormData from './utils/form-data';
3
+ import { Component, IRequestConfig } from './utils/interfaces';
4
+ export declare class Handler {
5
+ component?: Component;
6
+ app?: any;
7
+ /**
8
+ * @param {unknown} app The app used to fetch access tokens to sign API requests.
9
+ * @constructor
10
+ */
11
+ constructor(app?: unknown, component?: Component);
12
+ send(options: IRequestConfig): Promise<DefaultHttpResponse>;
13
+ }
14
+ export { RequestType, ResponseType } from './utils/enums';
15
+ export { CatalystAPIError } from './utils/errors';
16
+ export { FormData, IRequestConfig };