theauthapi 1.0.7 → 1.0.10

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
@@ -6,10 +6,11 @@
6
6
  - [Installation](#installation)
7
7
  - [Configuration](#configuration)
8
8
  - [Usage](#usage)
9
- - [Example: Validating an api-key](#example-validating-an-api-key)
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
- - [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)
12
+ - [Example: Listing projects and associated API-Keys](#example-listing-projects-and-associated-api-keys)
13
+ - [Example: Creating an API-Key](#example-creating-an-api-key)
13
14
  - [Handling Errors](#handling-errors)
14
15
  - [Typescript](#typescript)
15
16
  - [📙 Further Reading](#-further-reading)
@@ -30,7 +31,7 @@ yarn add theauthapi
30
31
 
31
32
  ## Configuration
32
33
 
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
+ 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.
34
35
 
35
36
  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
 
@@ -99,11 +100,11 @@ 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
 
106
- #### Example: Validating an api-key
107
+ #### Example: Validating an API-Key
107
108
 
108
109
  You can easily validate an API key using `apiKeys.isValidKey` which returns `true` if the key is valid, `false` otherwise.
109
110
  `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
@@ -138,6 +139,55 @@ try {
138
139
  }
139
140
  ```
140
141
 
142
+ **Note:** If you want to consume the API key and get the API key entity in return, you can use `apiKeys.authenticateKey` which returns an ApiKey.
143
+
144
+ #### Example: Listing API-keys
145
+
146
+ ```javascript
147
+ theAuthAPI.apiKeys
148
+ .getKeys()
149
+ .then((keys) => console.log(keys))
150
+ .catch((error) => console.log(error));
151
+ ```
152
+
153
+ **Using async/await**
154
+
155
+ ```javascript
156
+ try {
157
+ const keys = await theAuthAPI.apiKeys.getKeys();
158
+ } catch (error) {
159
+ console.log(error);
160
+ }
161
+ ```
162
+
163
+ **Filtering API Keys**: You can filter the listed API keys by passing an object of type filter as an argument to `getKeys`
164
+
165
+ ```typescript
166
+ type ApiKeyFilter = {
167
+ projectId?: string;
168
+ customAccountId?: string;
169
+ customUserId?: string;
170
+ isActive?: boolean;
171
+ };
172
+ ```
173
+
174
+ **Example**: filtering api-keys with a specific `projectId` where the keys are not active
175
+
176
+ ```javascript
177
+ try {
178
+ const keys = await theAuthAPI.apiKeys.getKeys({
179
+ projectId: "PROJECT_ID",
180
+ isActive: false,
181
+ });
182
+ } catch (error) {
183
+ console.log(error);
184
+ }
185
+ ```
186
+
187
+ **NOTE** that if your access key is at account level, you need to specify `projectId` when listing the API keys:
188
+ `getKeys({ projectId: "PROJECT_ID" })`, otherwise if your access key is created at project level, you don't have to specify `projectId`,
189
+ the access key's `projectId` will be used to get the API-keys (i.e. you'll see only the keys of the project your access key is created against)
190
+
141
191
  #### Example: Listing the projects of an account
142
192
 
143
193
  ```javascript
@@ -157,7 +207,7 @@ try {
157
207
  }
158
208
  ```
159
209
 
160
- #### Example: Listing projects and associated API Keys
210
+ #### Example: Listing projects and associated API-Keys
161
211
 
162
212
  ```javascript
163
213
  async function getProjectsWithKeys(accountId: string) {
@@ -174,7 +224,7 @@ async function getProjectsWithKeys(accountId: string) {
174
224
  }
175
225
  ```
176
226
 
177
- #### Example: Creating an API Key
227
+ #### Example: Creating an API-Key
178
228
 
179
229
  ```javascript
180
230
  theAuthAPI.apiKeys
@@ -280,5 +330,6 @@ async function getProjectsIds(accountId: string): Promise<string[]> {
280
330
 
281
331
  - Create your account [https://theauthapi.com](https://theauthapi.com)
282
332
  - View our [Knowledge Base](https://thatapicompany.notion.site/The-Auth-API-Knowledge-Base-21660cee84e640729714fad43d9ce546) help centre
333
+ - Read our [API docs](https://docs.theauthapi.com)
283
334
  - Articles on best Auth practice - [https://theauthapi.com/articles](https://theauthapi.com/articles)
284
335
  - Meet the team behind The Auth API - [That API Company](https://thatapicompany.com/)
@@ -1,16 +1,20 @@
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
+ authenticateKey(apikey: string): Promise<ApiKey>;
10
+ getKeys(filter?: ApiKeyFilter): Promise<ApiKey[]>;
9
11
  getKey(apikey: string): Promise<ApiKey>;
10
12
  createKey(apiKey: ApiKeyInput): Promise<ApiKey>;
11
- updateKey(apiKey: string, updateTo: UpdateApiKeyInput): Promise<ApiKey>;
13
+ updateKey(apiKey: string, updatedKey: UpdateApiKeyInput): Promise<ApiKey>;
12
14
  deleteKey(apiKey: string): Promise<boolean>;
13
15
  private validateCreateKeyInput;
14
16
  private validateUpdateKeyInput;
17
+ private validateFiltersInput;
18
+ private getKeysFilterEndpoint;
15
19
  }
16
20
  export default ApiKeys;
@@ -19,12 +19,13 @@ 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* () {
25
26
  (0, util_1.validateString)("apikey", apikey);
26
27
  try {
27
- const key = yield this.api.request(HttpMethod_1.HttpMethod.GET, `/api-keys/${apikey}`);
28
+ const key = yield this.api.request(HttpMethod_1.HttpMethod.GET, `/api-keys/auth/${apikey}`);
28
29
  return key.key !== undefined;
29
30
  }
30
31
  catch (error) {
@@ -35,10 +36,16 @@ class ApiKeys {
35
36
  }
36
37
  });
37
38
  }
38
- getKeys(projectId) {
39
+ authenticateKey(apikey) {
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
+ (0, util_1.validateString)("apikey", apikey);
42
+ return yield this.api.request(HttpMethod_1.HttpMethod.GET, `/api-keys/auth/${apikey}`);
43
+ });
44
+ }
45
+ getKeys(filter) {
46
+ return __awaiter(this, void 0, void 0, function* () {
47
+ const endpoint = this.getKeysFilterEndpoint(filter);
48
+ return yield this.api.request(HttpMethod_1.HttpMethod.GET, endpoint);
42
49
  });
43
50
  }
44
51
  getKey(apikey) {
@@ -53,10 +60,10 @@ class ApiKeys {
53
60
  return yield this.api.request(HttpMethod_1.HttpMethod.POST, "/api-keys", apiKey);
54
61
  });
55
62
  }
56
- updateKey(apiKey, updateTo) {
63
+ updateKey(apiKey, updatedKey) {
57
64
  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);
65
+ this.validateUpdateKeyInput(apiKey, updatedKey);
66
+ return yield this.api.request(HttpMethod_1.HttpMethod.PATCH, `/api-keys/${apiKey}`, updatedKey);
60
67
  });
61
68
  }
62
69
  deleteKey(apiKey) {
@@ -69,8 +76,8 @@ class ApiKeys {
69
76
  if (!apiKey) {
70
77
  throw new TypeError("apiKey must be an object");
71
78
  }
72
- if (!apiKey.name || !apiKey.projectId) {
73
- throw TypeError("apiKey object must contain the properties [name, projectId]");
79
+ if (!apiKey.name) {
80
+ throw TypeError("apiKey object must contain the property name");
74
81
  }
75
82
  // validate string properties only
76
83
  for (const [key, value] of Object.entries((0, lodash_omit_1.default)(apiKey, ["customMetaData", "rateLimitConfigs"]))) {
@@ -87,5 +94,23 @@ class ApiKeys {
87
94
  (0, util_1.validateString)("apiKey", apiKey);
88
95
  (0, util_1.validateString)("name", updatedKey.name);
89
96
  }
97
+ validateFiltersInput(filter) {
98
+ if (filter) {
99
+ if (filter.isActive && typeof filter.isActive !== "boolean") {
100
+ throw TypeError("isActive must be a boolean");
101
+ }
102
+ Object.entries((0, lodash_omit_1.default)(filter, "isActive")).forEach(([key, value]) => {
103
+ (0, util_1.validateString)(key, value);
104
+ });
105
+ }
106
+ }
107
+ getKeysFilterEndpoint(filter) {
108
+ this.validateFiltersInput(filter);
109
+ let filters = [];
110
+ if (filter) {
111
+ filters = Object.entries(filter).map(([key, value]) => `${key}=${value}`);
112
+ }
113
+ return `${this.endpoint}${filter ? "?" : ""}${filters.join("&")}`;
114
+ }
90
115
  }
91
116
  exports.default = ApiKeys;
@@ -1,8 +1,9 @@
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
+ authenticateKey(apiKey: string): Promise<ApiKey>;
6
+ getKeys(filter?: ApiKeyFilter): Promise<ApiKey[]>;
6
7
  createKey(apiKey: ApiKeyInput): Promise<ApiKey>;
7
8
  updateKey(apiKey: string, updateTo: UpdateApiKeyInput): Promise<ApiKey>;
8
9
  deleteKey(apiKey: string): Promise<boolean>;
@@ -1 +1 @@
1
- export declare const version = "1.0.6";
1
+ export declare const version = "1.0.10";
@@ -1,4 +1,4 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.version = void 0;
4
- exports.version = "1.0.6";
4
+ exports.version = "1.0.10";
@@ -1,26 +1,34 @@
1
1
  export declare type ApiKey = {
2
2
  key: string;
3
3
  name: string;
4
- customMetaData: string;
4
+ customMetaData: object;
5
5
  customAccountId: string;
6
- env: string;
6
+ customUserId: string;
7
+ env: Environment;
7
8
  createdAt: Date;
8
9
  updatedAt: Date;
10
+ isActive: boolean;
9
11
  };
10
12
  export declare type RateLimitConfiguration = {
11
13
  rateLimitedEntity?: string;
12
- ratelimitedEnitityId?: any;
14
+ ratelimitedEnitityId?: string;
13
15
  rateLimit: number;
14
16
  rateLimitTtl: number;
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;
24
+ customUserId?: string;
22
25
  rateLimitConfigs?: RateLimitConfiguration;
26
+ };
27
+ export declare type ApiKeyFilter = {
28
+ projectId?: string;
29
+ customAccountId?: string;
23
30
  customUserId?: string;
31
+ isActive?: boolean;
24
32
  };
25
33
  export declare type UpdateApiKeyInput = {
26
34
  name: string;
@@ -45,7 +53,7 @@ export declare type Project = AuthBaseEntity & {
45
53
  id: string;
46
54
  name: string;
47
55
  accountId: string;
48
- env: string;
56
+ env: Environment;
49
57
  };
50
58
  export declare enum Environment {
51
59
  LIVE = "live",
package/package.json CHANGED
@@ -1,17 +1,19 @@
1
1
  {
2
2
  "name": "theauthapi",
3
- "version": "1.0.7",
3
+ "version": "1.0.10",
4
4
  "description": "Client library for TheAuthAPI.com",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
7
7
  "files": [
8
8
  "dist"
9
9
  ],
10
+ "keywords": [
11
+ "theauthapi",
12
+ "authapi"
13
+ ],
10
14
  "scripts": {
11
15
  "test": "jest --runInBand",
12
16
  "test:coverage": "jest --coverage --runInBand",
13
- "np": "np --no-publish",
14
- "release": "yarn run np",
15
17
  "build": "tsc",
16
18
  "prepublish": "npm run build"
17
19
  },
@@ -35,8 +37,6 @@
35
37
  "remove-trailing-slash": "^0.1.1"
36
38
  },
37
39
  "devDependencies": {
38
- "@size-limit/preset-small-lib": "^7.0.8",
39
- "@types/basic-auth": "^1.1.3",
40
40
  "@types/express": "^4.17.13",
41
41
  "@types/jest": "^27.4.1",
42
42
  "@types/lodash.isstring": "^4.0.6",
@@ -51,9 +51,6 @@
51
51
  "express": "^4.15.2",
52
52
  "jest": "^27.5.1",
53
53
  "prettier": "2.6.2",
54
- "size-limit": "^4.11.0",
55
- "ts-jest": "^27.1.4",
56
- "ts-node": "^10.7.0",
57
54
  "typescript": "^4.6.3"
58
55
  }
59
56
  }