@emilgroup/action-sdk 1.0.1-beta.20
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/.openapi-generator/FILES +16 -0
- package/.openapi-generator/VERSION +1 -0
- package/.openapi-generator-ignore +23 -0
- package/README.md +51 -0
- package/api/default-api.ts +124 -0
- package/api.ts +27 -0
- package/base.ts +331 -0
- package/common.ts +198 -0
- package/configuration.ts +110 -0
- package/dist/api/default-api.d.ts +70 -0
- package/dist/api/default-api.js +200 -0
- package/dist/api.d.ts +12 -0
- package/dist/api.js +30 -0
- package/dist/base.d.ts +86 -0
- package/dist/base.js +367 -0
- package/dist/common.d.ts +91 -0
- package/dist/common.js +276 -0
- package/dist/configuration.d.ts +89 -0
- package/dist/configuration.js +52 -0
- package/dist/index.d.ts +15 -0
- package/dist/index.js +36 -0
- package/dist/models/index.d.ts +2 -0
- package/dist/models/index.js +18 -0
- package/dist/models/inline-response200.d.ts +54 -0
- package/dist/models/inline-response200.js +15 -0
- package/dist/models/inline-response503.d.ts +54 -0
- package/dist/models/inline-response503.js +15 -0
- package/git_push.sh +57 -0
- package/index.ts +19 -0
- package/models/index.ts +2 -0
- package/models/inline-response200.ts +48 -0
- package/models/inline-response503.ts +48 -0
- package/package.json +26 -0
- package/tsconfig.json +23 -0
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
.gitignore
|
|
2
|
+
.npmignore
|
|
3
|
+
.openapi-generator-ignore
|
|
4
|
+
README.md
|
|
5
|
+
api.ts
|
|
6
|
+
api/default-api.ts
|
|
7
|
+
base.ts
|
|
8
|
+
common.ts
|
|
9
|
+
configuration.ts
|
|
10
|
+
git_push.sh
|
|
11
|
+
index.ts
|
|
12
|
+
models/index.ts
|
|
13
|
+
models/inline-response200.ts
|
|
14
|
+
models/inline-response503.ts
|
|
15
|
+
package.json
|
|
16
|
+
tsconfig.json
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
5.4.0
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
# OpenAPI Generator Ignore
|
|
2
|
+
# Generated by openapi-generator https://github.com/openapitools/openapi-generator
|
|
3
|
+
|
|
4
|
+
# Use this file to prevent files from being overwritten by the generator.
|
|
5
|
+
# The patterns follow closely to .gitignore or .dockerignore.
|
|
6
|
+
|
|
7
|
+
# As an example, the C# client generator defines ApiClient.cs.
|
|
8
|
+
# You can make changes and tell OpenAPI Generator to ignore just this file by uncommenting the following line:
|
|
9
|
+
#ApiClient.cs
|
|
10
|
+
|
|
11
|
+
# You can match any string of characters against a directory, file or extension with a single asterisk (*):
|
|
12
|
+
#foo/*/qux
|
|
13
|
+
# The above matches foo/bar/qux and foo/baz/qux, but not foo/bar/baz/qux
|
|
14
|
+
|
|
15
|
+
# You can recursively match patterns against a directory, file or extension with a double asterisk (**):
|
|
16
|
+
#foo/**/qux
|
|
17
|
+
# This matches foo/bar/qux, foo/baz/qux, and foo/bar/baz/qux
|
|
18
|
+
|
|
19
|
+
# You can also negate patterns with an exclamation (!).
|
|
20
|
+
# For example, you can ignore all files in a docs folder with the file extension .md:
|
|
21
|
+
#docs/*.md
|
|
22
|
+
# Then explicitly reverse the ignore rule for a single file:
|
|
23
|
+
#!docs/README.md
|
package/README.md
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
# Emil actions Service SDK
|
|
2
|
+
|
|
3
|
+
This TypeScript/JavaScript client utilizes [axios](https://github.com/axios/axios). The generated module can be used with client-based applications (i.e. React).
|
|
4
|
+
|
|
5
|
+
Language level
|
|
6
|
+
* ES5 - you must have a Promises/A+ library installed
|
|
7
|
+
* ES6
|
|
8
|
+
|
|
9
|
+
Module system
|
|
10
|
+
* CommonJS
|
|
11
|
+
* ES6 module system
|
|
12
|
+
|
|
13
|
+
Although this package can be used in both TypeScript and JavaScript, it is intended to be used with TypeScript. The definition should be automatically resolved via `package.json`. ([Reference](http://www.typescriptlang.org/docs/handbook/typings-for-npm-packages.html)). For more information, you can go to [Emil Api documentation](https://emil.stoplight.io/docs/emil-api/).
|
|
14
|
+
|
|
15
|
+
## Consuming
|
|
16
|
+
|
|
17
|
+
Navigate to the folder of your consuming project and run one of the following commands:
|
|
18
|
+
|
|
19
|
+
```
|
|
20
|
+
npm install @emilgroup/action-sdk@1.0.1-beta.20 --save
|
|
21
|
+
```
|
|
22
|
+
or
|
|
23
|
+
```
|
|
24
|
+
yarn add @emilgroup/action-sdk@1.0.1-beta.20
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
And then you can import `ActionsApi`.
|
|
28
|
+
|
|
29
|
+
```ts
|
|
30
|
+
import { ActionsApi } from '@emilgroup/action-sdk'
|
|
31
|
+
|
|
32
|
+
const actionsApi = new ActionsApi();
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
To use authentication protected endpoints, you have to first authorize. To do so, use the `authorize` function in `ActionsApi`:
|
|
36
|
+
|
|
37
|
+
```ts
|
|
38
|
+
async function listActionConfigurations(): Promise<Void> {
|
|
39
|
+
try {
|
|
40
|
+
const actionsApi = new ActionsApi();
|
|
41
|
+
|
|
42
|
+
await actionsApi.authorize('username', 'password');
|
|
43
|
+
|
|
44
|
+
const { data: { items } } = await actionsApi.listActionConfigurations();
|
|
45
|
+
|
|
46
|
+
console.log(items);
|
|
47
|
+
} catch(error) {
|
|
48
|
+
// process error
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
```
|
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
/* tslint:disable */
|
|
2
|
+
/* eslint-disable */
|
|
3
|
+
/**
|
|
4
|
+
* EMIL ActionService
|
|
5
|
+
* The EMIL ActionService API description
|
|
6
|
+
*
|
|
7
|
+
* The version of the OpenAPI document: 1.0
|
|
8
|
+
* Contact: kontakt@emil.de
|
|
9
|
+
*
|
|
10
|
+
* NOTE: This class is auto generated by OpenAPI Generator (https://openapi-generator.tech).
|
|
11
|
+
* https://openapi-generator.tech
|
|
12
|
+
* Do not edit the class manually.
|
|
13
|
+
*/
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
import globalAxios, { AxiosPromise, AxiosInstance, AxiosRequestConfig } from 'axios';
|
|
17
|
+
import { Configuration } from '../configuration';
|
|
18
|
+
// Some imports not used depending on template conditions
|
|
19
|
+
// @ts-ignore
|
|
20
|
+
import { DUMMY_BASE_URL, assertParamExists, setApiKeyToObject, setBasicAuthToObject, setBearerAuthToObject, setOAuthToObject, setSearchParams, serializeDataIfNeeded, toPathString, createRequestFunction } from '../common';
|
|
21
|
+
// @ts-ignore
|
|
22
|
+
import { BASE_PATH, COLLECTION_FORMATS, RequestArgs, BaseAPI, RequiredError } from '../base';
|
|
23
|
+
// @ts-ignore
|
|
24
|
+
import { InlineResponse200 } from '../models';
|
|
25
|
+
// @ts-ignore
|
|
26
|
+
import { InlineResponse503 } from '../models';
|
|
27
|
+
/**
|
|
28
|
+
* DefaultApi - axios parameter creator
|
|
29
|
+
* @export
|
|
30
|
+
*/
|
|
31
|
+
export const DefaultApiAxiosParamCreator = function (configuration?: Configuration) {
|
|
32
|
+
return {
|
|
33
|
+
/**
|
|
34
|
+
* Returns the health status of the ActionService service. This endpoint is used to monitor the operational status of the ActionService service. It typically returns a simple status indicator, such as \'UP\' or \'OK\', confirming that the service is operational and available.
|
|
35
|
+
* @summary Health Check
|
|
36
|
+
* @param {*} [options] Override http request option.
|
|
37
|
+
* @throws {RequiredError}
|
|
38
|
+
*/
|
|
39
|
+
check: async (options: AxiosRequestConfig = {}): Promise<RequestArgs> => {
|
|
40
|
+
const localVarPath = `/actionservice/health`;
|
|
41
|
+
// use dummy base URL string because the URL constructor only accepts absolute URLs.
|
|
42
|
+
const localVarUrlObj = new URL(localVarPath, DUMMY_BASE_URL);
|
|
43
|
+
let baseOptions;
|
|
44
|
+
let baseAccessToken;
|
|
45
|
+
if (configuration) {
|
|
46
|
+
baseOptions = configuration.baseOptions;
|
|
47
|
+
baseAccessToken = configuration.accessToken;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
const localVarRequestOptions = { method: 'GET', ...baseOptions, ...options};
|
|
51
|
+
const localVarHeaderParameter = {} as any;
|
|
52
|
+
const localVarQueryParameter = {} as any;
|
|
53
|
+
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+
setSearchParams(localVarUrlObj, localVarQueryParameter);
|
|
57
|
+
let headersFromBaseOptions = baseOptions && baseOptions.headers ? baseOptions.headers : {};
|
|
58
|
+
localVarRequestOptions.headers = {...localVarHeaderParameter, ...headersFromBaseOptions, ...options.headers};
|
|
59
|
+
|
|
60
|
+
return {
|
|
61
|
+
url: toPathString(localVarUrlObj),
|
|
62
|
+
options: localVarRequestOptions,
|
|
63
|
+
};
|
|
64
|
+
},
|
|
65
|
+
}
|
|
66
|
+
};
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* DefaultApi - functional programming interface
|
|
70
|
+
* @export
|
|
71
|
+
*/
|
|
72
|
+
export const DefaultApiFp = function(configuration?: Configuration) {
|
|
73
|
+
const localVarAxiosParamCreator = DefaultApiAxiosParamCreator(configuration)
|
|
74
|
+
return {
|
|
75
|
+
/**
|
|
76
|
+
* Returns the health status of the ActionService service. This endpoint is used to monitor the operational status of the ActionService service. It typically returns a simple status indicator, such as \'UP\' or \'OK\', confirming that the service is operational and available.
|
|
77
|
+
* @summary Health Check
|
|
78
|
+
* @param {*} [options] Override http request option.
|
|
79
|
+
* @throws {RequiredError}
|
|
80
|
+
*/
|
|
81
|
+
async check(options?: AxiosRequestConfig): Promise<(axios?: AxiosInstance, basePath?: string) => AxiosPromise<InlineResponse200>> {
|
|
82
|
+
const localVarAxiosArgs = await localVarAxiosParamCreator.check(options);
|
|
83
|
+
return createRequestFunction(localVarAxiosArgs, globalAxios, BASE_PATH, configuration);
|
|
84
|
+
},
|
|
85
|
+
}
|
|
86
|
+
};
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* DefaultApi - factory interface
|
|
90
|
+
* @export
|
|
91
|
+
*/
|
|
92
|
+
export const DefaultApiFactory = function (configuration?: Configuration, basePath?: string, axios?: AxiosInstance) {
|
|
93
|
+
const localVarFp = DefaultApiFp(configuration)
|
|
94
|
+
return {
|
|
95
|
+
/**
|
|
96
|
+
* Returns the health status of the ActionService service. This endpoint is used to monitor the operational status of the ActionService service. It typically returns a simple status indicator, such as \'UP\' or \'OK\', confirming that the service is operational and available.
|
|
97
|
+
* @summary Health Check
|
|
98
|
+
* @param {*} [options] Override http request option.
|
|
99
|
+
* @throws {RequiredError}
|
|
100
|
+
*/
|
|
101
|
+
check(options?: any): AxiosPromise<InlineResponse200> {
|
|
102
|
+
return localVarFp.check(options).then((request) => request(axios, basePath));
|
|
103
|
+
},
|
|
104
|
+
};
|
|
105
|
+
};
|
|
106
|
+
|
|
107
|
+
/**
|
|
108
|
+
* DefaultApi - object-oriented interface
|
|
109
|
+
* @export
|
|
110
|
+
* @class DefaultApi
|
|
111
|
+
* @extends {BaseAPI}
|
|
112
|
+
*/
|
|
113
|
+
export class DefaultApi extends BaseAPI {
|
|
114
|
+
/**
|
|
115
|
+
* Returns the health status of the ActionService service. This endpoint is used to monitor the operational status of the ActionService service. It typically returns a simple status indicator, such as \'UP\' or \'OK\', confirming that the service is operational and available.
|
|
116
|
+
* @summary Health Check
|
|
117
|
+
* @param {*} [options] Override http request option.
|
|
118
|
+
* @throws {RequiredError}
|
|
119
|
+
* @memberof DefaultApi
|
|
120
|
+
*/
|
|
121
|
+
public check(options?: AxiosRequestConfig) {
|
|
122
|
+
return DefaultApiFp(this.configuration).check(options).then((request) => request(this.axios, this.basePath));
|
|
123
|
+
}
|
|
124
|
+
}
|
package/api.ts
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
/* tslint:disable */
|
|
2
|
+
/* eslint-disable */
|
|
3
|
+
/**
|
|
4
|
+
* EMIL ActionService
|
|
5
|
+
* The EMIL ActionService API description
|
|
6
|
+
*
|
|
7
|
+
* The version of the OpenAPI document: 1.0
|
|
8
|
+
* Contact: kontakt@emil.de
|
|
9
|
+
*
|
|
10
|
+
* NOTE: This class is auto generated by OpenAPI Generator (https://openapi-generator.tech).
|
|
11
|
+
* https://openapi-generator.tech
|
|
12
|
+
* Do not edit the class manually.
|
|
13
|
+
*/
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
import { Configuration } from './configuration';
|
|
17
|
+
import globalAxios, { AxiosPromise, AxiosInstance, AxiosRequestConfig, AxiosResponse } from 'axios';
|
|
18
|
+
// Some imports not used depending on template conditions
|
|
19
|
+
// @ts-ignore
|
|
20
|
+
import { DUMMY_BASE_URL, assertParamExists, setApiKeyToObject, setBasicAuthToObject, setBearerAuthToObject, setOAuthToObject, setSearchParams, serializeDataIfNeeded, toPathString, createRequestFunction } from './common';
|
|
21
|
+
// @ts-ignore
|
|
22
|
+
import { BASE_PATH, COLLECTION_FORMATS, RequestArgs, BaseAPI, RequiredError } from './base';
|
|
23
|
+
import { DefaultApi } from './api';
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
export * from './api/default-api';
|
|
27
|
+
|
package/base.ts
ADDED
|
@@ -0,0 +1,331 @@
|
|
|
1
|
+
/* tslint:disable */
|
|
2
|
+
/* eslint-disable */
|
|
3
|
+
/**
|
|
4
|
+
* EMIL ActionService
|
|
5
|
+
* The EMIL ActionService API description
|
|
6
|
+
*
|
|
7
|
+
* The version of the OpenAPI document: 1.0
|
|
8
|
+
* Contact: kontakt@emil.de
|
|
9
|
+
*
|
|
10
|
+
* NOTE: This class is auto generated by OpenAPI Generator (https://openapi-generator.tech).
|
|
11
|
+
* https://openapi-generator.tech
|
|
12
|
+
* Do not edit the class manually.
|
|
13
|
+
*/
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
import { Configuration } from "./configuration";
|
|
17
|
+
import { defaultStorage } from "./common";
|
|
18
|
+
// Some imports not used depending on template conditions
|
|
19
|
+
// @ts-ignore
|
|
20
|
+
import globalAxios, { AxiosPromise, AxiosInstance, AxiosRequestConfig, AxiosResponse } from 'axios';
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
export const BASE_PATH = "https://apiv2.emil.de".replace(/\/+$/, "");
|
|
24
|
+
/**
|
|
25
|
+
*
|
|
26
|
+
* @export
|
|
27
|
+
*/
|
|
28
|
+
export const COLLECTION_FORMATS = {
|
|
29
|
+
csv: ",",
|
|
30
|
+
ssv: " ",
|
|
31
|
+
tsv: "\t",
|
|
32
|
+
pipes: "|",
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
export interface LoginClass {
|
|
36
|
+
accessToken: string;
|
|
37
|
+
permissions: string;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export interface SwitchWorkspaceRequest {
|
|
41
|
+
username: string;
|
|
42
|
+
targetWorkspace: string;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
export interface SwitchWorkspaceResponseClass {
|
|
46
|
+
accessToken: string;
|
|
47
|
+
permissions: string;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
export enum Environment {
|
|
51
|
+
Production = 'https://apiv2.emil.de',
|
|
52
|
+
Test = 'https://apiv2-test.emil.de',
|
|
53
|
+
Staging = 'https://apiv2-staging.emil.de',
|
|
54
|
+
Development = 'https://apiv2-dev.emil.de',
|
|
55
|
+
ProductionZurich = 'https://eu-central-2.apiv2.emil.de',
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
let _retry_count = 0
|
|
59
|
+
let _retry = null
|
|
60
|
+
|
|
61
|
+
export function resetRetry() {
|
|
62
|
+
_retry_count = 0
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
*
|
|
67
|
+
* @export
|
|
68
|
+
* @interface RequestArgs
|
|
69
|
+
*/
|
|
70
|
+
export interface RequestArgs {
|
|
71
|
+
url: string;
|
|
72
|
+
options: AxiosRequestConfig;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
interface TokenData {
|
|
76
|
+
accessToken?: string;
|
|
77
|
+
username?: string;
|
|
78
|
+
permissions?: string;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
const NETWORK_ERROR_MESSAGE = "Network Error";
|
|
82
|
+
const TOKEN_DATA = 'APP_TOKEN';
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
*
|
|
86
|
+
* @export
|
|
87
|
+
* @class BaseAPI
|
|
88
|
+
*/
|
|
89
|
+
export class BaseAPI {
|
|
90
|
+
protected configuration: Configuration | undefined;
|
|
91
|
+
private tokenData?: TokenData;
|
|
92
|
+
|
|
93
|
+
constructor(configuration?: Configuration,
|
|
94
|
+
protected basePath: string = BASE_PATH,
|
|
95
|
+
protected axios: AxiosInstance = globalAxios) {
|
|
96
|
+
|
|
97
|
+
this.loadTokenData();
|
|
98
|
+
|
|
99
|
+
if (configuration) {
|
|
100
|
+
const { accessToken } = this.tokenData;
|
|
101
|
+
this.configuration = configuration;
|
|
102
|
+
this.basePath = configuration.basePath || this.basePath;
|
|
103
|
+
|
|
104
|
+
// Use config token if provided, otherwise use tokenData token
|
|
105
|
+
const configToken = this.configuration.accessToken;
|
|
106
|
+
const storedToken = accessToken ? `Bearer ${accessToken}` : '';
|
|
107
|
+
this.configuration.accessToken = configToken || storedToken;
|
|
108
|
+
} else {
|
|
109
|
+
const { accessToken, username } = this.tokenData;
|
|
110
|
+
|
|
111
|
+
this.configuration = new Configuration({
|
|
112
|
+
basePath: this.basePath,
|
|
113
|
+
accessToken: accessToken ? `Bearer ${accessToken}` : '',
|
|
114
|
+
username,
|
|
115
|
+
});
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
this.attachInterceptor(axios);
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
selectEnvironment(env: Environment) {
|
|
122
|
+
this.selectBasePath(env);
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
selectBasePath(path: string) {
|
|
126
|
+
this.configuration.basePath = path;
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
getPermissions(): Array<string> {
|
|
130
|
+
if (!this.tokenData?.permissions) {
|
|
131
|
+
return [];
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
return this.tokenData.permissions.split(',');
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
async authorize(username: string, password: string, targetWorkspace?: string): Promise<void> {
|
|
138
|
+
const options: AxiosRequestConfig = {
|
|
139
|
+
method: 'POST',
|
|
140
|
+
url: `${this.configuration.basePath}/authservice/v1/login`,
|
|
141
|
+
headers: { 'Content-Type': 'application/json' },
|
|
142
|
+
data: {
|
|
143
|
+
username,
|
|
144
|
+
password,
|
|
145
|
+
},
|
|
146
|
+
withCredentials: true,
|
|
147
|
+
};
|
|
148
|
+
|
|
149
|
+
const response = await globalAxios.request<LoginClass>(options);
|
|
150
|
+
|
|
151
|
+
const { data: { accessToken, permissions } } = response;
|
|
152
|
+
|
|
153
|
+
this.configuration.username = username;
|
|
154
|
+
this.configuration.accessToken = `Bearer ${accessToken}`;
|
|
155
|
+
this.tokenData.username = username;
|
|
156
|
+
this.tokenData.accessToken = accessToken;
|
|
157
|
+
this.tokenData.permissions = permissions;
|
|
158
|
+
|
|
159
|
+
// Switch workspace if provided
|
|
160
|
+
if (targetWorkspace) {
|
|
161
|
+
await this.switchWorkspace(targetWorkspace);
|
|
162
|
+
} else {
|
|
163
|
+
// Only store if no workspace switch (since switchWorkspace will store after switching)
|
|
164
|
+
this.storeTokenData({
|
|
165
|
+
...this.tokenData
|
|
166
|
+
});
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
this.storeTokenData({
|
|
170
|
+
...this.tokenData
|
|
171
|
+
});
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
async switchWorkspace(targetWorkspace: string): Promise<void> {
|
|
175
|
+
const options: AxiosRequestConfig = {
|
|
176
|
+
method: 'POST',
|
|
177
|
+
url: `${this.configuration.basePath}/authservice/v1/workspaces/switch`,
|
|
178
|
+
headers: {
|
|
179
|
+
'Content-Type': 'application/json',
|
|
180
|
+
'Authorization': `Bearer ${this.configuration.accessToken}`,
|
|
181
|
+
},
|
|
182
|
+
data: {
|
|
183
|
+
username: this.configuration.username,
|
|
184
|
+
targetWorkspace,
|
|
185
|
+
} as SwitchWorkspaceRequest,
|
|
186
|
+
withCredentials: true,
|
|
187
|
+
};
|
|
188
|
+
|
|
189
|
+
const response = await globalAxios.request<SwitchWorkspaceResponseClass>(options);
|
|
190
|
+
|
|
191
|
+
const { data: { accessToken, permissions } } = response;
|
|
192
|
+
|
|
193
|
+
this.configuration.accessToken = `Bearer ${accessToken}`;
|
|
194
|
+
this.tokenData.accessToken = accessToken;
|
|
195
|
+
this.tokenData.permissions = permissions;
|
|
196
|
+
|
|
197
|
+
this.storeTokenData({
|
|
198
|
+
...this.tokenData
|
|
199
|
+
});
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
async refreshTokenInternal(): Promise<LoginClass> {
|
|
203
|
+
const { username } = this.configuration;
|
|
204
|
+
|
|
205
|
+
if (!username) {
|
|
206
|
+
throw new Error('Failed to refresh token.');
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
const refreshTokenAxios = globalAxios.create()
|
|
210
|
+
refreshTokenAxios.interceptors.response.use(response => {
|
|
211
|
+
const { permissions } = response.data;
|
|
212
|
+
|
|
213
|
+
this.tokenData.permissions = permissions;
|
|
214
|
+
this.storeTokenData(this.tokenData);
|
|
215
|
+
|
|
216
|
+
return response;
|
|
217
|
+
})
|
|
218
|
+
|
|
219
|
+
const options: AxiosRequestConfig = {
|
|
220
|
+
method: 'POST',
|
|
221
|
+
url: `${this.configuration.basePath}/authservice/v1/refresh-token`,
|
|
222
|
+
headers: {
|
|
223
|
+
'Content-Type': 'application/json',
|
|
224
|
+
},
|
|
225
|
+
data: { username: username },
|
|
226
|
+
withCredentials: true,
|
|
227
|
+
};
|
|
228
|
+
|
|
229
|
+
const response = await refreshTokenAxios.request<LoginClass>(options);
|
|
230
|
+
return response.data;
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
private storeTokenData(tokenData?: TokenData) {
|
|
234
|
+
if (typeof window !== 'undefined') {
|
|
235
|
+
defaultStorage().set<TokenData>(TOKEN_DATA, tokenData);
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
public loadTokenData() {
|
|
240
|
+
if (typeof window !== 'undefined') {
|
|
241
|
+
this.tokenData = defaultStorage().get<TokenData>(TOKEN_DATA) || {};
|
|
242
|
+
} else {
|
|
243
|
+
this.tokenData = {};
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
public cleanTokenData() {
|
|
248
|
+
this.storeTokenData(null);
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
private attachInterceptor(axios: AxiosInstance) {
|
|
252
|
+
axios.interceptors.response.use(
|
|
253
|
+
(res) => {
|
|
254
|
+
return res;
|
|
255
|
+
},
|
|
256
|
+
async (err) => {
|
|
257
|
+
let originalConfig = err.config;
|
|
258
|
+
if (err.response && !(err.response instanceof XMLHttpRequest)) { // sometimes buggy and is of type request
|
|
259
|
+
// Access Token was expired
|
|
260
|
+
if ((err.response.status === 401 || err.response.status === 403)
|
|
261
|
+
&& !originalConfig._retry) {
|
|
262
|
+
originalConfig._retry = true;
|
|
263
|
+
try {
|
|
264
|
+
const { accessToken: tokenString, permissions } = await this.refreshTokenInternal();
|
|
265
|
+
const accessToken = `Bearer ${tokenString}`;
|
|
266
|
+
this.tokenData.permissions = permissions;
|
|
267
|
+
|
|
268
|
+
delete originalConfig.headers['Authorization']
|
|
269
|
+
|
|
270
|
+
originalConfig.headers['Authorization'] = accessToken;
|
|
271
|
+
|
|
272
|
+
this.configuration.accessToken = accessToken;
|
|
273
|
+
this.tokenData.accessToken = tokenString;
|
|
274
|
+
|
|
275
|
+
this.storeTokenData(this.tokenData);
|
|
276
|
+
|
|
277
|
+
return axios(originalConfig);
|
|
278
|
+
} catch (_error) {
|
|
279
|
+
if (_error.response && _error.response.data) {
|
|
280
|
+
return Promise.reject(_error.response.data);
|
|
281
|
+
}
|
|
282
|
+
return Promise.reject(_error);
|
|
283
|
+
}
|
|
284
|
+
}
|
|
285
|
+
} else if (err.message === NETWORK_ERROR_MESSAGE
|
|
286
|
+
&& originalConfig.headers.hasOwnProperty('Authorization')
|
|
287
|
+
&& _retry_count < 4
|
|
288
|
+
) {
|
|
289
|
+
_retry_count++;
|
|
290
|
+
try {
|
|
291
|
+
const { accessToken: tokenString, permissions } = await this.refreshTokenInternal();
|
|
292
|
+
const accessToken = `Bearer ${tokenString}`;
|
|
293
|
+
this.tokenData.permissions = permissions;
|
|
294
|
+
|
|
295
|
+
_retry = true;
|
|
296
|
+
originalConfig.headers['Authorization'] = accessToken;
|
|
297
|
+
|
|
298
|
+
this.configuration.accessToken = accessToken;
|
|
299
|
+
this.tokenData.accessToken = tokenString;
|
|
300
|
+
|
|
301
|
+
this.storeTokenData(this.tokenData);
|
|
302
|
+
|
|
303
|
+
return axios.request({
|
|
304
|
+
...originalConfig,
|
|
305
|
+
});
|
|
306
|
+
} catch (_error) {
|
|
307
|
+
if (_error.response && _error.response.data) {
|
|
308
|
+
return Promise.reject(_error.response.data);
|
|
309
|
+
}
|
|
310
|
+
return Promise.reject(_error);
|
|
311
|
+
}
|
|
312
|
+
}
|
|
313
|
+
return Promise.reject(err);
|
|
314
|
+
}
|
|
315
|
+
);
|
|
316
|
+
}
|
|
317
|
+
};
|
|
318
|
+
|
|
319
|
+
/**
|
|
320
|
+
*
|
|
321
|
+
* @export
|
|
322
|
+
* @class RequiredError
|
|
323
|
+
* @extends {Error}
|
|
324
|
+
*/
|
|
325
|
+
export class RequiredError extends Error {
|
|
326
|
+
override name: "RequiredError" = "RequiredError";
|
|
327
|
+
constructor(public field: string, msg?: string) {
|
|
328
|
+
super(msg);
|
|
329
|
+
}
|
|
330
|
+
}
|
|
331
|
+
|