theauthapi 1.0.4 → 1.0.7

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 (57) hide show
  1. package/README.md +284 -2
  2. package/dist/endpoints/Accounts/Accounts.d.ts +10 -0
  3. package/dist/endpoints/Accounts/Accounts.js +26 -0
  4. package/dist/endpoints/Accounts/AccountsInterface.d.ts +4 -0
  5. package/dist/endpoints/Accounts/AccountsInterface.js +2 -0
  6. package/dist/endpoints/ApiKeys/ApiKeys.d.ts +16 -0
  7. package/dist/endpoints/ApiKeys/ApiKeys.js +91 -0
  8. package/dist/endpoints/ApiKeys/ApiKeysInterface.d.ts +9 -0
  9. package/dist/endpoints/ApiKeys/ApiKeysInterface.js +2 -0
  10. package/dist/endpoints/Projects/Projects.d.ts +16 -0
  11. package/dist/endpoints/Projects/Projects.js +68 -0
  12. package/dist/endpoints/Projects/ProjectsInterface.d.ts +8 -0
  13. package/dist/endpoints/Projects/ProjectsInterface.js +2 -0
  14. package/dist/index.d.ts +36 -0
  15. package/dist/index.js +89 -0
  16. package/dist/libraryMeta.d.ts +1 -0
  17. package/dist/libraryMeta.js +4 -0
  18. package/dist/services/ApiRequest/ApiCall.d.ts +5 -0
  19. package/dist/services/ApiRequest/ApiCall.js +2 -0
  20. package/dist/services/ApiRequest/ApiRequest.d.ts +26 -0
  21. package/dist/services/ApiRequest/ApiRequest.js +117 -0
  22. package/dist/services/ApiRequest/ApiRequestError.d.ts +10 -0
  23. package/dist/services/ApiRequest/ApiRequestError.js +15 -0
  24. package/dist/services/ApiRequest/ApiResponseError.d.ts +12 -0
  25. package/dist/services/ApiRequest/ApiResponseError.js +17 -0
  26. package/dist/services/ApiRequest/HttpMethod.d.ts +7 -0
  27. package/dist/services/ApiRequest/HttpMethod.js +11 -0
  28. package/dist/types/index.d.ts +65 -0
  29. package/dist/types/index.js +13 -0
  30. package/dist/util/index.d.ts +1 -0
  31. package/dist/util/index.js +13 -0
  32. package/package.json +4 -9
  33. package/.eslintrc.js +0 -15
  34. package/.prettierrc.json +0 -1
  35. package/jest.config.js +0 -5
  36. package/jest.setup.ts +0 -0
  37. package/src/endpoints/ApiKeys/ApiKeys.ts +0 -68
  38. package/src/endpoints/ApiKeys/ApiKeysInterface.ts +0 -11
  39. package/src/endpoints/Projects/Projects.ts +0 -62
  40. package/src/endpoints/Projects/ProjectsInterface.ts +0 -9
  41. package/src/index.ts +0 -103
  42. package/src/libraryMeta.ts +0 -1
  43. package/src/services/ApiRequest/ApiCall.ts +0 -7
  44. package/src/services/ApiRequest/ApiRequest.ts +0 -132
  45. package/src/services/ApiRequest/ApiRequestError.ts +0 -7
  46. package/src/services/ApiRequest/HttpMethod.ts +0 -7
  47. package/src/tests/endpoints/apiKeys.spec.ts +0 -102
  48. package/src/tests/endpoints/projects.spec.ts +0 -105
  49. package/src/tests/index.spec.ts +0 -35
  50. package/src/tests/testServer/middleware/middleware.ts +0 -17
  51. package/src/tests/testServer/routes/apiKeys.ts +0 -89
  52. package/src/tests/testServer/routes/auth.ts +0 -24
  53. package/src/tests/testServer/routes/projects.ts +0 -87
  54. package/src/tests/testServer/server.ts +0 -16
  55. package/src/types/index.ts +0 -59
  56. package/src/util/index.ts +0 -7
  57. package/tsconfig.json +0 -102
package/README.md CHANGED
@@ -1,2 +1,284 @@
1
- # theauthapi
2
- Client library for TheAuthAPI.com
1
+ # Client library for [TheAuthAPI](https://theauthapi.com/)
2
+
3
+ **Contents**
4
+
5
+ - [Client library for TheAuthAPI](#client-library-for-theauthapi)
6
+ - [Installation](#installation)
7
+ - [Configuration](#configuration)
8
+ - [Usage](#usage)
9
+ - [Example: Validating an api-key](#example-validating-an-api-key)
10
+ - [Example: Listing the projects of an account](#example-listing-the-projects-of-an-account)
11
+ - [Example: Listing projects and associated API Keys](#example-listing-projects-and-associated-api-keys)
12
+ - [Example: Creating an API Key](#example-creating-an-api-key)
13
+ - [Handling Errors](#handling-errors)
14
+ - [Typescript](#typescript)
15
+ - [📙 Further Reading](#-further-reading)
16
+
17
+ **Scalable API Key Management and Auth Control**
18
+ Secure your API with best in class API Key management, user access, all with great analytics.
19
+
20
+ ## Installation
21
+
22
+ This library is published on [npm](https://www.npmjs.com/package/theauthapi), you can add it as a dependency using the following
23
+ command
24
+
25
+ ```bash
26
+ npm install theauthapi
27
+ # or
28
+ yarn add theauthapi
29
+ ```
30
+
31
+ ## Configuration
32
+
33
+ You'll need to configure the library with your `access key` and `account id`, you can grab these from [TheAuthAPI](https://app.theauthapi.com/dashboard) dashboard.
34
+
35
+ For further instructions on creating an account, check out our [how to guides](https://thatapicompany.notion.site/The-Auth-API-Knowledge-Base-21660cee84e640729714fad43d9ce546).
36
+
37
+ ### Imports
38
+
39
+ #### CommonJS
40
+
41
+ ```javascript
42
+ const TheAuthAPI = require("theauthapi").default;
43
+ ```
44
+
45
+ #### ES Modules
46
+
47
+ ```typescript
48
+ import TheAuthAPI from "theauthapi";
49
+ ```
50
+
51
+ initialize the client using your access key:
52
+
53
+ ```javascript
54
+ const theAuthAPI = new TheAuthAPI("YOUR_ACCESS_KEY");
55
+ ```
56
+
57
+ You can also provide custom options:
58
+
59
+ ```javascript
60
+ const theAuthAPI = new TheAuthAPI("YOUR_ACCESS_KEY", {
61
+ timeout: 3600,
62
+ retryCount: 2,
63
+ });
64
+ ```
65
+
66
+ **Full option types:**
67
+
68
+ ```typescript
69
+ type Options = {
70
+ // server url
71
+ host?: string;
72
+ // request timeout in ms
73
+ timeout?: string | number;
74
+ // number of retries before failing
75
+ retryCount?: number;
76
+ };
77
+ ```
78
+
79
+ ## Usage
80
+
81
+ After initiating the client, you can access endpoint methods using the following pattern:
82
+ `[object instance].[endpoint].[method]`
83
+
84
+ For example, getting the projects for an account would be: `theAuthApiClient.projects.getProjects("ACCOUNT_ID")`,
85
+
86
+ Similarly, getting the api keys would be:
87
+ `theAuthApiClient.apiKeys.getKeys("PROJECT_ID")`
88
+
89
+ | endpoint | attribute | example |
90
+ | --------- | --------- | --------------------------------------------- |
91
+ | /api-keys | apiKeys | `client.apiKeys.createKey("MY_KEY")` |
92
+ | /projects | projects | `client.projects.createProject("MY_PROJECT")` |
93
+ | /accounts | accounts | `client.accounts.createAccount("MY_ACCOUNT")` |
94
+
95
+ For details on each endpoint accepted values, please reference these docs: [docs.theauthapi.com](https://docs.theauthapi.com/)
96
+
97
+ All methods return a promise containing the returned JSON as a javascript object. Each method of an endpoint maps HTTP methods to
98
+
99
+ | HTTP Method | method name | example |
100
+ | ----------- | ----------- | ------------------------------------------------------------------------- |
101
+ | POST | create\* | `client.apiKeys.createKey({ name: "KEY_NAME", projectId: "PROJECT_ID" })` |
102
+ | GET | get\* | `client.apiKeys.getKeys("PROJECT_ID")` |
103
+ | DELETE | delete\* | `client.apiKeys.deleteKey("MY_KEY")` |
104
+ | PATCH | update\* | `client.apiKeys.updateKey("MY_KEY", { name: "UPDATED_KEY_NAME" })` |
105
+
106
+ #### Example: Validating an api-key
107
+
108
+ You can easily validate an API key using `apiKeys.isValidKey` which returns `true` if the key is valid, `false` otherwise.
109
+ `isValidKey` throws an `ApiRequestError` if there's a network issue, it's advised to wrap it in a `try/catch` to handle the potential error
110
+
111
+ ```javascript
112
+ theAuthAPI.apiKeys
113
+ .isValidKey("API_KEY")
114
+ .then((isValidKey) => {
115
+ if (isValidKey) {
116
+ console.log("The API is valid!");
117
+ } else {
118
+ console.log("Invalid API key!");
119
+ }
120
+ })
121
+ .catch((error) => {
122
+ // handle network error
123
+ });
124
+ ```
125
+
126
+ **Using async/await**
127
+
128
+ ```javascript
129
+ try {
130
+ const isValidKey = await theAuthAPI.apiKeys.isValidKey("API_KEY");
131
+ if (isValidKey) {
132
+ console.log("The API is valid!");
133
+ } else {
134
+ console.log("Invalid API key!");
135
+ }
136
+ } catch (error) {
137
+ // handle network error
138
+ }
139
+ ```
140
+
141
+ #### Example: Listing the projects of an account
142
+
143
+ ```javascript
144
+ theAuthAPI.projects
145
+ .getProjects("ACCOUNT_ID")
146
+ .then((projects) => console.log(projects))
147
+ .catch((error) => console.log(error));
148
+ ```
149
+
150
+ **Using async/await**
151
+
152
+ ```javascript
153
+ try {
154
+ const projects = await client.projects.getProjects("ACCOUNT_ID");
155
+ } catch (error) {
156
+ console.log(error);
157
+ }
158
+ ```
159
+
160
+ #### Example: Listing projects and associated API Keys
161
+
162
+ ```javascript
163
+ async function getProjectsWithKeys(accountId: string) {
164
+ try {
165
+ const projects = await theAuthAPI.projects.getProjects(accountId);
166
+ const projectsKeys = projects.map(async (project) => {
167
+ const keys = await theAuthAPI.apiKeys.getKeys(project.id);
168
+ return { project, keys };
169
+ });
170
+ return await Promise.all(projectsKeys);
171
+ } catch (error) {
172
+ // handle error
173
+ }
174
+ }
175
+ ```
176
+
177
+ #### Example: Creating an API Key
178
+
179
+ ```javascript
180
+ theAuthAPI.apiKeys
181
+ .createKey({
182
+ projectId: "PROJECT_ID",
183
+ customMetaData: { metadata_val: "value to store" },
184
+ customAccountId: "[any info you want]",
185
+ name: "[any info you want e.g. name of customer or the key]",
186
+ })
187
+ .then((key) => console.log("Key created > ", key))
188
+ .catch((error) => console.log("Couldn't make the key", error));
189
+ ```
190
+
191
+ **Using async/await**
192
+
193
+ ```javascript
194
+ try {
195
+ const key = await theAuthAPI.apiKeys.createKey({
196
+ projectId: "PROJECT_ID",
197
+ customMetaData: { metadata_val: "value to store" },
198
+ customAccountId: "[any info you want]",
199
+ name: "[any info you want e.g. name of customer or the key]",
200
+ });
201
+ console.log("Key created > ", key);
202
+ } catch (error) {
203
+ console.log("Couldn't make the key ", error);
204
+ }
205
+ ```
206
+
207
+ ### Handling Errors
208
+
209
+ [comment]: <> (All methods that return a promise throw 3 types of errors)
210
+
211
+ ##### ApiRequestError
212
+
213
+ Thrown when there's a network or a connectivity issue, for example, if the client didn't establish any network connection with the host
214
+
215
+ ```
216
+ ApiRequestError: getaddrinfo EAI_AGAIN api.theauthapi.com
217
+ ```
218
+
219
+ ##### ApiResponseError
220
+
221
+ Thrown when the server responds with an HTTP status code not in the `2xx` range. `ApiRequestError` provides two properties to distinguish the type of the error
222
+
223
+ - `statusCode` HTTP status code
224
+ - `message` the message the server responded with in the body
225
+
226
+ This is the most common thrown error, you should expect and handle it each time you use any of the library methods
227
+
228
+ ##### Example: Getting a key throws an ApiResponseError if the key is invalid
229
+
230
+ If you try to GET an invalid key using `apiKeys.getKey("invalid-key")`, the server responds with a 404 error and an `ApiResponseError` is thrown
231
+
232
+ ```
233
+ ApiResponseError: (404): Invalid client key
234
+ ```
235
+
236
+ "404" is the `statusCode`, "Invalid client Key" is the `message`, you can access these properties using `error.statusCode` and `error.message` respectively
237
+
238
+ ##### Error
239
+
240
+ Unknown error, just a normal javascript error
241
+
242
+ #### Handling Errors the Right Way
243
+
244
+ Since all the possible thrown errors are instances of classes, we can check the type of the thrown error and handle it accordingly
245
+
246
+ ```javascript
247
+ try {
248
+ const key = await theAuthAPI.apiKeys.getKey("KEY");
249
+ } catch (error) {
250
+ if (error instanceof ApiResponseError) {
251
+ // handle response error
252
+ }
253
+ if (error instanceof ApiRequestError) {
254
+ // handle network error
255
+ }
256
+ // unknown error
257
+ throw error;
258
+ }
259
+ ```
260
+
261
+ ### Typescript
262
+
263
+ This library is written in [Typescript](https://www.typescriptlang.org/), types are provided out of the box.
264
+
265
+ Example of usage with Typescript:
266
+
267
+ ```typescript
268
+ import TheAuthAPI from "theauthapi";
269
+ import { Project } from "theauthapi/types";
270
+
271
+ const client = new TheAuthAPI("ACCESS_KEY");
272
+
273
+ async function getProjectsIds(accountId: string): Promise<string[]> {
274
+ const projects: Project[] = await client.projects.getProjects(accountId);
275
+ return projects.map((project) => project.name);
276
+ }
277
+ ```
278
+
279
+ ### 📙 Further Reading
280
+
281
+ - Create your account [https://theauthapi.com](https://theauthapi.com)
282
+ - View our [Knowledge Base](https://thatapicompany.notion.site/The-Auth-API-Knowledge-Base-21660cee84e640729714fad43d9ce546) help centre
283
+ - Articles on best Auth practice - [https://theauthapi.com/articles](https://theauthapi.com/articles)
284
+ - Meet the team behind The Auth API - [That API Company](https://thatapicompany.com/)
@@ -0,0 +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;
@@ -0,0 +1,26 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ Object.defineProperty(exports, "__esModule", { value: true });
12
+ const HttpMethod_1 = require("../../services/ApiRequest/HttpMethod");
13
+ const util_1 = require("../../util");
14
+ class Accounts {
15
+ constructor(apiService) {
16
+ this.api = apiService;
17
+ this.endpoint = "/accounts";
18
+ }
19
+ getAccount(accountId) {
20
+ return __awaiter(this, void 0, void 0, function* () {
21
+ (0, util_1.validateString)("accountId", accountId);
22
+ return yield this.api.request(HttpMethod_1.HttpMethod.GET, `${this.endpoint}/${accountId}`);
23
+ });
24
+ }
25
+ }
26
+ exports.default = Accounts;
@@ -0,0 +1,4 @@
1
+ import { Account } from "../../types";
2
+ export interface AccountsInterface {
3
+ getAccount(accountId: string): Promise<Account>;
4
+ }
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,16 @@
1
+ import ApiRequest from "../../services/ApiRequest/ApiRequest";
2
+ import { ApiKey, ApiKeyInput, UpdateApiKeyInput } from "../../types";
3
+ import { ApiKeysInterface } from "./ApiKeysInterface";
4
+ declare class ApiKeys implements ApiKeysInterface {
5
+ api: ApiRequest;
6
+ constructor(apiService: ApiRequest);
7
+ isValidKey(apikey: string): Promise<boolean>;
8
+ getKeys(projectId: string): Promise<ApiKey[]>;
9
+ getKey(apikey: string): Promise<ApiKey>;
10
+ createKey(apiKey: ApiKeyInput): Promise<ApiKey>;
11
+ updateKey(apiKey: string, updateTo: UpdateApiKeyInput): Promise<ApiKey>;
12
+ deleteKey(apiKey: string): Promise<boolean>;
13
+ private validateCreateKeyInput;
14
+ private validateUpdateKeyInput;
15
+ }
16
+ export default ApiKeys;
@@ -0,0 +1,91 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ var __importDefault = (this && this.__importDefault) || function (mod) {
12
+ return (mod && mod.__esModule) ? mod : { "default": mod };
13
+ };
14
+ Object.defineProperty(exports, "__esModule", { value: true });
15
+ const HttpMethod_1 = require("../../services/ApiRequest/HttpMethod");
16
+ const lodash_omit_1 = __importDefault(require("lodash.omit"));
17
+ const util_1 = require("../../util");
18
+ const ApiResponseError_1 = __importDefault(require("../../services/ApiRequest/ApiResponseError"));
19
+ class ApiKeys {
20
+ constructor(apiService) {
21
+ this.api = apiService;
22
+ }
23
+ isValidKey(apikey) {
24
+ return __awaiter(this, void 0, void 0, function* () {
25
+ (0, util_1.validateString)("apikey", apikey);
26
+ try {
27
+ const key = yield this.api.request(HttpMethod_1.HttpMethod.GET, `/api-keys/${apikey}`);
28
+ return key.key !== undefined;
29
+ }
30
+ catch (error) {
31
+ if (error instanceof ApiResponseError_1.default && error.statusCode === 404) {
32
+ return false;
33
+ }
34
+ throw error;
35
+ }
36
+ });
37
+ }
38
+ getKeys(projectId) {
39
+ return __awaiter(this, void 0, void 0, function* () {
40
+ (0, util_1.validateString)("projectId", projectId);
41
+ return yield this.api.request(HttpMethod_1.HttpMethod.GET, `/api-keys/?projectId=${projectId}`);
42
+ });
43
+ }
44
+ getKey(apikey) {
45
+ return __awaiter(this, void 0, void 0, function* () {
46
+ (0, util_1.validateString)("apikey", apikey);
47
+ return yield this.api.request(HttpMethod_1.HttpMethod.GET, `/api-keys/${apikey}`);
48
+ });
49
+ }
50
+ createKey(apiKey) {
51
+ return __awaiter(this, void 0, void 0, function* () {
52
+ this.validateCreateKeyInput(apiKey);
53
+ return yield this.api.request(HttpMethod_1.HttpMethod.POST, "/api-keys", apiKey);
54
+ });
55
+ }
56
+ updateKey(apiKey, updateTo) {
57
+ return __awaiter(this, void 0, void 0, function* () {
58
+ this.validateUpdateKeyInput(apiKey, updateTo);
59
+ return yield this.api.request(HttpMethod_1.HttpMethod.PATCH, `/api-keys/${apiKey}`, updateTo);
60
+ });
61
+ }
62
+ deleteKey(apiKey) {
63
+ return __awaiter(this, void 0, void 0, function* () {
64
+ (0, util_1.validateString)("apiKey", apiKey);
65
+ return yield this.api.request(HttpMethod_1.HttpMethod.DELETE, `/api-keys/${apiKey}`);
66
+ });
67
+ }
68
+ validateCreateKeyInput(apiKey) {
69
+ if (!apiKey) {
70
+ throw new TypeError("apiKey must be an object");
71
+ }
72
+ if (!apiKey.name || !apiKey.projectId) {
73
+ throw TypeError("apiKey object must contain the properties [name, projectId]");
74
+ }
75
+ // validate string properties only
76
+ for (const [key, value] of Object.entries((0, lodash_omit_1.default)(apiKey, ["customMetaData", "rateLimitConfigs"]))) {
77
+ (0, util_1.validateString)(key, value);
78
+ }
79
+ }
80
+ validateUpdateKeyInput(apiKey, updatedKey) {
81
+ if (!updatedKey) {
82
+ throw new TypeError("updatedKey must be an object");
83
+ }
84
+ if (!updatedKey.name) {
85
+ throw TypeError("updatedKey object must contain the property [name]");
86
+ }
87
+ (0, util_1.validateString)("apiKey", apiKey);
88
+ (0, util_1.validateString)("name", updatedKey.name);
89
+ }
90
+ }
91
+ exports.default = ApiKeys;
@@ -0,0 +1,9 @@
1
+ import { ApiKey, ApiKeyInput, UpdateApiKeyInput } from "../../types";
2
+ export interface ApiKeysInterface {
3
+ isValidKey(apiKey: string): Promise<boolean>;
4
+ getKey(apiKey: string): Promise<ApiKey>;
5
+ getKeys(projectId: string): Promise<ApiKey[]>;
6
+ createKey(apiKey: ApiKeyInput): Promise<ApiKey>;
7
+ updateKey(apiKey: string, updateTo: UpdateApiKeyInput): Promise<ApiKey>;
8
+ deleteKey(apiKey: string): Promise<boolean>;
9
+ }
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,16 @@
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
+ private validateCreateProjectInput;
14
+ private validateUpdateProjectInput;
15
+ }
16
+ export default Projects;
@@ -0,0 +1,68 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ Object.defineProperty(exports, "__esModule", { value: true });
12
+ const types_1 = require("../../types");
13
+ const HttpMethod_1 = require("../../services/ApiRequest/HttpMethod");
14
+ const util_1 = require("../../util");
15
+ class Projects {
16
+ constructor(apiService) {
17
+ this.api = apiService;
18
+ this.endpoint = "/projects";
19
+ }
20
+ getProjects(accountId) {
21
+ return __awaiter(this, void 0, void 0, function* () {
22
+ (0, util_1.validateString)("accountId", accountId);
23
+ return yield this.api.request(HttpMethod_1.HttpMethod.GET, `${this.endpoint}?accountId=${accountId}`);
24
+ });
25
+ }
26
+ getProject(projectId) {
27
+ return __awaiter(this, void 0, void 0, function* () {
28
+ (0, util_1.validateString)("projectId", projectId);
29
+ return yield this.api.request(HttpMethod_1.HttpMethod.GET, `${this.endpoint}/${projectId}`);
30
+ });
31
+ }
32
+ deleteProject(projectId) {
33
+ return __awaiter(this, void 0, void 0, function* () {
34
+ (0, util_1.validateString)("projectId", projectId);
35
+ return yield this.api.request(HttpMethod_1.HttpMethod.DELETE, `${this.endpoint}/${projectId}`);
36
+ });
37
+ }
38
+ createProject(project) {
39
+ return __awaiter(this, void 0, void 0, function* () {
40
+ this.validateCreateProjectInput(project);
41
+ return yield this.api.request(HttpMethod_1.HttpMethod.POST, this.endpoint, project);
42
+ });
43
+ }
44
+ updateProject(projectId, project) {
45
+ return __awaiter(this, void 0, void 0, function* () {
46
+ this.validateUpdateProjectInput(projectId, project);
47
+ return this.api.request(HttpMethod_1.HttpMethod.PATCH, `${this.endpoint}/${projectId}`, project);
48
+ });
49
+ }
50
+ validateCreateProjectInput(project) {
51
+ if (!project) {
52
+ throw new TypeError("project must be an object");
53
+ }
54
+ (0, util_1.validateString)("name", project.name);
55
+ (0, util_1.validateString)("accountId", project.accountId);
56
+ if (!Object.values(types_1.Environment).includes(project.env)) {
57
+ throw TypeError(`expected env to be one of [${Object.values(types_1.Environment).map((v) => `"${v}"`)}], got: ${project.env}`);
58
+ }
59
+ }
60
+ validateUpdateProjectInput(projectId, project) {
61
+ if (!project) {
62
+ throw new TypeError("project must be an object");
63
+ }
64
+ (0, util_1.validateString)("projectId", projectId);
65
+ (0, util_1.validateString)("name", project.name);
66
+ }
67
+ }
68
+ exports.default = Projects;
@@ -0,0 +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
+ }
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,36 @@
1
+ import ApiRequest from "./services/ApiRequest/ApiRequest";
2
+ import ApiKeys from "./endpoints/ApiKeys/ApiKeys";
3
+ import Projects from "./endpoints/Projects/Projects";
4
+ import Accounts from "./endpoints/Accounts/Accounts";
5
+ declare type Options = {
6
+ host?: string;
7
+ timeout?: string | number;
8
+ cacheTTL?: number;
9
+ enable?: boolean;
10
+ retryCount?: number;
11
+ };
12
+ declare class TheAuthAPI {
13
+ queue: [];
14
+ accessKey: string;
15
+ host: string;
16
+ timeout: number | string | undefined;
17
+ cacheTTL: number;
18
+ api: ApiRequest;
19
+ apiKeys: ApiKeys;
20
+ projects: Projects;
21
+ accounts: Accounts;
22
+ /**
23
+ * Initialize a new `Analytics` with your Segment project's `writeKey` and an
24
+ * optional dictionary of `options`.
25
+ *
26
+ * @param {String} accessKey
27
+ * @param {Object} [options] (optional)
28
+ * @property {Number} flushAt (default: 20)
29
+ * @property {Number} flushInterval (default: 10000)
30
+ * @property {String} host (default: 'https://api.segment.io')
31
+ * @property {Boolean} enable (default: true)
32
+ */
33
+ constructor(accessKey: string, options?: Options);
34
+ authenticateAPIKey(key: string, callback?: (err: any, data: any) => any): Promise<unknown>;
35
+ }
36
+ export default TheAuthAPI;