theauthapi 1.0.6 → 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.
- package/README.md +283 -4
- package/dist/endpoints/ApiKeys/ApiKeys.d.ts +4 -1
- package/dist/endpoints/ApiKeys/ApiKeys.js +42 -10
- package/dist/endpoints/ApiKeys/ApiKeysInterface.d.ts +2 -1
- package/dist/endpoints/Projects/Projects.d.ts +5 -3
- package/dist/endpoints/Projects/Projects.js +24 -10
- package/dist/endpoints/Projects/ProjectsInterface.d.ts +2 -2
- package/dist/services/ApiRequest/ApiRequest.js +3 -2
- package/dist/services/ApiRequest/ApiRequestError.d.ts +7 -1
- package/dist/services/ApiRequest/ApiRequestError.js +9 -2
- package/dist/services/ApiRequest/ApiResponseError.d.ts +12 -0
- package/dist/services/ApiRequest/ApiResponseError.js +17 -0
- package/dist/types/index.d.ts +9 -0
- package/dist/types/index.js +6 -1
- package/dist/util/index.js +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,5 +1,284 @@
|
|
|
1
|
-
#
|
|
2
|
-
## Scalable API Key Management and Auth Control
|
|
3
|
-
## Secure your API with best in class API Key management, user access, all with great analytics.
|
|
1
|
+
# Client library for [TheAuthAPI](https://theauthapi.com/)
|
|
4
2
|
|
|
5
|
-
|
|
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/)
|
|
@@ -4,10 +4,13 @@ import { ApiKeysInterface } from "./ApiKeysInterface";
|
|
|
4
4
|
declare class ApiKeys implements ApiKeysInterface {
|
|
5
5
|
api: ApiRequest;
|
|
6
6
|
constructor(apiService: ApiRequest);
|
|
7
|
-
|
|
7
|
+
isValidKey(apikey: string): Promise<boolean>;
|
|
8
8
|
getKeys(projectId: string): Promise<ApiKey[]>;
|
|
9
|
+
getKey(apikey: string): Promise<ApiKey>;
|
|
9
10
|
createKey(apiKey: ApiKeyInput): Promise<ApiKey>;
|
|
10
11
|
updateKey(apiKey: string, updateTo: UpdateApiKeyInput): Promise<ApiKey>;
|
|
11
12
|
deleteKey(apiKey: string): Promise<boolean>;
|
|
13
|
+
private validateCreateKeyInput;
|
|
14
|
+
private validateUpdateKeyInput;
|
|
12
15
|
}
|
|
13
16
|
export default ApiKeys;
|
|
@@ -15,14 +15,24 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
15
15
|
const HttpMethod_1 = require("../../services/ApiRequest/HttpMethod");
|
|
16
16
|
const lodash_omit_1 = __importDefault(require("lodash.omit"));
|
|
17
17
|
const util_1 = require("../../util");
|
|
18
|
+
const ApiResponseError_1 = __importDefault(require("../../services/ApiRequest/ApiResponseError"));
|
|
18
19
|
class ApiKeys {
|
|
19
20
|
constructor(apiService) {
|
|
20
21
|
this.api = apiService;
|
|
21
22
|
}
|
|
22
|
-
|
|
23
|
+
isValidKey(apikey) {
|
|
23
24
|
return __awaiter(this, void 0, void 0, function* () {
|
|
24
25
|
(0, util_1.validateString)("apikey", apikey);
|
|
25
|
-
|
|
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
|
+
}
|
|
26
36
|
});
|
|
27
37
|
}
|
|
28
38
|
getKeys(projectId) {
|
|
@@ -31,21 +41,21 @@ class ApiKeys {
|
|
|
31
41
|
return yield this.api.request(HttpMethod_1.HttpMethod.GET, `/api-keys/?projectId=${projectId}`);
|
|
32
42
|
});
|
|
33
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
|
+
}
|
|
34
50
|
createKey(apiKey) {
|
|
35
51
|
return __awaiter(this, void 0, void 0, function* () {
|
|
36
|
-
|
|
37
|
-
for (const [key, value] of Object.entries((0, lodash_omit_1.default)(apiKey, ["customMetaData", "rateLimitConfigs"]))) {
|
|
38
|
-
(0, util_1.validateString)(key, value);
|
|
39
|
-
}
|
|
52
|
+
this.validateCreateKeyInput(apiKey);
|
|
40
53
|
return yield this.api.request(HttpMethod_1.HttpMethod.POST, "/api-keys", apiKey);
|
|
41
54
|
});
|
|
42
55
|
}
|
|
43
56
|
updateKey(apiKey, updateTo) {
|
|
44
57
|
return __awaiter(this, void 0, void 0, function* () {
|
|
45
|
-
|
|
46
|
-
for (const [key, value] of Object.entries((0, lodash_omit_1.default)(updateTo, "customMetaData"))) {
|
|
47
|
-
(0, util_1.validateString)(key, value);
|
|
48
|
-
}
|
|
58
|
+
this.validateUpdateKeyInput(apiKey, updateTo);
|
|
49
59
|
return yield this.api.request(HttpMethod_1.HttpMethod.PATCH, `/api-keys/${apiKey}`, updateTo);
|
|
50
60
|
});
|
|
51
61
|
}
|
|
@@ -55,5 +65,27 @@ class ApiKeys {
|
|
|
55
65
|
return yield this.api.request(HttpMethod_1.HttpMethod.DELETE, `/api-keys/${apiKey}`);
|
|
56
66
|
});
|
|
57
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
|
+
}
|
|
58
90
|
}
|
|
59
91
|
exports.default = ApiKeys;
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { ApiKey, ApiKeyInput, UpdateApiKeyInput } from "../../types";
|
|
2
2
|
export interface ApiKeysInterface {
|
|
3
|
-
|
|
3
|
+
isValidKey(apiKey: string): Promise<boolean>;
|
|
4
|
+
getKey(apiKey: string): Promise<ApiKey>;
|
|
4
5
|
getKeys(projectId: string): Promise<ApiKey[]>;
|
|
5
6
|
createKey(apiKey: ApiKeyInput): Promise<ApiKey>;
|
|
6
7
|
updateKey(apiKey: string, updateTo: UpdateApiKeyInput): Promise<ApiKey>;
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import ApiRequest from "../../services/ApiRequest/ApiRequest";
|
|
2
|
-
import { Project, UpdateProjectInput } from "../../types";
|
|
2
|
+
import { CreateProjectInput, Project, UpdateProjectInput } from "../../types";
|
|
3
3
|
import { ProjectsInterface } from "./ProjectsInterface";
|
|
4
4
|
declare class Projects implements ProjectsInterface {
|
|
5
5
|
api: ApiRequest;
|
|
@@ -8,7 +8,9 @@ declare class Projects implements ProjectsInterface {
|
|
|
8
8
|
getProjects(accountId: string): Promise<Project[]>;
|
|
9
9
|
getProject(projectId: string): Promise<Project>;
|
|
10
10
|
deleteProject(projectId: string): Promise<boolean>;
|
|
11
|
-
createProject(
|
|
12
|
-
updateProject(projectId: string,
|
|
11
|
+
createProject(project: CreateProjectInput): Promise<Project>;
|
|
12
|
+
updateProject(projectId: string, project: UpdateProjectInput): Promise<Project>;
|
|
13
|
+
private validateCreateProjectInput;
|
|
14
|
+
private validateUpdateProjectInput;
|
|
13
15
|
}
|
|
14
16
|
export default Projects;
|
|
@@ -9,6 +9,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
9
9
|
});
|
|
10
10
|
};
|
|
11
11
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
+
const types_1 = require("../../types");
|
|
12
13
|
const HttpMethod_1 = require("../../services/ApiRequest/HttpMethod");
|
|
13
14
|
const util_1 = require("../../util");
|
|
14
15
|
class Projects {
|
|
@@ -34,21 +35,34 @@ class Projects {
|
|
|
34
35
|
return yield this.api.request(HttpMethod_1.HttpMethod.DELETE, `${this.endpoint}/${projectId}`);
|
|
35
36
|
});
|
|
36
37
|
}
|
|
37
|
-
createProject(
|
|
38
|
+
createProject(project) {
|
|
38
39
|
return __awaiter(this, void 0, void 0, function* () {
|
|
39
|
-
|
|
40
|
-
(
|
|
41
|
-
return yield this.api.request(HttpMethod_1.HttpMethod.POST, this.endpoint, {
|
|
42
|
-
name,
|
|
43
|
-
accountId,
|
|
44
|
-
});
|
|
40
|
+
this.validateCreateProjectInput(project);
|
|
41
|
+
return yield this.api.request(HttpMethod_1.HttpMethod.POST, this.endpoint, project);
|
|
45
42
|
});
|
|
46
43
|
}
|
|
47
|
-
updateProject(projectId,
|
|
44
|
+
updateProject(projectId, project) {
|
|
48
45
|
return __awaiter(this, void 0, void 0, function* () {
|
|
49
|
-
|
|
50
|
-
return this.api.request(HttpMethod_1.HttpMethod.PATCH, `${this.endpoint}/${projectId}`,
|
|
46
|
+
this.validateUpdateProjectInput(projectId, project);
|
|
47
|
+
return this.api.request(HttpMethod_1.HttpMethod.PATCH, `${this.endpoint}/${projectId}`, project);
|
|
51
48
|
});
|
|
52
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
|
+
}
|
|
53
67
|
}
|
|
54
68
|
exports.default = Projects;
|
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
import { Project, UpdateProjectInput } from "../../types";
|
|
1
|
+
import { CreateProjectInput, Project, UpdateProjectInput } from "../../types";
|
|
2
2
|
export interface ProjectsInterface {
|
|
3
3
|
getProjects(accountId: string): Promise<Project[]>;
|
|
4
4
|
getProject(projectId: string): Promise<Project>;
|
|
5
5
|
deleteProject(projectId: string): Promise<boolean>;
|
|
6
|
-
createProject(
|
|
6
|
+
createProject(project: CreateProjectInput): Promise<Project>;
|
|
7
7
|
updateProject(name: string, updateTo: UpdateProjectInput): Promise<Project>;
|
|
8
8
|
}
|
|
@@ -17,6 +17,7 @@ const libraryMeta_1 = require("../../libraryMeta");
|
|
|
17
17
|
const ApiRequestError_1 = __importDefault(require("./ApiRequestError"));
|
|
18
18
|
const axios_retry_1 = __importDefault(require("axios-retry"));
|
|
19
19
|
const ms_1 = __importDefault(require("ms"));
|
|
20
|
+
const ApiResponseError_1 = __importDefault(require("./ApiResponseError"));
|
|
20
21
|
class ApiRequest {
|
|
21
22
|
constructor(config) {
|
|
22
23
|
const { host, accessKey, headers, retryCount, timeout } = config;
|
|
@@ -76,10 +77,10 @@ class ApiRequest {
|
|
|
76
77
|
catch (error) {
|
|
77
78
|
if (axios_1.default.isAxiosError(error)) {
|
|
78
79
|
if (error.response) {
|
|
79
|
-
throw new
|
|
80
|
+
throw new ApiResponseError_1.default(error.response.status, (_a = error.response.data.message) !== null && _a !== void 0 ? _a : error.response.statusText);
|
|
80
81
|
}
|
|
81
82
|
else if (error.request) {
|
|
82
|
-
throw new
|
|
83
|
+
throw new ApiRequestError_1.default(error.message);
|
|
83
84
|
}
|
|
84
85
|
}
|
|
85
86
|
throw error;
|
|
@@ -1,4 +1,10 @@
|
|
|
1
|
+
/**
|
|
2
|
+
*
|
|
3
|
+
* Throws when no response was received from the server
|
|
4
|
+
* @param message - error message
|
|
5
|
+
*
|
|
6
|
+
* */
|
|
1
7
|
declare class ApiRequestError extends Error {
|
|
2
|
-
constructor(
|
|
8
|
+
constructor(message: string);
|
|
3
9
|
}
|
|
4
10
|
export default ApiRequestError;
|
|
@@ -1,8 +1,15 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
/**
|
|
4
|
+
*
|
|
5
|
+
* Throws when no response was received from the server
|
|
6
|
+
* @param message - error message
|
|
7
|
+
*
|
|
8
|
+
* */
|
|
3
9
|
class ApiRequestError extends Error {
|
|
4
|
-
constructor(
|
|
5
|
-
super(
|
|
10
|
+
constructor(message) {
|
|
11
|
+
super(message);
|
|
12
|
+
this.name = 'ApiRequestError';
|
|
6
13
|
}
|
|
7
14
|
}
|
|
8
15
|
exports.default = ApiRequestError;
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
/**
|
|
2
|
+
*
|
|
3
|
+
* Throws when the server responds with a status code that falls out of the range 2xx
|
|
4
|
+
* @param statusCode - HTTP status code the server responded with
|
|
5
|
+
* @param message - error message
|
|
6
|
+
*
|
|
7
|
+
* */
|
|
8
|
+
declare class ApiResponseError extends Error {
|
|
9
|
+
statusCode: number;
|
|
10
|
+
constructor(statusCode: number, message: string);
|
|
11
|
+
}
|
|
12
|
+
export default ApiResponseError;
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
/**
|
|
4
|
+
*
|
|
5
|
+
* Throws when the server responds with a status code that falls out of the range 2xx
|
|
6
|
+
* @param statusCode - HTTP status code the server responded with
|
|
7
|
+
* @param message - error message
|
|
8
|
+
*
|
|
9
|
+
* */
|
|
10
|
+
class ApiResponseError extends Error {
|
|
11
|
+
constructor(statusCode, message) {
|
|
12
|
+
super(`(${statusCode}): ${message}`);
|
|
13
|
+
this.statusCode = statusCode;
|
|
14
|
+
this.name = "ApiResponseError";
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
exports.default = ApiResponseError;
|
package/dist/types/index.d.ts
CHANGED
|
@@ -47,6 +47,15 @@ export declare type Project = AuthBaseEntity & {
|
|
|
47
47
|
accountId: string;
|
|
48
48
|
env: string;
|
|
49
49
|
};
|
|
50
|
+
export declare enum Environment {
|
|
51
|
+
LIVE = "live",
|
|
52
|
+
TEST = "test"
|
|
53
|
+
}
|
|
54
|
+
export declare type CreateProjectInput = {
|
|
55
|
+
name: string;
|
|
56
|
+
accountId: string;
|
|
57
|
+
env: Environment;
|
|
58
|
+
};
|
|
50
59
|
export declare type UpdateProjectInput = {
|
|
51
60
|
name: string;
|
|
52
61
|
};
|
package/dist/types/index.js
CHANGED
|
@@ -1,8 +1,13 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.AuthedEntityType = void 0;
|
|
3
|
+
exports.Environment = exports.AuthedEntityType = void 0;
|
|
4
4
|
var AuthedEntityType;
|
|
5
5
|
(function (AuthedEntityType) {
|
|
6
6
|
AuthedEntityType["USER"] = "USER";
|
|
7
7
|
AuthedEntityType["ACCESS_KEY"] = "ACCESS_KEY";
|
|
8
8
|
})(AuthedEntityType = exports.AuthedEntityType || (exports.AuthedEntityType = {}));
|
|
9
|
+
var Environment;
|
|
10
|
+
(function (Environment) {
|
|
11
|
+
Environment["LIVE"] = "live";
|
|
12
|
+
Environment["TEST"] = "test";
|
|
13
|
+
})(Environment = exports.Environment || (exports.Environment = {}));
|
package/dist/util/index.js
CHANGED
|
@@ -7,7 +7,7 @@ exports.validateString = void 0;
|
|
|
7
7
|
const lodash_isstring_1 = __importDefault(require("lodash.isstring"));
|
|
8
8
|
function validateString(variableName, value) {
|
|
9
9
|
if (!value || !(0, lodash_isstring_1.default)(value)) {
|
|
10
|
-
throw new
|
|
10
|
+
throw new TypeError(`${variableName} must be a string, got: ${value}`);
|
|
11
11
|
}
|
|
12
12
|
}
|
|
13
13
|
exports.validateString = validateString;
|