caprover-api 0.0.11 → 0.0.14
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/dist/api/ApiManager.d.ts +2 -1
- package/dist/api/ApiManager.js +4 -3
- package/dist/api/HttpClient.d.ts +3 -3
- package/dist/api/HttpClient.js +53 -15
- package/package.json +2 -2
- package/src/api/ApiManager.ts +8 -5
- package/src/api/HttpClient.ts +79 -17
package/dist/api/ApiManager.d.ts
CHANGED
|
@@ -42,10 +42,11 @@ export declare class SimpleAuthenticationProvider implements AuthenticationProvi
|
|
|
42
42
|
onAuthTokenUpdated(newAuthToken: string): void;
|
|
43
43
|
}
|
|
44
44
|
export default class ApiManager {
|
|
45
|
+
private authProvider;
|
|
45
46
|
private http;
|
|
46
47
|
constructor(baseDomain: string, authProvider: AuthenticationProvider);
|
|
47
48
|
destroy(): void;
|
|
48
|
-
login(password: string, otpToken?: string): Promise<
|
|
49
|
+
login(password: string, otpToken?: string): Promise<void>;
|
|
49
50
|
getAllThemes(): Promise<{
|
|
50
51
|
themes: CapRoverTheme[] | undefined;
|
|
51
52
|
}>;
|
package/dist/api/ApiManager.js
CHANGED
|
@@ -23,6 +23,7 @@ class SimpleAuthenticationProvider {
|
|
|
23
23
|
exports.SimpleAuthenticationProvider = SimpleAuthenticationProvider;
|
|
24
24
|
class ApiManager {
|
|
25
25
|
constructor(baseDomain, authProvider) {
|
|
26
|
+
this.authProvider = authProvider;
|
|
26
27
|
const self = this;
|
|
27
28
|
const URL = baseDomain + '/api/v2';
|
|
28
29
|
this.http = new HttpClient_1.default(URL, () => {
|
|
@@ -34,9 +35,6 @@ class ApiManager {
|
|
|
34
35
|
})
|
|
35
36
|
.then((authContent) => {
|
|
36
37
|
return self.login(authContent.password, authContent.otpToken);
|
|
37
|
-
}) //
|
|
38
|
-
.then((authToken) => {
|
|
39
|
-
authProvider.onAuthTokenUpdated(authToken);
|
|
40
38
|
});
|
|
41
39
|
});
|
|
42
40
|
}
|
|
@@ -56,6 +54,9 @@ class ApiManager {
|
|
|
56
54
|
})
|
|
57
55
|
.then(function (data) {
|
|
58
56
|
return data.token;
|
|
57
|
+
})
|
|
58
|
+
.then((authToken) => {
|
|
59
|
+
self.authProvider.onAuthTokenUpdated(authToken);
|
|
59
60
|
});
|
|
60
61
|
}
|
|
61
62
|
getAllThemes() {
|
package/dist/api/HttpClient.d.ts
CHANGED
|
@@ -9,7 +9,7 @@ export default class HttpClient {
|
|
|
9
9
|
createHeaders(): Promise<any>;
|
|
10
10
|
destroy(): void;
|
|
11
11
|
fetch(method: 'GET' | 'POST', endpoint: string, variables: any): () => Promise<any>;
|
|
12
|
-
fetchInternal(method: 'GET' | 'POST', endpoint: string, variables: any): Promise<
|
|
13
|
-
getReq(endpoint: string, variables: any): Promise<
|
|
14
|
-
postReq(endpoint: string, variables: any): Promise<
|
|
12
|
+
fetchInternal(method: 'GET' | 'POST', endpoint: string, variables: any): Promise<any>;
|
|
13
|
+
getReq(endpoint: string, variables: any): Promise<any>;
|
|
14
|
+
postReq(endpoint: string, variables: any): Promise<any>;
|
|
15
15
|
}
|
package/dist/api/HttpClient.js
CHANGED
|
@@ -3,11 +3,54 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
3
3
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
4
|
};
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
const axios_1 = __importDefault(require("axios"));
|
|
7
6
|
const ErrorFactory_1 = __importDefault(require("./ErrorFactory"));
|
|
7
|
+
const cross_fetch_1 = __importDefault(require("cross-fetch"));
|
|
8
|
+
function buildQueryParams(params) {
|
|
9
|
+
if (!params || Object.keys(params).length === 0)
|
|
10
|
+
return '';
|
|
11
|
+
return ('?' +
|
|
12
|
+
Object.entries(params)
|
|
13
|
+
.map(([key, value]) => encodeURIComponent(key) + '=' + encodeURIComponent(value))
|
|
14
|
+
.join('&'));
|
|
15
|
+
}
|
|
8
16
|
let TOKEN_HEADER = 'x-captain-auth';
|
|
9
17
|
let NAMESPACE = 'x-namespace';
|
|
10
18
|
let CAPTAIN = 'captain';
|
|
19
|
+
class CrossFetchEngine {
|
|
20
|
+
static async post(url, variables, headers) {
|
|
21
|
+
const res = await (0, cross_fetch_1.default)(url, {
|
|
22
|
+
method: 'POST',
|
|
23
|
+
headers: {
|
|
24
|
+
'Content-Type': 'application/json',
|
|
25
|
+
...headers,
|
|
26
|
+
},
|
|
27
|
+
body: JSON.stringify(variables),
|
|
28
|
+
});
|
|
29
|
+
if (!res.ok) {
|
|
30
|
+
const errBody = await res.text();
|
|
31
|
+
throw new Error(`HTTP ${res.status}: ${errBody}`);
|
|
32
|
+
}
|
|
33
|
+
return res.json();
|
|
34
|
+
}
|
|
35
|
+
static async get(url, params, headers) {
|
|
36
|
+
const fullUrl = url +
|
|
37
|
+
(params && Object.keys(params).length > 0
|
|
38
|
+
? buildQueryParams(params)
|
|
39
|
+
: '');
|
|
40
|
+
const res = await (0, cross_fetch_1.default)(fullUrl, {
|
|
41
|
+
method: 'GET',
|
|
42
|
+
headers: {
|
|
43
|
+
'Content-Type': 'application/json',
|
|
44
|
+
...headers,
|
|
45
|
+
},
|
|
46
|
+
});
|
|
47
|
+
if (!res.ok) {
|
|
48
|
+
const errBody = await res.text();
|
|
49
|
+
throw new Error(`HTTP ${res.status}: ${errBody}`);
|
|
50
|
+
}
|
|
51
|
+
return res.json();
|
|
52
|
+
}
|
|
53
|
+
}
|
|
11
54
|
class HttpClient {
|
|
12
55
|
constructor(baseUrl, authTokenProvider, onLoginRequested) {
|
|
13
56
|
this.baseUrl = baseUrl;
|
|
@@ -42,18 +85,18 @@ class HttpClient {
|
|
|
42
85
|
.then(function () {
|
|
43
86
|
return self.fetchInternal(method, endpoint, variables); //
|
|
44
87
|
})
|
|
45
|
-
.then(function (
|
|
46
|
-
const data = axiosResponse.data; // this is an axios thing!
|
|
88
|
+
.then(function (fetchResponse) {
|
|
47
89
|
if (
|
|
48
90
|
// if we ever get STATUS_ERROR_NOT_AUTHORIZED when trying to log in, we will end up in an infinite loop!
|
|
49
|
-
|
|
91
|
+
fetchResponse.status ===
|
|
92
|
+
ErrorFactory_1.default.STATUS_AUTH_TOKEN_INVALID) {
|
|
50
93
|
return self
|
|
51
94
|
.onLoginRequested() //
|
|
52
95
|
.then(function () {
|
|
53
96
|
return self
|
|
54
97
|
.fetchInternal(method, endpoint, variables)
|
|
55
|
-
.then(function (
|
|
56
|
-
return
|
|
98
|
+
.then(function (httpResponse) {
|
|
99
|
+
return httpResponse;
|
|
57
100
|
});
|
|
58
101
|
})
|
|
59
102
|
.catch(function (error) {
|
|
@@ -61,7 +104,7 @@ class HttpClient {
|
|
|
61
104
|
});
|
|
62
105
|
}
|
|
63
106
|
else {
|
|
64
|
-
return
|
|
107
|
+
return fetchResponse;
|
|
65
108
|
}
|
|
66
109
|
})
|
|
67
110
|
.then(function (data) {
|
|
@@ -105,10 +148,7 @@ class HttpClient {
|
|
|
105
148
|
return self.createHeaders();
|
|
106
149
|
})
|
|
107
150
|
.then(function (headers) {
|
|
108
|
-
return
|
|
109
|
-
params: variables,
|
|
110
|
-
headers: headers,
|
|
111
|
-
});
|
|
151
|
+
return CrossFetchEngine.get(self.baseUrl + endpoint, variables, headers);
|
|
112
152
|
})
|
|
113
153
|
.then(function (data) {
|
|
114
154
|
// console.log(data);
|
|
@@ -122,10 +162,8 @@ class HttpClient {
|
|
|
122
162
|
return self.createHeaders();
|
|
123
163
|
})
|
|
124
164
|
.then(function (headers) {
|
|
125
|
-
return
|
|
126
|
-
|
|
127
|
-
}); //
|
|
128
|
-
}) //
|
|
165
|
+
return CrossFetchEngine.post(self.baseUrl + endpoint, variables, headers);
|
|
166
|
+
})
|
|
129
167
|
.then(function (data) {
|
|
130
168
|
// console.log(data);
|
|
131
169
|
return data;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "caprover-api",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.14",
|
|
4
4
|
"description": "API client for CapRover",
|
|
5
5
|
"types": "dist/index.d.ts",
|
|
6
6
|
"main": "dist/index.js",
|
|
@@ -25,7 +25,7 @@
|
|
|
25
25
|
"author": "",
|
|
26
26
|
"license": "ISC",
|
|
27
27
|
"dependencies": {
|
|
28
|
-
"
|
|
28
|
+
"cross-fetch": "^4.1.0"
|
|
29
29
|
},
|
|
30
30
|
"devDependencies": {
|
|
31
31
|
"@types/node": "^24.0.10",
|
package/src/api/ApiManager.ts
CHANGED
|
@@ -65,7 +65,10 @@ export class SimpleAuthenticationProvider implements AuthenticationProvider {
|
|
|
65
65
|
export default class ApiManager {
|
|
66
66
|
private http: HttpClient
|
|
67
67
|
|
|
68
|
-
constructor(
|
|
68
|
+
constructor(
|
|
69
|
+
baseDomain: string,
|
|
70
|
+
private authProvider: AuthenticationProvider
|
|
71
|
+
) {
|
|
69
72
|
const self = this
|
|
70
73
|
const URL = baseDomain + '/api/v2'
|
|
71
74
|
this.http = new HttpClient(
|
|
@@ -83,9 +86,6 @@ export default class ApiManager {
|
|
|
83
86
|
authContent.password,
|
|
84
87
|
authContent.otpToken
|
|
85
88
|
)
|
|
86
|
-
}) //
|
|
87
|
-
.then((authToken) => {
|
|
88
|
-
authProvider.onAuthTokenUpdated(authToken)
|
|
89
89
|
})
|
|
90
90
|
}
|
|
91
91
|
)
|
|
@@ -95,7 +95,7 @@ export default class ApiManager {
|
|
|
95
95
|
this.http.destroy()
|
|
96
96
|
}
|
|
97
97
|
|
|
98
|
-
login(password: string, otpToken?: string): Promise<
|
|
98
|
+
login(password: string, otpToken?: string): Promise<void> {
|
|
99
99
|
const self = this
|
|
100
100
|
const http = self.http
|
|
101
101
|
|
|
@@ -112,6 +112,9 @@ export default class ApiManager {
|
|
|
112
112
|
.then(function (data) {
|
|
113
113
|
return data.token
|
|
114
114
|
})
|
|
115
|
+
.then((authToken) => {
|
|
116
|
+
self.authProvider.onAuthTokenUpdated(authToken)
|
|
117
|
+
})
|
|
115
118
|
}
|
|
116
119
|
|
|
117
120
|
getAllThemes(): Promise<{ themes: CapRoverTheme[] | undefined }> {
|
package/src/api/HttpClient.ts
CHANGED
|
@@ -1,11 +1,71 @@
|
|
|
1
|
-
import axios from 'axios'
|
|
2
1
|
import ErrorFactory from './ErrorFactory'
|
|
3
|
-
import
|
|
2
|
+
import fetch from 'cross-fetch'
|
|
4
3
|
|
|
4
|
+
function buildQueryParams(params: Record<string, any>): string {
|
|
5
|
+
if (!params || Object.keys(params).length === 0) return ''
|
|
6
|
+
return (
|
|
7
|
+
'?' +
|
|
8
|
+
Object.entries(params)
|
|
9
|
+
.map(
|
|
10
|
+
([key, value]) =>
|
|
11
|
+
encodeURIComponent(key) + '=' + encodeURIComponent(value)
|
|
12
|
+
)
|
|
13
|
+
.join('&')
|
|
14
|
+
)
|
|
15
|
+
}
|
|
5
16
|
let TOKEN_HEADER = 'x-captain-auth'
|
|
6
17
|
let NAMESPACE = 'x-namespace'
|
|
7
18
|
let CAPTAIN = 'captain'
|
|
8
19
|
|
|
20
|
+
class CrossFetchEngine {
|
|
21
|
+
public static async post(
|
|
22
|
+
url: string,
|
|
23
|
+
variables: Record<string, any>,
|
|
24
|
+
headers: Record<string, string>
|
|
25
|
+
): Promise<any> {
|
|
26
|
+
const res = await fetch(url, {
|
|
27
|
+
method: 'POST',
|
|
28
|
+
headers: {
|
|
29
|
+
'Content-Type': 'application/json',
|
|
30
|
+
...headers,
|
|
31
|
+
},
|
|
32
|
+
body: JSON.stringify(variables),
|
|
33
|
+
})
|
|
34
|
+
|
|
35
|
+
if (!res.ok) {
|
|
36
|
+
const errBody = await res.text()
|
|
37
|
+
throw new Error(`HTTP ${res.status}: ${errBody}`)
|
|
38
|
+
}
|
|
39
|
+
return res.json()
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
public static async get(
|
|
43
|
+
url: string,
|
|
44
|
+
params: Record<string, any>,
|
|
45
|
+
headers: Record<string, string>
|
|
46
|
+
): Promise<any> {
|
|
47
|
+
const fullUrl =
|
|
48
|
+
url +
|
|
49
|
+
(params && Object.keys(params).length > 0
|
|
50
|
+
? buildQueryParams(params)
|
|
51
|
+
: '')
|
|
52
|
+
|
|
53
|
+
const res = await fetch(fullUrl, {
|
|
54
|
+
method: 'GET',
|
|
55
|
+
headers: {
|
|
56
|
+
'Content-Type': 'application/json',
|
|
57
|
+
...headers,
|
|
58
|
+
},
|
|
59
|
+
})
|
|
60
|
+
|
|
61
|
+
if (!res.ok) {
|
|
62
|
+
const errBody = await res.text()
|
|
63
|
+
throw new Error(`HTTP ${res.status}: ${errBody}`)
|
|
64
|
+
}
|
|
65
|
+
return res.json()
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
|
|
9
69
|
export default class HttpClient {
|
|
10
70
|
public readonly GET = 'GET'
|
|
11
71
|
public readonly POST = 'POST'
|
|
@@ -46,27 +106,26 @@ export default class HttpClient {
|
|
|
46
106
|
.then(function () {
|
|
47
107
|
return self.fetchInternal(method, endpoint, variables) //
|
|
48
108
|
})
|
|
49
|
-
.then(function (
|
|
50
|
-
const data = axiosResponse.data // this is an axios thing!
|
|
51
|
-
|
|
109
|
+
.then(function (fetchResponse) {
|
|
52
110
|
if (
|
|
53
111
|
// if we ever get STATUS_ERROR_NOT_AUTHORIZED when trying to log in, we will end up in an infinite loop!
|
|
54
|
-
|
|
112
|
+
fetchResponse.status ===
|
|
113
|
+
ErrorFactory.STATUS_AUTH_TOKEN_INVALID
|
|
55
114
|
) {
|
|
56
115
|
return self
|
|
57
116
|
.onLoginRequested() //
|
|
58
117
|
.then(function () {
|
|
59
118
|
return self
|
|
60
119
|
.fetchInternal(method, endpoint, variables)
|
|
61
|
-
.then(function (
|
|
62
|
-
return
|
|
120
|
+
.then(function (httpResponse) {
|
|
121
|
+
return httpResponse
|
|
63
122
|
})
|
|
64
123
|
})
|
|
65
124
|
.catch(function (error) {
|
|
66
125
|
return Promise.reject(error)
|
|
67
126
|
})
|
|
68
127
|
} else {
|
|
69
|
-
return
|
|
128
|
+
return fetchResponse
|
|
70
129
|
}
|
|
71
130
|
})
|
|
72
131
|
.then(function (data) {
|
|
@@ -115,10 +174,11 @@ export default class HttpClient {
|
|
|
115
174
|
return self.createHeaders()
|
|
116
175
|
})
|
|
117
176
|
.then(function (headers) {
|
|
118
|
-
return
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
177
|
+
return CrossFetchEngine.get(
|
|
178
|
+
self.baseUrl + endpoint,
|
|
179
|
+
variables,
|
|
180
|
+
headers
|
|
181
|
+
)
|
|
122
182
|
})
|
|
123
183
|
.then(function (data) {
|
|
124
184
|
// console.log(data);
|
|
@@ -133,10 +193,12 @@ export default class HttpClient {
|
|
|
133
193
|
return self.createHeaders()
|
|
134
194
|
})
|
|
135
195
|
.then(function (headers) {
|
|
136
|
-
return
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
196
|
+
return CrossFetchEngine.post(
|
|
197
|
+
self.baseUrl + endpoint,
|
|
198
|
+
variables,
|
|
199
|
+
headers
|
|
200
|
+
)
|
|
201
|
+
})
|
|
140
202
|
.then(function (data) {
|
|
141
203
|
// console.log(data);
|
|
142
204
|
return data
|