@trayio/cdk-cli-commons 4.87.0
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.txt +22 -0
- package/dist/utils/check-env.d.ts +3 -0
- package/dist/utils/check-env.d.ts.map +1 -0
- package/dist/utils/check-env.js +41 -0
- package/dist/utils/check-env.unit.test.d.ts +2 -0
- package/dist/utils/check-env.unit.test.d.ts.map +1 -0
- package/dist/utils/check-env.unit.test.js +62 -0
- package/dist/utils/colorizeString.d.ts +5 -0
- package/dist/utils/colorizeString.d.ts.map +1 -0
- package/dist/utils/colorizeString.js +15 -0
- package/dist/utils/region.d.ts +20 -0
- package/dist/utils/region.d.ts.map +1 -0
- package/dist/utils/region.js +43 -0
- package/dist/utils/region.unit.test.d.ts +2 -0
- package/dist/utils/region.unit.test.d.ts.map +1 -0
- package/dist/utils/region.unit.test.js +50 -0
- package/package.json +35 -0
package/LICENSE.txt
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2023 Tray.io, Inc
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
|
6
|
+
a copy of this software and associated documentation files (the
|
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
|
11
|
+
the following conditions:
|
|
12
|
+
|
|
13
|
+
The above copyright notice and this permission notice shall be
|
|
14
|
+
included in all copies or substantial portions of the Software.
|
|
15
|
+
|
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"check-env.d.ts","sourceRoot":"","sources":["../../src/utils/check-env.ts"],"names":[],"mappings":"AAAA,eAAO,MAAM,aAAa,YAGzB,CAAC;AAEF,eAAO,MAAM,cAAc,YAG1B,CAAC"}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.checkApiUrlEnv = exports.checkTokenEnv = void 0;
|
|
4
|
+
const checkTokenEnv = () => {
|
|
5
|
+
checkMissingTokenEnv();
|
|
6
|
+
isValidApiToken();
|
|
7
|
+
};
|
|
8
|
+
exports.checkTokenEnv = checkTokenEnv;
|
|
9
|
+
const checkApiUrlEnv = () => {
|
|
10
|
+
checkMissingApiUrlEnv();
|
|
11
|
+
isApiUrlValid();
|
|
12
|
+
};
|
|
13
|
+
exports.checkApiUrlEnv = checkApiUrlEnv;
|
|
14
|
+
const checkMissingTokenEnv = () => {
|
|
15
|
+
if (!process.env.TRAY_API_TOKEN) {
|
|
16
|
+
throw new Error('required env TRAY_API_TOKEN is not set');
|
|
17
|
+
}
|
|
18
|
+
};
|
|
19
|
+
const isValidApiToken = () => {
|
|
20
|
+
let apiToken = process.env.TRAY_API_TOKEN;
|
|
21
|
+
if (apiToken.includes('Bearer ') || apiToken.includes('bearer ')) {
|
|
22
|
+
apiToken = apiToken.replace(/Bearer |bearer /, '');
|
|
23
|
+
process.env.TRAY_API_TOKEN = apiToken;
|
|
24
|
+
}
|
|
25
|
+
const validApiTokenRegex = /^.{36,}$/; // Token length is atleast 36 characters
|
|
26
|
+
if (!validApiTokenRegex.test(apiToken)) {
|
|
27
|
+
throw new Error(`TRAY_API_TOKEN is in a invalid format.`);
|
|
28
|
+
}
|
|
29
|
+
};
|
|
30
|
+
const checkMissingApiUrlEnv = () => {
|
|
31
|
+
if (!process.env.TRAY_API_URL) {
|
|
32
|
+
throw new Error('required env TRAY_API_URL is not set, or use the --us, --eu, or --ap region flags');
|
|
33
|
+
}
|
|
34
|
+
};
|
|
35
|
+
const isApiUrlValid = () => {
|
|
36
|
+
const apiUrl = process.env.TRAY_API_URL;
|
|
37
|
+
const validApiUrlRegex = /^https:\/\/api\.(?:ap1\.|eu1\.|staging\.)?tray\.io$/;
|
|
38
|
+
if (!validApiUrlRegex.test(apiUrl)) {
|
|
39
|
+
throw new Error(`TRAY_API_URL is invalid. It should be one of the following: https://api.tray.io, https://api.ap1.tray.io, https://api.eu1.tray.io. Or use the --us, --eu, or --ap region flags`);
|
|
40
|
+
}
|
|
41
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"check-env.unit.test.d.ts","sourceRoot":"","sources":["../../src/utils/check-env.unit.test.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const check_env_1 = require("./check-env");
|
|
4
|
+
describe('checkTokenEnv', () => {
|
|
5
|
+
const subject = check_env_1.checkTokenEnv;
|
|
6
|
+
const { env } = process;
|
|
7
|
+
afterEach(() => {
|
|
8
|
+
process.env = env;
|
|
9
|
+
});
|
|
10
|
+
it('should throw an error if TRAY_API_TOKEN is not set', () => {
|
|
11
|
+
process.env = {
|
|
12
|
+
TRAY_API_URL: 'https://api.tray.io',
|
|
13
|
+
TRAY_API_TOKEN: undefined,
|
|
14
|
+
};
|
|
15
|
+
expect(() => subject()).toThrowError('required env TRAY_API_TOKEN is not set');
|
|
16
|
+
});
|
|
17
|
+
it('should remove bearer prefix if TRAY_API_TOKEN contains Bearer prefix', () => {
|
|
18
|
+
process.env = {
|
|
19
|
+
TRAY_API_URL: 'https://api.tray.io',
|
|
20
|
+
TRAY_API_TOKEN: 'Bearer 1234567890123456789012345678901234567890123456789012345678901234',
|
|
21
|
+
};
|
|
22
|
+
subject();
|
|
23
|
+
expect(process.env.TRAY_API_TOKEN).toEqual('1234567890123456789012345678901234567890123456789012345678901234');
|
|
24
|
+
});
|
|
25
|
+
it('should throw an error if TRAY_API_TOKEN is in an invalid format', () => {
|
|
26
|
+
process.env = {
|
|
27
|
+
TRAY_API_URL: 'https://api.tray.io',
|
|
28
|
+
TRAY_API_TOKEN: '123',
|
|
29
|
+
};
|
|
30
|
+
expect(() => subject()).toThrowError('TRAY_API_TOKEN is in a invalid format.');
|
|
31
|
+
});
|
|
32
|
+
it('should not throw an error if TRAY_API_TOKEN is valid', () => {
|
|
33
|
+
process.env = {
|
|
34
|
+
TRAY_API_TOKEN: '1234567890123456789012345678901234567890123456789012345678901234',
|
|
35
|
+
};
|
|
36
|
+
expect(() => subject()).not.toThrowError();
|
|
37
|
+
});
|
|
38
|
+
});
|
|
39
|
+
describe('checkApiUrlEnv', () => {
|
|
40
|
+
const subject = check_env_1.checkApiUrlEnv;
|
|
41
|
+
const { env } = process;
|
|
42
|
+
afterEach(() => {
|
|
43
|
+
process.env = env;
|
|
44
|
+
});
|
|
45
|
+
it('should throw an error if TRAY_API_URL is not set', () => {
|
|
46
|
+
process.env = { TRAY_API_URL: undefined };
|
|
47
|
+
expect(() => subject()).toThrowError('required env TRAY_API_URL is not set');
|
|
48
|
+
});
|
|
49
|
+
it('should throw an error if TRAY_API_URL is invalid', () => {
|
|
50
|
+
process.env = {
|
|
51
|
+
TRAY_API_URL: 'https://api.tray.io/invalid',
|
|
52
|
+
TRAY_API_TOKEN: 'random',
|
|
53
|
+
};
|
|
54
|
+
expect(() => subject()).toThrowError('TRAY_API_URL is invalid. It should be one of the following: https://api.tray.io, https://api.ap1.tray.io, https://api.eu1.tray.io');
|
|
55
|
+
});
|
|
56
|
+
it('should not throw an error if TRAY_API_URL is valid', () => {
|
|
57
|
+
process.env = {
|
|
58
|
+
TRAY_API_URL: 'https://api.tray.io',
|
|
59
|
+
};
|
|
60
|
+
expect(() => subject()).not.toThrowError();
|
|
61
|
+
});
|
|
62
|
+
});
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
export declare const success: (message: string) => string;
|
|
2
|
+
export declare const error: (message: string) => string;
|
|
3
|
+
export declare const warning: (message: string) => string;
|
|
4
|
+
export declare const info: (message: string) => string;
|
|
5
|
+
//# sourceMappingURL=colorizeString.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"colorizeString.d.ts","sourceRoot":"","sources":["../../src/utils/colorizeString.ts"],"names":[],"mappings":"AAEA,eAAO,MAAM,OAAO,YAAa,MAAM,WAAqC,CAAC;AAE7E,eAAO,MAAM,KAAK,YAAa,MAAM,WAAmC,CAAC;AAEzE,eAAO,MAAM,OAAO,YAAa,MAAM,WAAsC,CAAC;AAE9E,eAAO,MAAM,IAAI,YAAa,MAAM,WAAoC,CAAC"}
|
|
@@ -0,0 +1,15 @@
|
|
|
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.info = exports.warning = exports.error = exports.success = void 0;
|
|
7
|
+
const chalk_1 = __importDefault(require("chalk"));
|
|
8
|
+
const success = (message) => chalk_1.default.bold(chalk_1.default.green(message));
|
|
9
|
+
exports.success = success;
|
|
10
|
+
const error = (message) => chalk_1.default.bold(chalk_1.default.red(message));
|
|
11
|
+
exports.error = error;
|
|
12
|
+
const warning = (message) => chalk_1.default.bold(chalk_1.default.yellow(message));
|
|
13
|
+
exports.warning = warning;
|
|
14
|
+
const info = (message) => chalk_1.default.bold(chalk_1.default.blue(message));
|
|
15
|
+
exports.info = info;
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { ParserInput } from '@oclif/core/lib/interfaces/parser';
|
|
2
|
+
import { HttpClient } from '@trayio/commons/src/http/HttpClient';
|
|
3
|
+
import { HttpConfig } from '@trayio/tray-client/src/base/BaseClient';
|
|
4
|
+
export declare const getBaseUrl: (flags: ParserInput['flags']) => string;
|
|
5
|
+
export declare const regionFlags: {
|
|
6
|
+
us: import("@oclif/core/lib/interfaces/parser").BooleanFlag<boolean>;
|
|
7
|
+
eu: import("@oclif/core/lib/interfaces/parser").BooleanFlag<boolean>;
|
|
8
|
+
ap: import("@oclif/core/lib/interfaces/parser").BooleanFlag<boolean>;
|
|
9
|
+
};
|
|
10
|
+
interface HttpImplementation<T> {
|
|
11
|
+
new (httpConfig: HttpConfig, httpClient: HttpClient): T;
|
|
12
|
+
}
|
|
13
|
+
export declare class RegionHandler<T> {
|
|
14
|
+
private readonly HttpImplementation;
|
|
15
|
+
private readonly httpClient;
|
|
16
|
+
constructor(HttpImplementation: HttpImplementation<T>, httpClient: HttpClient);
|
|
17
|
+
invoke(flags: ParserInput['flags']): T;
|
|
18
|
+
}
|
|
19
|
+
export {};
|
|
20
|
+
//# sourceMappingURL=region.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"region.d.ts","sourceRoot":"","sources":["../../src/utils/region.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,WAAW,EAAE,MAAM,mCAAmC,CAAC;AAChE,OAAO,EAAE,UAAU,EAAE,MAAM,qCAAqC,CAAC;AACjE,OAAO,EAAE,UAAU,EAAE,MAAM,yCAAyC,CAAC;AAGrE,eAAO,MAAM,UAAU,UAAW,WAAW,CAAC,OAAO,CAAC,KAAG,MAYxD,CAAC;AAEF,eAAO,MAAM,WAAW;;;;CAUvB,CAAC;AAEF,UAAU,kBAAkB,CAAC,CAAC;IAC7B,KAAK,UAAU,EAAE,UAAU,EAAE,UAAU,EAAE,UAAU,GAAG,CAAC,CAAC;CACxD;AAED,qBAAa,aAAa,CAAC,CAAC;IAE1B,OAAO,CAAC,QAAQ,CAAC,kBAAkB;IACnC,OAAO,CAAC,QAAQ,CAAC,UAAU;gBADV,kBAAkB,EAAE,kBAAkB,CAAC,CAAC,CAAC,EACzC,UAAU,EAAE,UAAU;IAGjC,MAAM,CAAC,KAAK,EAAE,WAAW,CAAC,OAAO,CAAC;CAIzC"}
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.RegionHandler = exports.regionFlags = exports.getBaseUrl = void 0;
|
|
4
|
+
const core_1 = require("@oclif/core");
|
|
5
|
+
const check_env_1 = require("./check-env");
|
|
6
|
+
const getBaseUrl = (flags) => {
|
|
7
|
+
if (flags.us) {
|
|
8
|
+
return 'https://api.tray.io';
|
|
9
|
+
}
|
|
10
|
+
if (flags.eu) {
|
|
11
|
+
return 'https://api.eu1.tray.io';
|
|
12
|
+
}
|
|
13
|
+
if (flags.ap) {
|
|
14
|
+
return 'https://api.ap1.tray.io';
|
|
15
|
+
}
|
|
16
|
+
(0, check_env_1.checkApiUrlEnv)();
|
|
17
|
+
return process.env.TRAY_API_URL;
|
|
18
|
+
};
|
|
19
|
+
exports.getBaseUrl = getBaseUrl;
|
|
20
|
+
exports.regionFlags = {
|
|
21
|
+
us: core_1.Flags.boolean({
|
|
22
|
+
description: 'Use the Tray US region',
|
|
23
|
+
}),
|
|
24
|
+
eu: core_1.Flags.boolean({
|
|
25
|
+
description: 'Use the Tray EU region',
|
|
26
|
+
}),
|
|
27
|
+
ap: core_1.Flags.boolean({
|
|
28
|
+
description: 'Use the Tray APAC region',
|
|
29
|
+
}),
|
|
30
|
+
};
|
|
31
|
+
class RegionHandler {
|
|
32
|
+
HttpImplementation;
|
|
33
|
+
httpClient;
|
|
34
|
+
constructor(HttpImplementation, httpClient) {
|
|
35
|
+
this.HttpImplementation = HttpImplementation;
|
|
36
|
+
this.httpClient = httpClient;
|
|
37
|
+
}
|
|
38
|
+
invoke(flags) {
|
|
39
|
+
const baseUrl = (0, exports.getBaseUrl)(flags);
|
|
40
|
+
return new this.HttpImplementation({ baseUrl }, this.httpClient);
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
exports.RegionHandler = RegionHandler;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"region.unit.test.d.ts","sourceRoot":"","sources":["../../src/utils/region.unit.test.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const AxiosHttpClient_1 = require("@trayio/axios/http/AxiosHttpClient");
|
|
4
|
+
const region_1 = require("./region");
|
|
5
|
+
class HttpImplementationStub {
|
|
6
|
+
httpConfig;
|
|
7
|
+
httpClient;
|
|
8
|
+
constructor(httpConfig, httpClient) {
|
|
9
|
+
this.httpConfig = httpConfig;
|
|
10
|
+
this.httpClient = httpClient;
|
|
11
|
+
}
|
|
12
|
+
get() {
|
|
13
|
+
return this.httpConfig.baseUrl;
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
describe('RegionHandler', () => {
|
|
17
|
+
it('should use TRAY_API_URL env var when passed no flag', () => {
|
|
18
|
+
process.env = {
|
|
19
|
+
TRAY_API_URL: 'https://api.tray.io',
|
|
20
|
+
};
|
|
21
|
+
const regionHandler = new region_1.RegionHandler(HttpImplementationStub, new AxiosHttpClient_1.AxiosHttpClient());
|
|
22
|
+
const result = regionHandler.invoke({}).get();
|
|
23
|
+
expect(result).toEqual('https://api.tray.io');
|
|
24
|
+
});
|
|
25
|
+
it('should throw an error when passed no flag and TRAY_API_URL env var is not set', () => {
|
|
26
|
+
process.env = {
|
|
27
|
+
TRAY_API_URL: '',
|
|
28
|
+
};
|
|
29
|
+
const regionHandler = new region_1.RegionHandler(HttpImplementationStub, new AxiosHttpClient_1.AxiosHttpClient());
|
|
30
|
+
expect(() => regionHandler.invoke({}).get()).toThrowError('required env TRAY_API_URL is not set, or use the --us, --eu, or --ap region flags');
|
|
31
|
+
});
|
|
32
|
+
it('should use US baseUrl when passed us flag', () => {
|
|
33
|
+
const regionHandler = new region_1.RegionHandler(HttpImplementationStub, new AxiosHttpClient_1.AxiosHttpClient());
|
|
34
|
+
const flags = { us: true };
|
|
35
|
+
const result = regionHandler.invoke(flags).get();
|
|
36
|
+
expect(result).toEqual('https://api.tray.io');
|
|
37
|
+
});
|
|
38
|
+
it('should use EU baseUrl when passed eu flag', () => {
|
|
39
|
+
const regionHandler = new region_1.RegionHandler(HttpImplementationStub, new AxiosHttpClient_1.AxiosHttpClient());
|
|
40
|
+
const flags = { eu: true };
|
|
41
|
+
const result = regionHandler.invoke(flags).get();
|
|
42
|
+
expect(result).toEqual('https://api.eu1.tray.io');
|
|
43
|
+
});
|
|
44
|
+
it('should use AP baseUrl when passed ap flag', () => {
|
|
45
|
+
const regionHandler = new region_1.RegionHandler(HttpImplementationStub, new AxiosHttpClient_1.AxiosHttpClient());
|
|
46
|
+
const flags = { ap: true };
|
|
47
|
+
const result = regionHandler.invoke(flags).get();
|
|
48
|
+
expect(result).toEqual('https://api.ap1.tray.io');
|
|
49
|
+
});
|
|
50
|
+
});
|
package/package.json
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@trayio/cdk-cli-commons",
|
|
3
|
+
"version": "4.87.0",
|
|
4
|
+
"description": "Shared utils between CDK ClIs",
|
|
5
|
+
"exports": {
|
|
6
|
+
"./*": "./dist/*.js"
|
|
7
|
+
},
|
|
8
|
+
"author": "Tray.io",
|
|
9
|
+
"license": "MIT",
|
|
10
|
+
"engines": {
|
|
11
|
+
"node": ">=18.x"
|
|
12
|
+
},
|
|
13
|
+
"publishConfig": {
|
|
14
|
+
"access": "public"
|
|
15
|
+
},
|
|
16
|
+
"dependencies": {
|
|
17
|
+
"@oclif/core": "3.18.1",
|
|
18
|
+
"@oclif/test": "3.1.12",
|
|
19
|
+
"@trayio/axios": "4.87.0",
|
|
20
|
+
"@trayio/commons": "4.87.0",
|
|
21
|
+
"@trayio/tray-client": "4.87.0",
|
|
22
|
+
"chalk": "4.1.2"
|
|
23
|
+
},
|
|
24
|
+
"typesVersions": {
|
|
25
|
+
"*": {
|
|
26
|
+
"*": [
|
|
27
|
+
"*",
|
|
28
|
+
"dist/*"
|
|
29
|
+
]
|
|
30
|
+
}
|
|
31
|
+
},
|
|
32
|
+
"files": [
|
|
33
|
+
"/dist"
|
|
34
|
+
]
|
|
35
|
+
}
|