theauthapi 1.0.0 → 1.0.1-2.1

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 (33) hide show
  1. package/README.md +366 -2
  2. package/dist/endpoints/Accounts/Accounts.d.ts +10 -0
  3. package/dist/endpoints/Accounts/Accounts.js +24 -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 +19 -0
  7. package/dist/endpoints/ApiKeys/ApiKeys.js +85 -0
  8. package/dist/endpoints/ApiKeys/ApiKeysInterface.d.ts +12 -0
  9. package/dist/endpoints/ApiKeys/ApiKeysInterface.js +2 -0
  10. package/dist/endpoints/Projects/Projects.d.ts +14 -0
  11. package/dist/endpoints/Projects/Projects.js +44 -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 +26 -0
  15. package/dist/index.js +74 -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 +24 -0
  21. package/dist/services/ApiRequest/ApiRequest.js +115 -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 +85 -0
  29. package/dist/types/index.js +13 -0
  30. package/package.json +30 -34
  31. package/History.md +0 -5
  32. package/index.js +0 -182
  33. package/test.js +0 -113
package/dist/index.js ADDED
@@ -0,0 +1,74 @@
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 ApiKeys_1 = __importDefault(require("./endpoints/ApiKeys/ApiKeys"));
19
+ const HttpMethod_1 = require("./services/ApiRequest/HttpMethod");
20
+ const Projects_1 = __importDefault(require("./endpoints/Projects/Projects"));
21
+ const Accounts_1 = __importDefault(require("./endpoints/Accounts/Accounts"));
22
+ // eslint-disable-next-line @typescript-eslint/no-empty-function
23
+ const noop = () => { };
24
+ class TheAuthAPI {
25
+ /**
26
+ * @param {String} accessKey
27
+ * @param {Object} [options] (optional)
28
+ * @property {String} host (default: 'https://api.segment.io')
29
+ * @property {number} retryCount (default: 3)
30
+ */
31
+ constructor(accessKey, options) {
32
+ var _a;
33
+ (0, assert_1.default)(accessKey, "You must pass your project's write key.");
34
+ this.accessKey = accessKey;
35
+ this.host = (0, remove_trailing_slash_1.default)((options === null || options === void 0 ? void 0 : options.host) || "https://api.theauthapi.com");
36
+ this.api = new ApiRequest_1.default({
37
+ accessKey: this.accessKey,
38
+ host: this.host,
39
+ retryCount: (_a = options === null || options === void 0 ? void 0 : options.retryCount) !== null && _a !== void 0 ? _a : 3,
40
+ });
41
+ this.apiKeys = new ApiKeys_1.default(this.api);
42
+ this.projects = new Projects_1.default(this.api);
43
+ this.accounts = new Accounts_1.default(this.api);
44
+ }
45
+ /*
46
+ @deprecated
47
+ */
48
+ authenticateAPIKey(key, callback) {
49
+ return __awaiter(this, void 0, void 0, function* () {
50
+ const cb = callback || noop;
51
+ const done = (err) => {
52
+ cb(err, data);
53
+ };
54
+ const data = {
55
+ credentials: { api_key: key },
56
+ timestamp: new Date().getTime(),
57
+ sentAt: new Date().getTime(),
58
+ };
59
+ try {
60
+ const key = yield this.api.request(HttpMethod_1.HttpMethod.POST, "/auth/authenticate", data);
61
+ done(key);
62
+ return key;
63
+ }
64
+ catch (err) {
65
+ if (err.response) {
66
+ const error = new Error(err.response.statusText);
67
+ return done(error);
68
+ }
69
+ done(err);
70
+ }
71
+ });
72
+ }
73
+ }
74
+ exports.default = TheAuthAPI;
@@ -0,0 +1 @@
1
+ export declare const version = "1.0.11";
@@ -0,0 +1,4 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.version = void 0;
4
+ exports.version = "1.0.11";
@@ -0,0 +1,5 @@
1
+ import { HttpMethod } from "./HttpMethod";
2
+ interface ApiCall {
3
+ request<T>(method: HttpMethod, endpoint: string, payload?: any): Promise<T>;
4
+ }
5
+ export default ApiCall;
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,24 @@
1
+ import { HttpMethod } from "./HttpMethod";
2
+ import ApiCall from "./ApiCall";
3
+ type Config = {
4
+ host: string;
5
+ accessKey: string;
6
+ headers?: object;
7
+ retryCount?: number;
8
+ };
9
+ declare class ApiRequest implements ApiCall {
10
+ host: string;
11
+ headers: object;
12
+ accessKey: string;
13
+ retryCount: number;
14
+ constructor(config: Config);
15
+ _init(): void;
16
+ request<T>(method: HttpMethod, endpoint: string, payload?: any): Promise<T>;
17
+ _generateDefaultHeaders(): {
18
+ "user-agent": string;
19
+ "x-api-key": string;
20
+ "api-key": string;
21
+ };
22
+ _isErrorRetryable(error: any): boolean;
23
+ }
24
+ export default ApiRequest;
@@ -0,0 +1,115 @@
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 ApiResponseError_1 = __importDefault(require("./ApiResponseError"));
20
+ class ApiRequest {
21
+ constructor(config) {
22
+ const { host, accessKey, headers, retryCount } = config;
23
+ this.host = host;
24
+ this.accessKey = accessKey;
25
+ this.headers = this._generateDefaultHeaders();
26
+ this.retryCount = retryCount !== null && retryCount !== void 0 ? retryCount : 3;
27
+ if (headers) {
28
+ this.headers = Object.assign(Object.assign({}, this.headers), { headers });
29
+ }
30
+ this._init();
31
+ }
32
+ _init() {
33
+ var _a;
34
+ const isoDateFormat = /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(?:\.\d*)?(?:[-+]\d{2}:?\d{2}|Z)?$/;
35
+ function isIsoDateString(value) {
36
+ return value && typeof value === "string" && isoDateFormat.test(value);
37
+ }
38
+ function handleDates(body) {
39
+ if (body === null || body === undefined || typeof body !== "object") {
40
+ return body;
41
+ }
42
+ for (const key of Object.keys(body)) {
43
+ const value = body[key];
44
+ if (isIsoDateString(value)) {
45
+ body[key] = new Date(value);
46
+ }
47
+ else if (typeof value === "object") {
48
+ handleDates(value);
49
+ }
50
+ }
51
+ }
52
+ axios_1.default.interceptors.response.use((response) => {
53
+ handleDates(response.data);
54
+ return response;
55
+ });
56
+ (0, axios_retry_1.default)(axios_1.default, {
57
+ retries: (_a = this.retryCount) !== null && _a !== void 0 ? _a : 3,
58
+ retryCondition: this._isErrorRetryable,
59
+ retryDelay: axios_retry_1.default.exponentialDelay,
60
+ });
61
+ }
62
+ request(method, endpoint, payload) {
63
+ var _a;
64
+ return __awaiter(this, void 0, void 0, function* () {
65
+ try {
66
+ const response = yield axios_1.default.request({
67
+ baseURL: this.host,
68
+ method: method,
69
+ url: endpoint,
70
+ data: payload,
71
+ headers: this.headers,
72
+ });
73
+ return response.data;
74
+ }
75
+ catch (error) {
76
+ if (axios_1.default.isAxiosError(error)) {
77
+ if (error.response) {
78
+ throw new ApiResponseError_1.default(error.response.status, (_a = error.response.data.message) !== null && _a !== void 0 ? _a : error.response.statusText);
79
+ }
80
+ else if (error.request) {
81
+ throw new ApiRequestError_1.default(error.message);
82
+ }
83
+ }
84
+ throw error;
85
+ }
86
+ });
87
+ }
88
+ _generateDefaultHeaders() {
89
+ return {
90
+ "user-agent": `theauthapi-client-node/${libraryMeta_1.version}`,
91
+ "x-api-key": this.accessKey,
92
+ "api-key": this.accessKey,
93
+ };
94
+ }
95
+ _isErrorRetryable(error) {
96
+ // Retry Network Errors.
97
+ if (axios_retry_1.default.isNetworkError(error)) {
98
+ return true;
99
+ }
100
+ if (!error.response) {
101
+ // Cannot determine if the request can be retried
102
+ return false;
103
+ }
104
+ // Retry Server Errors (5xx).
105
+ if (error.response.status >= 500 && error.response.status <= 599) {
106
+ return true;
107
+ }
108
+ // Retry if rate limited.
109
+ if (error.response.status === 429) {
110
+ return true;
111
+ }
112
+ return false;
113
+ }
114
+ }
115
+ exports.default = ApiRequest;
@@ -0,0 +1,10 @@
1
+ /**
2
+ *
3
+ * Throws when no response was received from the server
4
+ * @param message - error message
5
+ *
6
+ * */
7
+ declare class ApiRequestError extends Error {
8
+ constructor(message: string);
9
+ }
10
+ export default ApiRequestError;
@@ -0,0 +1,15 @@
1
+ "use strict";
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
+ * */
9
+ class ApiRequestError extends Error {
10
+ constructor(message) {
11
+ super(message);
12
+ this.name = 'ApiRequestError';
13
+ }
14
+ }
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;
@@ -0,0 +1,7 @@
1
+ export declare enum HttpMethod {
2
+ GET = "GET",
3
+ POST = "POST",
4
+ DELETE = "DELETE",
5
+ PATCH = "PATCH",
6
+ PUT = "PUT"
7
+ }
@@ -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,85 @@
1
+ export type ApiKey = {
2
+ key: string;
3
+ name: string;
4
+ customMetaData: AnyJson;
5
+ customAccountId: string;
6
+ customUserId: string;
7
+ env: Environment;
8
+ createdAt: Date;
9
+ updatedAt: Date;
10
+ isActive: boolean;
11
+ rateLimitConfigs: RateLimitConfiguration;
12
+ expiry: Date;
13
+ };
14
+ export type RateLimitConfiguration = {
15
+ rateLimit: number;
16
+ rateLimitTtl: number;
17
+ };
18
+ export type ApiKeyInput = {
19
+ name: string;
20
+ projectId?: string;
21
+ key?: string;
22
+ customMetaData?: AnyJson;
23
+ customAccountId?: string;
24
+ customUserId?: string;
25
+ rateLimitConfigs?: RateLimitConfiguration;
26
+ expiry?: Date;
27
+ };
28
+ export type ApiKeyFilter = {
29
+ projectId?: string;
30
+ name?: string;
31
+ customAccountId?: string | null;
32
+ customUserId?: string | null;
33
+ isActive?: boolean;
34
+ };
35
+ export type UpdateApiKeyInput = {
36
+ name: string;
37
+ key?: string;
38
+ customMetaData?: AnyJson;
39
+ customAccountId?: string;
40
+ customUserId?: string;
41
+ expiry?: Date | null;
42
+ rateLimitConfigs?: RateLimitConfiguration | null;
43
+ };
44
+ export declare enum AuthedEntityType {
45
+ USER = "USER",
46
+ ACCESS_KEY = "ACCESS_KEY"
47
+ }
48
+ export type AuthBaseEntity = {
49
+ isActive: boolean;
50
+ createdBy: string;
51
+ createdByType?: AuthedEntityType;
52
+ createdIn: string;
53
+ lastChangedBy: string;
54
+ lastChangedByType?: AuthedEntityType;
55
+ updatedAt: Date;
56
+ createdAt: Date;
57
+ };
58
+ export type Project = AuthBaseEntity & {
59
+ id: string;
60
+ name: string;
61
+ accountId: string;
62
+ env: Environment;
63
+ };
64
+ export declare enum Environment {
65
+ LIVE = "live",
66
+ TEST = "test"
67
+ }
68
+ export type CreateProjectInput = {
69
+ name: string;
70
+ accountId: string;
71
+ env: Environment;
72
+ };
73
+ export type UpdateProjectInput = {
74
+ name: string;
75
+ };
76
+ export type Account = AuthBaseEntity & {
77
+ id: string;
78
+ name: string;
79
+ };
80
+ type AnyJson = boolean | number | string | null | JsonArray | JsonMap;
81
+ type JsonMap = {
82
+ [key: string]: AnyJson;
83
+ };
84
+ type JsonArray = Array<AnyJson>;
85
+ export {};
@@ -0,0 +1,13 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.Environment = 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 = {}));
9
+ var Environment;
10
+ (function (Environment) {
11
+ Environment["LIVE"] = "live";
12
+ Environment["TEST"] = "test";
13
+ })(Environment = exports.Environment || (exports.Environment = {}));
package/package.json CHANGED
@@ -1,25 +1,28 @@
1
1
  {
2
2
  "name": "theauthapi",
3
- "version": "1.0.0",
3
+ "version": "1.0.1-2.1",
4
4
  "description": "Client library for TheAuthAPI.com",
5
- "main": "index.js",
5
+ "main": "dist/index.js",
6
+ "types": "dist/index.d.ts",
7
+ "files": [
8
+ "dist"
9
+ ],
10
+ "keywords": [
11
+ "theauthapi",
12
+ "authapi"
13
+ ],
6
14
  "scripts": {
7
- "dependencies": "yarn",
8
- "size": "size-limit",
9
- "test": "standard && nyc ava",
10
- "report-coverage": "nyc report --reporter=lcov > coverage.lcov && codecov",
11
- "np": "np --no-publish",
12
- "release": "yarn run np",
13
- "babel-node": "babel-node --presets='@babel/preset-env'",
14
- "dev": "nodemon --exec npm run babel-node -- src/app.js",
15
- "test-mocha": "ava mocha"
15
+ "test": "jest --runInBand",
16
+ "test:coverage": "jest --coverage --runInBand",
17
+ "build": "tsc",
18
+ "prepublish": "npm run build"
16
19
  },
17
20
  "repository": {
18
21
  "type": "git",
19
22
  "url": "git+https://github.com/thatapicompany/theauthapi.git"
20
23
  },
21
24
  "author": "Aden Forshaw",
22
- "license": "ISC",
25
+ "license": "MIT",
23
26
  "bugs": {
24
27
  "url": "https://github.com/thatapicompany/theauthapi/issues"
25
28
  },
@@ -28,31 +31,24 @@
28
31
  "assert": "^2.0.0",
29
32
  "axios": "^0.21.4",
30
33
  "axios-retry": "^3.1.9",
31
- "core-js": "^3.17.3",
32
- "lodash.isstring": "^4.0.1",
33
- "ms": "^2.1.3",
34
- "node-cache": "^5.1.2",
35
- "regenerator-runtime": "^0.13.9",
36
34
  "remove-trailing-slash": "^0.1.1"
37
35
  },
38
36
  "devDependencies": {
39
- "ava": "^3.15.0",
40
- "basic-auth": "^2.0.1",
41
- "body-parser": "^1.17.1",
42
- "chai": "^4.3.4",
43
- "codecov": "^3.0.0",
44
- "commander": "^5.1.0",
45
- "delay": "^4.2.0",
37
+ "@types/express": "^4.17.13",
38
+ "@types/jest": "^27.5.2",
39
+ "@types/lodash.isstring": "^4.0.6",
40
+ "@types/lodash.omit": "^4.5.6",
41
+ "@types/ms": "^0.7.31",
42
+ "@types/node": "^17.0.23",
43
+ "@typescript-eslint/eslint-plugin": "^5.17.0",
44
+ "@typescript-eslint/parser": "^5.17.0",
45
+ "body-parser": "^1.20.0",
46
+ "eslint": "^7.14.0",
47
+ "eslint-config-prettier": "^8.5.0",
46
48
  "express": "^4.15.2",
47
- "husky": "^3.0.4",
48
- "mocha": "^9.1.1",
49
- "nyc": "^14.1.1",
50
- "pify": "^4.0.1",
51
- "sinon": "^7.3.2",
52
- "sinon-chai": "^3.7.0",
53
- "size-limit": "^4.11.0",
54
- "snyk": "^1.328.0",
55
- "standard": "^12.0.1",
56
- "supertest": "^6.1.6"
49
+ "jest": "^29.7.0",
50
+ "prettier": "2.6.2",
51
+ "ts-jest": "^29.1.1",
52
+ "typescript": "^4.6.3"
57
53
  }
58
54
  }
package/History.md DELETED
@@ -1,5 +0,0 @@
1
-
2
- v1.0.0 / 2021-09-11
3
- ==========================
4
-
5
- * Released to NPM for Alpha testing