@dishantlangayan/solace-cloud-cli 0.0.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/LICENSE.txt +202 -0
- package/README.md +638 -0
- package/bin/dev.cmd +3 -0
- package/bin/dev.js +5 -0
- package/bin/run.cmd +3 -0
- package/bin/run.js +5 -0
- package/dist/commands/missionctrl/broker/create.d.ts +18 -0
- package/dist/commands/missionctrl/broker/create.js +107 -0
- package/dist/commands/missionctrl/broker/delete.d.ts +11 -0
- package/dist/commands/missionctrl/broker/delete.js +54 -0
- package/dist/commands/missionctrl/broker/display.d.ts +12 -0
- package/dist/commands/missionctrl/broker/display.js +69 -0
- package/dist/commands/missionctrl/broker/list.d.ts +13 -0
- package/dist/commands/missionctrl/broker/list.js +65 -0
- package/dist/commands/platform/env/create.d.ts +14 -0
- package/dist/commands/platform/env/create.js +63 -0
- package/dist/commands/platform/env/delete.d.ts +11 -0
- package/dist/commands/platform/env/delete.js +41 -0
- package/dist/commands/platform/env/display.d.ts +12 -0
- package/dist/commands/platform/env/display.js +64 -0
- package/dist/commands/platform/env/list.d.ts +13 -0
- package/dist/commands/platform/env/list.js +56 -0
- package/dist/commands/platform/env/update.d.ts +15 -0
- package/dist/commands/platform/env/update.js +76 -0
- package/dist/config/env-vars.d.ts +44 -0
- package/dist/config/env-vars.js +47 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +1 -0
- package/dist/sc-command.d.ts +22 -0
- package/dist/sc-command.js +54 -0
- package/dist/types/broker.d.ts +60 -0
- package/dist/types/broker.js +1 -0
- package/dist/types/environment.d.ts +36 -0
- package/dist/types/environment.js +1 -0
- package/dist/util/internal.d.ts +6 -0
- package/dist/util/internal.js +12 -0
- package/dist/util/json.d.ts +15 -0
- package/dist/util/json.js +19 -0
- package/dist/util/sc-connection.d.ts +12 -0
- package/dist/util/sc-connection.js +47 -0
- package/oclif.manifest.json +521 -0
- package/package.json +77 -0
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
import { Command, Flags } from '@oclif/core';
|
|
2
|
+
import { table } from 'table';
|
|
3
|
+
import { camelCaseToTitleCase } from '../../../util/internal.js';
|
|
4
|
+
import { ScConnection } from '../../../util/sc-connection.js';
|
|
5
|
+
export default class PlatformEnvUpdate extends Command {
|
|
6
|
+
static args = {};
|
|
7
|
+
static description = `Modify an environment's attributes
|
|
8
|
+
|
|
9
|
+
Use either the Environment's ID (--env-id) or name of the Environment (--name).
|
|
10
|
+
|
|
11
|
+
Token Permissions: [ environments:edit ]
|
|
12
|
+
`;
|
|
13
|
+
static examples = [
|
|
14
|
+
'<%= config.bin %> <%= command.id %> --name=MyEnvName --new-name=MyNewEnvName --desc="My description to update" --isDefault',
|
|
15
|
+
'<%= config.bin %> <%= command.id %> --env-id=MyEnvId --new-name=MyNewEnvName --desc="My description to update" --isDefault'
|
|
16
|
+
];
|
|
17
|
+
static flags = {
|
|
18
|
+
desc: Flags.string({ char: 'd', description: 'Description of the environment to update.' }),
|
|
19
|
+
'env-id': Flags.string({ char: 'e', description: 'Id of the environment.', exactlyOne: ['env-id', 'name'] }),
|
|
20
|
+
isDefault: Flags.boolean({ description: `Indicates this is the organization's default environment. The default value is false.` }),
|
|
21
|
+
name: Flags.string({ char: 'n', description: 'Current name of the environment.', exactlyOne: ['env-id', 'name'] }),
|
|
22
|
+
'new-name': Flags.string({ description: 'New name of the environment.' }),
|
|
23
|
+
};
|
|
24
|
+
async run() {
|
|
25
|
+
const { flags } = await this.parse(PlatformEnvUpdate);
|
|
26
|
+
const desc = flags.desc ?? '';
|
|
27
|
+
const envId = flags['env-id'] ?? '';
|
|
28
|
+
const name = flags.name ?? '';
|
|
29
|
+
const newName = flags['new-name'] ?? '';
|
|
30
|
+
// API body
|
|
31
|
+
const body = {
|
|
32
|
+
...(flags.isDefault && { isDefault: flags.isDefault }),
|
|
33
|
+
...(desc && { description: desc }),
|
|
34
|
+
...(newName && { name: newName }),
|
|
35
|
+
};
|
|
36
|
+
const conn = new ScConnection();
|
|
37
|
+
// API url
|
|
38
|
+
let apiUrl = `/platform/environments`;
|
|
39
|
+
let envIdToUdpate = envId;
|
|
40
|
+
// If env name provided, get the environment matching provided name and delete. If more than one environment matches, an error will be thrown.
|
|
41
|
+
// If env id provided, delete environment with that id
|
|
42
|
+
if (name) {
|
|
43
|
+
// API call to get environment by name
|
|
44
|
+
const getEnvApiUrl = `${apiUrl}?name=${name}`;
|
|
45
|
+
const resp = await conn.get(getEnvApiUrl);
|
|
46
|
+
if (resp.data.length > 1) {
|
|
47
|
+
this.error(`Multiple environments found with: ${name}. Exactly one environment must match the provided name.`);
|
|
48
|
+
}
|
|
49
|
+
else {
|
|
50
|
+
envIdToUdpate = resp.data[0]?.id;
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
// API call to delete environment by id
|
|
54
|
+
apiUrl += `/${envIdToUdpate}`;
|
|
55
|
+
const resp = await conn.put(apiUrl, body);
|
|
56
|
+
this.log(`Environment with id '${envIdToUdpate}' has been updated successfully.`);
|
|
57
|
+
this.print(resp.data);
|
|
58
|
+
}
|
|
59
|
+
print(environment) {
|
|
60
|
+
this.log();
|
|
61
|
+
const tableRows = [
|
|
62
|
+
['Key', 'Value'],
|
|
63
|
+
...Object.entries(environment).map(([key, value]) => [camelCaseToTitleCase(key), value]),
|
|
64
|
+
];
|
|
65
|
+
// Table config
|
|
66
|
+
const config = {
|
|
67
|
+
columns: {
|
|
68
|
+
1: { width: 50, wrapWord: true },
|
|
69
|
+
},
|
|
70
|
+
drawHorizontalLine(lineIndex, rowCount) {
|
|
71
|
+
return lineIndex === 0 || lineIndex === 1 || lineIndex === rowCount;
|
|
72
|
+
},
|
|
73
|
+
};
|
|
74
|
+
this.log(table(tableRows, config));
|
|
75
|
+
}
|
|
76
|
+
}
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
export declare enum EnvironmentVariable {
|
|
2
|
+
'SC_ACCESS_TOKEN' = "SC_ACCESS_TOKEN",
|
|
3
|
+
'SC_API_VERSION' = "SC_API_VERSION",
|
|
4
|
+
'SC_BASE_URL' = "SC_BASE_URL"
|
|
5
|
+
}
|
|
6
|
+
export declare const DefaultBaseUrl = "https://api.solace.cloud";
|
|
7
|
+
/**
|
|
8
|
+
* An injectable abstraction on top of `process.env` with various convenience functions
|
|
9
|
+
* for accessing environment variables.
|
|
10
|
+
*/
|
|
11
|
+
export declare class EnvVars {
|
|
12
|
+
private env;
|
|
13
|
+
constructor(env?: NodeJS.ProcessEnv);
|
|
14
|
+
/**
|
|
15
|
+
* Gets a `string` value for a given key.
|
|
16
|
+
*
|
|
17
|
+
* @param key The name of the envar.
|
|
18
|
+
*/
|
|
19
|
+
getString(key: string): string | undefined;
|
|
20
|
+
/**
|
|
21
|
+
* Gets a `string` value for a given key.
|
|
22
|
+
*
|
|
23
|
+
* @param key The name of the envar.
|
|
24
|
+
* @param def A default value.
|
|
25
|
+
*/
|
|
26
|
+
getString(key: string, def: string): string;
|
|
27
|
+
/**
|
|
28
|
+
* Sets a `string` value for a given key, or removes the current value when no value is given.
|
|
29
|
+
*
|
|
30
|
+
* @param key The name of the envar.
|
|
31
|
+
* @param value The value to set.
|
|
32
|
+
*/
|
|
33
|
+
setString(key: string, value: string): void;
|
|
34
|
+
/**
|
|
35
|
+
* Un-sets a value for a given key.
|
|
36
|
+
*
|
|
37
|
+
* @param key The name of the environment variable.
|
|
38
|
+
*/
|
|
39
|
+
unset(key: string): void;
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* The default `EnvVars` instance, which wraps `process.env`.
|
|
43
|
+
*/
|
|
44
|
+
export declare const envVars: EnvVars;
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
export var EnvironmentVariable;
|
|
2
|
+
(function (EnvironmentVariable) {
|
|
3
|
+
EnvironmentVariable["SC_ACCESS_TOKEN"] = "SC_ACCESS_TOKEN";
|
|
4
|
+
EnvironmentVariable["SC_API_VERSION"] = "SC_API_VERSION";
|
|
5
|
+
EnvironmentVariable["SC_BASE_URL"] = "SC_BASE_URL";
|
|
6
|
+
})(EnvironmentVariable || (EnvironmentVariable = {}));
|
|
7
|
+
export const DefaultBaseUrl = 'https://api.solace.cloud';
|
|
8
|
+
/**
|
|
9
|
+
* An injectable abstraction on top of `process.env` with various convenience functions
|
|
10
|
+
* for accessing environment variables.
|
|
11
|
+
*/
|
|
12
|
+
export class EnvVars {
|
|
13
|
+
env;
|
|
14
|
+
constructor(env = process.env || {}) {
|
|
15
|
+
this.env = env;
|
|
16
|
+
this.env = env;
|
|
17
|
+
}
|
|
18
|
+
// underlying method
|
|
19
|
+
getString(key, def) {
|
|
20
|
+
return this.env[key] ?? def;
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* Sets a `string` value for a given key, or removes the current value when no value is given.
|
|
24
|
+
*
|
|
25
|
+
* @param key The name of the envar.
|
|
26
|
+
* @param value The value to set.
|
|
27
|
+
*/
|
|
28
|
+
setString(key, value) {
|
|
29
|
+
if (value === '') {
|
|
30
|
+
this.unset(key);
|
|
31
|
+
return;
|
|
32
|
+
}
|
|
33
|
+
this.env[key] = value;
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* Un-sets a value for a given key.
|
|
37
|
+
*
|
|
38
|
+
* @param key The name of the environment variable.
|
|
39
|
+
*/
|
|
40
|
+
unset(key) {
|
|
41
|
+
delete this.env[key];
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* The default `EnvVars` instance, which wraps `process.env`.
|
|
46
|
+
*/
|
|
47
|
+
export const envVars = new EnvVars();
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { run } from '@oclif/core';
|
package/dist/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { run } from '@oclif/core';
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { Command, Interfaces } from '@oclif/core';
|
|
2
|
+
export type Flags<T extends typeof Command> = Interfaces.InferredFlags<T['flags'] & typeof ScCommand['baseFlags']>;
|
|
3
|
+
export type Args<T extends typeof Command> = Interfaces.InferredArgs<T['args']>;
|
|
4
|
+
/**
|
|
5
|
+
* A base command that provided common functionality for all sc commands.
|
|
6
|
+
*
|
|
7
|
+
* All implementations of this class need to implement the run() method.
|
|
8
|
+
*
|
|
9
|
+
*/
|
|
10
|
+
export declare abstract class ScCommand<T extends typeof Command> extends Command {
|
|
11
|
+
static baseFlags: {
|
|
12
|
+
'log-level': Interfaces.OptionFlag<"error" | "warn" | "debug" | "info" | "trace", Interfaces.CustomOptions>;
|
|
13
|
+
};
|
|
14
|
+
static enableJsonFlag: boolean;
|
|
15
|
+
protected args: Args<T>;
|
|
16
|
+
protected flags: Flags<T>;
|
|
17
|
+
protected catch(err: Error & {
|
|
18
|
+
exitCode?: number;
|
|
19
|
+
}): Promise<unknown>;
|
|
20
|
+
protected finally(_: Error | undefined): Promise<unknown>;
|
|
21
|
+
init(): Promise<void>;
|
|
22
|
+
}
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import { Command, Flags } from '@oclif/core';
|
|
2
|
+
import { DefaultBaseUrl, EnvironmentVariable, envVars } from './config/env-vars.js';
|
|
3
|
+
/**
|
|
4
|
+
* A base command that provided common functionality for all sc commands.
|
|
5
|
+
*
|
|
6
|
+
* All implementations of this class need to implement the run() method.
|
|
7
|
+
*
|
|
8
|
+
*/
|
|
9
|
+
export class ScCommand extends Command {
|
|
10
|
+
// define flags that can be inherited by any command that extends BaseCommand
|
|
11
|
+
static baseFlags = {
|
|
12
|
+
'log-level': Flags.option({
|
|
13
|
+
default: 'info',
|
|
14
|
+
helpGroup: 'GLOBAL',
|
|
15
|
+
options: ['debug', 'warn', 'error', 'info', 'trace'],
|
|
16
|
+
summary: 'Specify level for logging.',
|
|
17
|
+
})(),
|
|
18
|
+
};
|
|
19
|
+
// add the --json flag
|
|
20
|
+
static enableJsonFlag = true;
|
|
21
|
+
args;
|
|
22
|
+
flags;
|
|
23
|
+
async catch(err) {
|
|
24
|
+
// add any custom logic to handle errors from the command
|
|
25
|
+
// or simply return the parent class error handling
|
|
26
|
+
return super.catch(err);
|
|
27
|
+
}
|
|
28
|
+
async finally(_) {
|
|
29
|
+
// called after run and catch regardless of whether or not the command errored
|
|
30
|
+
return super.finally(_);
|
|
31
|
+
}
|
|
32
|
+
async init() {
|
|
33
|
+
await super.init();
|
|
34
|
+
const { args, flags } = await this.parse({
|
|
35
|
+
args: this.ctor.args,
|
|
36
|
+
baseFlags: super.ctor.baseFlags,
|
|
37
|
+
enableJsonFlag: this.ctor.enableJsonFlag,
|
|
38
|
+
flags: this.ctor.flags,
|
|
39
|
+
strict: this.ctor.strict,
|
|
40
|
+
});
|
|
41
|
+
this.flags = flags;
|
|
42
|
+
this.args = args;
|
|
43
|
+
// set base url if not defined in environment variables
|
|
44
|
+
if (!envVars.getString(EnvironmentVariable.SC_BASE_URL)) {
|
|
45
|
+
this.debug(`Environment variable '${EnvironmentVariable.SC_BASE_URL}' not set, using default '${DefaultBaseUrl}'`);
|
|
46
|
+
envVars.setString(EnvironmentVariable.SC_BASE_URL, DefaultBaseUrl);
|
|
47
|
+
}
|
|
48
|
+
// Check if Access Token is set
|
|
49
|
+
const value = envVars.getString(EnvironmentVariable.SC_ACCESS_TOKEN);
|
|
50
|
+
if (!value) {
|
|
51
|
+
throw new Error(`Environment variable ${EnvironmentVariable.SC_ACCESS_TOKEN} is not set and required for any API operations.`);
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
}
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
export interface EventBrokerCreateDetail {
|
|
2
|
+
completedTime?: string;
|
|
3
|
+
createdBy?: string;
|
|
4
|
+
createdTime?: string;
|
|
5
|
+
error?: {
|
|
6
|
+
errorId: string;
|
|
7
|
+
message: string;
|
|
8
|
+
};
|
|
9
|
+
id: string;
|
|
10
|
+
operationType?: string;
|
|
11
|
+
progressLogs?: {
|
|
12
|
+
message: string;
|
|
13
|
+
step: string;
|
|
14
|
+
timestamp: string;
|
|
15
|
+
}[];
|
|
16
|
+
resourceId?: string;
|
|
17
|
+
resourceType?: string;
|
|
18
|
+
status?: string;
|
|
19
|
+
type?: string;
|
|
20
|
+
}
|
|
21
|
+
export interface EventBrokerCreateApiResponse {
|
|
22
|
+
data: EventBrokerCreateDetail;
|
|
23
|
+
meta: {
|
|
24
|
+
additionalProp?: Record<string, unknown>;
|
|
25
|
+
};
|
|
26
|
+
}
|
|
27
|
+
export interface EventBrokerDeleteListApiResponse {
|
|
28
|
+
data: EventBrokerCreateDetail[];
|
|
29
|
+
meta: {
|
|
30
|
+
additionalProp?: Record<string, unknown>;
|
|
31
|
+
};
|
|
32
|
+
}
|
|
33
|
+
export interface EventBrokerListApiResponse {
|
|
34
|
+
data: EventBrokerServiceDetail[];
|
|
35
|
+
meta: {
|
|
36
|
+
pagination: {
|
|
37
|
+
count: number;
|
|
38
|
+
nextPage: null | string;
|
|
39
|
+
pageNumber: number;
|
|
40
|
+
pageSize: number;
|
|
41
|
+
totalPages: number;
|
|
42
|
+
};
|
|
43
|
+
};
|
|
44
|
+
}
|
|
45
|
+
export interface EventBrokerServiceDetail {
|
|
46
|
+
adminState?: string;
|
|
47
|
+
createdTime?: string;
|
|
48
|
+
creationState?: string;
|
|
49
|
+
datacenterId?: string;
|
|
50
|
+
environmentId?: string;
|
|
51
|
+
eventBrokerServiceVersion?: string;
|
|
52
|
+
id: string;
|
|
53
|
+
infrastructureId?: string;
|
|
54
|
+
locked?: boolean;
|
|
55
|
+
msgVpnName?: string;
|
|
56
|
+
name: string;
|
|
57
|
+
ownedBy?: string;
|
|
58
|
+
serviceClassId?: string;
|
|
59
|
+
type?: string;
|
|
60
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
export interface Environment {
|
|
2
|
+
bgColor?: string;
|
|
3
|
+
createdBy?: string;
|
|
4
|
+
createdTime?: string;
|
|
5
|
+
description?: string;
|
|
6
|
+
fgColor?: string;
|
|
7
|
+
icon?: string;
|
|
8
|
+
id?: string;
|
|
9
|
+
isDefault?: boolean;
|
|
10
|
+
isProduction?: boolean;
|
|
11
|
+
name: string;
|
|
12
|
+
organization?: {
|
|
13
|
+
id: string;
|
|
14
|
+
internal: boolean;
|
|
15
|
+
name: string;
|
|
16
|
+
organizationType: string;
|
|
17
|
+
};
|
|
18
|
+
type?: string;
|
|
19
|
+
updatedBy?: string;
|
|
20
|
+
updatedTime?: string;
|
|
21
|
+
}
|
|
22
|
+
export interface EnvironmentApiResponse {
|
|
23
|
+
data: Environment[];
|
|
24
|
+
meta: {
|
|
25
|
+
pagination: {
|
|
26
|
+
count: number;
|
|
27
|
+
nextPage: null | string;
|
|
28
|
+
pageNumber: number;
|
|
29
|
+
pageSize: number;
|
|
30
|
+
totalPages: number;
|
|
31
|
+
};
|
|
32
|
+
};
|
|
33
|
+
}
|
|
34
|
+
export interface EnvironmentDetail {
|
|
35
|
+
data: Environment;
|
|
36
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Formats a camel case style `string` into a title case.
|
|
3
|
+
*
|
|
4
|
+
* @param text Text to transform.
|
|
5
|
+
*/
|
|
6
|
+
export function camelCaseToTitleCase(text) {
|
|
7
|
+
return text
|
|
8
|
+
.replaceAll(/(^\w|\s\w)/g, (m) => m.toUpperCase())
|
|
9
|
+
.replaceAll(/([A-Z][a-z]+)/g, ' $1')
|
|
10
|
+
.replaceAll(/\s{2,}/g, ' ')
|
|
11
|
+
.trim();
|
|
12
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Parse JSON `string` data.
|
|
3
|
+
*
|
|
4
|
+
* @param data Data to parse.
|
|
5
|
+
*/
|
|
6
|
+
export declare function parseJson(data: string, _throwOnEmpty?: boolean): unknown;
|
|
7
|
+
/**
|
|
8
|
+
* Finds a the key with the provided value in the JSON array and returns the JSON object.
|
|
9
|
+
*
|
|
10
|
+
* @param array The JSON array to search
|
|
11
|
+
* @param key The key to lookup in the array
|
|
12
|
+
* @param value The value of the key to match
|
|
13
|
+
* @returns The JSON object that matches the key and value
|
|
14
|
+
*/
|
|
15
|
+
export declare function findByKey<T>(array: T[], key: keyof T, value: unknown): T | undefined;
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Parse JSON `string` data.
|
|
3
|
+
*
|
|
4
|
+
* @param data Data to parse.
|
|
5
|
+
*/
|
|
6
|
+
export function parseJson(data, _throwOnEmpty = true) {
|
|
7
|
+
return typeof data === 'string' ? JSON.parse(data) : data;
|
|
8
|
+
}
|
|
9
|
+
/**
|
|
10
|
+
* Finds a the key with the provided value in the JSON array and returns the JSON object.
|
|
11
|
+
*
|
|
12
|
+
* @param array The JSON array to search
|
|
13
|
+
* @param key The key to lookup in the array
|
|
14
|
+
* @param value The value of the key to match
|
|
15
|
+
* @returns The JSON object that matches the key and value
|
|
16
|
+
*/
|
|
17
|
+
export function findByKey(array, key, value) {
|
|
18
|
+
return array.find(item => item[key] === value);
|
|
19
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { AxiosRequestConfig } from 'axios';
|
|
2
|
+
export declare class ScConnection {
|
|
3
|
+
private axiosInstance;
|
|
4
|
+
private endpointUrl;
|
|
5
|
+
constructor(baseURL?: string, accessToken?: string, timeout?: number);
|
|
6
|
+
delete<T>(url: string, config?: AxiosRequestConfig): Promise<T>;
|
|
7
|
+
get<T>(url: string, config?: AxiosRequestConfig): Promise<T>;
|
|
8
|
+
post<T>(url: string, data?: unknown, config?: AxiosRequestConfig): Promise<T>;
|
|
9
|
+
put<T>(url: string, data?: unknown, config?: AxiosRequestConfig): Promise<T>;
|
|
10
|
+
private joinPaths;
|
|
11
|
+
}
|
|
12
|
+
export default ScConnection;
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import axios from 'axios';
|
|
2
|
+
import { DefaultBaseUrl, EnvironmentVariable, envVars } from '../config/env-vars.js';
|
|
3
|
+
export class ScConnection {
|
|
4
|
+
axiosInstance;
|
|
5
|
+
endpointUrl = '';
|
|
6
|
+
constructor(baseURL = envVars.getString(EnvironmentVariable.SC_BASE_URL, DefaultBaseUrl), accessToken = envVars.getString(EnvironmentVariable.SC_ACCESS_TOKEN, ''), timeout = 10_000) {
|
|
7
|
+
const apiVersion = envVars.getString(EnvironmentVariable.SC_API_VERSION, 'v2');
|
|
8
|
+
this.endpointUrl = this.joinPaths(baseURL, `/api/${apiVersion}`);
|
|
9
|
+
this.axiosInstance = axios.create({
|
|
10
|
+
baseURL: this.endpointUrl,
|
|
11
|
+
headers: {
|
|
12
|
+
'Authorization': `Bearer ${accessToken}`,
|
|
13
|
+
'Content-Type': 'application/json',
|
|
14
|
+
},
|
|
15
|
+
timeout,
|
|
16
|
+
});
|
|
17
|
+
// Add interceptors if needed
|
|
18
|
+
this.axiosInstance.interceptors.response.use((response) => response, (error) => {
|
|
19
|
+
console.error('API Error:', error.response?.data || error.message);
|
|
20
|
+
return Promise.reject(error);
|
|
21
|
+
});
|
|
22
|
+
}
|
|
23
|
+
// DELETE request
|
|
24
|
+
async delete(url, config) {
|
|
25
|
+
const response = await this.axiosInstance.delete(url, config);
|
|
26
|
+
return response.data;
|
|
27
|
+
}
|
|
28
|
+
// GET request
|
|
29
|
+
async get(url, config) {
|
|
30
|
+
const response = await this.axiosInstance.get(url, config);
|
|
31
|
+
return response.data;
|
|
32
|
+
}
|
|
33
|
+
// POST request
|
|
34
|
+
async post(url, data, config) {
|
|
35
|
+
const response = await this.axiosInstance.post(url, data, config);
|
|
36
|
+
return response.data;
|
|
37
|
+
}
|
|
38
|
+
// PUT request
|
|
39
|
+
async put(url, data, config) {
|
|
40
|
+
const response = await this.axiosInstance.put(url, data, config);
|
|
41
|
+
return response.data;
|
|
42
|
+
}
|
|
43
|
+
joinPaths(base, path) {
|
|
44
|
+
return `${base.replace(/\/+$/, '')}/${path.replace(/^\/+/, '')}`;
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
export default ScConnection;
|