@contentstack/cli-utilities 0.1.1-beta.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.
- package/.eslintignore +2 -0
- package/.eslintrc +40 -0
- package/.mocharc.json +12 -0
- package/.nycrc.json +5 -0
- package/LICENSE +21 -0
- package/lib/cli-error.js +16 -0
- package/lib/cli-ux.js +59 -0
- package/lib/config-handler.js +128 -0
- package/lib/flag-deprecation-check.js +40 -0
- package/lib/http-client/client.js +313 -0
- package/lib/http-client/http-response.js +46 -0
- package/lib/http-client/index.js +7 -0
- package/lib/index.js +17 -0
- package/lib/interfaces/index.js +2 -0
- package/lib/logger.js +92 -0
- package/lib/message-handler.js +43 -0
- package/lib/selectors/index.js +376 -0
- package/lib/selectors/interfaces.js +2 -0
- package/lib/selectors/validations.js +9 -0
- package/messages/auth.json +46 -0
- package/messages/config.json +9 -0
- package/messages/core.json +6 -0
- package/package.json +68 -0
- package/src/cli-error.ts +15 -0
- package/src/cli-ux.ts +78 -0
- package/src/config-handler.ts +142 -0
- package/src/flag-deprecation-check.ts +41 -0
- package/src/http-client/client.ts +369 -0
- package/src/http-client/http-response.ts +55 -0
- package/src/http-client/index.ts +7 -0
- package/src/index.ts +7 -0
- package/src/interfaces/index.ts +61 -0
- package/src/logger.ts +101 -0
- package/src/message-handler.ts +45 -0
- package/src/selectors/index.ts +381 -0
- package/src/selectors/interfaces.ts +39 -0
- package/src/selectors/validations.ts +5 -0
- package/test/helpers/init.js +4 -0
- package/test/tsconfig.json +6 -0
- package/tsconfig.json +20 -0
- package/types/cli-error.d.ts +4 -0
- package/types/cli-ux.d.ts +22 -0
- package/types/config-handler.d.ts +17 -0
- package/types/flag-deprecation-check.d.ts +7 -0
- package/types/http-client.d.ts +54 -0
- package/types/index.d.ts +7 -0
- package/types/interfaces/index.d.ts +50 -0
- package/types/logger.d.ts +17 -0
- package/types/message-handler.d.ts +12 -0
- package/types/selectors/index.d.ts +12 -0
- package/types/selectors/interfaces.d.ts +32 -0
- package/types/selectors/validations.d.ts +1 -0
package/.eslintignore
ADDED
package/.eslintrc
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
{
|
|
2
|
+
"env": {
|
|
3
|
+
"node": true
|
|
4
|
+
},
|
|
5
|
+
"parser": "@typescript-eslint/parser",
|
|
6
|
+
"parserOptions": {
|
|
7
|
+
"project": "tsconfig.json",
|
|
8
|
+
"sourceType": "module"
|
|
9
|
+
},
|
|
10
|
+
"extends": [
|
|
11
|
+
"plugin:@typescript-eslint/recommended"
|
|
12
|
+
],
|
|
13
|
+
"rules": {
|
|
14
|
+
"@typescript-eslint/no-unused-vars": [
|
|
15
|
+
"error",
|
|
16
|
+
{
|
|
17
|
+
"args": "none"
|
|
18
|
+
}
|
|
19
|
+
],
|
|
20
|
+
"@typescript-eslint/prefer-namespace-keyword": "error",
|
|
21
|
+
"@typescript-eslint/quotes": [
|
|
22
|
+
"error",
|
|
23
|
+
"single",
|
|
24
|
+
{
|
|
25
|
+
"avoidEscape": true,
|
|
26
|
+
"allowTemplateLiterals": true
|
|
27
|
+
}
|
|
28
|
+
],
|
|
29
|
+
"semi": "off",
|
|
30
|
+
"@typescript-eslint/type-annotation-spacing": "error",
|
|
31
|
+
"@typescript-eslint/no-redeclare": "off",
|
|
32
|
+
"eqeqeq": [
|
|
33
|
+
"error",
|
|
34
|
+
"smart"
|
|
35
|
+
],
|
|
36
|
+
"id-match": "error",
|
|
37
|
+
"no-eval": "error",
|
|
38
|
+
"no-var": "error"
|
|
39
|
+
}
|
|
40
|
+
}
|
package/.mocharc.json
ADDED
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2022 Contentstack
|
|
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/lib/cli-error.js
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
class CLIError extends Error {
|
|
4
|
+
constructor(message, suggestions) {
|
|
5
|
+
super(message);
|
|
6
|
+
this.suggestions = suggestions;
|
|
7
|
+
this.message = message;
|
|
8
|
+
if (typeof Error.captureStackTrace === 'function') {
|
|
9
|
+
Error.captureStackTrace(this, this.constructor);
|
|
10
|
+
}
|
|
11
|
+
else {
|
|
12
|
+
this.stack = new Error(message).stack;
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
exports.default = CLIError;
|
package/lib/cli-ux.js
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const tslib_1 = require("tslib");
|
|
4
|
+
const chalk_1 = tslib_1.__importDefault(require("chalk"));
|
|
5
|
+
const inquirer_1 = tslib_1.__importDefault(require("inquirer"));
|
|
6
|
+
const cli_ux_1 = require("@oclif/core/lib/cli-ux");
|
|
7
|
+
const message_handler_1 = tslib_1.__importDefault(require("./message-handler"));
|
|
8
|
+
/**
|
|
9
|
+
* CLI Interface
|
|
10
|
+
*/
|
|
11
|
+
class CLIInterface {
|
|
12
|
+
constructor() {
|
|
13
|
+
this.loading = false;
|
|
14
|
+
}
|
|
15
|
+
get uxTable() {
|
|
16
|
+
return cli_ux_1.ux.table;
|
|
17
|
+
}
|
|
18
|
+
init(context) { }
|
|
19
|
+
print(message, opts) {
|
|
20
|
+
if (opts && opts.color) {
|
|
21
|
+
cli_ux_1.ux.log(chalk_1.default[opts.color](message_handler_1.default.parse(message)));
|
|
22
|
+
return;
|
|
23
|
+
}
|
|
24
|
+
cli_ux_1.ux.log(message_handler_1.default.parse(message));
|
|
25
|
+
}
|
|
26
|
+
success(message) {
|
|
27
|
+
cli_ux_1.ux.log(chalk_1.default.green(message_handler_1.default.parse(message)));
|
|
28
|
+
}
|
|
29
|
+
error(message, ...params) {
|
|
30
|
+
cli_ux_1.ux.log(chalk_1.default.red(message_handler_1.default.parse(message) + (params && params.length > 0 ? ': ' : '')), ...params);
|
|
31
|
+
}
|
|
32
|
+
loader(message = '') {
|
|
33
|
+
if (!this.loading) {
|
|
34
|
+
cli_ux_1.ux.action.start(message_handler_1.default.parse(message));
|
|
35
|
+
}
|
|
36
|
+
else {
|
|
37
|
+
cli_ux_1.ux.action.stop(message_handler_1.default.parse(message));
|
|
38
|
+
}
|
|
39
|
+
this.loading = !this.loading;
|
|
40
|
+
}
|
|
41
|
+
table(data, columns, options) {
|
|
42
|
+
cli_ux_1.ux.table(data, columns, options);
|
|
43
|
+
}
|
|
44
|
+
async inquire(inquirePayload) {
|
|
45
|
+
inquirePayload.message = message_handler_1.default.parse(inquirePayload.message);
|
|
46
|
+
const result = await inquirer_1.default.prompt(inquirePayload);
|
|
47
|
+
return result[inquirePayload.name];
|
|
48
|
+
}
|
|
49
|
+
prompt(name, options) {
|
|
50
|
+
return cli_ux_1.ux.prompt(name, options);
|
|
51
|
+
}
|
|
52
|
+
confirm(message) {
|
|
53
|
+
return cli_ux_1.ux.confirm(message);
|
|
54
|
+
}
|
|
55
|
+
progress(options) {
|
|
56
|
+
return cli_ux_1.ux.progress(options);
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
exports.default = new CLIInterface();
|
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const tslib_1 = require("tslib");
|
|
4
|
+
const conf_1 = tslib_1.__importDefault(require("conf"));
|
|
5
|
+
const uuid_1 = require("uuid");
|
|
6
|
+
const fs_1 = require("fs");
|
|
7
|
+
const ENC_KEY = 'encryptionKey';
|
|
8
|
+
const ENCRYPT_CONF = true;
|
|
9
|
+
const CONFIG_NAME = 'contentstack_cli';
|
|
10
|
+
const ENC_CONFIG_NAME = 'contentstack_cli_obfuscate';
|
|
11
|
+
class Config {
|
|
12
|
+
constructor() {
|
|
13
|
+
this.init();
|
|
14
|
+
}
|
|
15
|
+
init() {
|
|
16
|
+
return ENCRYPT_CONF === true
|
|
17
|
+
? this.getEncryptedConfig()
|
|
18
|
+
: this.getDecryptedConfig();
|
|
19
|
+
}
|
|
20
|
+
fallbackInit() {
|
|
21
|
+
return new conf_1.default({ configName: CONFIG_NAME, encryptionKey: ENC_KEY });
|
|
22
|
+
}
|
|
23
|
+
getObfuscationKey() {
|
|
24
|
+
const obfuscationKeyName = 'obfuscation_key';
|
|
25
|
+
const encConfig = new conf_1.default({ configName: ENC_CONFIG_NAME });
|
|
26
|
+
let obfuscationKey = encConfig.get(obfuscationKeyName);
|
|
27
|
+
if (!obfuscationKey) {
|
|
28
|
+
encConfig.set(obfuscationKeyName, (0, uuid_1.v4)());
|
|
29
|
+
obfuscationKey = encConfig.get(obfuscationKeyName);
|
|
30
|
+
}
|
|
31
|
+
return obfuscationKey;
|
|
32
|
+
}
|
|
33
|
+
getConfigDataAndUnlinkConfigFile(config) {
|
|
34
|
+
let configData;
|
|
35
|
+
if (config === null || config === void 0 ? void 0 : config.path) {
|
|
36
|
+
if ((0, fs_1.existsSync)(config.path)) {
|
|
37
|
+
configData = JSON.parse(JSON.stringify((config === null || config === void 0 ? void 0 : config.store) || {})); // NOTE convert prototype object to plain object
|
|
38
|
+
(0, fs_1.unlinkSync)(config.path); // NOTE remove old config file
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
return configData;
|
|
42
|
+
}
|
|
43
|
+
getEncryptedConfig(configData, skip = false) {
|
|
44
|
+
const getEncryptedDataElseFallBack = () => {
|
|
45
|
+
var _a;
|
|
46
|
+
try {
|
|
47
|
+
// NOTE reading current code base encrypted file if exist
|
|
48
|
+
const encryptionKey = this.getObfuscationKey();
|
|
49
|
+
this.config = new conf_1.default({ configName: CONFIG_NAME, encryptionKey });
|
|
50
|
+
if ((_a = Object.keys(configData || {})) === null || _a === void 0 ? void 0 : _a.length) {
|
|
51
|
+
this.config.set(configData); // NOTE set config data if passed any
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
catch (error) {
|
|
55
|
+
// NOTE reading old code base encrypted file if exist
|
|
56
|
+
try {
|
|
57
|
+
const config = this.fallbackInit();
|
|
58
|
+
const configData = this.getConfigDataAndUnlinkConfigFile(config);
|
|
59
|
+
this.getEncryptedConfig(configData, true);
|
|
60
|
+
}
|
|
61
|
+
catch (error) {
|
|
62
|
+
// console.trace(error.message)
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
};
|
|
66
|
+
try {
|
|
67
|
+
if (skip === false) {
|
|
68
|
+
const config = new conf_1.default({ configName: CONFIG_NAME });
|
|
69
|
+
const configData = this.getConfigDataAndUnlinkConfigFile(config);
|
|
70
|
+
this.getEncryptedConfig(configData, true);
|
|
71
|
+
}
|
|
72
|
+
else {
|
|
73
|
+
getEncryptedDataElseFallBack();
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
catch (error) {
|
|
77
|
+
// console.trace(error.message)
|
|
78
|
+
// NOTE reading current code base encrypted file if exist
|
|
79
|
+
getEncryptedDataElseFallBack();
|
|
80
|
+
}
|
|
81
|
+
return this.config;
|
|
82
|
+
}
|
|
83
|
+
getDecryptedConfig(configData) {
|
|
84
|
+
var _a;
|
|
85
|
+
try {
|
|
86
|
+
this.config = new conf_1.default({ configName: CONFIG_NAME });
|
|
87
|
+
if ((_a = Object.keys(configData || {})) === null || _a === void 0 ? void 0 : _a.length) {
|
|
88
|
+
this.config.set(configData); // NOTE set config data if passed any
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
catch (error) {
|
|
92
|
+
// console.trace(error.message)
|
|
93
|
+
try {
|
|
94
|
+
const encryptionKey = this.getObfuscationKey();
|
|
95
|
+
let config = new conf_1.default({ configName: CONFIG_NAME, encryptionKey });
|
|
96
|
+
const configData = this.getConfigDataAndUnlinkConfigFile(config);
|
|
97
|
+
this.getDecryptedConfig(configData); // NOTE NOTE reinitialize the config with old data and new decrypted file
|
|
98
|
+
}
|
|
99
|
+
catch (error) {
|
|
100
|
+
// console.trace(error.message)
|
|
101
|
+
try {
|
|
102
|
+
const config = this.fallbackInit();
|
|
103
|
+
const configData = this.getConfigDataAndUnlinkConfigFile(config);
|
|
104
|
+
this.getDecryptedConfig(configData); // NOTE reinitialize the config with old data and new decrypted file
|
|
105
|
+
}
|
|
106
|
+
catch (error) {
|
|
107
|
+
// console.trace(error.message)
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
return this.config;
|
|
112
|
+
}
|
|
113
|
+
get(key) {
|
|
114
|
+
return this.config.get(key);
|
|
115
|
+
}
|
|
116
|
+
async set(key, value) {
|
|
117
|
+
this.config.set(key, value);
|
|
118
|
+
return this.config;
|
|
119
|
+
}
|
|
120
|
+
delete(key) {
|
|
121
|
+
this.config.delete(key);
|
|
122
|
+
return this.config;
|
|
123
|
+
}
|
|
124
|
+
clear() {
|
|
125
|
+
this.config.clear();
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
exports.default = new Config();
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const tslib_1 = require("tslib");
|
|
4
|
+
const cli_ux_1 = tslib_1.__importDefault(require("./cli-ux"));
|
|
5
|
+
/**
|
|
6
|
+
* checks the deprecation and prints it
|
|
7
|
+
* @param {Array} deprecatedFlags flags to be deprecated
|
|
8
|
+
* @param {String} customMessage [optional] a custom message
|
|
9
|
+
* @returns flag parser
|
|
10
|
+
*/
|
|
11
|
+
function default_1(deprecatedFlags = [], suggestions = [], customMessage) {
|
|
12
|
+
return (input, command) => {
|
|
13
|
+
const { context: { flagWarningPrintState = {} } = {} } = command;
|
|
14
|
+
let isCommandHasDeprecationFlag = false;
|
|
15
|
+
deprecatedFlags.forEach((item) => {
|
|
16
|
+
if (command.argv.indexOf(item) !== -1) {
|
|
17
|
+
if (flagWarningPrintState[command.id + item]) {
|
|
18
|
+
return input;
|
|
19
|
+
}
|
|
20
|
+
flagWarningPrintState[command.id + item] = true;
|
|
21
|
+
isCommandHasDeprecationFlag = true;
|
|
22
|
+
}
|
|
23
|
+
});
|
|
24
|
+
if (isCommandHasDeprecationFlag) {
|
|
25
|
+
let depreactionMessage = '';
|
|
26
|
+
if (customMessage) {
|
|
27
|
+
depreactionMessage = customMessage;
|
|
28
|
+
}
|
|
29
|
+
else {
|
|
30
|
+
depreactionMessage = `WARNING!!! You're using the old (soon to be deprecated) Contentstack CLI flags (${deprecatedFlags.join(', ')}).`;
|
|
31
|
+
if (suggestions.length > 0) {
|
|
32
|
+
depreactionMessage += ` We recommend you to use the updated flags (${suggestions.join(', ')}).`;
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
cli_ux_1.default.print(depreactionMessage, { color: 'yellow' });
|
|
36
|
+
}
|
|
37
|
+
return input;
|
|
38
|
+
};
|
|
39
|
+
}
|
|
40
|
+
exports.default = default_1;
|
|
@@ -0,0 +1,313 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.HttpClient = void 0;
|
|
4
|
+
const tslib_1 = require("tslib");
|
|
5
|
+
const axios_1 = tslib_1.__importDefault(require("axios"));
|
|
6
|
+
const http_response_1 = require("./http-response");
|
|
7
|
+
class HttpClient {
|
|
8
|
+
/**
|
|
9
|
+
* Createa new pending HTTP request instance.
|
|
10
|
+
*/
|
|
11
|
+
constructor() {
|
|
12
|
+
/**
|
|
13
|
+
* The payload format for a JSON or form-url-encoded request.
|
|
14
|
+
*/
|
|
15
|
+
this.bodyFormat = 'json';
|
|
16
|
+
this.request = {};
|
|
17
|
+
this.axiosInstance = axios_1.default.create();
|
|
18
|
+
// Sets payload format as json by default
|
|
19
|
+
this.asJson();
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* Create a reusable HttpClient instance.
|
|
23
|
+
*
|
|
24
|
+
* @returns {HttpClient}
|
|
25
|
+
*/
|
|
26
|
+
static create() {
|
|
27
|
+
return new this();
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* Returns the Axios request config.
|
|
31
|
+
*
|
|
32
|
+
* @returns {AxiosRequestConfig}
|
|
33
|
+
*/
|
|
34
|
+
requestConfig() {
|
|
35
|
+
return this.request;
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* Resets the request config.
|
|
39
|
+
*
|
|
40
|
+
* @returns {AxiosRequestConfig}
|
|
41
|
+
*/
|
|
42
|
+
resetConfig() {
|
|
43
|
+
this.request = {};
|
|
44
|
+
return this;
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* Use the given `baseUrl` for all requests.
|
|
48
|
+
*
|
|
49
|
+
* @param {String} baseUrl
|
|
50
|
+
*
|
|
51
|
+
* @returns {HttpClient}
|
|
52
|
+
*/
|
|
53
|
+
baseUrl(baseUrl) {
|
|
54
|
+
if (typeof baseUrl !== 'string') {
|
|
55
|
+
throw new Error(`The base URL must be a string. Received "${typeof baseUrl}"`);
|
|
56
|
+
}
|
|
57
|
+
this.request.baseURL = baseUrl;
|
|
58
|
+
return this;
|
|
59
|
+
}
|
|
60
|
+
/**
|
|
61
|
+
* Add request headers.
|
|
62
|
+
* @returns {HttpClient}
|
|
63
|
+
*/
|
|
64
|
+
headers(headers) {
|
|
65
|
+
this.request.headers = Object.assign(Object.assign({}, this.request.headers), headers);
|
|
66
|
+
return this;
|
|
67
|
+
}
|
|
68
|
+
/**
|
|
69
|
+
* Add query parameters to the request.
|
|
70
|
+
*
|
|
71
|
+
* @param {Object} queryParams
|
|
72
|
+
*
|
|
73
|
+
* @returns {HttpClient}
|
|
74
|
+
*/
|
|
75
|
+
queryParams(queryParams) {
|
|
76
|
+
this.request.params = Object.assign(Object.assign({}, this.request.params), queryParams);
|
|
77
|
+
return this;
|
|
78
|
+
}
|
|
79
|
+
/**
|
|
80
|
+
* Add basic authentication via `username` and `password` to the request.
|
|
81
|
+
*
|
|
82
|
+
* @param {String} username
|
|
83
|
+
* @param {String} password
|
|
84
|
+
*
|
|
85
|
+
* @returns {HttpClient}
|
|
86
|
+
*/
|
|
87
|
+
basicAuth(username, password) {
|
|
88
|
+
this.request.auth = { username, password };
|
|
89
|
+
return this;
|
|
90
|
+
}
|
|
91
|
+
/**
|
|
92
|
+
* Add an authorization `token` to the request.
|
|
93
|
+
*
|
|
94
|
+
* @param {String} token
|
|
95
|
+
* @param {String} type
|
|
96
|
+
*
|
|
97
|
+
* @returns {HttpClient}
|
|
98
|
+
*/
|
|
99
|
+
token(token, type = 'Bearer') {
|
|
100
|
+
return this.headers({
|
|
101
|
+
Authorization: `${type} ${token}`.trim(),
|
|
102
|
+
});
|
|
103
|
+
}
|
|
104
|
+
/**
|
|
105
|
+
* Merge your own custom Axios options into the request.
|
|
106
|
+
*
|
|
107
|
+
* @param {Object} options
|
|
108
|
+
*
|
|
109
|
+
* @returns {HttpClient}
|
|
110
|
+
*/
|
|
111
|
+
options(options = {}) {
|
|
112
|
+
Object.assign(this.request, options);
|
|
113
|
+
return this;
|
|
114
|
+
}
|
|
115
|
+
/**
|
|
116
|
+
* Add a request payload.
|
|
117
|
+
*
|
|
118
|
+
* @param {*} data
|
|
119
|
+
*
|
|
120
|
+
* @returns {HttpClient}
|
|
121
|
+
*/
|
|
122
|
+
payload(data) {
|
|
123
|
+
this.request.data = data;
|
|
124
|
+
return this;
|
|
125
|
+
}
|
|
126
|
+
/**
|
|
127
|
+
* Define the request `timeout` in milliseconds.
|
|
128
|
+
*
|
|
129
|
+
* @param {Number} timeout
|
|
130
|
+
*
|
|
131
|
+
* @returns {HttpClient}
|
|
132
|
+
*/
|
|
133
|
+
timeout(timeout) {
|
|
134
|
+
this.request.timeout = timeout;
|
|
135
|
+
return this;
|
|
136
|
+
}
|
|
137
|
+
/**
|
|
138
|
+
* Tell HttpClient to send the request as JSON payload.
|
|
139
|
+
*
|
|
140
|
+
* @returns {HttpClient}
|
|
141
|
+
*/
|
|
142
|
+
asJson() {
|
|
143
|
+
return this.payloadFormat('json').contentType('application/json');
|
|
144
|
+
}
|
|
145
|
+
/**
|
|
146
|
+
* Tell HttpClient to send the request as form parameters,
|
|
147
|
+
* encoded as URL query parameters.
|
|
148
|
+
*
|
|
149
|
+
* @returns {HttpClient}
|
|
150
|
+
*/
|
|
151
|
+
asFormParams() {
|
|
152
|
+
return this.payloadFormat('formParams').contentType('application/x-www-form-urlencoded');
|
|
153
|
+
}
|
|
154
|
+
/**
|
|
155
|
+
* Set the request payload format.
|
|
156
|
+
*
|
|
157
|
+
* @param {String} format
|
|
158
|
+
*
|
|
159
|
+
* @returns {HttpClient}
|
|
160
|
+
*/
|
|
161
|
+
payloadFormat(format) {
|
|
162
|
+
this.bodyFormat = format;
|
|
163
|
+
return this;
|
|
164
|
+
}
|
|
165
|
+
/**
|
|
166
|
+
* Set the `Accept` request header. This indicates what
|
|
167
|
+
* content type the server should return.
|
|
168
|
+
*
|
|
169
|
+
* @param {String} accept
|
|
170
|
+
*
|
|
171
|
+
* @returns {HttpClient}
|
|
172
|
+
*/
|
|
173
|
+
accept(accept) {
|
|
174
|
+
return this.headers({ Accept: accept });
|
|
175
|
+
}
|
|
176
|
+
/**
|
|
177
|
+
* Set the `Accept` request header to JSON. This indicates
|
|
178
|
+
* that the server should return JSON data.
|
|
179
|
+
*
|
|
180
|
+
* @param {String} accept
|
|
181
|
+
*
|
|
182
|
+
* @returns {HttpClient}
|
|
183
|
+
*/
|
|
184
|
+
acceptJson() {
|
|
185
|
+
return this.accept('application/json');
|
|
186
|
+
}
|
|
187
|
+
/**
|
|
188
|
+
* Set the `Content-Type` request header.
|
|
189
|
+
*
|
|
190
|
+
* @param {String} contentType
|
|
191
|
+
*
|
|
192
|
+
* @returns {HttpClient}
|
|
193
|
+
*/
|
|
194
|
+
contentType(contentType) {
|
|
195
|
+
return this.headers({ 'Content-Type': contentType });
|
|
196
|
+
}
|
|
197
|
+
/**
|
|
198
|
+
* Send an HTTP GET request, optionally with the given `queryParams`.
|
|
199
|
+
*
|
|
200
|
+
* @param {String} url
|
|
201
|
+
* @param {Object} queryParams
|
|
202
|
+
*
|
|
203
|
+
* @returns {HttpResponse}
|
|
204
|
+
*
|
|
205
|
+
* @throws
|
|
206
|
+
*/
|
|
207
|
+
async get(url, queryParams = {}) {
|
|
208
|
+
this.queryParams(queryParams);
|
|
209
|
+
return this.send('GET', url);
|
|
210
|
+
}
|
|
211
|
+
/**
|
|
212
|
+
* Send an HTTP POST request, optionally with the given `payload`.
|
|
213
|
+
*
|
|
214
|
+
* @param {String} url
|
|
215
|
+
* @param {Object} payload
|
|
216
|
+
*
|
|
217
|
+
* @returns {HttpResponse}
|
|
218
|
+
*
|
|
219
|
+
* @throws
|
|
220
|
+
*/
|
|
221
|
+
async post(url, payload) {
|
|
222
|
+
if (payload) {
|
|
223
|
+
this.payload(payload);
|
|
224
|
+
}
|
|
225
|
+
return this.send('POST', url);
|
|
226
|
+
}
|
|
227
|
+
/**
|
|
228
|
+
* Send an HTTP PUT request, optionally with the given `payload`.
|
|
229
|
+
*
|
|
230
|
+
* @param {String} url
|
|
231
|
+
* @param {Object} payload
|
|
232
|
+
*
|
|
233
|
+
* @returns {HttpResponse}
|
|
234
|
+
*
|
|
235
|
+
* @throws
|
|
236
|
+
*/
|
|
237
|
+
async put(url, payload) {
|
|
238
|
+
if (payload) {
|
|
239
|
+
this.payload(payload);
|
|
240
|
+
}
|
|
241
|
+
return this.send('PUT', url);
|
|
242
|
+
}
|
|
243
|
+
/**
|
|
244
|
+
* Send an HTTP PATCH request, optionally with the given `payload`.
|
|
245
|
+
*
|
|
246
|
+
* @param {String} url
|
|
247
|
+
* @param {Object} payload
|
|
248
|
+
*
|
|
249
|
+
* @returns {HttpResponse}
|
|
250
|
+
*
|
|
251
|
+
* @throws
|
|
252
|
+
*/
|
|
253
|
+
async patch(url, payload) {
|
|
254
|
+
if (payload) {
|
|
255
|
+
this.payload(payload);
|
|
256
|
+
}
|
|
257
|
+
return this.send('PATCH', url);
|
|
258
|
+
}
|
|
259
|
+
/**
|
|
260
|
+
* Send an HTTP DELETE request, optionally with the given `queryParams`.
|
|
261
|
+
*
|
|
262
|
+
* @param {String} url
|
|
263
|
+
* @param {Object} queryParams
|
|
264
|
+
*
|
|
265
|
+
* @returns {HttpResponse}
|
|
266
|
+
*
|
|
267
|
+
* @throws
|
|
268
|
+
*/
|
|
269
|
+
async delete(url, queryParams = {}) {
|
|
270
|
+
this.queryParams(queryParams);
|
|
271
|
+
return this.send('DELETE', url);
|
|
272
|
+
}
|
|
273
|
+
/**
|
|
274
|
+
* Send the HTTP request.
|
|
275
|
+
*
|
|
276
|
+
* @param {String} method
|
|
277
|
+
* @param {String} url
|
|
278
|
+
*
|
|
279
|
+
* @returns {HttpResponse}
|
|
280
|
+
*
|
|
281
|
+
* @throws
|
|
282
|
+
*/
|
|
283
|
+
async send(method, url) {
|
|
284
|
+
try {
|
|
285
|
+
return new http_response_1.HttpResponse(await this.createAndSendRequest(method, url));
|
|
286
|
+
}
|
|
287
|
+
catch (error) {
|
|
288
|
+
if (error.response) {
|
|
289
|
+
return new http_response_1.HttpResponse(error.response);
|
|
290
|
+
}
|
|
291
|
+
throw error;
|
|
292
|
+
}
|
|
293
|
+
}
|
|
294
|
+
/**
|
|
295
|
+
* Create and send the HTTP request.
|
|
296
|
+
*
|
|
297
|
+
* @param {String} method
|
|
298
|
+
* @param {String} url
|
|
299
|
+
*
|
|
300
|
+
* @returns {Request}
|
|
301
|
+
*/
|
|
302
|
+
async createAndSendRequest(method, url) {
|
|
303
|
+
return await this.axiosInstance(Object.assign(Object.assign({ url,
|
|
304
|
+
method, withCredentials: true }, this.request), { data: this.prepareRequestPayload() }));
|
|
305
|
+
}
|
|
306
|
+
/**
|
|
307
|
+
* Returns the request payload depending on the selected request payload format.
|
|
308
|
+
*/
|
|
309
|
+
prepareRequestPayload() {
|
|
310
|
+
return this.bodyFormat === 'formParams' ? new URLSearchParams(this.request.data).toString() : this.request.data;
|
|
311
|
+
}
|
|
312
|
+
}
|
|
313
|
+
exports.HttpClient = HttpClient;
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.HttpResponse = void 0;
|
|
4
|
+
class HttpResponse {
|
|
5
|
+
/**
|
|
6
|
+
* Wrap the given Axios `response` into a new response instance.
|
|
7
|
+
*
|
|
8
|
+
* @param {AxiosResponse} response
|
|
9
|
+
*/
|
|
10
|
+
constructor(response) {
|
|
11
|
+
this.response = response;
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* Returns the response status.
|
|
15
|
+
*
|
|
16
|
+
* @returns {Number}
|
|
17
|
+
*/
|
|
18
|
+
get status() {
|
|
19
|
+
return this.response.status;
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* Returns the response payload. This method is an alias for `response.payload()`.
|
|
23
|
+
*
|
|
24
|
+
* @returns {*}
|
|
25
|
+
*/
|
|
26
|
+
get data() {
|
|
27
|
+
return this.payload;
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* Returns the response payload.
|
|
31
|
+
*
|
|
32
|
+
* @returns {*}
|
|
33
|
+
*/
|
|
34
|
+
get payload() {
|
|
35
|
+
return this.response.data;
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* Returns the response headers.
|
|
39
|
+
*
|
|
40
|
+
* @returns {Object}
|
|
41
|
+
*/
|
|
42
|
+
get headers() {
|
|
43
|
+
return this.response.headers;
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
exports.HttpResponse = HttpResponse;
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const tslib_1 = require("tslib");
|
|
4
|
+
const client_1 = require("./client");
|
|
5
|
+
exports.default = client_1.HttpClient;
|
|
6
|
+
tslib_1.__exportStar(require("./client"), exports);
|
|
7
|
+
tslib_1.__exportStar(require("./http-response"), exports);
|