theauthapi 1.0.4 → 1.0.5
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 +5 -2
- package/dist/endpoints/Accounts/Accounts.d.ts +10 -0
- package/dist/endpoints/Accounts/Accounts.js +26 -0
- package/dist/endpoints/Accounts/AccountsInterface.d.ts +4 -0
- package/dist/endpoints/Accounts/AccountsInterface.js +2 -0
- package/dist/endpoints/ApiKeys/ApiKeys.d.ts +13 -0
- package/dist/endpoints/ApiKeys/ApiKeys.js +58 -0
- package/dist/endpoints/ApiKeys/ApiKeysInterface.d.ts +9 -0
- package/dist/endpoints/ApiKeys/ApiKeysInterface.js +2 -0
- package/dist/endpoints/Projects/Projects.d.ts +14 -0
- package/dist/endpoints/Projects/Projects.js +54 -0
- package/dist/endpoints/Projects/ProjectsInterface.d.ts +8 -0
- package/dist/endpoints/Projects/ProjectsInterface.js +2 -0
- package/dist/index.d.ts +36 -0
- package/dist/index.js +89 -0
- package/dist/libraryMeta.d.ts +1 -0
- package/dist/libraryMeta.js +4 -0
- package/dist/services/ApiRequest/ApiCall.d.ts +5 -0
- package/dist/services/ApiRequest/ApiCall.js +2 -0
- package/dist/services/ApiRequest/ApiRequest.d.ts +26 -0
- package/dist/services/ApiRequest/ApiRequest.js +116 -0
- package/dist/services/ApiRequest/ApiRequestError.d.ts +4 -0
- package/dist/services/ApiRequest/ApiRequestError.js +7 -0
- package/dist/services/ApiRequest/HttpMethod.d.ts +7 -0
- package/dist/services/ApiRequest/HttpMethod.js +11 -0
- package/dist/types/index.d.ts +56 -0
- package/dist/types/index.js +8 -0
- package/dist/usage.ts.js +8 -0
- package/dist/util/index.d.ts +1 -0
- package/dist/util/index.js +13 -0
- package/package.json +4 -9
- package/.eslintrc.js +0 -15
- package/.prettierrc.json +0 -1
- package/jest.config.js +0 -5
- package/jest.setup.ts +0 -0
- package/src/endpoints/ApiKeys/ApiKeys.ts +0 -68
- package/src/endpoints/ApiKeys/ApiKeysInterface.ts +0 -11
- package/src/endpoints/Projects/Projects.ts +0 -62
- package/src/endpoints/Projects/ProjectsInterface.ts +0 -9
- package/src/index.ts +0 -103
- package/src/libraryMeta.ts +0 -1
- package/src/services/ApiRequest/ApiCall.ts +0 -7
- package/src/services/ApiRequest/ApiRequest.ts +0 -132
- package/src/services/ApiRequest/ApiRequestError.ts +0 -7
- package/src/services/ApiRequest/HttpMethod.ts +0 -7
- package/src/tests/endpoints/apiKeys.spec.ts +0 -102
- package/src/tests/endpoints/projects.spec.ts +0 -105
- package/src/tests/index.spec.ts +0 -35
- package/src/tests/testServer/middleware/middleware.ts +0 -17
- package/src/tests/testServer/routes/apiKeys.ts +0 -89
- package/src/tests/testServer/routes/auth.ts +0 -24
- package/src/tests/testServer/routes/projects.ts +0 -87
- package/src/tests/testServer/server.ts +0 -16
- package/src/types/index.ts +0 -59
- package/src/util/index.ts +0 -7
- package/tsconfig.json +0 -102
package/README.md
CHANGED
|
@@ -1,2 +1,5 @@
|
|
|
1
|
-
#
|
|
2
|
-
|
|
1
|
+
# The Auth API node library
|
|
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.
|
|
4
|
+
|
|
5
|
+
Client library for [TheAuthAPI.com](https://theauthapi.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,13 @@
|
|
|
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
|
+
authenticateKey(apikey: string): Promise<ApiKey>;
|
|
8
|
+
getKeys(projectId: string): Promise<ApiKey[]>;
|
|
9
|
+
createKey(apiKey: ApiKeyInput): Promise<ApiKey>;
|
|
10
|
+
updateKey(apiKey: string, updateTo: UpdateApiKeyInput): Promise<ApiKey>;
|
|
11
|
+
deleteKey(apiKey: string): Promise<boolean>;
|
|
12
|
+
}
|
|
13
|
+
export = ApiKeys;
|
|
@@ -0,0 +1,58 @@
|
|
|
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
|
+
const HttpMethod_1 = require("../../services/ApiRequest/HttpMethod");
|
|
15
|
+
const lodash_omit_1 = __importDefault(require("lodash.omit"));
|
|
16
|
+
const util_1 = require("../../util");
|
|
17
|
+
class ApiKeys {
|
|
18
|
+
constructor(apiService) {
|
|
19
|
+
this.api = apiService;
|
|
20
|
+
}
|
|
21
|
+
authenticateKey(apikey) {
|
|
22
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
23
|
+
(0, util_1.validateString)("apikey", apikey);
|
|
24
|
+
return yield this.api.request(HttpMethod_1.HttpMethod.GET, `/api-keys/${apikey}`);
|
|
25
|
+
});
|
|
26
|
+
}
|
|
27
|
+
getKeys(projectId) {
|
|
28
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
29
|
+
(0, util_1.validateString)("projectId", projectId);
|
|
30
|
+
return yield this.api.request(HttpMethod_1.HttpMethod.GET, `/api-keys/?projectId=${projectId}`);
|
|
31
|
+
});
|
|
32
|
+
}
|
|
33
|
+
createKey(apiKey) {
|
|
34
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
35
|
+
// validate string properties only
|
|
36
|
+
for (const [key, value] of Object.entries((0, lodash_omit_1.default)(apiKey, ["customMetaData", "rateLimitConfigs"]))) {
|
|
37
|
+
(0, util_1.validateString)(key, value);
|
|
38
|
+
}
|
|
39
|
+
return yield this.api.request(HttpMethod_1.HttpMethod.POST, "/api-keys", apiKey);
|
|
40
|
+
});
|
|
41
|
+
}
|
|
42
|
+
updateKey(apiKey, updateTo) {
|
|
43
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
44
|
+
(0, util_1.validateString)("apiKey", apiKey);
|
|
45
|
+
for (const [key, value] of Object.entries((0, lodash_omit_1.default)(updateTo, "customMetaData"))) {
|
|
46
|
+
(0, util_1.validateString)(key, value);
|
|
47
|
+
}
|
|
48
|
+
return yield this.api.request(HttpMethod_1.HttpMethod.PATCH, `/api-keys/${apiKey}`, updateTo);
|
|
49
|
+
});
|
|
50
|
+
}
|
|
51
|
+
deleteKey(apiKey) {
|
|
52
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
53
|
+
(0, util_1.validateString)("apiKey", apiKey);
|
|
54
|
+
return yield this.api.request(HttpMethod_1.HttpMethod.DELETE, `/api-keys/${apiKey}`);
|
|
55
|
+
});
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
module.exports = ApiKeys;
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { ApiKey, ApiKeyInput, UpdateApiKeyInput } from "../../types";
|
|
2
|
+
interface ApiKeysInterface {
|
|
3
|
+
authenticateKey(apiKey: string): Promise<ApiKey>;
|
|
4
|
+
getKeys(projectId: string): Promise<ApiKey[]>;
|
|
5
|
+
createKey(apiKey: ApiKeyInput): Promise<ApiKey>;
|
|
6
|
+
updateKey(apiKey: string, updateTo: UpdateApiKeyInput): Promise<ApiKey>;
|
|
7
|
+
deleteKey(apiKey: string): Promise<boolean>;
|
|
8
|
+
}
|
|
9
|
+
export = ApiKeysInterface;
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import ApiRequest from "../../services/ApiRequest/ApiRequest";
|
|
2
|
+
import { 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(name: string, accountId: string): Promise<Project>;
|
|
12
|
+
updateProject(projectId: string, updateTo: UpdateProjectInput): Promise<Project>;
|
|
13
|
+
}
|
|
14
|
+
export default Projects;
|
|
@@ -0,0 +1,54 @@
|
|
|
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 Projects {
|
|
15
|
+
constructor(apiService) {
|
|
16
|
+
this.api = apiService;
|
|
17
|
+
this.endpoint = "/projects";
|
|
18
|
+
}
|
|
19
|
+
getProjects(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=${accountId}`);
|
|
23
|
+
});
|
|
24
|
+
}
|
|
25
|
+
getProject(projectId) {
|
|
26
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
27
|
+
(0, util_1.validateString)("projectId", projectId);
|
|
28
|
+
return yield this.api.request(HttpMethod_1.HttpMethod.GET, `${this.endpoint}/${projectId}`);
|
|
29
|
+
});
|
|
30
|
+
}
|
|
31
|
+
deleteProject(projectId) {
|
|
32
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
33
|
+
(0, util_1.validateString)("projectId", projectId);
|
|
34
|
+
return yield this.api.request(HttpMethod_1.HttpMethod.DELETE, `${this.endpoint}/${projectId}`);
|
|
35
|
+
});
|
|
36
|
+
}
|
|
37
|
+
createProject(name, accountId) {
|
|
38
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
39
|
+
(0, util_1.validateString)("name", name);
|
|
40
|
+
(0, util_1.validateString)("accountId", accountId);
|
|
41
|
+
return yield this.api.request(HttpMethod_1.HttpMethod.POST, this.endpoint, {
|
|
42
|
+
name,
|
|
43
|
+
accountId,
|
|
44
|
+
});
|
|
45
|
+
});
|
|
46
|
+
}
|
|
47
|
+
updateProject(projectId, updateTo) {
|
|
48
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
49
|
+
(0, util_1.validateString)("projectId", projectId);
|
|
50
|
+
return this.api.request(HttpMethod_1.HttpMethod.PATCH, `${this.endpoint}/${projectId}`, updateTo);
|
|
51
|
+
});
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
exports.default = Projects;
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { 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(name: string, accountId: string): Promise<Project>;
|
|
7
|
+
updateProject(name: string, updateTo: UpdateProjectInput): Promise<Project>;
|
|
8
|
+
}
|
package/dist/index.d.ts
ADDED
|
@@ -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;
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,89 @@
|
|
|
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 assert_1 = __importDefault(require("assert"));
|
|
16
|
+
const remove_trailing_slash_1 = __importDefault(require("remove-trailing-slash"));
|
|
17
|
+
const ApiRequest_1 = __importDefault(require("./services/ApiRequest/ApiRequest"));
|
|
18
|
+
const util_1 = require("./util");
|
|
19
|
+
const ApiKeys_1 = __importDefault(require("./endpoints/ApiKeys/ApiKeys"));
|
|
20
|
+
const HttpMethod_1 = require("./services/ApiRequest/HttpMethod");
|
|
21
|
+
const Projects_1 = __importDefault(require("./endpoints/Projects/Projects"));
|
|
22
|
+
const Accounts_1 = __importDefault(require("./endpoints/Accounts/Accounts"));
|
|
23
|
+
// eslint-disable-next-line @typescript-eslint/no-empty-function
|
|
24
|
+
const noop = () => { };
|
|
25
|
+
class TheAuthAPI {
|
|
26
|
+
/**
|
|
27
|
+
* Initialize a new `Analytics` with your Segment project's `writeKey` and an
|
|
28
|
+
* optional dictionary of `options`.
|
|
29
|
+
*
|
|
30
|
+
* @param {String} accessKey
|
|
31
|
+
* @param {Object} [options] (optional)
|
|
32
|
+
* @property {Number} flushAt (default: 20)
|
|
33
|
+
* @property {Number} flushInterval (default: 10000)
|
|
34
|
+
* @property {String} host (default: 'https://api.segment.io')
|
|
35
|
+
* @property {Boolean} enable (default: true)
|
|
36
|
+
*/
|
|
37
|
+
constructor(accessKey, options) {
|
|
38
|
+
var _a;
|
|
39
|
+
(0, assert_1.default)(accessKey, "You must pass your project's write key.");
|
|
40
|
+
this.queue = [];
|
|
41
|
+
this.accessKey = accessKey;
|
|
42
|
+
this.host = (0, remove_trailing_slash_1.default)((options === null || options === void 0 ? void 0 : options.host) || "https://api.theauthapi.com");
|
|
43
|
+
this.timeout = options === null || options === void 0 ? void 0 : options.timeout;
|
|
44
|
+
this.cacheTTL = (_a = options === null || options === void 0 ? void 0 : options.cacheTTL) !== null && _a !== void 0 ? _a : 60;
|
|
45
|
+
this.api = new ApiRequest_1.default({
|
|
46
|
+
accessKey: this.accessKey,
|
|
47
|
+
host: this.host,
|
|
48
|
+
});
|
|
49
|
+
this.apiKeys = new ApiKeys_1.default(this.api);
|
|
50
|
+
this.projects = new Projects_1.default(this.api);
|
|
51
|
+
this.accounts = new Accounts_1.default(this.api);
|
|
52
|
+
Object.defineProperty(this, "enable", {
|
|
53
|
+
configurable: false,
|
|
54
|
+
writable: false,
|
|
55
|
+
enumerable: true,
|
|
56
|
+
value: typeof (options === null || options === void 0 ? void 0 : options.enable) === "boolean" ? options.enable : true,
|
|
57
|
+
});
|
|
58
|
+
}
|
|
59
|
+
/*
|
|
60
|
+
@deprecated
|
|
61
|
+
*/
|
|
62
|
+
authenticateAPIKey(key, callback) {
|
|
63
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
64
|
+
(0, util_1.validateString)("key", key);
|
|
65
|
+
const cb = callback || noop;
|
|
66
|
+
const done = (err) => {
|
|
67
|
+
cb(err, data);
|
|
68
|
+
};
|
|
69
|
+
const data = {
|
|
70
|
+
credentials: { api_key: key },
|
|
71
|
+
timestamp: new Date().getTime(),
|
|
72
|
+
sentAt: new Date().getTime(),
|
|
73
|
+
};
|
|
74
|
+
try {
|
|
75
|
+
const key = yield this.api.request(HttpMethod_1.HttpMethod.POST, "/auth/authenticate", data);
|
|
76
|
+
done(key);
|
|
77
|
+
return key;
|
|
78
|
+
}
|
|
79
|
+
catch (err) {
|
|
80
|
+
if (err.response) {
|
|
81
|
+
const error = new Error(err.response.statusText);
|
|
82
|
+
return done(error);
|
|
83
|
+
}
|
|
84
|
+
done(err);
|
|
85
|
+
}
|
|
86
|
+
});
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
exports.default = TheAuthAPI;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const version = "1.0.1";
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { HttpMethod } from "./HttpMethod";
|
|
2
|
+
import ApiCall from "./ApiCall";
|
|
3
|
+
declare type Config = {
|
|
4
|
+
host: string;
|
|
5
|
+
accessKey: string;
|
|
6
|
+
headers?: object;
|
|
7
|
+
retryCount?: number;
|
|
8
|
+
timeout?: number | string;
|
|
9
|
+
};
|
|
10
|
+
declare class ApiRequest implements ApiCall {
|
|
11
|
+
host: string;
|
|
12
|
+
headers: object;
|
|
13
|
+
accessKey: string;
|
|
14
|
+
timeout: number;
|
|
15
|
+
retryCount: number;
|
|
16
|
+
constructor(config: Config);
|
|
17
|
+
_init(): void;
|
|
18
|
+
request<T>(method: HttpMethod, endpoint: string, payload?: any): Promise<T>;
|
|
19
|
+
_generateDefaultHeaders(): {
|
|
20
|
+
"user-agent": string;
|
|
21
|
+
"x-api-key": string;
|
|
22
|
+
"api-key": string;
|
|
23
|
+
};
|
|
24
|
+
_isErrorRetryable(error: any): boolean;
|
|
25
|
+
}
|
|
26
|
+
export default ApiRequest;
|
|
@@ -0,0 +1,116 @@
|
|
|
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 axios_1 = __importDefault(require("axios"));
|
|
16
|
+
const libraryMeta_1 = require("../../libraryMeta");
|
|
17
|
+
const ApiRequestError_1 = __importDefault(require("./ApiRequestError"));
|
|
18
|
+
const axios_retry_1 = __importDefault(require("axios-retry"));
|
|
19
|
+
const ms_1 = __importDefault(require("ms"));
|
|
20
|
+
class ApiRequest {
|
|
21
|
+
constructor(config) {
|
|
22
|
+
const { host, accessKey, headers, retryCount, timeout } = config;
|
|
23
|
+
this.host = host;
|
|
24
|
+
this.accessKey = accessKey;
|
|
25
|
+
this.headers = this._generateDefaultHeaders();
|
|
26
|
+
this.timeout = timeout ? (typeof timeout === "string" ? (0, ms_1.default)(timeout) : timeout) : 0;
|
|
27
|
+
this.retryCount = retryCount !== null && retryCount !== void 0 ? retryCount : 3;
|
|
28
|
+
if (headers) {
|
|
29
|
+
this.headers = Object.assign(Object.assign({}, this.headers), { headers });
|
|
30
|
+
}
|
|
31
|
+
this._init();
|
|
32
|
+
}
|
|
33
|
+
_init() {
|
|
34
|
+
var _a;
|
|
35
|
+
const isoDateFormat = /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(?:\.\d*)?(?:[-+]\d{2}:?\d{2}|Z)?$/;
|
|
36
|
+
function isIsoDateString(value) {
|
|
37
|
+
return value && typeof value === "string" && isoDateFormat.test(value);
|
|
38
|
+
}
|
|
39
|
+
function handleDates(body) {
|
|
40
|
+
if (body === null || body === undefined || typeof body !== "object") {
|
|
41
|
+
return body;
|
|
42
|
+
}
|
|
43
|
+
for (const key of Object.keys(body)) {
|
|
44
|
+
const value = body[key];
|
|
45
|
+
if (isIsoDateString(value)) {
|
|
46
|
+
body[key] = new Date(value);
|
|
47
|
+
}
|
|
48
|
+
else if (typeof value === "object") {
|
|
49
|
+
handleDates(value);
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
axios_1.default.interceptors.response.use((response) => {
|
|
54
|
+
handleDates(response.data);
|
|
55
|
+
return response;
|
|
56
|
+
});
|
|
57
|
+
(0, axios_retry_1.default)(axios_1.default, {
|
|
58
|
+
retries: (_a = this.retryCount) !== null && _a !== void 0 ? _a : 3,
|
|
59
|
+
retryCondition: this._isErrorRetryable,
|
|
60
|
+
retryDelay: axios_retry_1.default.exponentialDelay,
|
|
61
|
+
});
|
|
62
|
+
}
|
|
63
|
+
request(method, endpoint, payload) {
|
|
64
|
+
var _a;
|
|
65
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
66
|
+
try {
|
|
67
|
+
const response = yield axios_1.default.request({
|
|
68
|
+
baseURL: this.host,
|
|
69
|
+
method: method,
|
|
70
|
+
url: endpoint,
|
|
71
|
+
data: payload,
|
|
72
|
+
headers: this.headers,
|
|
73
|
+
});
|
|
74
|
+
return response.data;
|
|
75
|
+
}
|
|
76
|
+
catch (error) {
|
|
77
|
+
if (axios_1.default.isAxiosError(error)) {
|
|
78
|
+
if (error.response) {
|
|
79
|
+
throw new ApiRequestError_1.default(error.response.status, (_a = error.response.data.message) !== null && _a !== void 0 ? _a : error.response.statusText);
|
|
80
|
+
}
|
|
81
|
+
else if (error.request) {
|
|
82
|
+
throw new Error(error.request);
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
throw error;
|
|
86
|
+
}
|
|
87
|
+
});
|
|
88
|
+
}
|
|
89
|
+
_generateDefaultHeaders() {
|
|
90
|
+
return {
|
|
91
|
+
"user-agent": `theauthapi-client-node/${libraryMeta_1.version}`,
|
|
92
|
+
"x-api-key": this.accessKey,
|
|
93
|
+
"api-key": this.accessKey,
|
|
94
|
+
};
|
|
95
|
+
}
|
|
96
|
+
_isErrorRetryable(error) {
|
|
97
|
+
// Retry Network Errors.
|
|
98
|
+
if (axios_retry_1.default.isNetworkError(error)) {
|
|
99
|
+
return true;
|
|
100
|
+
}
|
|
101
|
+
if (!error.response) {
|
|
102
|
+
// Cannot determine if the request can be retried
|
|
103
|
+
return false;
|
|
104
|
+
}
|
|
105
|
+
// Retry Server Errors (5xx).
|
|
106
|
+
if (error.response.status >= 500 && error.response.status <= 599) {
|
|
107
|
+
return true;
|
|
108
|
+
}
|
|
109
|
+
// Retry if rate limited.
|
|
110
|
+
if (error.response.status === 429) {
|
|
111
|
+
return true;
|
|
112
|
+
}
|
|
113
|
+
return false;
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
exports.default = ApiRequest;
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.HttpMethod = void 0;
|
|
4
|
+
var HttpMethod;
|
|
5
|
+
(function (HttpMethod) {
|
|
6
|
+
HttpMethod["GET"] = "GET";
|
|
7
|
+
HttpMethod["POST"] = "POST";
|
|
8
|
+
HttpMethod["DELETE"] = "DELETE";
|
|
9
|
+
HttpMethod["PATCH"] = "PATCH";
|
|
10
|
+
HttpMethod["PUT"] = "PUT";
|
|
11
|
+
})(HttpMethod = exports.HttpMethod || (exports.HttpMethod = {}));
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
export declare type ApiKey = {
|
|
2
|
+
key: string;
|
|
3
|
+
name: string;
|
|
4
|
+
customMetaData: string;
|
|
5
|
+
customAccountId: string;
|
|
6
|
+
env: string;
|
|
7
|
+
createdAt: Date;
|
|
8
|
+
updatedAt: Date;
|
|
9
|
+
};
|
|
10
|
+
export declare type RateLimitConfiguration = {
|
|
11
|
+
rateLimitedEntity?: string;
|
|
12
|
+
ratelimitedEnitityId?: any;
|
|
13
|
+
rateLimit: number;
|
|
14
|
+
rateLimitTtl: number;
|
|
15
|
+
};
|
|
16
|
+
export declare type ApiKeyInput = {
|
|
17
|
+
name: string;
|
|
18
|
+
projectId: string;
|
|
19
|
+
key?: string;
|
|
20
|
+
customMetaData?: object;
|
|
21
|
+
customAccountId?: string;
|
|
22
|
+
rateLimitConfigs?: RateLimitConfiguration;
|
|
23
|
+
customUserId?: string;
|
|
24
|
+
};
|
|
25
|
+
export declare type UpdateApiKeyInput = {
|
|
26
|
+
name: string;
|
|
27
|
+
customMetaData?: object;
|
|
28
|
+
customAccountId?: string;
|
|
29
|
+
};
|
|
30
|
+
export declare enum AuthedEntityType {
|
|
31
|
+
USER = "USER",
|
|
32
|
+
ACCESS_KEY = "ACCESS_KEY"
|
|
33
|
+
}
|
|
34
|
+
export declare type AuthBaseEntity = {
|
|
35
|
+
isActive: boolean;
|
|
36
|
+
createdBy: string;
|
|
37
|
+
createdByType?: AuthedEntityType;
|
|
38
|
+
createdIn: string;
|
|
39
|
+
lastChangedBy: string;
|
|
40
|
+
lastChangedByType?: AuthedEntityType;
|
|
41
|
+
updatedAt: Date;
|
|
42
|
+
createdAt: Date;
|
|
43
|
+
};
|
|
44
|
+
export declare type Project = AuthBaseEntity & {
|
|
45
|
+
id: string;
|
|
46
|
+
name: string;
|
|
47
|
+
accountId: string;
|
|
48
|
+
env: string;
|
|
49
|
+
};
|
|
50
|
+
export declare type UpdateProjectInput = {
|
|
51
|
+
name: string;
|
|
52
|
+
};
|
|
53
|
+
export declare type Account = AuthBaseEntity & {
|
|
54
|
+
id: string;
|
|
55
|
+
name: string;
|
|
56
|
+
};
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.AuthedEntityType = void 0;
|
|
4
|
+
var AuthedEntityType;
|
|
5
|
+
(function (AuthedEntityType) {
|
|
6
|
+
AuthedEntityType["USER"] = "USER";
|
|
7
|
+
AuthedEntityType["ACCESS_KEY"] = "ACCESS_KEY";
|
|
8
|
+
})(AuthedEntityType = exports.AuthedEntityType || (exports.AuthedEntityType = {}));
|
package/dist/usage.ts.js
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
const TheAuthAPI = require('./index').default;
|
|
2
|
+
|
|
3
|
+
const api = new TheAuthAPI("live_access_j5OLGJz9SkRUSEiiGdQRFMyRLpnpjtbXidEQVmL8D0Ly3xaPs1FcTnl2z4MGARlD", {
|
|
4
|
+
host: 'http://localhost:8080'
|
|
5
|
+
});
|
|
6
|
+
const keyData = api.projects.getProjects('0527d593-2e86-48ef-b2a7-043afb46ff68')
|
|
7
|
+
.then(data => console.log(data))
|
|
8
|
+
.catch(error => console.log(error))
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function validateString(variableName: string, value: string): void;
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.validateString = void 0;
|
|
7
|
+
const lodash_isstring_1 = __importDefault(require("lodash.isstring"));
|
|
8
|
+
function validateString(variableName, value) {
|
|
9
|
+
if (!value || !(0, lodash_isstring_1.default)(value)) {
|
|
10
|
+
throw new Error(`${variableName} must be a string`);
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
exports.validateString = validateString;
|