theauthapi 1.0.11 → 1.0.13

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/README.md +366 -334
  2. package/dist/endpoints/Accounts/Accounts.d.ts +10 -10
  3. package/dist/endpoints/Accounts/AccountsInterface.d.ts +4 -4
  4. package/dist/endpoints/ApiKeys/ApiKeys.d.ts +19 -18
  5. package/dist/endpoints/ApiKeys/ApiKeysInterface.d.ts +12 -11
  6. package/dist/endpoints/Projects/Projects.d.ts +14 -14
  7. package/dist/endpoints/Projects/ProjectsInterface.d.ts +8 -8
  8. package/dist/index.cjs +336 -0
  9. package/dist/index.d.ts +200 -26
  10. package/dist/index.mjs +334 -0
  11. package/dist/libraryMeta.d.ts +1 -1
  12. package/dist/services/ApiRequest/ApiCall.d.ts +5 -5
  13. package/dist/services/ApiRequest/ApiRequest.d.ts +24 -24
  14. package/dist/services/ApiRequest/ApiRequestError.d.ts +10 -10
  15. package/dist/services/ApiRequest/ApiResponseError.d.ts +12 -12
  16. package/dist/services/ApiRequest/HttpMethod.d.ts +7 -7
  17. package/dist/types/index.d.ts +85 -85
  18. package/package.json +14 -7
  19. package/History.md +0 -5
  20. package/dist/endpoints/Accounts/Accounts.js +0 -24
  21. package/dist/endpoints/Accounts/AccountsInterface.js +0 -2
  22. package/dist/endpoints/ApiKeys/ApiKeys.js +0 -80
  23. package/dist/endpoints/ApiKeys/ApiKeysInterface.js +0 -2
  24. package/dist/endpoints/Projects/Projects.js +0 -44
  25. package/dist/endpoints/Projects/ProjectsInterface.js +0 -2
  26. package/dist/index.js +0 -74
  27. package/dist/libraryMeta.js +0 -4
  28. package/dist/services/ApiRequest/ApiCall.js +0 -2
  29. package/dist/services/ApiRequest/ApiRequest.js +0 -115
  30. package/dist/services/ApiRequest/ApiRequestError.js +0 -15
  31. package/dist/services/ApiRequest/ApiResponseError.js +0 -17
  32. package/dist/services/ApiRequest/HttpMethod.js +0 -11
  33. package/dist/types/index.js +0 -13
  34. package/dist/usage.js +0 -20
  35. package/dist/util/index.d.ts +0 -1
  36. package/dist/util/index.js +0 -13
@@ -1,10 +1,10 @@
1
- import { AccountsInterface } from "./AccountsInterface";
2
- import ApiRequest from "../../services/ApiRequest/ApiRequest";
3
- import { Account } from "../../types";
4
- declare class Accounts implements AccountsInterface {
5
- api: ApiRequest;
6
- endpoint: string;
7
- constructor(apiService: ApiRequest);
8
- getAccount(accountId: string): Promise<Account>;
9
- }
10
- export default Accounts;
1
+ import { AccountsInterface } from "./AccountsInterface";
2
+ import ApiRequest from "../../services/ApiRequest/ApiRequest";
3
+ import { Account } from "../../types";
4
+ declare class Accounts implements AccountsInterface {
5
+ api: ApiRequest;
6
+ endpoint: string;
7
+ constructor(apiService: ApiRequest);
8
+ getAccount(accountId: string): Promise<Account>;
9
+ }
10
+ export default Accounts;
@@ -1,4 +1,4 @@
1
- import { Account } from "../../types";
2
- export interface AccountsInterface {
3
- getAccount(accountId: string): Promise<Account>;
4
- }
1
+ import { Account } from "../../types";
2
+ export interface AccountsInterface {
3
+ getAccount(accountId: string): Promise<Account>;
4
+ }
@@ -1,18 +1,19 @@
1
- import ApiRequest from "../../services/ApiRequest/ApiRequest";
2
- import { ApiKey, ApiKeyFilter, ApiKeyInput, UpdateApiKeyInput } from "../../types";
3
- import { ApiKeysInterface } from "./ApiKeysInterface";
4
- declare class ApiKeys implements ApiKeysInterface {
5
- api: ApiRequest;
6
- private readonly endpoint;
7
- constructor(apiService: ApiRequest);
8
- isValidKey(apikey: string): Promise<boolean>;
9
- authenticateKey(apikey: string): Promise<ApiKey>;
10
- getKeys(filter?: ApiKeyFilter): Promise<ApiKey[]>;
11
- getKey(apikey: string): Promise<ApiKey>;
12
- createKey(apiKey: ApiKeyInput): Promise<ApiKey>;
13
- updateKey(apiKey: string, updatedKey: UpdateApiKeyInput): Promise<ApiKey>;
14
- deleteKey(apiKey: string): Promise<boolean>;
15
- reactivateKey(apiKey: string): Promise<ApiKey>;
16
- private getKeysFilterEndpoint;
17
- }
18
- export default ApiKeys;
1
+ import ApiRequest from "../../services/ApiRequest/ApiRequest";
2
+ import { ApiKey, ApiKeyFilter, ApiKeyInput, UpdateApiKeyInput } from "../../types";
3
+ import { ApiKeysInterface } from "./ApiKeysInterface";
4
+ declare class ApiKeys implements ApiKeysInterface {
5
+ api: ApiRequest;
6
+ private readonly endpoint;
7
+ constructor(apiService: ApiRequest);
8
+ isValidKey(apikey: string): Promise<boolean>;
9
+ authenticateKey(apikey: string): Promise<ApiKey>;
10
+ getKeys(filter?: ApiKeyFilter): Promise<ApiKey[]>;
11
+ getKey(apikey: string): Promise<ApiKey>;
12
+ createKey(apiKey: ApiKeyInput): Promise<ApiKey>;
13
+ updateKey(apiKey: string, updatedKey: UpdateApiKeyInput): Promise<ApiKey>;
14
+ deleteKey(apiKey: string): Promise<boolean>;
15
+ reactivateKey(apiKey: string): Promise<ApiKey>;
16
+ rotateKey(apiKey: string): Promise<ApiKey>;
17
+ private getKeysFilterEndpoint;
18
+ }
19
+ export default ApiKeys;
@@ -1,11 +1,12 @@
1
- import { ApiKey, ApiKeyFilter, ApiKeyInput, UpdateApiKeyInput } from "../../types";
2
- export interface ApiKeysInterface {
3
- isValidKey(apiKey: string): Promise<boolean>;
4
- getKey(apiKey: string): Promise<ApiKey>;
5
- authenticateKey(apiKey: string): Promise<ApiKey>;
6
- getKeys(filter?: ApiKeyFilter): Promise<ApiKey[]>;
7
- createKey(apiKey: ApiKeyInput): Promise<ApiKey>;
8
- updateKey(apiKey: string, updateTo: UpdateApiKeyInput): Promise<ApiKey>;
9
- deleteKey(apiKey: string): Promise<boolean>;
10
- reactivateKey(apiKey: string): Promise<ApiKey>;
11
- }
1
+ import { ApiKey, ApiKeyFilter, ApiKeyInput, UpdateApiKeyInput } from "../../types";
2
+ export interface ApiKeysInterface {
3
+ isValidKey(apiKey: string): Promise<boolean>;
4
+ getKey(apiKey: string): Promise<ApiKey>;
5
+ authenticateKey(apiKey: string): Promise<ApiKey>;
6
+ getKeys(filter?: ApiKeyFilter): Promise<ApiKey[]>;
7
+ createKey(apiKey: ApiKeyInput): Promise<ApiKey>;
8
+ updateKey(apiKey: string, updateTo: UpdateApiKeyInput): Promise<ApiKey>;
9
+ deleteKey(apiKey: string): Promise<boolean>;
10
+ reactivateKey(apiKey: string): Promise<ApiKey>;
11
+ rotateKey(apiKey: string): Promise<ApiKey>;
12
+ }
@@ -1,14 +1,14 @@
1
- import ApiRequest from "../../services/ApiRequest/ApiRequest";
2
- import { CreateProjectInput, Project, UpdateProjectInput } from "../../types";
3
- import { ProjectsInterface } from "./ProjectsInterface";
4
- declare class Projects implements ProjectsInterface {
5
- api: ApiRequest;
6
- endpoint: string;
7
- constructor(apiService: ApiRequest);
8
- getProjects(accountId: string): Promise<Project[]>;
9
- getProject(projectId: string): Promise<Project>;
10
- deleteProject(projectId: string): Promise<boolean>;
11
- createProject(project: CreateProjectInput): Promise<Project>;
12
- updateProject(projectId: string, project: UpdateProjectInput): Promise<Project>;
13
- }
14
- export default Projects;
1
+ import ApiRequest from "../../services/ApiRequest/ApiRequest";
2
+ import { CreateProjectInput, Project, UpdateProjectInput } from "../../types";
3
+ import { ProjectsInterface } from "./ProjectsInterface";
4
+ declare class Projects implements ProjectsInterface {
5
+ api: ApiRequest;
6
+ endpoint: string;
7
+ constructor(apiService: ApiRequest);
8
+ getProjects(accountId: string): Promise<Project[]>;
9
+ getProject(projectId: string): Promise<Project>;
10
+ deleteProject(projectId: string): Promise<boolean>;
11
+ createProject(project: CreateProjectInput): Promise<Project>;
12
+ updateProject(projectId: string, project: UpdateProjectInput): Promise<Project>;
13
+ }
14
+ export default Projects;
@@ -1,8 +1,8 @@
1
- import { CreateProjectInput, Project, UpdateProjectInput } from "../../types";
2
- export interface ProjectsInterface {
3
- getProjects(accountId: string): Promise<Project[]>;
4
- getProject(projectId: string): Promise<Project>;
5
- deleteProject(projectId: string): Promise<boolean>;
6
- createProject(project: CreateProjectInput): Promise<Project>;
7
- updateProject(name: string, updateTo: UpdateProjectInput): Promise<Project>;
8
- }
1
+ import { CreateProjectInput, Project, UpdateProjectInput } from "../../types";
2
+ export interface ProjectsInterface {
3
+ getProjects(accountId: string): Promise<Project[]>;
4
+ getProject(projectId: string): Promise<Project>;
5
+ deleteProject(projectId: string): Promise<boolean>;
6
+ createProject(project: CreateProjectInput): Promise<Project>;
7
+ updateProject(name: string, updateTo: UpdateProjectInput): Promise<Project>;
8
+ }
package/dist/index.cjs ADDED
@@ -0,0 +1,336 @@
1
+ 'use strict';
2
+
3
+ var assert = require('assert');
4
+ var removeSlash = require('remove-trailing-slash');
5
+ var axios = require('axios');
6
+ var axiosRetry = require('axios-retry');
7
+
8
+ /*! *****************************************************************************
9
+ Copyright (c) Microsoft Corporation.
10
+
11
+ Permission to use, copy, modify, and/or distribute this software for any
12
+ purpose with or without fee is hereby granted.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
15
+ REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
16
+ AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
17
+ INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
18
+ LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
19
+ OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
20
+ PERFORMANCE OF THIS SOFTWARE.
21
+ ***************************************************************************** */
22
+ /* global Reflect, Promise */
23
+
24
+
25
+ function __awaiter(thisArg, _arguments, P, generator) {
26
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
27
+ return new (P || (P = Promise))(function (resolve, reject) {
28
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
29
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
30
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
31
+ step((generator = generator.apply(thisArg, [])).next());
32
+ });
33
+ }
34
+
35
+ const version = "1.0.11";
36
+
37
+ /**
38
+ *
39
+ * Throws when no response was received from the server
40
+ * @param message - error message
41
+ *
42
+ * */
43
+ class ApiRequestError extends Error {
44
+ constructor(message) {
45
+ super(message);
46
+ this.name = 'ApiRequestError';
47
+ }
48
+ }
49
+
50
+ /**
51
+ *
52
+ * Throws when the server responds with a status code that falls out of the range 2xx
53
+ * @param statusCode - HTTP status code the server responded with
54
+ * @param message - error message
55
+ *
56
+ * */
57
+ class ApiResponseError extends Error {
58
+ constructor(statusCode, message) {
59
+ super(`(${statusCode}): ${message}`);
60
+ this.statusCode = statusCode;
61
+ this.name = "ApiResponseError";
62
+ }
63
+ }
64
+
65
+ class ApiRequest {
66
+ constructor(config) {
67
+ const { host, accessKey, headers, retryCount } = config;
68
+ this.host = host;
69
+ this.accessKey = accessKey;
70
+ this.headers = this._generateDefaultHeaders();
71
+ this.retryCount = retryCount !== null && retryCount !== void 0 ? retryCount : 3;
72
+ if (headers) {
73
+ this.headers = Object.assign(Object.assign({}, this.headers), { headers });
74
+ }
75
+ this._init();
76
+ }
77
+ _init() {
78
+ var _a;
79
+ const isoDateFormat = /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(?:\.\d*)?(?:[-+]\d{2}:?\d{2}|Z)?$/;
80
+ function isIsoDateString(value) {
81
+ return value && typeof value === "string" && isoDateFormat.test(value);
82
+ }
83
+ function handleDates(body) {
84
+ if (body === null || body === undefined || typeof body !== "object") {
85
+ return body;
86
+ }
87
+ for (const key of Object.keys(body)) {
88
+ const value = body[key];
89
+ if (isIsoDateString(value)) {
90
+ body[key] = new Date(value);
91
+ }
92
+ else if (typeof value === "object") {
93
+ handleDates(value);
94
+ }
95
+ }
96
+ }
97
+ axios.interceptors.response.use((response) => {
98
+ handleDates(response.data);
99
+ return response;
100
+ });
101
+ axiosRetry(axios, {
102
+ retries: (_a = this.retryCount) !== null && _a !== void 0 ? _a : 3,
103
+ retryCondition: this._isErrorRetryable,
104
+ retryDelay: axiosRetry.exponentialDelay,
105
+ });
106
+ }
107
+ request(method, endpoint, payload) {
108
+ var _a;
109
+ return __awaiter(this, void 0, void 0, function* () {
110
+ try {
111
+ const response = yield axios.request({
112
+ baseURL: this.host,
113
+ method: method,
114
+ url: endpoint,
115
+ data: payload,
116
+ headers: this.headers,
117
+ });
118
+ return response.data;
119
+ }
120
+ catch (error) {
121
+ if (axios.isAxiosError(error)) {
122
+ if (error.response) {
123
+ throw new ApiResponseError(error.response.status, (_a = error.response.data.message) !== null && _a !== void 0 ? _a : error.response.statusText);
124
+ }
125
+ else if (error.request) {
126
+ throw new ApiRequestError(error.message);
127
+ }
128
+ }
129
+ throw error;
130
+ }
131
+ });
132
+ }
133
+ _generateDefaultHeaders() {
134
+ return {
135
+ "user-agent": `theauthapi-client-node/${version}`,
136
+ "x-api-key": this.accessKey,
137
+ "api-key": this.accessKey,
138
+ };
139
+ }
140
+ _isErrorRetryable(error) {
141
+ // Retry Network Errors.
142
+ if (axiosRetry.isNetworkError(error)) {
143
+ return true;
144
+ }
145
+ if (!error.response) {
146
+ // Cannot determine if the request can be retried
147
+ return false;
148
+ }
149
+ // Retry Server Errors (5xx).
150
+ if (error.response.status >= 500 && error.response.status <= 599) {
151
+ return true;
152
+ }
153
+ // Retry if rate limited.
154
+ if (error.response.status === 429) {
155
+ return true;
156
+ }
157
+ return false;
158
+ }
159
+ }
160
+
161
+ var HttpMethod;
162
+ (function (HttpMethod) {
163
+ HttpMethod["GET"] = "GET";
164
+ HttpMethod["POST"] = "POST";
165
+ HttpMethod["DELETE"] = "DELETE";
166
+ HttpMethod["PATCH"] = "PATCH";
167
+ HttpMethod["PUT"] = "PUT";
168
+ })(HttpMethod || (HttpMethod = {}));
169
+
170
+ class ApiKeys {
171
+ constructor(apiService) {
172
+ this.api = apiService;
173
+ this.endpoint = "/api-keys/";
174
+ }
175
+ isValidKey(apikey) {
176
+ return __awaiter(this, void 0, void 0, function* () {
177
+ try {
178
+ const key = yield this.authenticateKey(apikey);
179
+ return key.key !== undefined;
180
+ }
181
+ catch (error) {
182
+ if (error instanceof ApiResponseError && error.statusCode === 404) {
183
+ return false;
184
+ }
185
+ throw error;
186
+ }
187
+ });
188
+ }
189
+ authenticateKey(apikey) {
190
+ return __awaiter(this, void 0, void 0, function* () {
191
+ return yield this.api.request(HttpMethod.POST, `/api-keys/auth/${apikey}`);
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
+ }
237
+ }
238
+
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
+ }
269
+ }
270
+
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
+ }
281
+ }
282
+
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
+ }
334
+ }
335
+
336
+ module.exports = TheAuthAPI;