@paid-ai/paid-node 1.0.0 → 1.0.1-alpha0
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/dist/cjs/BaseClient.d.ts +4 -17
- package/dist/cjs/BaseClient.js +0 -59
- package/dist/cjs/Client.d.ts +28 -28
- package/dist/cjs/Client.js +52 -11
- package/dist/cjs/api/errors/BadRequestError.js +1 -5
- package/dist/cjs/api/errors/ForbiddenError.js +1 -5
- package/dist/cjs/api/errors/InternalServerError.js +1 -5
- package/dist/cjs/api/errors/NotFoundError.js +1 -5
- package/dist/cjs/api/resources/agents/client/Client.d.ts +23 -22
- package/dist/cjs/api/resources/agents/client/Client.js +158 -55
- package/dist/cjs/api/resources/contacts/client/Client.d.ts +19 -18
- package/dist/cjs/api/resources/contacts/client/Client.js +122 -43
- package/dist/cjs/api/resources/customers/client/Client.d.ts +37 -36
- package/dist/cjs/api/resources/customers/client/Client.js +315 -113
- package/dist/cjs/api/resources/orders/client/Client.d.ts +28 -27
- package/dist/cjs/api/resources/orders/client/Client.js +177 -62
- package/dist/cjs/api/resources/orders/resources/lines/client/Client.d.ts +9 -8
- package/dist/cjs/api/resources/orders/resources/lines/client/Client.js +32 -13
- package/dist/cjs/api/resources/plans/client/Client.d.ts +13 -12
- package/dist/cjs/api/resources/plans/client/Client.js +82 -32
- package/dist/cjs/api/resources/products/client/Client.d.ts +23 -22
- package/dist/cjs/api/resources/products/client/Client.js +158 -55
- package/dist/cjs/api/resources/products/client/requests/index.d.ts +1 -1
- package/dist/cjs/api/resources/products/client/requests/index.js +0 -3
- package/dist/cjs/api/resources/traces/client/Client.d.ts +9 -8
- package/dist/cjs/api/resources/traces/client/Client.js +57 -23
- package/dist/cjs/api/resources/usage/client/Client.d.ts +13 -12
- package/dist/cjs/api/resources/usage/client/Client.js +68 -25
- package/dist/cjs/core/auth/AuthProvider.d.ts +1 -4
- package/dist/cjs/core/auth/BearerToken.d.ts +1 -3
- package/dist/cjs/core/auth/BearerToken.js +6 -7
- package/dist/cjs/core/auth/index.d.ts +0 -1
- package/dist/cjs/core/auth/index.js +1 -3
- package/dist/cjs/core/fetcher/BinaryResponse.d.ts +7 -6
- package/dist/cjs/core/fetcher/EndpointSupplier.d.ts +2 -2
- package/dist/cjs/core/fetcher/Fetcher.d.ts +4 -10
- package/dist/cjs/core/fetcher/Fetcher.js +10 -204
- package/dist/cjs/core/fetcher/ResponseWithBody.d.ts +4 -0
- package/dist/cjs/core/fetcher/ResponseWithBody.js +6 -0
- package/dist/cjs/core/fetcher/getRequestBody.d.ts +1 -1
- package/dist/cjs/core/fetcher/getRequestBody.js +0 -4
- package/dist/cjs/core/fetcher/getResponseBody.js +4 -18
- package/dist/cjs/core/fetcher/makeRequest.d.ts +1 -1
- package/dist/cjs/core/fetcher/makeRequest.js +2 -0
- package/dist/cjs/core/fetcher/requestWithRetries.js +9 -0
- package/dist/cjs/core/fetcher/signals.d.ts +7 -1
- package/dist/cjs/core/fetcher/signals.js +12 -0
- package/dist/cjs/core/headers.d.ts +2 -2
- package/dist/cjs/core/headers.js +4 -6
- package/dist/cjs/core/index.d.ts +0 -1
- package/dist/cjs/core/index.js +1 -2
- package/dist/cjs/core/runtime/runtime.js +10 -11
- package/dist/cjs/core/url/join.js +1 -0
- package/dist/cjs/errors/PaidError.js +1 -5
- package/dist/cjs/errors/PaidTimeoutError.js +1 -5
- package/dist/cjs/index.d.ts +0 -1
- package/dist/cjs/index.js +0 -4
- package/dist/cjs/version.d.ts +1 -1
- package/dist/cjs/version.js +1 -1
- package/dist/esm/BaseClient.d.mts +4 -17
- package/dist/esm/BaseClient.mjs +1 -24
- package/dist/esm/Client.d.mts +28 -28
- package/dist/esm/Client.mjs +27 -19
- package/dist/esm/api/errors/BadRequestError.mjs +1 -5
- package/dist/esm/api/errors/ForbiddenError.mjs +1 -5
- package/dist/esm/api/errors/InternalServerError.mjs +1 -5
- package/dist/esm/api/errors/NotFoundError.mjs +1 -5
- package/dist/esm/api/resources/agents/client/Client.d.mts +23 -22
- package/dist/esm/api/resources/agents/client/Client.mjs +157 -54
- package/dist/esm/api/resources/contacts/client/Client.d.mts +19 -18
- package/dist/esm/api/resources/contacts/client/Client.mjs +121 -42
- package/dist/esm/api/resources/customers/client/Client.d.mts +37 -36
- package/dist/esm/api/resources/customers/client/Client.mjs +314 -112
- package/dist/esm/api/resources/orders/client/Client.d.mts +28 -27
- package/dist/esm/api/resources/orders/client/Client.mjs +177 -62
- package/dist/esm/api/resources/orders/resources/lines/client/Client.d.mts +9 -8
- package/dist/esm/api/resources/orders/resources/lines/client/Client.mjs +31 -12
- package/dist/esm/api/resources/plans/client/Client.d.mts +13 -12
- package/dist/esm/api/resources/plans/client/Client.mjs +81 -31
- package/dist/esm/api/resources/products/client/Client.d.mts +23 -22
- package/dist/esm/api/resources/products/client/Client.mjs +157 -54
- package/dist/esm/api/resources/products/client/requests/index.d.mts +1 -1
- package/dist/esm/api/resources/products/client/requests/index.mjs +1 -1
- package/dist/esm/api/resources/traces/client/Client.d.mts +9 -8
- package/dist/esm/api/resources/traces/client/Client.mjs +56 -22
- package/dist/esm/api/resources/usage/client/Client.d.mts +13 -12
- package/dist/esm/api/resources/usage/client/Client.mjs +67 -24
- package/dist/esm/core/auth/AuthProvider.d.mts +1 -4
- package/dist/esm/core/auth/BearerToken.d.mts +1 -3
- package/dist/esm/core/auth/BearerToken.mjs +6 -7
- package/dist/esm/core/auth/index.d.mts +0 -1
- package/dist/esm/core/auth/index.mjs +0 -1
- package/dist/esm/core/fetcher/BinaryResponse.d.mts +7 -6
- package/dist/esm/core/fetcher/EndpointSupplier.d.mts +2 -2
- package/dist/esm/core/fetcher/Fetcher.d.mts +4 -10
- package/dist/esm/core/fetcher/Fetcher.mjs +10 -204
- package/dist/esm/core/fetcher/ResponseWithBody.d.mts +4 -0
- package/dist/esm/core/fetcher/ResponseWithBody.mjs +3 -0
- package/dist/esm/core/fetcher/getRequestBody.d.mts +1 -1
- package/dist/esm/core/fetcher/getRequestBody.mjs +0 -4
- package/dist/esm/core/fetcher/getResponseBody.mjs +4 -18
- package/dist/esm/core/fetcher/makeRequest.d.mts +1 -1
- package/dist/esm/core/fetcher/makeRequest.mjs +2 -0
- package/dist/esm/core/fetcher/requestWithRetries.mjs +9 -0
- package/dist/esm/core/fetcher/signals.d.mts +7 -1
- package/dist/esm/core/fetcher/signals.mjs +12 -0
- package/dist/esm/core/headers.d.mts +2 -2
- package/dist/esm/core/headers.mjs +4 -6
- package/dist/esm/core/index.d.mts +0 -1
- package/dist/esm/core/index.mjs +0 -1
- package/dist/esm/core/runtime/runtime.mjs +10 -11
- package/dist/esm/core/url/join.mjs +1 -0
- package/dist/esm/errors/PaidError.mjs +1 -5
- package/dist/esm/errors/PaidTimeoutError.mjs +1 -5
- package/dist/esm/index.d.mts +0 -1
- package/dist/esm/index.mjs +0 -1
- package/dist/esm/version.d.mts +1 -1
- package/dist/esm/version.mjs +1 -1
- package/package.json +3 -6
- package/reference.md +54 -54
- package/dist/cjs/auth/BearerAuthProvider.d.ts +0 -20
- package/dist/cjs/auth/BearerAuthProvider.js +0 -79
- package/dist/cjs/auth/index.d.ts +0 -1
- package/dist/cjs/auth/index.js +0 -5
- package/dist/cjs/core/auth/NoOpAuthProvider.d.ts +0 -5
- package/dist/cjs/core/auth/NoOpAuthProvider.js +0 -9
- package/dist/cjs/core/exports.d.ts +0 -1
- package/dist/cjs/core/exports.js +0 -17
- package/dist/cjs/core/logging/exports.d.ts +0 -18
- package/dist/cjs/core/logging/exports.js +0 -45
- package/dist/cjs/core/logging/index.d.ts +0 -1
- package/dist/cjs/core/logging/index.js +0 -17
- package/dist/cjs/core/logging/logger.d.ts +0 -126
- package/dist/cjs/core/logging/logger.js +0 -144
- package/dist/cjs/errors/handleNonStatusCodeError.d.ts +0 -2
- package/dist/cjs/errors/handleNonStatusCodeError.js +0 -65
- package/dist/cjs/exports.d.ts +0 -1
- package/dist/cjs/exports.js +0 -17
- package/dist/esm/auth/BearerAuthProvider.d.mts +0 -20
- package/dist/esm/auth/BearerAuthProvider.mjs +0 -42
- package/dist/esm/auth/index.d.mts +0 -1
- package/dist/esm/auth/index.mjs +0 -1
- package/dist/esm/core/auth/NoOpAuthProvider.d.mts +0 -5
- package/dist/esm/core/auth/NoOpAuthProvider.mjs +0 -5
- package/dist/esm/core/exports.d.mts +0 -1
- package/dist/esm/core/exports.mjs +0 -1
- package/dist/esm/core/logging/exports.d.mts +0 -18
- package/dist/esm/core/logging/exports.mjs +0 -9
- package/dist/esm/core/logging/index.d.mts +0 -1
- package/dist/esm/core/logging/index.mjs +0 -1
- package/dist/esm/core/logging/logger.d.mts +0 -126
- package/dist/esm/core/logging/logger.mjs +0 -138
- package/dist/esm/errors/handleNonStatusCodeError.d.mts +0 -2
- package/dist/esm/errors/handleNonStatusCodeError.mjs +0 -29
- package/dist/esm/exports.d.mts +0 -1
- package/dist/esm/exports.mjs +0 -1
|
@@ -8,22 +8,20 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
8
8
|
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
9
|
});
|
|
10
10
|
};
|
|
11
|
-
import {
|
|
12
|
-
import { mergeHeaders } from "../../../../core/headers.mjs";
|
|
11
|
+
import { mergeHeaders, mergeOnlyDefinedHeaders } from "../../../../core/headers.mjs";
|
|
13
12
|
import * as core from "../../../../core/index.mjs";
|
|
14
13
|
import * as environments from "../../../../environments.mjs";
|
|
15
|
-
import { handleNonStatusCodeError } from "../../../../errors/handleNonStatusCodeError.mjs";
|
|
16
14
|
import * as errors from "../../../../errors/index.mjs";
|
|
17
15
|
import * as Paid from "../../../index.mjs";
|
|
18
|
-
export class
|
|
19
|
-
constructor(
|
|
20
|
-
this._options =
|
|
16
|
+
export class Usage {
|
|
17
|
+
constructor(_options = {}) {
|
|
18
|
+
this._options = _options;
|
|
21
19
|
}
|
|
22
20
|
/**
|
|
23
21
|
* DEPRECATED: Use POST /usage/v2/signals/bulk instead for cleaner field names.
|
|
24
22
|
*
|
|
25
23
|
* @param {Paid.UsageRecordBulkRequest} request
|
|
26
|
-
* @param {
|
|
24
|
+
* @param {Usage.RequestOptions} requestOptions - Request-specific configuration.
|
|
27
25
|
*
|
|
28
26
|
* @example
|
|
29
27
|
* await client.usage.recordBulk({
|
|
@@ -35,8 +33,8 @@ export class UsageClient {
|
|
|
35
33
|
}
|
|
36
34
|
__recordBulk() {
|
|
37
35
|
return __awaiter(this, arguments, void 0, function* (request = {}, requestOptions) {
|
|
38
|
-
var _a, _b, _c, _d, _e, _f, _g, _h
|
|
39
|
-
const _headers = mergeHeaders((_a = this._options) === null || _a === void 0 ? void 0 : _a.headers, requestOptions === null || requestOptions === void 0 ? void 0 : requestOptions.headers);
|
|
36
|
+
var _a, _b, _c, _d, _e, _f, _g, _h;
|
|
37
|
+
const _headers = mergeHeaders((_a = this._options) === null || _a === void 0 ? void 0 : _a.headers, mergeOnlyDefinedHeaders({ Authorization: yield this._getAuthorizationHeader() }), requestOptions === null || requestOptions === void 0 ? void 0 : requestOptions.headers);
|
|
40
38
|
const _response = yield core.fetcher({
|
|
41
39
|
url: core.url.join((_c = (_b = (yield core.Supplier.get(this._options.baseUrl))) !== null && _b !== void 0 ? _b : (yield core.Supplier.get(this._options.environment))) !== null && _c !== void 0 ? _c : environments.PaidEnvironment.Production, "usage/signals/bulk"),
|
|
42
40
|
method: "POST",
|
|
@@ -48,8 +46,6 @@ export class UsageClient {
|
|
|
48
46
|
timeoutMs: ((_f = (_d = requestOptions === null || requestOptions === void 0 ? void 0 : requestOptions.timeoutInSeconds) !== null && _d !== void 0 ? _d : (_e = this._options) === null || _e === void 0 ? void 0 : _e.timeoutInSeconds) !== null && _f !== void 0 ? _f : 60) * 1000,
|
|
49
47
|
maxRetries: (_g = requestOptions === null || requestOptions === void 0 ? void 0 : requestOptions.maxRetries) !== null && _g !== void 0 ? _g : (_h = this._options) === null || _h === void 0 ? void 0 : _h.maxRetries,
|
|
50
48
|
abortSignal: requestOptions === null || requestOptions === void 0 ? void 0 : requestOptions.abortSignal,
|
|
51
|
-
fetchFn: (_j = this._options) === null || _j === void 0 ? void 0 : _j.fetch,
|
|
52
|
-
logging: this._options.logging,
|
|
53
49
|
});
|
|
54
50
|
if (_response.ok) {
|
|
55
51
|
return { data: undefined, rawResponse: _response.rawResponse };
|
|
@@ -61,12 +57,26 @@ export class UsageClient {
|
|
|
61
57
|
rawResponse: _response.rawResponse,
|
|
62
58
|
});
|
|
63
59
|
}
|
|
64
|
-
|
|
60
|
+
switch (_response.error.reason) {
|
|
61
|
+
case "non-json":
|
|
62
|
+
throw new errors.PaidError({
|
|
63
|
+
statusCode: _response.error.statusCode,
|
|
64
|
+
body: _response.error.rawBody,
|
|
65
|
+
rawResponse: _response.rawResponse,
|
|
66
|
+
});
|
|
67
|
+
case "timeout":
|
|
68
|
+
throw new errors.PaidTimeoutError("Timeout exceeded when calling POST /usage/signals/bulk.");
|
|
69
|
+
case "unknown":
|
|
70
|
+
throw new errors.PaidError({
|
|
71
|
+
message: _response.error.errorMessage,
|
|
72
|
+
rawResponse: _response.rawResponse,
|
|
73
|
+
});
|
|
74
|
+
}
|
|
65
75
|
});
|
|
66
76
|
}
|
|
67
77
|
/**
|
|
68
78
|
* @param {Paid.UsageRecordBulkV2Request} request
|
|
69
|
-
* @param {
|
|
79
|
+
* @param {Usage.RequestOptions} requestOptions - Request-specific configuration.
|
|
70
80
|
*
|
|
71
81
|
* @throws {@link Paid.BadRequestError}
|
|
72
82
|
*
|
|
@@ -96,8 +106,8 @@ export class UsageClient {
|
|
|
96
106
|
}
|
|
97
107
|
__usageRecordBulkV2() {
|
|
98
108
|
return __awaiter(this, arguments, void 0, function* (request = {}, requestOptions) {
|
|
99
|
-
var _a, _b, _c, _d, _e, _f, _g, _h
|
|
100
|
-
const _headers = mergeHeaders((_a = this._options) === null || _a === void 0 ? void 0 : _a.headers, requestOptions === null || requestOptions === void 0 ? void 0 : requestOptions.headers);
|
|
109
|
+
var _a, _b, _c, _d, _e, _f, _g, _h;
|
|
110
|
+
const _headers = mergeHeaders((_a = this._options) === null || _a === void 0 ? void 0 : _a.headers, mergeOnlyDefinedHeaders({ Authorization: yield this._getAuthorizationHeader() }), requestOptions === null || requestOptions === void 0 ? void 0 : requestOptions.headers);
|
|
101
111
|
const _response = yield core.fetcher({
|
|
102
112
|
url: core.url.join((_c = (_b = (yield core.Supplier.get(this._options.baseUrl))) !== null && _b !== void 0 ? _b : (yield core.Supplier.get(this._options.environment))) !== null && _c !== void 0 ? _c : environments.PaidEnvironment.Production, "usage/v2/signals/bulk"),
|
|
103
113
|
method: "POST",
|
|
@@ -109,8 +119,6 @@ export class UsageClient {
|
|
|
109
119
|
timeoutMs: ((_f = (_d = requestOptions === null || requestOptions === void 0 ? void 0 : requestOptions.timeoutInSeconds) !== null && _d !== void 0 ? _d : (_e = this._options) === null || _e === void 0 ? void 0 : _e.timeoutInSeconds) !== null && _f !== void 0 ? _f : 60) * 1000,
|
|
110
120
|
maxRetries: (_g = requestOptions === null || requestOptions === void 0 ? void 0 : requestOptions.maxRetries) !== null && _g !== void 0 ? _g : (_h = this._options) === null || _h === void 0 ? void 0 : _h.maxRetries,
|
|
111
121
|
abortSignal: requestOptions === null || requestOptions === void 0 ? void 0 : requestOptions.abortSignal,
|
|
112
|
-
fetchFn: (_j = this._options) === null || _j === void 0 ? void 0 : _j.fetch,
|
|
113
|
-
logging: this._options.logging,
|
|
114
122
|
});
|
|
115
123
|
if (_response.ok) {
|
|
116
124
|
return { data: undefined, rawResponse: _response.rawResponse };
|
|
@@ -127,12 +135,26 @@ export class UsageClient {
|
|
|
127
135
|
});
|
|
128
136
|
}
|
|
129
137
|
}
|
|
130
|
-
|
|
138
|
+
switch (_response.error.reason) {
|
|
139
|
+
case "non-json":
|
|
140
|
+
throw new errors.PaidError({
|
|
141
|
+
statusCode: _response.error.statusCode,
|
|
142
|
+
body: _response.error.rawBody,
|
|
143
|
+
rawResponse: _response.rawResponse,
|
|
144
|
+
});
|
|
145
|
+
case "timeout":
|
|
146
|
+
throw new errors.PaidTimeoutError("Timeout exceeded when calling POST /usage/v2/signals/bulk.");
|
|
147
|
+
case "unknown":
|
|
148
|
+
throw new errors.PaidError({
|
|
149
|
+
message: _response.error.errorMessage,
|
|
150
|
+
rawResponse: _response.rawResponse,
|
|
151
|
+
});
|
|
152
|
+
}
|
|
131
153
|
});
|
|
132
154
|
}
|
|
133
155
|
/**
|
|
134
156
|
* @param {Paid.UsageCheckUsageRequest} request
|
|
135
|
-
* @param {
|
|
157
|
+
* @param {Usage.RequestOptions} requestOptions - Request-specific configuration.
|
|
136
158
|
*
|
|
137
159
|
* @throws {@link Paid.BadRequestError}
|
|
138
160
|
* @throws {@link Paid.NotFoundError}
|
|
@@ -149,8 +171,8 @@ export class UsageClient {
|
|
|
149
171
|
}
|
|
150
172
|
__checkUsage(request, requestOptions) {
|
|
151
173
|
return __awaiter(this, void 0, void 0, function* () {
|
|
152
|
-
var _a, _b, _c, _d, _e, _f, _g, _h
|
|
153
|
-
const _headers = mergeHeaders((_a = this._options) === null || _a === void 0 ? void 0 : _a.headers, requestOptions === null || requestOptions === void 0 ? void 0 : requestOptions.headers);
|
|
174
|
+
var _a, _b, _c, _d, _e, _f, _g, _h;
|
|
175
|
+
const _headers = mergeHeaders((_a = this._options) === null || _a === void 0 ? void 0 : _a.headers, mergeOnlyDefinedHeaders({ Authorization: yield this._getAuthorizationHeader() }), requestOptions === null || requestOptions === void 0 ? void 0 : requestOptions.headers);
|
|
154
176
|
const _response = yield core.fetcher({
|
|
155
177
|
url: core.url.join((_c = (_b = (yield core.Supplier.get(this._options.baseUrl))) !== null && _b !== void 0 ? _b : (yield core.Supplier.get(this._options.environment))) !== null && _c !== void 0 ? _c : environments.PaidEnvironment.Production, "usage/check-usage"),
|
|
156
178
|
method: "POST",
|
|
@@ -162,8 +184,6 @@ export class UsageClient {
|
|
|
162
184
|
timeoutMs: ((_f = (_d = requestOptions === null || requestOptions === void 0 ? void 0 : requestOptions.timeoutInSeconds) !== null && _d !== void 0 ? _d : (_e = this._options) === null || _e === void 0 ? void 0 : _e.timeoutInSeconds) !== null && _f !== void 0 ? _f : 60) * 1000,
|
|
163
185
|
maxRetries: (_g = requestOptions === null || requestOptions === void 0 ? void 0 : requestOptions.maxRetries) !== null && _g !== void 0 ? _g : (_h = this._options) === null || _h === void 0 ? void 0 : _h.maxRetries,
|
|
164
186
|
abortSignal: requestOptions === null || requestOptions === void 0 ? void 0 : requestOptions.abortSignal,
|
|
165
|
-
fetchFn: (_j = this._options) === null || _j === void 0 ? void 0 : _j.fetch,
|
|
166
|
-
logging: this._options.logging,
|
|
167
187
|
});
|
|
168
188
|
if (_response.ok) {
|
|
169
189
|
return { data: _response.body, rawResponse: _response.rawResponse };
|
|
@@ -184,7 +204,30 @@ export class UsageClient {
|
|
|
184
204
|
});
|
|
185
205
|
}
|
|
186
206
|
}
|
|
187
|
-
|
|
207
|
+
switch (_response.error.reason) {
|
|
208
|
+
case "non-json":
|
|
209
|
+
throw new errors.PaidError({
|
|
210
|
+
statusCode: _response.error.statusCode,
|
|
211
|
+
body: _response.error.rawBody,
|
|
212
|
+
rawResponse: _response.rawResponse,
|
|
213
|
+
});
|
|
214
|
+
case "timeout":
|
|
215
|
+
throw new errors.PaidTimeoutError("Timeout exceeded when calling POST /usage/check-usage.");
|
|
216
|
+
case "unknown":
|
|
217
|
+
throw new errors.PaidError({
|
|
218
|
+
message: _response.error.errorMessage,
|
|
219
|
+
rawResponse: _response.rawResponse,
|
|
220
|
+
});
|
|
221
|
+
}
|
|
222
|
+
});
|
|
223
|
+
}
|
|
224
|
+
_getAuthorizationHeader() {
|
|
225
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
226
|
+
const bearer = yield core.Supplier.get(this._options.token);
|
|
227
|
+
if (bearer != null) {
|
|
228
|
+
return `Bearer ${bearer}`;
|
|
229
|
+
}
|
|
230
|
+
return undefined;
|
|
188
231
|
});
|
|
189
232
|
}
|
|
190
233
|
}
|
|
@@ -1,7 +1,4 @@
|
|
|
1
|
-
import type { EndpointMetadata } from "../fetcher/EndpointMetadata.mjs";
|
|
2
1
|
import type { AuthRequest } from "./AuthRequest.mjs";
|
|
3
2
|
export interface AuthProvider {
|
|
4
|
-
getAuthRequest(
|
|
5
|
-
endpointMetadata?: EndpointMetadata;
|
|
6
|
-
}): Promise<AuthRequest>;
|
|
3
|
+
getAuthRequest(): Promise<AuthRequest>;
|
|
7
4
|
}
|
|
@@ -1,7 +1,5 @@
|
|
|
1
1
|
export type BearerToken = string;
|
|
2
|
-
declare function toAuthorizationHeader(token: string | undefined): string | undefined;
|
|
3
2
|
export declare const BearerToken: {
|
|
4
|
-
toAuthorizationHeader:
|
|
3
|
+
toAuthorizationHeader: (token: BearerToken | undefined) => string | undefined;
|
|
5
4
|
fromAuthorizationHeader: (header: string) => BearerToken;
|
|
6
5
|
};
|
|
7
|
-
export {};
|
|
@@ -1,12 +1,11 @@
|
|
|
1
1
|
const BEARER_AUTH_HEADER_PREFIX = /^Bearer /i;
|
|
2
|
-
function toAuthorizationHeader(token) {
|
|
3
|
-
if (token == null) {
|
|
4
|
-
return undefined;
|
|
5
|
-
}
|
|
6
|
-
return `Bearer ${token}`;
|
|
7
|
-
}
|
|
8
2
|
export const BearerToken = {
|
|
9
|
-
toAuthorizationHeader:
|
|
3
|
+
toAuthorizationHeader: (token) => {
|
|
4
|
+
if (token == null) {
|
|
5
|
+
return undefined;
|
|
6
|
+
}
|
|
7
|
+
return `Bearer ${token}`;
|
|
8
|
+
},
|
|
10
9
|
fromAuthorizationHeader: (header) => {
|
|
11
10
|
return header.replace(BEARER_AUTH_HEADER_PREFIX, "").trim();
|
|
12
11
|
},
|
|
@@ -1,19 +1,20 @@
|
|
|
1
|
+
import type { ResponseWithBody } from "./ResponseWithBody.mjs";
|
|
1
2
|
export type BinaryResponse = {
|
|
2
3
|
/** [MDN Reference](https://developer.mozilla.org/docs/Web/API/Request/bodyUsed) */
|
|
3
|
-
bodyUsed:
|
|
4
|
+
bodyUsed: boolean;
|
|
4
5
|
/**
|
|
5
6
|
* Returns a ReadableStream of the response body.
|
|
6
7
|
* [MDN Reference](https://developer.mozilla.org/docs/Web/API/Request/body)
|
|
7
8
|
*/
|
|
8
|
-
stream: () =>
|
|
9
|
+
stream: () => ReadableStream<Uint8Array>;
|
|
9
10
|
/** [MDN Reference](https://developer.mozilla.org/docs/Web/API/Request/arrayBuffer) */
|
|
10
|
-
arrayBuffer: () =>
|
|
11
|
+
arrayBuffer: () => Promise<ArrayBuffer>;
|
|
11
12
|
/** [MDN Reference](https://developer.mozilla.org/docs/Web/API/Request/blob) */
|
|
12
|
-
blob: () =>
|
|
13
|
+
blob: () => Promise<Blob>;
|
|
13
14
|
/**
|
|
14
15
|
* [MDN Reference](https://developer.mozilla.org/docs/Web/API/Request/bytes)
|
|
15
16
|
* Some versions of the Fetch API may not support this method.
|
|
16
17
|
*/
|
|
17
|
-
bytes?():
|
|
18
|
+
bytes?(): Promise<Uint8Array>;
|
|
18
19
|
};
|
|
19
|
-
export declare function getBinaryResponse(response:
|
|
20
|
+
export declare function getBinaryResponse(response: ResponseWithBody): BinaryResponse;
|
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
import type { EndpointMetadata } from "./EndpointMetadata.mjs";
|
|
2
2
|
import type { Supplier } from "./Supplier.mjs";
|
|
3
3
|
type EndpointSupplierFn<T> = (arg: {
|
|
4
|
-
endpointMetadata
|
|
4
|
+
endpointMetadata: EndpointMetadata;
|
|
5
5
|
}) => T | Promise<T>;
|
|
6
6
|
export type EndpointSupplier<T> = Supplier<T> | EndpointSupplierFn<T>;
|
|
7
7
|
export declare const EndpointSupplier: {
|
|
8
8
|
get: <T>(supplier: EndpointSupplier<T>, arg: {
|
|
9
|
-
endpointMetadata
|
|
9
|
+
endpointMetadata: EndpointMetadata;
|
|
10
10
|
}) => Promise<T>;
|
|
11
11
|
};
|
|
12
12
|
export {};
|
|
@@ -1,27 +1,25 @@
|
|
|
1
|
-
import { type LogConfig, type Logger } from "../logging/logger.mjs";
|
|
2
1
|
import type { APIResponse } from "./APIResponse.mjs";
|
|
3
2
|
import type { EndpointMetadata } from "./EndpointMetadata.mjs";
|
|
3
|
+
import { EndpointSupplier } from "./EndpointSupplier.mjs";
|
|
4
4
|
export type FetchFunction = <R = unknown>(args: Fetcher.Args) => Promise<APIResponse<R, Fetcher.Error>>;
|
|
5
5
|
export declare namespace Fetcher {
|
|
6
6
|
interface Args {
|
|
7
7
|
url: string;
|
|
8
8
|
method: string;
|
|
9
9
|
contentType?: string;
|
|
10
|
-
headers?: Record<string,
|
|
10
|
+
headers?: Record<string, string | EndpointSupplier<string | null | undefined> | null | undefined>;
|
|
11
11
|
queryParameters?: Record<string, unknown>;
|
|
12
12
|
body?: unknown;
|
|
13
13
|
timeoutMs?: number;
|
|
14
14
|
maxRetries?: number;
|
|
15
15
|
withCredentials?: boolean;
|
|
16
16
|
abortSignal?: AbortSignal;
|
|
17
|
-
requestType?: "json" | "file" | "bytes"
|
|
17
|
+
requestType?: "json" | "file" | "bytes";
|
|
18
18
|
responseType?: "json" | "blob" | "sse" | "streaming" | "text" | "arrayBuffer" | "binary-response";
|
|
19
19
|
duplex?: "half";
|
|
20
20
|
endpointMetadata?: EndpointMetadata;
|
|
21
|
-
fetchFn?: typeof fetch;
|
|
22
|
-
logging?: LogConfig | Logger;
|
|
23
21
|
}
|
|
24
|
-
type Error = FailedStatusCodeError | NonJsonError |
|
|
22
|
+
type Error = FailedStatusCodeError | NonJsonError | TimeoutError | UnknownError;
|
|
25
23
|
interface FailedStatusCodeError {
|
|
26
24
|
reason: "status-code";
|
|
27
25
|
statusCode: number;
|
|
@@ -32,10 +30,6 @@ export declare namespace Fetcher {
|
|
|
32
30
|
statusCode: number;
|
|
33
31
|
rawBody: string;
|
|
34
32
|
}
|
|
35
|
-
interface BodyIsNullError {
|
|
36
|
-
reason: "body-is-null";
|
|
37
|
-
statusCode: number;
|
|
38
|
-
}
|
|
39
33
|
interface TimeoutError {
|
|
40
34
|
reason: "timeout";
|
|
41
35
|
}
|
|
@@ -8,153 +8,21 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
8
8
|
});
|
|
9
9
|
};
|
|
10
10
|
import { toJson } from "../json.mjs";
|
|
11
|
-
import { createLogger } from "../logging/logger.mjs";
|
|
12
11
|
import { createRequestUrl } from "./createRequestUrl.mjs";
|
|
13
12
|
import { EndpointSupplier } from "./EndpointSupplier.mjs";
|
|
14
13
|
import { getErrorResponseBody } from "./getErrorResponseBody.mjs";
|
|
15
14
|
import { getFetchFn } from "./getFetchFn.mjs";
|
|
16
15
|
import { getRequestBody } from "./getRequestBody.mjs";
|
|
17
16
|
import { getResponseBody } from "./getResponseBody.mjs";
|
|
18
|
-
import { Headers } from "./Headers.mjs";
|
|
19
17
|
import { makeRequest } from "./makeRequest.mjs";
|
|
20
18
|
import { abortRawResponse, toRawResponse, unknownRawResponse } from "./RawResponse.mjs";
|
|
21
19
|
import { requestWithRetries } from "./requestWithRetries.mjs";
|
|
22
|
-
const SENSITIVE_HEADERS = new Set([
|
|
23
|
-
"authorization",
|
|
24
|
-
"www-authenticate",
|
|
25
|
-
"x-api-key",
|
|
26
|
-
"api-key",
|
|
27
|
-
"apikey",
|
|
28
|
-
"x-api-token",
|
|
29
|
-
"x-auth-token",
|
|
30
|
-
"auth-token",
|
|
31
|
-
"cookie",
|
|
32
|
-
"set-cookie",
|
|
33
|
-
"proxy-authorization",
|
|
34
|
-
"proxy-authenticate",
|
|
35
|
-
"x-csrf-token",
|
|
36
|
-
"x-xsrf-token",
|
|
37
|
-
"x-session-token",
|
|
38
|
-
"x-access-token",
|
|
39
|
-
]);
|
|
40
|
-
function redactHeaders(headers) {
|
|
41
|
-
const filtered = {};
|
|
42
|
-
for (const [key, value] of headers instanceof Headers ? headers.entries() : Object.entries(headers)) {
|
|
43
|
-
if (SENSITIVE_HEADERS.has(key.toLowerCase())) {
|
|
44
|
-
filtered[key] = "[REDACTED]";
|
|
45
|
-
}
|
|
46
|
-
else {
|
|
47
|
-
filtered[key] = value;
|
|
48
|
-
}
|
|
49
|
-
}
|
|
50
|
-
return filtered;
|
|
51
|
-
}
|
|
52
|
-
const SENSITIVE_QUERY_PARAMS = new Set([
|
|
53
|
-
"api_key",
|
|
54
|
-
"api-key",
|
|
55
|
-
"apikey",
|
|
56
|
-
"token",
|
|
57
|
-
"access_token",
|
|
58
|
-
"access-token",
|
|
59
|
-
"auth_token",
|
|
60
|
-
"auth-token",
|
|
61
|
-
"password",
|
|
62
|
-
"passwd",
|
|
63
|
-
"secret",
|
|
64
|
-
"api_secret",
|
|
65
|
-
"api-secret",
|
|
66
|
-
"apisecret",
|
|
67
|
-
"key",
|
|
68
|
-
"session",
|
|
69
|
-
"session_id",
|
|
70
|
-
"session-id",
|
|
71
|
-
]);
|
|
72
|
-
function redactQueryParameters(queryParameters) {
|
|
73
|
-
if (queryParameters == null) {
|
|
74
|
-
return queryParameters;
|
|
75
|
-
}
|
|
76
|
-
const redacted = {};
|
|
77
|
-
for (const [key, value] of Object.entries(queryParameters)) {
|
|
78
|
-
if (SENSITIVE_QUERY_PARAMS.has(key.toLowerCase())) {
|
|
79
|
-
redacted[key] = "[REDACTED]";
|
|
80
|
-
}
|
|
81
|
-
else {
|
|
82
|
-
redacted[key] = value;
|
|
83
|
-
}
|
|
84
|
-
}
|
|
85
|
-
return redacted;
|
|
86
|
-
}
|
|
87
|
-
function redactUrl(url) {
|
|
88
|
-
const protocolIndex = url.indexOf("://");
|
|
89
|
-
if (protocolIndex === -1)
|
|
90
|
-
return url;
|
|
91
|
-
const afterProtocol = protocolIndex + 3;
|
|
92
|
-
// Find the first delimiter that marks the end of the authority section
|
|
93
|
-
const pathStart = url.indexOf("/", afterProtocol);
|
|
94
|
-
let queryStart = url.indexOf("?", afterProtocol);
|
|
95
|
-
let fragmentStart = url.indexOf("#", afterProtocol);
|
|
96
|
-
const firstDelimiter = Math.min(pathStart === -1 ? url.length : pathStart, queryStart === -1 ? url.length : queryStart, fragmentStart === -1 ? url.length : fragmentStart);
|
|
97
|
-
// Find the LAST @ before the delimiter (handles multiple @ in credentials)
|
|
98
|
-
let atIndex = -1;
|
|
99
|
-
for (let i = afterProtocol; i < firstDelimiter; i++) {
|
|
100
|
-
if (url[i] === "@") {
|
|
101
|
-
atIndex = i;
|
|
102
|
-
}
|
|
103
|
-
}
|
|
104
|
-
if (atIndex !== -1) {
|
|
105
|
-
url = `${url.slice(0, afterProtocol)}[REDACTED]@${url.slice(atIndex + 1)}`;
|
|
106
|
-
}
|
|
107
|
-
// Recalculate queryStart since url might have changed
|
|
108
|
-
queryStart = url.indexOf("?");
|
|
109
|
-
if (queryStart === -1)
|
|
110
|
-
return url;
|
|
111
|
-
fragmentStart = url.indexOf("#", queryStart);
|
|
112
|
-
const queryEnd = fragmentStart !== -1 ? fragmentStart : url.length;
|
|
113
|
-
const queryString = url.slice(queryStart + 1, queryEnd);
|
|
114
|
-
if (queryString.length === 0)
|
|
115
|
-
return url;
|
|
116
|
-
// FAST PATH: Quick check if any sensitive keywords present
|
|
117
|
-
// Using indexOf is faster than regex for simple substring matching
|
|
118
|
-
const lower = queryString.toLowerCase();
|
|
119
|
-
const hasSensitive = lower.includes("token") ||
|
|
120
|
-
lower.includes("key") ||
|
|
121
|
-
lower.includes("password") ||
|
|
122
|
-
lower.includes("passwd") ||
|
|
123
|
-
lower.includes("secret") ||
|
|
124
|
-
lower.includes("session") ||
|
|
125
|
-
lower.includes("auth");
|
|
126
|
-
if (!hasSensitive) {
|
|
127
|
-
return url;
|
|
128
|
-
}
|
|
129
|
-
// SLOW PATH: Parse and redact
|
|
130
|
-
const redactedParams = [];
|
|
131
|
-
const params = queryString.split("&");
|
|
132
|
-
for (const param of params) {
|
|
133
|
-
const equalIndex = param.indexOf("=");
|
|
134
|
-
if (equalIndex === -1) {
|
|
135
|
-
redactedParams.push(param);
|
|
136
|
-
continue;
|
|
137
|
-
}
|
|
138
|
-
const key = param.slice(0, equalIndex);
|
|
139
|
-
let shouldRedact = SENSITIVE_QUERY_PARAMS.has(key.toLowerCase());
|
|
140
|
-
if (!shouldRedact && key.includes("%")) {
|
|
141
|
-
try {
|
|
142
|
-
const decodedKey = decodeURIComponent(key);
|
|
143
|
-
shouldRedact = SENSITIVE_QUERY_PARAMS.has(decodedKey.toLowerCase());
|
|
144
|
-
}
|
|
145
|
-
catch (_a) { }
|
|
146
|
-
}
|
|
147
|
-
redactedParams.push(shouldRedact ? `${key}=[REDACTED]` : param);
|
|
148
|
-
}
|
|
149
|
-
return url.slice(0, queryStart + 1) + redactedParams.join("&") + url.slice(queryEnd);
|
|
150
|
-
}
|
|
151
20
|
function getHeaders(args) {
|
|
152
21
|
return __awaiter(this, void 0, void 0, function* () {
|
|
153
22
|
var _a;
|
|
154
|
-
const newHeaders =
|
|
155
|
-
newHeaders.set("Accept", args.responseType === "json" ? "application/json" : args.responseType === "text" ? "text/plain" : "*/*");
|
|
23
|
+
const newHeaders = {};
|
|
156
24
|
if (args.body !== undefined && args.contentType != null) {
|
|
157
|
-
newHeaders
|
|
25
|
+
newHeaders["Content-Type"] = args.contentType;
|
|
158
26
|
}
|
|
159
27
|
if (args.headers == null) {
|
|
160
28
|
return newHeaders;
|
|
@@ -162,70 +30,39 @@ function getHeaders(args) {
|
|
|
162
30
|
for (const [key, value] of Object.entries(args.headers)) {
|
|
163
31
|
const result = yield EndpointSupplier.get(value, { endpointMetadata: (_a = args.endpointMetadata) !== null && _a !== void 0 ? _a : {} });
|
|
164
32
|
if (typeof result === "string") {
|
|
165
|
-
newHeaders
|
|
33
|
+
newHeaders[key] = result;
|
|
166
34
|
continue;
|
|
167
35
|
}
|
|
168
36
|
if (result == null) {
|
|
169
37
|
continue;
|
|
170
38
|
}
|
|
171
|
-
newHeaders
|
|
39
|
+
newHeaders[key] = `${result}`;
|
|
172
40
|
}
|
|
173
41
|
return newHeaders;
|
|
174
42
|
});
|
|
175
43
|
}
|
|
176
44
|
export function fetcherImpl(args) {
|
|
177
45
|
return __awaiter(this, void 0, void 0, function* () {
|
|
178
|
-
var _a
|
|
46
|
+
var _a;
|
|
179
47
|
const url = createRequestUrl(args.url, args.queryParameters);
|
|
180
48
|
const requestBody = yield getRequestBody({
|
|
181
49
|
body: args.body,
|
|
182
|
-
type:
|
|
50
|
+
type: args.requestType === "json" ? "json" : "other",
|
|
183
51
|
});
|
|
184
|
-
const fetchFn =
|
|
185
|
-
const headers = yield getHeaders(args);
|
|
186
|
-
const logger = createLogger(args.logging);
|
|
187
|
-
if (logger.isDebug()) {
|
|
188
|
-
const metadata = {
|
|
189
|
-
method: args.method,
|
|
190
|
-
url: redactUrl(url),
|
|
191
|
-
headers: redactHeaders(headers),
|
|
192
|
-
queryParameters: redactQueryParameters(args.queryParameters),
|
|
193
|
-
hasBody: requestBody != null,
|
|
194
|
-
};
|
|
195
|
-
logger.debug("Making HTTP request", metadata);
|
|
196
|
-
}
|
|
52
|
+
const fetchFn = yield getFetchFn();
|
|
197
53
|
try {
|
|
198
54
|
const response = yield requestWithRetries(() => __awaiter(this, void 0, void 0, function* () {
|
|
199
|
-
return makeRequest(fetchFn, url, args.method,
|
|
55
|
+
return makeRequest(fetchFn, url, args.method, yield getHeaders(args), requestBody, args.timeoutMs, args.abortSignal, args.withCredentials, args.duplex);
|
|
200
56
|
}), args.maxRetries);
|
|
201
57
|
if (response.status >= 200 && response.status < 400) {
|
|
202
|
-
if (logger.isDebug()) {
|
|
203
|
-
const metadata = {
|
|
204
|
-
method: args.method,
|
|
205
|
-
url: redactUrl(url),
|
|
206
|
-
statusCode: response.status,
|
|
207
|
-
responseHeaders: redactHeaders(response.headers),
|
|
208
|
-
};
|
|
209
|
-
logger.debug("HTTP request succeeded", metadata);
|
|
210
|
-
}
|
|
211
|
-
const body = yield getResponseBody(response, args.responseType);
|
|
212
58
|
return {
|
|
213
59
|
ok: true,
|
|
214
|
-
body:
|
|
60
|
+
body: (yield getResponseBody(response, args.responseType)),
|
|
215
61
|
headers: response.headers,
|
|
216
62
|
rawResponse: toRawResponse(response),
|
|
217
63
|
};
|
|
218
64
|
}
|
|
219
65
|
else {
|
|
220
|
-
if (logger.isError()) {
|
|
221
|
-
const metadata = {
|
|
222
|
-
method: args.method,
|
|
223
|
-
url: redactUrl(url),
|
|
224
|
-
statusCode: response.status,
|
|
225
|
-
responseHeaders: redactHeaders(Object.fromEntries(response.headers.entries())),
|
|
226
|
-
};
|
|
227
|
-
logger.error("HTTP request failed with error status", metadata);
|
|
228
|
-
}
|
|
229
66
|
return {
|
|
230
67
|
ok: false,
|
|
231
68
|
error: {
|
|
@@ -238,14 +75,7 @@ export function fetcherImpl(args) {
|
|
|
238
75
|
}
|
|
239
76
|
}
|
|
240
77
|
catch (error) {
|
|
241
|
-
if ((
|
|
242
|
-
if (logger.isError()) {
|
|
243
|
-
const metadata = {
|
|
244
|
-
method: args.method,
|
|
245
|
-
url: redactUrl(url),
|
|
246
|
-
};
|
|
247
|
-
logger.error("HTTP request was aborted", metadata);
|
|
248
|
-
}
|
|
78
|
+
if ((_a = args.abortSignal) === null || _a === void 0 ? void 0 : _a.aborted) {
|
|
249
79
|
return {
|
|
250
80
|
ok: false,
|
|
251
81
|
error: {
|
|
@@ -256,14 +86,6 @@ export function fetcherImpl(args) {
|
|
|
256
86
|
};
|
|
257
87
|
}
|
|
258
88
|
else if (error instanceof Error && error.name === "AbortError") {
|
|
259
|
-
if (logger.isError()) {
|
|
260
|
-
const metadata = {
|
|
261
|
-
method: args.method,
|
|
262
|
-
url: redactUrl(url),
|
|
263
|
-
timeoutMs: args.timeoutMs,
|
|
264
|
-
};
|
|
265
|
-
logger.error("HTTP request timed out", metadata);
|
|
266
|
-
}
|
|
267
89
|
return {
|
|
268
90
|
ok: false,
|
|
269
91
|
error: {
|
|
@@ -273,14 +95,6 @@ export function fetcherImpl(args) {
|
|
|
273
95
|
};
|
|
274
96
|
}
|
|
275
97
|
else if (error instanceof Error) {
|
|
276
|
-
if (logger.isError()) {
|
|
277
|
-
const metadata = {
|
|
278
|
-
method: args.method,
|
|
279
|
-
url: redactUrl(url),
|
|
280
|
-
errorMessage: error.message,
|
|
281
|
-
};
|
|
282
|
-
logger.error("HTTP request failed with error", metadata);
|
|
283
|
-
}
|
|
284
98
|
return {
|
|
285
99
|
ok: false,
|
|
286
100
|
error: {
|
|
@@ -290,14 +104,6 @@ export function fetcherImpl(args) {
|
|
|
290
104
|
rawResponse: unknownRawResponse,
|
|
291
105
|
};
|
|
292
106
|
}
|
|
293
|
-
if (logger.isError()) {
|
|
294
|
-
const metadata = {
|
|
295
|
-
method: args.method,
|
|
296
|
-
url: redactUrl(url),
|
|
297
|
-
error: toJson(error),
|
|
298
|
-
};
|
|
299
|
-
logger.error("HTTP request failed with unknown error", metadata);
|
|
300
|
-
}
|
|
301
107
|
return {
|
|
302
108
|
ok: false,
|
|
303
109
|
error: {
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
export declare namespace GetRequestBody {
|
|
2
2
|
interface Args {
|
|
3
3
|
body: unknown;
|
|
4
|
-
type: "json" | "file" | "bytes" | "
|
|
4
|
+
type: "json" | "file" | "bytes" | "other";
|
|
5
5
|
}
|
|
6
6
|
}
|
|
7
7
|
export declare function getRequestBody({ body, type }: GetRequestBody.Args): Promise<BodyInit | undefined>;
|
|
@@ -8,12 +8,8 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
8
8
|
});
|
|
9
9
|
};
|
|
10
10
|
import { toJson } from "../json.mjs";
|
|
11
|
-
import { toQueryString } from "../url/qs.mjs";
|
|
12
11
|
export function getRequestBody(_a) {
|
|
13
12
|
return __awaiter(this, arguments, void 0, function* ({ body, type }) {
|
|
14
|
-
if (type === "form") {
|
|
15
|
-
return toQueryString(body, { arrayFormat: "repeat", encode: true });
|
|
16
|
-
}
|
|
17
13
|
if (type.includes("json")) {
|
|
18
14
|
return toJson(body);
|
|
19
15
|
}
|