theauthapi 1.0.13 → 1.0.15

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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 That API Company
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md CHANGED
@@ -347,7 +347,7 @@ Example of usage with Typescript:
347
347
 
348
348
  ```typescript
349
349
  import TheAuthAPI from "theauthapi";
350
- import { Project } from "theauthapi/types";
350
+ import { Project } from "theauthapi/dist/types";
351
351
 
352
352
  const client = new TheAuthAPI("ACCESS_KEY");
353
353
 
@@ -0,0 +1,22 @@
1
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
2
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
3
+ return new (P || (P = Promise))(function (resolve, reject) {
4
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
5
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
6
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
7
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
8
+ });
9
+ };
10
+ import { HttpMethod } from "../../services/ApiRequest/HttpMethod";
11
+ class Accounts {
12
+ constructor(apiService) {
13
+ this.api = apiService;
14
+ this.endpoint = "/accounts";
15
+ }
16
+ getAccount(accountId) {
17
+ return __awaiter(this, void 0, void 0, function* () {
18
+ return yield this.api.request(HttpMethod.GET, `${this.endpoint}/${accountId}`);
19
+ });
20
+ }
21
+ }
22
+ export default Accounts;
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,80 @@
1
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
2
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
3
+ return new (P || (P = Promise))(function (resolve, reject) {
4
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
5
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
6
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
7
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
8
+ });
9
+ };
10
+ import { HttpMethod } from "../../services/ApiRequest/HttpMethod";
11
+ import ApiResponseError from "../../services/ApiRequest/ApiResponseError";
12
+ class ApiKeys {
13
+ constructor(apiService) {
14
+ this.api = apiService;
15
+ this.endpoint = "/api-keys/";
16
+ }
17
+ isValidKey(apikey) {
18
+ return __awaiter(this, void 0, void 0, function* () {
19
+ try {
20
+ const key = yield this.authenticateKey(apikey);
21
+ return key.key !== undefined;
22
+ }
23
+ catch (error) {
24
+ if (error instanceof ApiResponseError && error.statusCode === 404) {
25
+ return false;
26
+ }
27
+ throw error;
28
+ }
29
+ });
30
+ }
31
+ authenticateKey(apikey) {
32
+ return __awaiter(this, void 0, void 0, function* () {
33
+ return yield this.api.request(HttpMethod.POST, `/api-keys/auth/${apikey}`);
34
+ });
35
+ }
36
+ getKeys(filter) {
37
+ return __awaiter(this, void 0, void 0, function* () {
38
+ const endpoint = this.getKeysFilterEndpoint(filter);
39
+ return yield this.api.request(HttpMethod.GET, endpoint);
40
+ });
41
+ }
42
+ getKey(apikey) {
43
+ return __awaiter(this, void 0, void 0, function* () {
44
+ return yield this.api.request(HttpMethod.GET, `/api-keys/${apikey}`);
45
+ });
46
+ }
47
+ createKey(apiKey) {
48
+ return __awaiter(this, void 0, void 0, function* () {
49
+ return yield this.api.request(HttpMethod.POST, "/api-keys", apiKey);
50
+ });
51
+ }
52
+ updateKey(apiKey, updatedKey) {
53
+ return __awaiter(this, void 0, void 0, function* () {
54
+ return yield this.api.request(HttpMethod.PATCH, `/api-keys/${apiKey}`, updatedKey);
55
+ });
56
+ }
57
+ deleteKey(apiKey) {
58
+ return __awaiter(this, void 0, void 0, function* () {
59
+ return yield this.api.request(HttpMethod.DELETE, `/api-keys/${apiKey}`);
60
+ });
61
+ }
62
+ reactivateKey(apiKey) {
63
+ return __awaiter(this, void 0, void 0, function* () {
64
+ return yield this.api.request(HttpMethod.PATCH, `/api-keys/${apiKey}/reactivate`);
65
+ });
66
+ }
67
+ rotateKey(apiKey) {
68
+ return __awaiter(this, void 0, void 0, function* () {
69
+ return yield this.api.request(HttpMethod.POST, `/api-keys/${apiKey}/rotate`);
70
+ });
71
+ }
72
+ getKeysFilterEndpoint(filter) {
73
+ let filters = [];
74
+ if (filter !== undefined) {
75
+ filters = Object.entries(filter).map(([key, value]) => `${key}=${value}`);
76
+ }
77
+ return `${this.endpoint}${filter !== undefined ? "?" : ""}${filters.join("&")}`;
78
+ }
79
+ }
80
+ export default ApiKeys;
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,42 @@
1
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
2
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
3
+ return new (P || (P = Promise))(function (resolve, reject) {
4
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
5
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
6
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
7
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
8
+ });
9
+ };
10
+ import { HttpMethod } from "../../services/ApiRequest/HttpMethod";
11
+ class Projects {
12
+ constructor(apiService) {
13
+ this.api = apiService;
14
+ this.endpoint = "/projects";
15
+ }
16
+ getProjects(accountId) {
17
+ return __awaiter(this, void 0, void 0, function* () {
18
+ return yield this.api.request(HttpMethod.GET, `${this.endpoint}?accountId=${accountId}`);
19
+ });
20
+ }
21
+ getProject(projectId) {
22
+ return __awaiter(this, void 0, void 0, function* () {
23
+ return yield this.api.request(HttpMethod.GET, `${this.endpoint}/${projectId}`);
24
+ });
25
+ }
26
+ deleteProject(projectId) {
27
+ return __awaiter(this, void 0, void 0, function* () {
28
+ return yield this.api.request(HttpMethod.DELETE, `${this.endpoint}/${projectId}`);
29
+ });
30
+ }
31
+ createProject(project) {
32
+ return __awaiter(this, void 0, void 0, function* () {
33
+ return yield this.api.request(HttpMethod.POST, this.endpoint, project);
34
+ });
35
+ }
36
+ updateProject(projectId, project) {
37
+ return __awaiter(this, void 0, void 0, function* () {
38
+ return this.api.request(HttpMethod.PATCH, `${this.endpoint}/${projectId}`, project);
39
+ });
40
+ }
41
+ }
42
+ export default Projects;
@@ -0,0 +1 @@
1
+ export {};
package/dist/index.js ADDED
@@ -0,0 +1,69 @@
1
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
2
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
3
+ return new (P || (P = Promise))(function (resolve, reject) {
4
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
5
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
6
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
7
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
8
+ });
9
+ };
10
+ import assert from "assert";
11
+ import removeSlash from "remove-trailing-slash";
12
+ import ApiRequest from "./services/ApiRequest/ApiRequest";
13
+ import ApiKeys from "./endpoints/ApiKeys/ApiKeys";
14
+ import { HttpMethod } from "./services/ApiRequest/HttpMethod";
15
+ import Projects from "./endpoints/Projects/Projects";
16
+ import Accounts from "./endpoints/Accounts/Accounts";
17
+ // eslint-disable-next-line @typescript-eslint/no-empty-function
18
+ const noop = () => { };
19
+ class TheAuthAPI {
20
+ /**
21
+ * @param {String} accessKey
22
+ * @param {Object} [options] (optional)
23
+ * @property {String} host (default: 'https://api.segment.io')
24
+ * @property {number} retryCount (default: 3)
25
+ */
26
+ constructor(accessKey, options) {
27
+ var _a;
28
+ assert(accessKey, "You must pass your project's write key.");
29
+ this.accessKey = accessKey;
30
+ this.host = removeSlash((options === null || options === void 0 ? void 0 : options.host) || "https://api.theauthapi.com");
31
+ this.api = new ApiRequest({
32
+ accessKey: this.accessKey,
33
+ host: this.host,
34
+ retryCount: (_a = options === null || options === void 0 ? void 0 : options.retryCount) !== null && _a !== void 0 ? _a : 3,
35
+ });
36
+ this.apiKeys = new ApiKeys(this.api);
37
+ this.projects = new Projects(this.api);
38
+ this.accounts = new Accounts(this.api);
39
+ }
40
+ /*
41
+ @deprecated
42
+ */
43
+ authenticateAPIKey(key, callback) {
44
+ return __awaiter(this, void 0, void 0, function* () {
45
+ const cb = callback || noop;
46
+ const done = (err) => {
47
+ cb(err, data);
48
+ };
49
+ const data = {
50
+ credentials: { api_key: key },
51
+ timestamp: new Date().getTime(),
52
+ sentAt: new Date().getTime(),
53
+ };
54
+ try {
55
+ const key = yield this.api.request(HttpMethod.POST, "/auth/authenticate", data);
56
+ done(key);
57
+ return key;
58
+ }
59
+ catch (err) {
60
+ if (err.response) {
61
+ const error = new Error(err.response.statusText);
62
+ return done(error);
63
+ }
64
+ done(err);
65
+ }
66
+ });
67
+ }
68
+ }
69
+ export default TheAuthAPI;
@@ -0,0 +1 @@
1
+ export const version = "1.0.11";
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,110 @@
1
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
2
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
3
+ return new (P || (P = Promise))(function (resolve, reject) {
4
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
5
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
6
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
7
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
8
+ });
9
+ };
10
+ import axios from "axios";
11
+ import { version } from "../../libraryMeta";
12
+ import ApiRequestError from "./ApiRequestError";
13
+ import axiosRetry from "axios-retry";
14
+ import ApiResponseError from "./ApiResponseError";
15
+ class ApiRequest {
16
+ constructor(config) {
17
+ const { host, accessKey, headers, retryCount } = config;
18
+ this.host = host;
19
+ this.accessKey = accessKey;
20
+ this.headers = this._generateDefaultHeaders();
21
+ this.retryCount = retryCount !== null && retryCount !== void 0 ? retryCount : 3;
22
+ if (headers) {
23
+ this.headers = Object.assign(Object.assign({}, this.headers), { headers });
24
+ }
25
+ this._init();
26
+ }
27
+ _init() {
28
+ var _a;
29
+ const isoDateFormat = /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(?:\.\d*)?(?:[-+]\d{2}:?\d{2}|Z)?$/;
30
+ function isIsoDateString(value) {
31
+ return value && typeof value === "string" && isoDateFormat.test(value);
32
+ }
33
+ function handleDates(body) {
34
+ if (body === null || body === undefined || typeof body !== "object") {
35
+ return body;
36
+ }
37
+ for (const key of Object.keys(body)) {
38
+ const value = body[key];
39
+ if (isIsoDateString(value)) {
40
+ body[key] = new Date(value);
41
+ }
42
+ else if (typeof value === "object") {
43
+ handleDates(value);
44
+ }
45
+ }
46
+ }
47
+ axios.interceptors.response.use((response) => {
48
+ handleDates(response.data);
49
+ return response;
50
+ });
51
+ axiosRetry(axios, {
52
+ retries: (_a = this.retryCount) !== null && _a !== void 0 ? _a : 3,
53
+ retryCondition: this._isErrorRetryable,
54
+ retryDelay: axiosRetry.exponentialDelay,
55
+ });
56
+ }
57
+ request(method, endpoint, payload) {
58
+ var _a;
59
+ return __awaiter(this, void 0, void 0, function* () {
60
+ try {
61
+ const response = yield axios.request({
62
+ baseURL: this.host,
63
+ method: method,
64
+ url: endpoint,
65
+ data: payload,
66
+ headers: this.headers,
67
+ });
68
+ return response.data;
69
+ }
70
+ catch (error) {
71
+ if (axios.isAxiosError(error)) {
72
+ if (error.response) {
73
+ throw new ApiResponseError(error.response.status, (_a = error.response.data.message) !== null && _a !== void 0 ? _a : error.response.statusText);
74
+ }
75
+ else if (error.request) {
76
+ throw new ApiRequestError(error.message);
77
+ }
78
+ }
79
+ throw error;
80
+ }
81
+ });
82
+ }
83
+ _generateDefaultHeaders() {
84
+ return {
85
+ "user-agent": `theauthapi-client-node/${version}`,
86
+ "x-api-key": this.accessKey,
87
+ "api-key": this.accessKey,
88
+ };
89
+ }
90
+ _isErrorRetryable(error) {
91
+ // Retry Network Errors.
92
+ if (axiosRetry.isNetworkError(error)) {
93
+ return true;
94
+ }
95
+ if (!error.response) {
96
+ // Cannot determine if the request can be retried
97
+ return false;
98
+ }
99
+ // Retry Server Errors (5xx).
100
+ if (error.response.status >= 500 && error.response.status <= 599) {
101
+ return true;
102
+ }
103
+ // Retry if rate limited.
104
+ if (error.response.status === 429) {
105
+ return true;
106
+ }
107
+ return false;
108
+ }
109
+ }
110
+ export default ApiRequest;
@@ -0,0 +1,13 @@
1
+ /**
2
+ *
3
+ * Throws when no response was received from the server
4
+ * @param message - error message
5
+ *
6
+ * */
7
+ class ApiRequestError extends Error {
8
+ constructor(message) {
9
+ super(message);
10
+ this.name = 'ApiRequestError';
11
+ }
12
+ }
13
+ export default ApiRequestError;
@@ -0,0 +1,15 @@
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
+ class ApiResponseError extends Error {
9
+ constructor(statusCode, message) {
10
+ super(`(${statusCode}): ${message}`);
11
+ this.statusCode = statusCode;
12
+ this.name = "ApiResponseError";
13
+ }
14
+ }
15
+ export default ApiResponseError;
@@ -0,0 +1,8 @@
1
+ export var HttpMethod;
2
+ (function (HttpMethod) {
3
+ HttpMethod["GET"] = "GET";
4
+ HttpMethod["POST"] = "POST";
5
+ HttpMethod["DELETE"] = "DELETE";
6
+ HttpMethod["PATCH"] = "PATCH";
7
+ HttpMethod["PUT"] = "PUT";
8
+ })(HttpMethod || (HttpMethod = {}));
@@ -0,0 +1,10 @@
1
+ export var AuthedEntityType;
2
+ (function (AuthedEntityType) {
3
+ AuthedEntityType["USER"] = "USER";
4
+ AuthedEntityType["ACCESS_KEY"] = "ACCESS_KEY";
5
+ })(AuthedEntityType || (AuthedEntityType = {}));
6
+ export var Environment;
7
+ (function (Environment) {
8
+ Environment["LIVE"] = "live";
9
+ Environment["TEST"] = "test";
10
+ })(Environment || (Environment = {}));
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "theauthapi",
3
- "version": "1.0.13",
3
+ "version": "1.0.15",
4
4
  "description": "Client library for TheAuthAPI.com",
5
5
  "main": "dist/index.cjs",
6
6
  "module": "dist/index.mjs",
@@ -17,7 +17,10 @@
17
17
  "test": "jest --runInBand",
18
18
  "test:coverage": "jest --coverage --runInBand",
19
19
  "build": "rollup -c",
20
- "prepublish": "npm run build"
20
+ "ci": "npm run build && npm run test",
21
+ "changeset": "npx changeset",
22
+ "prepublishOnly": "npm run ci",
23
+ "local-release": "changeset version && changeset publish"
21
24
  },
22
25
  "repository": {
23
26
  "type": "git",
@@ -36,6 +39,7 @@
36
39
  "remove-trailing-slash": "^0.1.1"
37
40
  },
38
41
  "devDependencies": {
42
+ "@changesets/cli": "^2.28.1",
39
43
  "@rollup/plugin-commonjs": "^28.0.3",
40
44
  "@rollup/plugin-node-resolve": "^16.0.1",
41
45
  "@rollup/plugin-typescript": "^12.1.2",