theauthapi 1.0.7 → 1.0.8

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 CHANGED
@@ -7,6 +7,7 @@
7
7
  - [Configuration](#configuration)
8
8
  - [Usage](#usage)
9
9
  - [Example: Validating an api-key](#example-validating-an-api-key)
10
+ - [Example: Listing API-keys](#example-listing-api-keys)
10
11
  - [Example: Listing the projects of an account](#example-listing-the-projects-of-an-account)
11
12
  - [Example: Listing projects and associated API Keys](#example-listing-projects-and-associated-api-keys)
12
13
  - [Example: Creating an API Key](#example-creating-an-api-key)
@@ -99,7 +100,7 @@ All methods return a promise containing the returned JSON as a javascript object
99
100
  | HTTP Method | method name | example |
100
101
  | ----------- | ----------- | ------------------------------------------------------------------------- |
101
102
  | POST | create\* | `client.apiKeys.createKey({ name: "KEY_NAME", projectId: "PROJECT_ID" })` |
102
- | GET | get\* | `client.apiKeys.getKeys("PROJECT_ID")` |
103
+ | GET | get\* | `client.apiKeys.getKeys()` |
103
104
  | DELETE | delete\* | `client.apiKeys.deleteKey("MY_KEY")` |
104
105
  | PATCH | update\* | `client.apiKeys.updateKey("MY_KEY", { name: "UPDATED_KEY_NAME" })` |
105
106
 
@@ -138,6 +139,49 @@ try {
138
139
  }
139
140
  ```
140
141
 
142
+ #### Example: Listing API-keys
143
+
144
+ ```javascript
145
+ theAuthAPI.apiKeys
146
+ .getKeys()
147
+ .then((keys) => console.log(keys))
148
+ .catch((error) => console.log(error));
149
+ ```
150
+
151
+ **Using async/await**
152
+
153
+ ```javascript
154
+ try {
155
+ const keys = await theAuthAPI.apiKeys.getKeys();
156
+ } catch (error) {
157
+ console.log(error);
158
+ }
159
+ ```
160
+
161
+ **Filtering API Keys**: You can filter the listed API keys by passing an object of type filter as an argument to `getKeys`
162
+
163
+ ```typescript
164
+ type ApiKeyFilter = {
165
+ projectId?: string;
166
+ customAccountId?: string;
167
+ customUserId?: string;
168
+ isActive?: boolean;
169
+ };
170
+ ```
171
+
172
+ **Example**: filtering api-keys with a specific `projectId` where the keys are not active
173
+
174
+ ```javascript
175
+ try {
176
+ const keys = await theAuthAPI.apiKeys.getKeys({
177
+ projectId: "PROJECT_ID",
178
+ isActive: false,
179
+ });
180
+ } catch (error) {
181
+ console.log(error);
182
+ }
183
+ ```
184
+
141
185
  #### Example: Listing the projects of an account
142
186
 
143
187
  ```javascript
@@ -1,16 +1,19 @@
1
1
  import ApiRequest from "../../services/ApiRequest/ApiRequest";
2
- import { ApiKey, ApiKeyInput, UpdateApiKeyInput } from "../../types";
2
+ import { ApiKey, ApiKeyFilter, ApiKeyInput, UpdateApiKeyInput } from "../../types";
3
3
  import { ApiKeysInterface } from "./ApiKeysInterface";
4
4
  declare class ApiKeys implements ApiKeysInterface {
5
5
  api: ApiRequest;
6
+ private readonly endpoint;
6
7
  constructor(apiService: ApiRequest);
7
8
  isValidKey(apikey: string): Promise<boolean>;
8
- getKeys(projectId: string): Promise<ApiKey[]>;
9
+ getKeys(filter?: ApiKeyFilter): Promise<ApiKey[]>;
9
10
  getKey(apikey: string): Promise<ApiKey>;
10
11
  createKey(apiKey: ApiKeyInput): Promise<ApiKey>;
11
- updateKey(apiKey: string, updateTo: UpdateApiKeyInput): Promise<ApiKey>;
12
+ updateKey(apiKey: string, updatedKey: UpdateApiKeyInput): Promise<ApiKey>;
12
13
  deleteKey(apiKey: string): Promise<boolean>;
13
14
  private validateCreateKeyInput;
14
15
  private validateUpdateKeyInput;
16
+ private validateFiltersInput;
17
+ private getKeysFilterEndpoint;
15
18
  }
16
19
  export default ApiKeys;
@@ -19,6 +19,7 @@ const ApiResponseError_1 = __importDefault(require("../../services/ApiRequest/Ap
19
19
  class ApiKeys {
20
20
  constructor(apiService) {
21
21
  this.api = apiService;
22
+ this.endpoint = "/api-keys/";
22
23
  }
23
24
  isValidKey(apikey) {
24
25
  return __awaiter(this, void 0, void 0, function* () {
@@ -35,10 +36,10 @@ class ApiKeys {
35
36
  }
36
37
  });
37
38
  }
38
- getKeys(projectId) {
39
+ getKeys(filter) {
39
40
  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}`);
41
+ const endpoint = this.getKeysFilterEndpoint(filter);
42
+ return yield this.api.request(HttpMethod_1.HttpMethod.GET, endpoint);
42
43
  });
43
44
  }
44
45
  getKey(apikey) {
@@ -53,10 +54,10 @@ class ApiKeys {
53
54
  return yield this.api.request(HttpMethod_1.HttpMethod.POST, "/api-keys", apiKey);
54
55
  });
55
56
  }
56
- updateKey(apiKey, updateTo) {
57
+ updateKey(apiKey, updatedKey) {
57
58
  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);
59
+ this.validateUpdateKeyInput(apiKey, updatedKey);
60
+ return yield this.api.request(HttpMethod_1.HttpMethod.PATCH, `/api-keys/${apiKey}`, updatedKey);
60
61
  });
61
62
  }
62
63
  deleteKey(apiKey) {
@@ -69,8 +70,8 @@ class ApiKeys {
69
70
  if (!apiKey) {
70
71
  throw new TypeError("apiKey must be an object");
71
72
  }
72
- if (!apiKey.name || !apiKey.projectId) {
73
- throw TypeError("apiKey object must contain the properties [name, projectId]");
73
+ if (!apiKey.name) {
74
+ throw TypeError("apiKey object must contain the property name");
74
75
  }
75
76
  // validate string properties only
76
77
  for (const [key, value] of Object.entries((0, lodash_omit_1.default)(apiKey, ["customMetaData", "rateLimitConfigs"]))) {
@@ -87,5 +88,23 @@ class ApiKeys {
87
88
  (0, util_1.validateString)("apiKey", apiKey);
88
89
  (0, util_1.validateString)("name", updatedKey.name);
89
90
  }
91
+ validateFiltersInput(filter) {
92
+ if (filter) {
93
+ if (filter.isActive && typeof filter.isActive !== "boolean") {
94
+ throw TypeError("isActive must be a boolean");
95
+ }
96
+ Object.entries((0, lodash_omit_1.default)(filter, "isActive")).forEach(([key, value]) => {
97
+ (0, util_1.validateString)(key, value);
98
+ });
99
+ }
100
+ }
101
+ getKeysFilterEndpoint(filter) {
102
+ this.validateFiltersInput(filter);
103
+ let filters = [];
104
+ if (filter) {
105
+ filters = Object.entries(filter).map(([key, value]) => `${key}=${value}`);
106
+ }
107
+ return `${this.endpoint}${filter ? "?" : ""}${filters.join("&")}`;
108
+ }
90
109
  }
91
110
  exports.default = ApiKeys;
@@ -1,8 +1,8 @@
1
- import { ApiKey, ApiKeyInput, UpdateApiKeyInput } from "../../types";
1
+ import { ApiKey, ApiKeyFilter, ApiKeyInput, UpdateApiKeyInput } from "../../types";
2
2
  export interface ApiKeysInterface {
3
3
  isValidKey(apiKey: string): Promise<boolean>;
4
4
  getKey(apiKey: string): Promise<ApiKey>;
5
- getKeys(projectId: string): Promise<ApiKey[]>;
5
+ getKeys(filter?: ApiKeyFilter): Promise<ApiKey[]>;
6
6
  createKey(apiKey: ApiKeyInput): Promise<ApiKey>;
7
7
  updateKey(apiKey: string, updateTo: UpdateApiKeyInput): Promise<ApiKey>;
8
8
  deleteKey(apiKey: string): Promise<boolean>;
@@ -3,9 +3,11 @@ export declare type ApiKey = {
3
3
  name: string;
4
4
  customMetaData: string;
5
5
  customAccountId: string;
6
+ customUserId: string;
6
7
  env: string;
7
8
  createdAt: Date;
8
9
  updatedAt: Date;
10
+ isActive: boolean;
9
11
  };
10
12
  export declare type RateLimitConfiguration = {
11
13
  rateLimitedEntity?: string;
@@ -15,13 +17,19 @@ export declare type RateLimitConfiguration = {
15
17
  };
16
18
  export declare type ApiKeyInput = {
17
19
  name: string;
18
- projectId: string;
20
+ projectId?: string;
19
21
  key?: string;
20
22
  customMetaData?: object;
21
23
  customAccountId?: string;
22
24
  rateLimitConfigs?: RateLimitConfiguration;
23
25
  customUserId?: string;
24
26
  };
27
+ export declare type ApiKeyFilter = {
28
+ projectId?: string;
29
+ customAccountId?: string;
30
+ customUserId?: string;
31
+ isActive?: boolean;
32
+ };
25
33
  export declare type UpdateApiKeyInput = {
26
34
  name: string;
27
35
  customMetaData?: object;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "theauthapi",
3
- "version": "1.0.7",
3
+ "version": "1.0.8",
4
4
  "description": "Client library for TheAuthAPI.com",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",