theauthapi 1.0.16 → 1.0.18

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 (36) hide show
  1. package/LICENSE +21 -21
  2. package/README.md +2 -0
  3. package/dist/index.cjs +285 -283
  4. package/dist/index.d.cts +178 -178
  5. package/dist/index.d.mts +177 -177
  6. package/dist/index.mjs +285 -283
  7. package/dist/{types/index.d.ts → types.d.cts} +87 -85
  8. package/dist/types.d.ts +87 -0
  9. package/package.json +11 -2
  10. package/dist/endpoints/Accounts/Accounts.d.ts +0 -10
  11. package/dist/endpoints/Accounts/Accounts.js +0 -22
  12. package/dist/endpoints/Accounts/AccountsInterface.d.ts +0 -4
  13. package/dist/endpoints/Accounts/AccountsInterface.js +0 -1
  14. package/dist/endpoints/ApiKeys/ApiKeys.d.ts +0 -19
  15. package/dist/endpoints/ApiKeys/ApiKeys.js +0 -80
  16. package/dist/endpoints/ApiKeys/ApiKeysInterface.d.ts +0 -12
  17. package/dist/endpoints/ApiKeys/ApiKeysInterface.js +0 -1
  18. package/dist/endpoints/Projects/Projects.d.ts +0 -14
  19. package/dist/endpoints/Projects/Projects.js +0 -42
  20. package/dist/endpoints/Projects/ProjectsInterface.d.ts +0 -8
  21. package/dist/endpoints/Projects/ProjectsInterface.js +0 -1
  22. package/dist/index.d.ts +0 -26
  23. package/dist/index.js +0 -69
  24. package/dist/libraryMeta.d.ts +0 -1
  25. package/dist/libraryMeta.js +0 -1
  26. package/dist/services/ApiRequest/ApiCall.d.ts +0 -5
  27. package/dist/services/ApiRequest/ApiCall.js +0 -1
  28. package/dist/services/ApiRequest/ApiRequest.d.ts +0 -24
  29. package/dist/services/ApiRequest/ApiRequest.js +0 -110
  30. package/dist/services/ApiRequest/ApiRequestError.d.ts +0 -10
  31. package/dist/services/ApiRequest/ApiRequestError.js +0 -13
  32. package/dist/services/ApiRequest/ApiResponseError.d.ts +0 -12
  33. package/dist/services/ApiRequest/ApiResponseError.js +0 -15
  34. package/dist/services/ApiRequest/HttpMethod.d.ts +0 -7
  35. package/dist/services/ApiRequest/HttpMethod.js +0 -8
  36. package/dist/types/index.js +0 -10
package/dist/index.mjs CHANGED
@@ -32,303 +32,305 @@ function __awaiter(thisArg, _arguments, P, generator) {
32
32
 
33
33
  const version = '1.0.11';
34
34
 
35
- /**
36
- *
37
- * Throws when no response was received from the server
38
- * @param message - error message
39
- *
40
- * */
41
- class ApiRequestError extends Error {
42
- constructor(message) {
43
- super(message);
44
- this.name = 'ApiRequestError';
45
- }
35
+ /**
36
+ *
37
+ * Throws when no response was received from the server
38
+ * @param message - error message
39
+ *
40
+ * */
41
+ class ApiRequestError extends Error {
42
+ constructor(message) {
43
+ super(message);
44
+ this.name = 'ApiRequestError';
45
+ }
46
46
  }
47
47
 
48
- /**
49
- *
50
- * Throws when the server responds with a status code that falls out of the range 2xx
51
- * @param statusCode - HTTP status code the server responded with
52
- * @param message - error message
53
- *
54
- * */
55
- class ApiResponseError extends Error {
56
- constructor(statusCode, message) {
57
- super(`(${statusCode}): ${message}`);
58
- this.statusCode = statusCode;
59
- this.name = 'ApiResponseError';
60
- }
48
+ /**
49
+ *
50
+ * Throws when the server responds with a status code that falls out of the range 2xx
51
+ * @param statusCode - HTTP status code the server responded with
52
+ * @param message - error message
53
+ *
54
+ * */
55
+ class ApiResponseError extends Error {
56
+ constructor(statusCode, message) {
57
+ super(`(${statusCode}): ${message}`);
58
+ this.statusCode = statusCode;
59
+ this.name = 'ApiResponseError';
60
+ }
61
61
  }
62
62
 
63
- class ApiRequest {
64
- constructor(config) {
65
- const { host, accessKey, headers, retryCount } = config;
66
- this.host = host;
67
- this.accessKey = accessKey;
68
- this.headers = this._generateDefaultHeaders();
69
- this.retryCount = retryCount !== null && retryCount !== void 0 ? retryCount : 3;
70
- if (headers) {
71
- this.headers = Object.assign(Object.assign({}, this.headers), { headers });
72
- }
73
- this._init();
74
- }
75
- _init() {
76
- var _a;
77
- const isoDateFormat = /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(?:\.\d*)?(?:[-+]\d{2}:?\d{2}|Z)?$/;
78
- function isIsoDateString(value) {
79
- return value && typeof value === 'string' && isoDateFormat.test(value);
80
- }
81
- function handleDates(body) {
82
- if (body === null || body === undefined || typeof body !== 'object') {
83
- return body;
84
- }
85
- for (const key of Object.keys(body)) {
86
- const value = body[key];
87
- if (isIsoDateString(value)) {
88
- body[key] = new Date(value);
89
- }
90
- else if (typeof value === 'object') {
91
- handleDates(value);
92
- }
93
- }
94
- }
95
- axios.interceptors.response.use((response) => {
96
- handleDates(response.data);
97
- return response;
98
- });
99
- axiosRetry(axios, {
100
- retries: (_a = this.retryCount) !== null && _a !== void 0 ? _a : 3,
101
- retryCondition: this._isErrorRetryable,
102
- retryDelay: axiosRetry.exponentialDelay,
103
- });
104
- }
105
- request(method, endpoint, payload) {
106
- var _a;
107
- return __awaiter(this, void 0, void 0, function* () {
108
- try {
109
- const response = yield axios.request({
110
- baseURL: this.host,
111
- method: method,
112
- url: endpoint,
113
- data: payload,
114
- headers: this.headers,
115
- });
116
- return response.data;
117
- }
118
- catch (error) {
119
- if (axios.isAxiosError(error)) {
120
- if (error.response) {
121
- throw new ApiResponseError(error.response.status, (_a = error.response.data.message) !== null && _a !== void 0 ? _a : error.response.statusText);
122
- }
123
- else if (error.request) {
124
- throw new ApiRequestError(error.message);
125
- }
126
- }
127
- throw error;
128
- }
129
- });
130
- }
131
- _generateDefaultHeaders() {
132
- return {
133
- 'user-agent': `theauthapi-client-node/${version}`,
134
- 'x-api-key': this.accessKey,
135
- 'api-key': this.accessKey,
136
- };
137
- }
138
- _isErrorRetryable(error) {
139
- // Retry Network Errors.
140
- if (axiosRetry.isNetworkError(error)) {
141
- return true;
142
- }
143
- if (!error.response) {
144
- // Cannot determine if the request can be retried
145
- return false;
146
- }
147
- // Retry Server Errors (5xx).
148
- if (error.response.status >= 500 && error.response.status <= 599) {
149
- return true;
150
- }
151
- // Retry if rate limited.
152
- if (error.response.status === 429) {
153
- return true;
154
- }
155
- return false;
156
- }
63
+ class ApiRequest {
64
+ constructor(config) {
65
+ const { host, accessKey, headers, retryCount } = config;
66
+ this.host = host;
67
+ this.accessKey = accessKey;
68
+ this.headers = this._generateDefaultHeaders();
69
+ this.retryCount = retryCount !== null && retryCount !== void 0 ? retryCount : 3;
70
+ if (headers) {
71
+ this.headers = Object.assign(Object.assign({}, this.headers), { headers });
72
+ }
73
+ this._init();
74
+ }
75
+ _init() {
76
+ var _a;
77
+ const isoDateFormat = /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(?:\.\d*)?(?:[-+]\d{2}:?\d{2}|Z)?$/;
78
+ function isIsoDateString(value) {
79
+ return value && typeof value === 'string' && isoDateFormat.test(value);
80
+ }
81
+ function handleDates(body) {
82
+ if (body === null || body === undefined || typeof body !== 'object') {
83
+ return body;
84
+ }
85
+ for (const key of Object.keys(body)) {
86
+ const value = body[key];
87
+ if (isIsoDateString(value)) {
88
+ body[key] = new Date(value);
89
+ }
90
+ else if (typeof value === 'object') {
91
+ handleDates(value);
92
+ }
93
+ }
94
+ }
95
+ axios.interceptors.response.use((response) => {
96
+ handleDates(response.data);
97
+ return response;
98
+ });
99
+ axiosRetry(axios, {
100
+ retries: (_a = this.retryCount) !== null && _a !== void 0 ? _a : 3,
101
+ retryCondition: this._isErrorRetryable,
102
+ retryDelay: axiosRetry.exponentialDelay,
103
+ });
104
+ }
105
+ request(method, endpoint, payload, customHeaders) {
106
+ var _a;
107
+ return __awaiter(this, void 0, void 0, function* () {
108
+ try {
109
+ const response = yield axios.request({
110
+ baseURL: this.host,
111
+ method: method,
112
+ url: endpoint,
113
+ data: payload,
114
+ headers: customHeaders
115
+ ? Object.assign(Object.assign({}, this.headers), customHeaders) : this.headers,
116
+ });
117
+ return response.data;
118
+ }
119
+ catch (error) {
120
+ if (axios.isAxiosError(error)) {
121
+ if (error.response) {
122
+ throw new ApiResponseError(error.response.status, (_a = error.response.data.message) !== null && _a !== void 0 ? _a : error.response.statusText);
123
+ }
124
+ else if (error.request) {
125
+ throw new ApiRequestError(error.message);
126
+ }
127
+ }
128
+ throw error;
129
+ }
130
+ });
131
+ }
132
+ _generateDefaultHeaders() {
133
+ return {
134
+ 'user-agent': `theauthapi-client-node/${version}`,
135
+ 'x-api-key': this.accessKey,
136
+ 'api-key': this.accessKey,
137
+ };
138
+ }
139
+ _isErrorRetryable(error) {
140
+ // Retry Network Errors.
141
+ if (axiosRetry.isNetworkError(error)) {
142
+ return true;
143
+ }
144
+ if (!error.response) {
145
+ // Cannot determine if the request can be retried
146
+ return false;
147
+ }
148
+ // Retry Server Errors (5xx).
149
+ if (error.response.status >= 500 && error.response.status <= 599) {
150
+ return true;
151
+ }
152
+ // Retry if rate limited.
153
+ if (error.response.status === 429) {
154
+ return true;
155
+ }
156
+ return false;
157
+ }
157
158
  }
158
159
 
159
- var HttpMethod;
160
- (function (HttpMethod) {
161
- HttpMethod["GET"] = "GET";
162
- HttpMethod["POST"] = "POST";
163
- HttpMethod["DELETE"] = "DELETE";
164
- HttpMethod["PATCH"] = "PATCH";
165
- HttpMethod["PUT"] = "PUT";
160
+ var HttpMethod;
161
+ (function (HttpMethod) {
162
+ HttpMethod["GET"] = "GET";
163
+ HttpMethod["POST"] = "POST";
164
+ HttpMethod["DELETE"] = "DELETE";
165
+ HttpMethod["PATCH"] = "PATCH";
166
+ HttpMethod["PUT"] = "PUT";
166
167
  })(HttpMethod || (HttpMethod = {}));
167
168
 
168
- class ApiKeys {
169
- constructor(apiService) {
170
- this.api = apiService;
171
- this.endpoint = '/api-keys/';
172
- }
173
- isValidKey(apikey) {
174
- return __awaiter(this, void 0, void 0, function* () {
175
- try {
176
- const key = yield this.authenticateKey(apikey);
177
- return key.key !== undefined;
178
- }
179
- catch (error) {
180
- if (error instanceof ApiResponseError && error.statusCode === 404) {
181
- return false;
182
- }
183
- throw error;
184
- }
185
- });
186
- }
187
- authenticateKey(apikey) {
188
- return __awaiter(this, void 0, void 0, function* () {
189
- return yield this.api.request(HttpMethod.POST, `/api-keys/auth/${apikey}`);
190
- });
191
- }
192
- getKeys(filter) {
193
- return __awaiter(this, void 0, void 0, function* () {
194
- const endpoint = this.getKeysFilterEndpoint(filter);
195
- return yield this.api.request(HttpMethod.GET, endpoint);
196
- });
197
- }
198
- getKey(apikey) {
199
- return __awaiter(this, void 0, void 0, function* () {
200
- return yield this.api.request(HttpMethod.GET, `/api-keys/${apikey}`);
201
- });
202
- }
203
- createKey(apiKey) {
204
- return __awaiter(this, void 0, void 0, function* () {
205
- return yield this.api.request(HttpMethod.POST, '/api-keys', apiKey);
206
- });
207
- }
208
- updateKey(apiKey, updatedKey) {
209
- return __awaiter(this, void 0, void 0, function* () {
210
- return yield this.api.request(HttpMethod.PATCH, `/api-keys/${apiKey}`, updatedKey);
211
- });
212
- }
213
- deleteKey(apiKey) {
214
- return __awaiter(this, void 0, void 0, function* () {
215
- return yield this.api.request(HttpMethod.DELETE, `/api-keys/${apiKey}`);
216
- });
217
- }
218
- reactivateKey(apiKey) {
219
- return __awaiter(this, void 0, void 0, function* () {
220
- return yield this.api.request(HttpMethod.PATCH, `/api-keys/${apiKey}/reactivate`);
221
- });
222
- }
223
- rotateKey(apiKey) {
224
- return __awaiter(this, void 0, void 0, function* () {
225
- return yield this.api.request(HttpMethod.POST, `/api-keys/${apiKey}/rotate`);
226
- });
227
- }
228
- getKeysFilterEndpoint(filter) {
229
- let filters = [];
230
- if (filter !== undefined) {
231
- filters = Object.entries(filter).map(([key, value]) => `${key}=${value}`);
232
- }
233
- return `${this.endpoint}${filter !== undefined ? '?' : ''}${filters.join('&')}`;
234
- }
169
+ class ApiKeys {
170
+ constructor(apiService) {
171
+ this.api = apiService;
172
+ this.endpoint = '/api-keys/';
173
+ }
174
+ isValidKey(apikey, origin) {
175
+ return __awaiter(this, void 0, void 0, function* () {
176
+ try {
177
+ const key = yield this.authenticateKey(apikey, origin);
178
+ return key.key !== undefined;
179
+ }
180
+ catch (error) {
181
+ if (error instanceof ApiResponseError && error.statusCode === 404) {
182
+ return false;
183
+ }
184
+ throw error;
185
+ }
186
+ });
187
+ }
188
+ authenticateKey(apikey, origin) {
189
+ return __awaiter(this, void 0, void 0, function* () {
190
+ const headers = origin ? { 'x-origin-domain': origin } : undefined;
191
+ return yield this.api.request(HttpMethod.POST, `/api-keys/auth/${apikey}`, undefined, headers);
192
+ });
193
+ }
194
+ getKeys(filter) {
195
+ return __awaiter(this, void 0, void 0, function* () {
196
+ const endpoint = this.getKeysFilterEndpoint(filter);
197
+ return yield this.api.request(HttpMethod.GET, endpoint);
198
+ });
199
+ }
200
+ getKey(apikey) {
201
+ return __awaiter(this, void 0, void 0, function* () {
202
+ return yield this.api.request(HttpMethod.GET, `/api-keys/${apikey}`);
203
+ });
204
+ }
205
+ createKey(apiKey) {
206
+ return __awaiter(this, void 0, void 0, function* () {
207
+ return yield this.api.request(HttpMethod.POST, '/api-keys', apiKey);
208
+ });
209
+ }
210
+ updateKey(apiKey, updatedKey) {
211
+ return __awaiter(this, void 0, void 0, function* () {
212
+ return yield this.api.request(HttpMethod.PATCH, `/api-keys/${apiKey}`, updatedKey);
213
+ });
214
+ }
215
+ deleteKey(apiKey) {
216
+ return __awaiter(this, void 0, void 0, function* () {
217
+ return yield this.api.request(HttpMethod.DELETE, `/api-keys/${apiKey}`);
218
+ });
219
+ }
220
+ reactivateKey(apiKey) {
221
+ return __awaiter(this, void 0, void 0, function* () {
222
+ return yield this.api.request(HttpMethod.PATCH, `/api-keys/${apiKey}/reactivate`);
223
+ });
224
+ }
225
+ rotateKey(apiKey) {
226
+ return __awaiter(this, void 0, void 0, function* () {
227
+ return yield this.api.request(HttpMethod.POST, `/api-keys/${apiKey}/rotate`);
228
+ });
229
+ }
230
+ getKeysFilterEndpoint(filter) {
231
+ let filters = [];
232
+ if (filter !== undefined) {
233
+ filters = Object.entries(filter).map(([key, value]) => `${key}=${value}`);
234
+ }
235
+ return `${this.endpoint}${filter !== undefined ? '?' : ''}${filters.join('&')}`;
236
+ }
235
237
  }
236
238
 
237
- class Projects {
238
- constructor(apiService) {
239
- this.api = apiService;
240
- this.endpoint = '/projects';
241
- }
242
- getProjects(accountId) {
243
- return __awaiter(this, void 0, void 0, function* () {
244
- return yield this.api.request(HttpMethod.GET, `${this.endpoint}?accountId=${accountId}`);
245
- });
246
- }
247
- getProject(projectId) {
248
- return __awaiter(this, void 0, void 0, function* () {
249
- return yield this.api.request(HttpMethod.GET, `${this.endpoint}/${projectId}`);
250
- });
251
- }
252
- deleteProject(projectId) {
253
- return __awaiter(this, void 0, void 0, function* () {
254
- return yield this.api.request(HttpMethod.DELETE, `${this.endpoint}/${projectId}`);
255
- });
256
- }
257
- createProject(project) {
258
- return __awaiter(this, void 0, void 0, function* () {
259
- return yield this.api.request(HttpMethod.POST, this.endpoint, project);
260
- });
261
- }
262
- updateProject(projectId, project) {
263
- return __awaiter(this, void 0, void 0, function* () {
264
- return this.api.request(HttpMethod.PATCH, `${this.endpoint}/${projectId}`, project);
265
- });
266
- }
239
+ class Projects {
240
+ constructor(apiService) {
241
+ this.api = apiService;
242
+ this.endpoint = '/projects';
243
+ }
244
+ getProjects(accountId) {
245
+ return __awaiter(this, void 0, void 0, function* () {
246
+ return yield this.api.request(HttpMethod.GET, `${this.endpoint}?accountId=${accountId}`);
247
+ });
248
+ }
249
+ getProject(projectId) {
250
+ return __awaiter(this, void 0, void 0, function* () {
251
+ return yield this.api.request(HttpMethod.GET, `${this.endpoint}/${projectId}`);
252
+ });
253
+ }
254
+ deleteProject(projectId) {
255
+ return __awaiter(this, void 0, void 0, function* () {
256
+ return yield this.api.request(HttpMethod.DELETE, `${this.endpoint}/${projectId}`);
257
+ });
258
+ }
259
+ createProject(project) {
260
+ return __awaiter(this, void 0, void 0, function* () {
261
+ return yield this.api.request(HttpMethod.POST, this.endpoint, project);
262
+ });
263
+ }
264
+ updateProject(projectId, project) {
265
+ return __awaiter(this, void 0, void 0, function* () {
266
+ return this.api.request(HttpMethod.PATCH, `${this.endpoint}/${projectId}`, project);
267
+ });
268
+ }
267
269
  }
268
270
 
269
- class Accounts {
270
- constructor(apiService) {
271
- this.api = apiService;
272
- this.endpoint = '/accounts';
273
- }
274
- getAccount(accountId) {
275
- return __awaiter(this, void 0, void 0, function* () {
276
- return yield this.api.request(HttpMethod.GET, `${this.endpoint}/${accountId}`);
277
- });
278
- }
271
+ class Accounts {
272
+ constructor(apiService) {
273
+ this.api = apiService;
274
+ this.endpoint = '/accounts';
275
+ }
276
+ getAccount(accountId) {
277
+ return __awaiter(this, void 0, void 0, function* () {
278
+ return yield this.api.request(HttpMethod.GET, `${this.endpoint}/${accountId}`);
279
+ });
280
+ }
279
281
  }
280
282
 
281
- // eslint-disable-next-line @typescript-eslint/no-empty-function
282
- const noop = () => { };
283
- class TheAuthAPI {
284
- /**
285
- * @param {String} accessKey
286
- * @param {Object} [options] (optional)
287
- * @property {String} host (default: 'https://api.segment.io')
288
- * @property {number} retryCount (default: 3)
289
- */
290
- constructor(accessKey, options) {
291
- var _a;
292
- assert(accessKey, "You must pass your project's write key.");
293
- this.accessKey = accessKey;
294
- this.host = removeSlash((options === null || options === void 0 ? void 0 : options.host) || 'https://api.theauthapi.com');
295
- this.api = new ApiRequest({
296
- accessKey: this.accessKey,
297
- host: this.host,
298
- retryCount: (_a = options === null || options === void 0 ? void 0 : options.retryCount) !== null && _a !== void 0 ? _a : 3,
299
- });
300
- this.apiKeys = new ApiKeys(this.api);
301
- this.projects = new Projects(this.api);
302
- this.accounts = new Accounts(this.api);
303
- }
304
- /*
305
- @deprecated
306
- */
307
- authenticateAPIKey(key, callback) {
308
- return __awaiter(this, void 0, void 0, function* () {
309
- const cb = callback || noop;
310
- const done = (err) => {
311
- cb(err, data);
312
- };
313
- const data = {
314
- credentials: { api_key: key },
315
- timestamp: new Date().getTime(),
316
- sentAt: new Date().getTime(),
317
- };
318
- try {
319
- const key = yield this.api.request(HttpMethod.POST, '/auth/authenticate', data);
320
- done(key);
321
- return key;
322
- }
323
- catch (err) {
324
- if (err.response) {
325
- const error = new Error(err.response.statusText);
326
- return done(error);
327
- }
328
- done(err);
329
- }
330
- });
331
- }
283
+ // eslint-disable-next-line @typescript-eslint/no-empty-function
284
+ const noop = () => { };
285
+ class TheAuthAPI {
286
+ /**
287
+ * @param {String} accessKey
288
+ * @param {Object} [options] (optional)
289
+ * @property {String} host (default: 'https://api.segment.io')
290
+ * @property {number} retryCount (default: 3)
291
+ */
292
+ constructor(accessKey, options) {
293
+ var _a;
294
+ assert(accessKey, "You must pass your project's write key.");
295
+ this.accessKey = accessKey;
296
+ this.host = removeSlash((options === null || options === void 0 ? void 0 : options.host) || 'https://api.theauthapi.com');
297
+ this.api = new ApiRequest({
298
+ accessKey: this.accessKey,
299
+ host: this.host,
300
+ retryCount: (_a = options === null || options === void 0 ? void 0 : options.retryCount) !== null && _a !== void 0 ? _a : 3,
301
+ });
302
+ this.apiKeys = new ApiKeys(this.api);
303
+ this.projects = new Projects(this.api);
304
+ this.accounts = new Accounts(this.api);
305
+ }
306
+ /*
307
+ @deprecated
308
+ */
309
+ authenticateAPIKey(key, callback) {
310
+ return __awaiter(this, void 0, void 0, function* () {
311
+ const cb = callback || noop;
312
+ const done = (err) => {
313
+ cb(err, data);
314
+ };
315
+ const data = {
316
+ credentials: { api_key: key },
317
+ timestamp: new Date().getTime(),
318
+ sentAt: new Date().getTime(),
319
+ };
320
+ try {
321
+ const key = yield this.api.request(HttpMethod.POST, '/auth/authenticate', data);
322
+ done(key);
323
+ return key;
324
+ }
325
+ catch (err) {
326
+ if (err.response) {
327
+ const error = new Error(err.response.statusText);
328
+ return done(error);
329
+ }
330
+ done(err);
331
+ }
332
+ });
333
+ }
332
334
  }
333
335
 
334
336
  export { TheAuthAPI as default };