@monocloud/management-core 0.1.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 +21 -0
- package/README.md +41 -0
- package/dist/index.cjs +350 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +159 -0
- package/dist/index.d.mts +159 -0
- package/dist/index.mjs +332 -0
- package/dist/index.mjs.map +1 -0
- package/package.json +44 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 MonoCloud
|
|
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/README.md
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
<div align="center">
|
|
2
|
+
<a href="https://www.monocloud.com?utm_source=github&utm_medium=monocloud_js" target="_blank" rel="noopener noreferrer">
|
|
3
|
+
<picture>
|
|
4
|
+
<img src="https://raw.githubusercontent.com/monocld/monocloud-management-js/refs/heads/main/MonoCloud.png" height="100" alt="MonoCloud Logo">
|
|
5
|
+
</picture>
|
|
6
|
+
</a>
|
|
7
|
+
<p>Secure, simple auth for everything</p>
|
|
8
|
+
</div>
|
|
9
|
+
|
|
10
|
+
<br /><br />
|
|
11
|
+
|
|
12
|
+
## Introduction
|
|
13
|
+
|
|
14
|
+
**MonoCloud Management SDK for JavaScript – programmatically manage apps, policies, configurations, and users via the MonoCloud Management APIs.**
|
|
15
|
+
|
|
16
|
+
[MonoCloud](https://www.monocloud.com) is a modern, developer-friendly Identity & Access Management platform.
|
|
17
|
+
|
|
18
|
+
This SDK provides a full-featured, typed JavaScript client for interacting with the **MonoCloud Management APIs**, allowing you to automate tenant administration programmatically.
|
|
19
|
+
|
|
20
|
+
## ⚠️ Warning
|
|
21
|
+
|
|
22
|
+
**This is a shared library** for [@monocloud/management-admin](https://www.npmjs.com/package/@monocloud/management-admin) and [@monocloud/management-identity](https://www.npmjs.com/package/@monocloud/management-identity).
|
|
23
|
+
|
|
24
|
+
## 📘 Documentation
|
|
25
|
+
|
|
26
|
+
- **Documentation:** https://www.monocloud.com/docs
|
|
27
|
+
|
|
28
|
+
## 🤝 Contributing & Support
|
|
29
|
+
|
|
30
|
+
### Issues & Feedback
|
|
31
|
+
|
|
32
|
+
- Use **GitHub Issues** for bug reports and feature requests.
|
|
33
|
+
- For tenant or account-specific help, contact MonoCloud Support through your dashboard.
|
|
34
|
+
|
|
35
|
+
### Security
|
|
36
|
+
|
|
37
|
+
Do **not** report security issues publicly. Please follow the contact instructions at: https://www.monocloud.com/contact
|
|
38
|
+
|
|
39
|
+
## 📄 License
|
|
40
|
+
|
|
41
|
+
Licensed under the **MIT License**. See the included [`LICENSE`](https://github.com/monocld/monocloud-management-js/blob/main/LICENSE) file.
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,350 @@
|
|
|
1
|
+
|
|
2
|
+
//#region src/exceptions/monocloud-exception.ts
|
|
3
|
+
var MonoCloudException = class extends Error {};
|
|
4
|
+
|
|
5
|
+
//#endregion
|
|
6
|
+
//#region src/exceptions/monocloud-request-exception.ts
|
|
7
|
+
var MonoCloudRequestException = class extends MonoCloudException {
|
|
8
|
+
constructor(...args) {
|
|
9
|
+
let problemDetails;
|
|
10
|
+
let errorMessage;
|
|
11
|
+
if (Array.isArray(args)) if (args.length === 2) {
|
|
12
|
+
problemDetails = args[0];
|
|
13
|
+
errorMessage = args[1];
|
|
14
|
+
} else if (args.length === 1 && typeof args[0] === "string") errorMessage = args[0];
|
|
15
|
+
else if (args.length === 1 && typeof args[0] === "object") {
|
|
16
|
+
problemDetails = args[0];
|
|
17
|
+
errorMessage = args[0].title;
|
|
18
|
+
} else throw new Error("Invalid arguments in constructor");
|
|
19
|
+
if (!errorMessage) throw new Error("Invalid error message");
|
|
20
|
+
super(errorMessage);
|
|
21
|
+
this.response = problemDetails;
|
|
22
|
+
}
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
//#endregion
|
|
26
|
+
//#region src/exceptions/monocloud-bad-request-exception.ts
|
|
27
|
+
var MonoCloudBadRequestException = class extends MonoCloudRequestException {
|
|
28
|
+
constructor(arg) {
|
|
29
|
+
if (typeof arg === "string") super(arg);
|
|
30
|
+
else super(arg);
|
|
31
|
+
}
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
//#endregion
|
|
35
|
+
//#region src/exceptions/monocloud-conflict-exception.ts
|
|
36
|
+
var MonoCloudConflictException = class extends MonoCloudRequestException {
|
|
37
|
+
constructor(arg) {
|
|
38
|
+
if (typeof arg === "string") super(arg);
|
|
39
|
+
else super(arg);
|
|
40
|
+
}
|
|
41
|
+
};
|
|
42
|
+
|
|
43
|
+
//#endregion
|
|
44
|
+
//#region src/exceptions/monocloud-error-code-validation-exception.ts
|
|
45
|
+
var MonoCloudErrorCodeValidationException = class extends MonoCloudRequestException {
|
|
46
|
+
constructor(response) {
|
|
47
|
+
super(response, `${response.title} : ${JSON.stringify(response.errors, void 0, 2)}`);
|
|
48
|
+
this.errors = response.errors;
|
|
49
|
+
}
|
|
50
|
+
};
|
|
51
|
+
|
|
52
|
+
//#endregion
|
|
53
|
+
//#region src/exceptions/monocloud-forbidden-exception.ts
|
|
54
|
+
var MonoCloudForbiddenException = class extends MonoCloudRequestException {
|
|
55
|
+
constructor(arg) {
|
|
56
|
+
if (typeof arg === "string") super(arg);
|
|
57
|
+
else super(arg);
|
|
58
|
+
}
|
|
59
|
+
};
|
|
60
|
+
|
|
61
|
+
//#endregion
|
|
62
|
+
//#region src/exceptions/monocloud-key-validation-exception.ts
|
|
63
|
+
var MonoCloudKeyValidationException = class extends MonoCloudRequestException {
|
|
64
|
+
constructor(response) {
|
|
65
|
+
super(response, `${response.title} : ${JSON.stringify(response.errors, void 0, 2)}`);
|
|
66
|
+
this.errors = response.errors;
|
|
67
|
+
}
|
|
68
|
+
};
|
|
69
|
+
|
|
70
|
+
//#endregion
|
|
71
|
+
//#region src/exceptions/monocloud-model-state-exception.ts
|
|
72
|
+
var MonoCloudModelStateException = class extends MonoCloudRequestException {
|
|
73
|
+
constructor(arg) {
|
|
74
|
+
if (typeof arg === "string") super(arg);
|
|
75
|
+
else super(arg);
|
|
76
|
+
}
|
|
77
|
+
};
|
|
78
|
+
|
|
79
|
+
//#endregion
|
|
80
|
+
//#region src/exceptions/monocloud-not-found-exception.ts
|
|
81
|
+
var MonoCloudNotFoundException = class extends MonoCloudRequestException {
|
|
82
|
+
constructor(arg) {
|
|
83
|
+
if (typeof arg === "string") super(arg);
|
|
84
|
+
else super(arg);
|
|
85
|
+
}
|
|
86
|
+
};
|
|
87
|
+
|
|
88
|
+
//#endregion
|
|
89
|
+
//#region src/exceptions/monocloud-resource-exhausted-exception.ts
|
|
90
|
+
var MonoCloudResourceExhaustedException = class extends MonoCloudRequestException {
|
|
91
|
+
constructor(arg) {
|
|
92
|
+
if (typeof arg === "string") super(arg);
|
|
93
|
+
else super(arg);
|
|
94
|
+
}
|
|
95
|
+
};
|
|
96
|
+
|
|
97
|
+
//#endregion
|
|
98
|
+
//#region src/exceptions/monocloud-server-exception.ts
|
|
99
|
+
var MonoCloudServerException = class extends MonoCloudRequestException {
|
|
100
|
+
constructor(arg) {
|
|
101
|
+
if (typeof arg === "string") super(arg);
|
|
102
|
+
else super(arg);
|
|
103
|
+
}
|
|
104
|
+
};
|
|
105
|
+
|
|
106
|
+
//#endregion
|
|
107
|
+
//#region src/exceptions/monocloud-unauthorized-exception.ts
|
|
108
|
+
var MonoCloudUnauthorizedException = class extends MonoCloudRequestException {
|
|
109
|
+
constructor(arg) {
|
|
110
|
+
if (typeof arg === "string") super(arg);
|
|
111
|
+
else super(arg);
|
|
112
|
+
}
|
|
113
|
+
};
|
|
114
|
+
|
|
115
|
+
//#endregion
|
|
116
|
+
//#region src/models/error-code-validation-error.ts
|
|
117
|
+
var ErrorCodeValidationError = class {
|
|
118
|
+
constructor(code, description, field) {
|
|
119
|
+
this.code = code;
|
|
120
|
+
this.description = description;
|
|
121
|
+
this.field = field;
|
|
122
|
+
}
|
|
123
|
+
};
|
|
124
|
+
|
|
125
|
+
//#endregion
|
|
126
|
+
//#region src/models/problem-details.ts
|
|
127
|
+
var ProblemDetails = class {
|
|
128
|
+
constructor(response) {
|
|
129
|
+
this.type = response.type;
|
|
130
|
+
this.title = response.title;
|
|
131
|
+
this.status = response.status;
|
|
132
|
+
this.detail = response.detail;
|
|
133
|
+
this.instance = response.instance;
|
|
134
|
+
Object.keys(response).filter((x) => x !== "type" && x !== "title" && x !== "status" && x !== "instance").forEach((key) => {
|
|
135
|
+
this[key] = response[key];
|
|
136
|
+
});
|
|
137
|
+
}
|
|
138
|
+
};
|
|
139
|
+
|
|
140
|
+
//#endregion
|
|
141
|
+
//#region src/models/error-code-validation-problem-details.ts
|
|
142
|
+
var ErrorCodeValidationProblemDetails = class extends ProblemDetails {
|
|
143
|
+
constructor(response) {
|
|
144
|
+
super(response);
|
|
145
|
+
this.errors = response.errors.map((err) => new ErrorCodeValidationError(err.code, err.description, err.field));
|
|
146
|
+
}
|
|
147
|
+
};
|
|
148
|
+
|
|
149
|
+
//#endregion
|
|
150
|
+
//#region src/models/key-validation-problem-details.ts
|
|
151
|
+
var KeyValidationProblemDetails = class extends ProblemDetails {
|
|
152
|
+
constructor(response) {
|
|
153
|
+
super(response);
|
|
154
|
+
this.errors = response.errors;
|
|
155
|
+
}
|
|
156
|
+
};
|
|
157
|
+
|
|
158
|
+
//#endregion
|
|
159
|
+
//#region src/models/monocloud-response.ts
|
|
160
|
+
var MonoCloudResponse = class {
|
|
161
|
+
constructor(status, headers, result) {
|
|
162
|
+
this.status = status;
|
|
163
|
+
this.headers = headers;
|
|
164
|
+
this.result = result;
|
|
165
|
+
}
|
|
166
|
+
};
|
|
167
|
+
|
|
168
|
+
//#endregion
|
|
169
|
+
//#region src/models/monocloud-page-response.ts
|
|
170
|
+
var MonoCloudPageResponse = class extends MonoCloudResponse {
|
|
171
|
+
constructor(status, headers, result, pageData) {
|
|
172
|
+
super(status, headers, result);
|
|
173
|
+
this.pageData = pageData;
|
|
174
|
+
}
|
|
175
|
+
};
|
|
176
|
+
|
|
177
|
+
//#endregion
|
|
178
|
+
//#region src/exceptions/validation-exception-types.ts
|
|
179
|
+
const ValidationExceptionTypes = {
|
|
180
|
+
ValidationError: "https://httpstatuses.io/422#validation-error",
|
|
181
|
+
IdentityValidationError: "https://httpstatuses.io/422#identity-validation-error"
|
|
182
|
+
};
|
|
183
|
+
|
|
184
|
+
//#endregion
|
|
185
|
+
//#region src/exceptions/monocloud-exception-handler.ts
|
|
186
|
+
var MonoCloudExceptionHandler = class {
|
|
187
|
+
static ThrowProblemErr(problemDetails) {
|
|
188
|
+
switch (problemDetails.status) {
|
|
189
|
+
case 400: throw new MonoCloudBadRequestException(problemDetails);
|
|
190
|
+
case 401: throw new MonoCloudUnauthorizedException(problemDetails);
|
|
191
|
+
case 403: throw new MonoCloudUnauthorizedException(problemDetails);
|
|
192
|
+
case 404: throw new MonoCloudNotFoundException(problemDetails);
|
|
193
|
+
case 409: throw new MonoCloudConflictException(problemDetails);
|
|
194
|
+
case 422:
|
|
195
|
+
if (problemDetails instanceof ErrorCodeValidationProblemDetails) throw new MonoCloudErrorCodeValidationException(problemDetails);
|
|
196
|
+
if (problemDetails instanceof KeyValidationProblemDetails) throw new MonoCloudKeyValidationException(problemDetails);
|
|
197
|
+
throw new MonoCloudModelStateException(problemDetails);
|
|
198
|
+
case 429: throw new MonoCloudResourceExhaustedException(problemDetails);
|
|
199
|
+
case 500: throw new MonoCloudServerException(problemDetails);
|
|
200
|
+
default:
|
|
201
|
+
var _problemDetails$title;
|
|
202
|
+
throw new MonoCloudException((_problemDetails$title = problemDetails.title) !== null && _problemDetails$title !== void 0 ? _problemDetails$title : "An Unknown Error Occured");
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
static ThrowErr(statusCode, message) {
|
|
206
|
+
switch (statusCode) {
|
|
207
|
+
case 400: throw new MonoCloudBadRequestException(message !== null && message !== void 0 ? message : "Bad Request");
|
|
208
|
+
case 401: throw new MonoCloudUnauthorizedException(message !== null && message !== void 0 ? message : "Unauthorized");
|
|
209
|
+
case 403: throw new MonoCloudUnauthorizedException(message !== null && message !== void 0 ? message : "Forbidden");
|
|
210
|
+
case 404: throw new MonoCloudNotFoundException(message !== null && message !== void 0 ? message : "Not Found");
|
|
211
|
+
case 409: throw new MonoCloudConflictException(message !== null && message !== void 0 ? message : "Conflict");
|
|
212
|
+
case 422: throw new MonoCloudModelStateException(message !== null && message !== void 0 ? message : "Unprocessable entity");
|
|
213
|
+
case 429: throw new MonoCloudResourceExhaustedException(message !== null && message !== void 0 ? message : "Resource Exhausted");
|
|
214
|
+
case 500: throw new MonoCloudServerException(message !== null && message !== void 0 ? message : "Server Error");
|
|
215
|
+
default: throw new MonoCloudException(message !== null && message !== void 0 ? message : "An Unknown Error Occured");
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
};
|
|
219
|
+
|
|
220
|
+
//#endregion
|
|
221
|
+
//#region src/base/monocloud-client-base.ts
|
|
222
|
+
var MonoCloudClientBase = class {
|
|
223
|
+
constructor(configuration, fetcher) {
|
|
224
|
+
if (fetcher) this.fetcher = fetcher;
|
|
225
|
+
else {
|
|
226
|
+
if (!configuration) throw new MonoCloudException("Configuration is required");
|
|
227
|
+
if (!configuration.domain) throw new MonoCloudException("Tenant Domain is required");
|
|
228
|
+
if (!configuration.apiKey) throw new MonoCloudException("Api Key is required");
|
|
229
|
+
const headers = {
|
|
230
|
+
"X-API-KEY": configuration.apiKey,
|
|
231
|
+
"Content-Type": "application/json"
|
|
232
|
+
};
|
|
233
|
+
const baseUrl = `${this.sanitizeUrl(configuration.domain)}/api/`;
|
|
234
|
+
this.fetcher = async (input, init) => {
|
|
235
|
+
var _configuration$config, _configuration$config2;
|
|
236
|
+
const url = new URL(input, baseUrl);
|
|
237
|
+
const signal = AbortSignal.timeout((_configuration$config = (_configuration$config2 = configuration.config) === null || _configuration$config2 === void 0 ? void 0 : _configuration$config2.timeout) !== null && _configuration$config !== void 0 ? _configuration$config : 1e4);
|
|
238
|
+
signal.throwIfAborted();
|
|
239
|
+
return await fetch(url.toString(), {
|
|
240
|
+
...init,
|
|
241
|
+
headers,
|
|
242
|
+
signal
|
|
243
|
+
});
|
|
244
|
+
};
|
|
245
|
+
}
|
|
246
|
+
}
|
|
247
|
+
async processRequest(request) {
|
|
248
|
+
try {
|
|
249
|
+
const url = this.buildUrl(request.url, request.queryParams);
|
|
250
|
+
const response = await this.fetcher(url, {
|
|
251
|
+
method: request.method,
|
|
252
|
+
body: request.body ? JSON.stringify(request.body) : void 0
|
|
253
|
+
});
|
|
254
|
+
if (!response.ok) await this.HandleErrorResponse(response);
|
|
255
|
+
const headers = {};
|
|
256
|
+
response.headers.forEach((value, key) => {
|
|
257
|
+
headers[key] = value;
|
|
258
|
+
});
|
|
259
|
+
const resp = response.body ? await response.text() : null;
|
|
260
|
+
return new MonoCloudResponse(response.status, headers, (resp === null || resp === void 0 ? void 0 : resp.length) ? JSON.parse(resp) : null);
|
|
261
|
+
} catch (e) {
|
|
262
|
+
if (e instanceof MonoCloudException) throw e;
|
|
263
|
+
if (e.name === "TimeoutError") throw new MonoCloudException(e.message);
|
|
264
|
+
throw new MonoCloudException("Something went wrong.");
|
|
265
|
+
}
|
|
266
|
+
}
|
|
267
|
+
async processPaginatedRequest(request) {
|
|
268
|
+
try {
|
|
269
|
+
const url = this.buildUrl(request.url, request.queryParams);
|
|
270
|
+
const response = await this.fetcher(url, {
|
|
271
|
+
method: request.method,
|
|
272
|
+
body: request.body ? JSON.stringify(request.body) : void 0
|
|
273
|
+
});
|
|
274
|
+
if (!response.ok) await this.HandleErrorResponse(response);
|
|
275
|
+
const headers = {};
|
|
276
|
+
response.headers.forEach((value, key) => {
|
|
277
|
+
headers[key] = value;
|
|
278
|
+
});
|
|
279
|
+
const paginationData = this.resolvePaginationHeader(response.headers);
|
|
280
|
+
return new MonoCloudPageResponse(response.status, headers, response.body ? await response.json() : null, paginationData);
|
|
281
|
+
} catch (e) {
|
|
282
|
+
if (e instanceof MonoCloudException) throw e;
|
|
283
|
+
if (e.name === "TimeoutError") throw new MonoCloudException(e.message);
|
|
284
|
+
throw new MonoCloudException("Something went wrong.");
|
|
285
|
+
}
|
|
286
|
+
}
|
|
287
|
+
async HandleErrorResponse(response) {
|
|
288
|
+
const contentType = response.headers.get("content-type");
|
|
289
|
+
if (contentType === null || contentType === void 0 ? void 0 : contentType.startsWith("application/problem+json")) {
|
|
290
|
+
const body = await response.json();
|
|
291
|
+
let result = body ? new ProblemDetails(body) : void 0;
|
|
292
|
+
if ((result === null || result === void 0 ? void 0 : result.type) === ValidationExceptionTypes.IdentityValidationError) result = new ErrorCodeValidationProblemDetails(result);
|
|
293
|
+
if ((result === null || result === void 0 ? void 0 : result.type) === ValidationExceptionTypes.ValidationError) result = new KeyValidationProblemDetails(result);
|
|
294
|
+
if (!result) throw new MonoCloudException("Invalid body");
|
|
295
|
+
MonoCloudExceptionHandler.ThrowProblemErr(result);
|
|
296
|
+
}
|
|
297
|
+
const respStrng = await response.text();
|
|
298
|
+
MonoCloudExceptionHandler.ThrowErr(response.status, respStrng && respStrng !== "" ? respStrng : response.statusText);
|
|
299
|
+
}
|
|
300
|
+
sanitizeUrl(url) {
|
|
301
|
+
let u = url;
|
|
302
|
+
if (!u.startsWith("https://")) u = `https://${u}`;
|
|
303
|
+
if (u.endsWith("/")) u = u.substring(0, u.length - 1);
|
|
304
|
+
return u;
|
|
305
|
+
}
|
|
306
|
+
resolvePaginationHeader(headers) {
|
|
307
|
+
var _pageData$page_size, _pageData$current_pag, _pageData$total_count, _pageData$has_previou, _pageData$has_next;
|
|
308
|
+
const paginationHeader = headers.get("x-pagination");
|
|
309
|
+
const pageData = paginationHeader ? JSON.parse(paginationHeader) : void 0;
|
|
310
|
+
return {
|
|
311
|
+
page_size: (_pageData$page_size = pageData === null || pageData === void 0 ? void 0 : pageData.page_size) !== null && _pageData$page_size !== void 0 ? _pageData$page_size : 0,
|
|
312
|
+
current_page: (_pageData$current_pag = pageData === null || pageData === void 0 ? void 0 : pageData.current_page) !== null && _pageData$current_pag !== void 0 ? _pageData$current_pag : 0,
|
|
313
|
+
total_count: (_pageData$total_count = pageData === null || pageData === void 0 ? void 0 : pageData.total_count) !== null && _pageData$total_count !== void 0 ? _pageData$total_count : 0,
|
|
314
|
+
has_previous: (_pageData$has_previou = pageData === null || pageData === void 0 ? void 0 : pageData.has_previous) !== null && _pageData$has_previou !== void 0 ? _pageData$has_previou : false,
|
|
315
|
+
has_next: (_pageData$has_next = pageData === null || pageData === void 0 ? void 0 : pageData.has_next) !== null && _pageData$has_next !== void 0 ? _pageData$has_next : false
|
|
316
|
+
};
|
|
317
|
+
}
|
|
318
|
+
buildUrl(url, queryParams) {
|
|
319
|
+
let urlStr = url;
|
|
320
|
+
if (urlStr.startsWith("/")) urlStr = urlStr.substring(1, urlStr.length);
|
|
321
|
+
if (!queryParams) return urlStr;
|
|
322
|
+
urlStr += "?";
|
|
323
|
+
Object.keys(queryParams).forEach((key) => {
|
|
324
|
+
urlStr += `${key}=${encodeURIComponent(queryParams[key])}&`;
|
|
325
|
+
});
|
|
326
|
+
urlStr = urlStr.substring(0, urlStr.length - 1);
|
|
327
|
+
return urlStr;
|
|
328
|
+
}
|
|
329
|
+
};
|
|
330
|
+
|
|
331
|
+
//#endregion
|
|
332
|
+
exports.ErrorCodeValidationProblemDetails = ErrorCodeValidationProblemDetails;
|
|
333
|
+
exports.KeyValidationProblemDetails = KeyValidationProblemDetails;
|
|
334
|
+
exports.MonoCloudBadRequestException = MonoCloudBadRequestException;
|
|
335
|
+
exports.MonoCloudClientBase = MonoCloudClientBase;
|
|
336
|
+
exports.MonoCloudConflictException = MonoCloudConflictException;
|
|
337
|
+
exports.MonoCloudErrorCodeValidationException = MonoCloudErrorCodeValidationException;
|
|
338
|
+
exports.MonoCloudException = MonoCloudException;
|
|
339
|
+
exports.MonoCloudForbiddenException = MonoCloudForbiddenException;
|
|
340
|
+
exports.MonoCloudKeyValidationException = MonoCloudKeyValidationException;
|
|
341
|
+
exports.MonoCloudModelStateException = MonoCloudModelStateException;
|
|
342
|
+
exports.MonoCloudNotFoundException = MonoCloudNotFoundException;
|
|
343
|
+
exports.MonoCloudPageResponse = MonoCloudPageResponse;
|
|
344
|
+
exports.MonoCloudRequestException = MonoCloudRequestException;
|
|
345
|
+
exports.MonoCloudResourceExhaustedException = MonoCloudResourceExhaustedException;
|
|
346
|
+
exports.MonoCloudResponse = MonoCloudResponse;
|
|
347
|
+
exports.MonoCloudServerException = MonoCloudServerException;
|
|
348
|
+
exports.MonoCloudUnauthorizedException = MonoCloudUnauthorizedException;
|
|
349
|
+
exports.ProblemDetails = ProblemDetails;
|
|
350
|
+
//# sourceMappingURL=index.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.cjs","names":["problemDetails: ProblemDetails | undefined","errorMessage: string | undefined","headers: Record<string, string>","headers: Record<string, any>","e: any"],"sources":["../src/exceptions/monocloud-exception.ts","../src/exceptions/monocloud-request-exception.ts","../src/exceptions/monocloud-bad-request-exception.ts","../src/exceptions/monocloud-conflict-exception.ts","../src/exceptions/monocloud-error-code-validation-exception.ts","../src/exceptions/monocloud-forbidden-exception.ts","../src/exceptions/monocloud-key-validation-exception.ts","../src/exceptions/monocloud-model-state-exception.ts","../src/exceptions/monocloud-not-found-exception.ts","../src/exceptions/monocloud-resource-exhausted-exception.ts","../src/exceptions/monocloud-server-exception.ts","../src/exceptions/monocloud-unauthorized-exception.ts","../src/models/error-code-validation-error.ts","../src/models/problem-details.ts","../src/models/error-code-validation-problem-details.ts","../src/models/key-validation-problem-details.ts","../src/models/monocloud-response.ts","../src/models/monocloud-page-response.ts","../src/exceptions/validation-exception-types.ts","../src/exceptions/monocloud-exception-handler.ts","../src/base/monocloud-client-base.ts"],"sourcesContent":["/// <summary>\n/// The MonoCloud Exception\n/// </summary>\nexport class MonoCloudException extends Error {}\n","import { ProblemDetails } from '../models/problem-details';\nimport { MonoCloudException } from './monocloud-exception';\n\nexport class MonoCloudRequestException extends MonoCloudException {\n /// <summary>\n /// The problem details received from the server.\n /// </summary>\n response?: ProblemDetails;\n\n /// <summary>\n /// Initializes the MonoCloudRequestException Class\n /// </summary>\n /// <param name=\"response\">The problem details returned from the server.</param>\n constructor(response: ProblemDetails);\n\n /// <summary>\n /// Initializes the MonoCloudRequestException Class\n /// </summary>\n /// <param name=\"message\">The error message.</param>\n constructor(message: string);\n\n /// <summary>\n /// Initializes the MonoCloudRequestException Class\n /// </summary>\n /// <param name=\"response\">The problem details returned from the server.</param>\n /// <param name=\"message\">The error message.</param>\n constructor(response: ProblemDetails, message: string);\n\n constructor(...args: [ProblemDetails, string] | [ProblemDetails] | [string]) {\n let problemDetails: ProblemDetails | undefined;\n let errorMessage: string | undefined;\n\n if (Array.isArray(args)) {\n if (args.length === 2) {\n problemDetails = args[0];\n errorMessage = args[1];\n } else if (args.length === 1 && typeof args[0] === 'string') {\n errorMessage = args[0];\n } else if (args.length === 1 && typeof args[0] === 'object') {\n problemDetails = args[0];\n errorMessage = args[0].title;\n } else {\n throw new Error('Invalid arguments in constructor');\n }\n }\n\n if (!errorMessage) {\n throw new Error('Invalid error message');\n }\n\n super(errorMessage);\n this.response = problemDetails;\n }\n}\n","import { ProblemDetails } from '../models/problem-details';\nimport { MonoCloudRequestException } from './monocloud-request-exception';\n\n/// <summary>\n/// The MonoCloud Bad Request Exception\n/// </summary>\nexport class MonoCloudBadRequestException extends MonoCloudRequestException {\n /// <summary>\n /// Initializes the MonoCloudBadRequestException Class\n /// </summary>\n /// <param name=\"response\">The problem details returned from the server.</param>\n constructor(response: ProblemDetails);\n\n /// <summary>\n /// Initializes the MonoCloudBadRequestException Class\n /// </summary>\n /// <param name=\"message\">The error message.</param>\n constructor(message: string);\n\n constructor(arg: ProblemDetails | string) {\n if (typeof arg === 'string') {\n super(arg);\n } else {\n super(arg);\n }\n }\n}\n","import { ProblemDetails } from '../models/problem-details';\nimport { MonoCloudRequestException } from './monocloud-request-exception';\n\n/// <summary>\n/// The MonoCloud Conflict Exception\n/// </summary>\nexport class MonoCloudConflictException extends MonoCloudRequestException {\n /// <summary>\n /// Initializes the MonoCloudConflictException Class\n /// </summary>\n /// <param name=\"response\">The problem details returned from the server.</param>\n constructor(response: ProblemDetails);\n\n /// <summary>\n /// Initializes the MonoCloudConflictException Class\n /// </summary>\n /// <param name=\"message\">The error message.</param>\n constructor(message: string);\n\n constructor(arg: ProblemDetails | string) {\n if (typeof arg === 'string') {\n super(arg);\n } else {\n super(arg);\n }\n }\n}\n","import { ErrorCodeValidationError } from '../models/error-code-validation-error';\nimport { ErrorCodeValidationProblemDetails } from '../models/error-code-validation-problem-details';\nimport { MonoCloudRequestException } from './monocloud-request-exception';\n\n/// <summary>\n/// The MonoCloud Error Code Validation Exception\n/// </summary>\nexport class MonoCloudErrorCodeValidationException extends MonoCloudRequestException {\n errors: ErrorCodeValidationError[];\n\n /// <summary>\n /// Initializes the MonoCloudErrorCodeValidationException Class\n /// </summary>\n /// <param name=\"response\">The problem details returned from the server.</param>\n constructor(response: ErrorCodeValidationProblemDetails) {\n super(\n response,\n `${response.title} : ${JSON.stringify(response.errors, undefined, 2)}`\n );\n this.errors = response.errors;\n }\n}\n","import { ProblemDetails } from '../models/problem-details';\nimport { MonoCloudRequestException } from './monocloud-request-exception';\n\n/// <summary>\n/// The MonoCloud Forbidden Exception\n/// </summary>\nexport class MonoCloudForbiddenException extends MonoCloudRequestException {\n /// <summary>\n /// Initializes the MonoCloudForbiddenException Class\n /// </summary>\n /// <param name=\"response\">The problem details returned from the server.</param>\n constructor(response: ProblemDetails);\n\n /// <summary>\n /// Initializes the MonoCloudForbiddenException Class\n /// </summary>\n /// <param name=\"message\">The error message.</param>\n constructor(message: string);\n\n constructor(arg: ProblemDetails | string) {\n if (typeof arg === 'string') {\n super(arg);\n } else {\n super(arg);\n }\n }\n}\n","import { KeyValidationProblemDetails } from '../models/key-validation-problem-details';\nimport { MonoCloudRequestException } from './monocloud-request-exception';\n\n/// <summary>\n/// The MonoCloud Key Validation Exception Exception\n/// </summary>\nexport class MonoCloudKeyValidationException extends MonoCloudRequestException {\n errors: Record<string, string[]>;\n\n /// <summary>\n /// Initializes the MonoCloudKeyValidationException Class\n /// </summary>\n /// <param name=\"response\">The problem details returned from the server.</param>\n constructor(response: KeyValidationProblemDetails) {\n super(\n response,\n `${response.title} : ${JSON.stringify(response.errors, undefined, 2)}`\n );\n this.errors = response.errors;\n }\n}\n","import { ProblemDetails } from '../models/problem-details';\nimport { MonoCloudRequestException } from './monocloud-request-exception';\n\n/// <summary>\n/// The MonoCloud Model State Exception\n/// </summary>\nexport class MonoCloudModelStateException extends MonoCloudRequestException {\n /// <summary>\n /// Initializes the MonoCloudModelStateException Class\n /// </summary>\n /// <param name=\"response\">The problem details returned from the server.</param>\n constructor(response: ProblemDetails);\n\n /// <summary>\n /// Initializes the MonoCloudModelStateException Class\n /// </summary>\n /// <param name=\"message\">The error message.</param>\n constructor(message: string);\n\n constructor(arg: ProblemDetails | string) {\n if (typeof arg === 'string') {\n super(arg);\n } else {\n super(arg);\n }\n }\n}\n","import { ProblemDetails } from '../models/problem-details';\nimport { MonoCloudRequestException } from './monocloud-request-exception';\n\n/// <summary>\n/// The MonoCloud Not Found Exception\n/// </summary>\nexport class MonoCloudNotFoundException extends MonoCloudRequestException {\n /// <summary>\n /// Initializes the MonoCloudNotFoundException Class\n /// </summary>\n /// <param name=\"response\">The problem details returned from the server.</param>\n constructor(response: ProblemDetails);\n\n /// <summary>\n /// Initializes the MonoCloudNotFoundException Class\n /// </summary>\n /// <param name=\"message\">The error message.</param>\n constructor(message: string);\n\n constructor(arg: ProblemDetails | string) {\n if (typeof arg === 'string') {\n super(arg);\n } else {\n super(arg);\n }\n }\n}\n","import { ProblemDetails } from '../models/problem-details';\nimport { MonoCloudRequestException } from './monocloud-request-exception';\n\n/// <summary>\n/// The MonoCloud Resource Exhausted Exception\n/// </summary>\nexport class MonoCloudResourceExhaustedException extends MonoCloudRequestException {\n /// <summary>\n /// Initializes the MonoCloudResourceExhaustedException Class\n /// </summary>\n /// <param name=\"response\">The problem details returned from the server.</param>\n constructor(response: ProblemDetails);\n\n /// <summary>\n /// Initializes the MonoCloudResourceExhaustedException Class\n /// </summary>\n /// <param name=\"message\">The error message.</param>\n constructor(message: string);\n\n constructor(arg: ProblemDetails | string) {\n if (typeof arg === 'string') {\n super(arg);\n } else {\n super(arg);\n }\n }\n}\n","import { ProblemDetails } from '../models/problem-details';\nimport { MonoCloudRequestException } from './monocloud-request-exception';\n\n/// <summary>\n/// The MonoCloud Server Exception\n/// </summary>\nexport class MonoCloudServerException extends MonoCloudRequestException {\n /// <summary>\n /// Initializes the MonoCloudServerException Class\n /// </summary>\n /// <param name=\"response\">The problem details returned from the server.</param>\n constructor(response: ProblemDetails);\n\n /// <summary>\n /// Initializes the MonoCloudServerException Class\n /// </summary>\n /// <param name=\"message\">The error message.</param>\n constructor(message: string);\n\n constructor(arg: ProblemDetails | string) {\n if (typeof arg === 'string') {\n super(arg);\n } else {\n super(arg);\n }\n }\n}\n","import { ProblemDetails } from '../models/problem-details';\nimport { MonoCloudRequestException } from './monocloud-request-exception';\n\n/// <summary>\n/// The MonoCloud Unauthorized Exception\n/// </summary>\nexport class MonoCloudUnauthorizedException extends MonoCloudRequestException {\n /// <summary>\n /// Initializes the MonoCloudUnauthorizedException Class\n /// </summary>\n /// <param name=\"response\">The problem details returned from the server.</param>\n constructor(response: ProblemDetails);\n\n /// <summary>\n /// Initializes the MonoCloudUnauthorizedException Class\n /// </summary>\n /// <param name=\"message\">The error message.</param>\n constructor(message: string);\n\n constructor(arg: ProblemDetails | string) {\n if (typeof arg === 'string') {\n super(arg);\n } else {\n super(arg);\n }\n }\n}\n","export class ErrorCodeValidationError {\n /// <summary>\n /// The error code.\n /// </summary>\n code: string;\n\n /// <summary>\n /// Brief explanation of the error.\n /// </summary>\n description: string;\n\n /// <summary>\n /// The field the error belongs to.\n /// </summary>\n field?: string;\n\n constructor(code: string, description: string, field?: string) {\n this.code = code;\n this.description = description;\n this.field = field;\n }\n}\n","export class ProblemDetails {\n /// <summary>\n /// The type of error\n /// </summary>\n type: string;\n\n /// <summary>\n /// The title of the error\n /// </summary>\n title: string;\n\n /// <summary>\n /// The status code representing the error\n /// </summary>\n status: number;\n\n /// <summary>\n /// The error details\n /// </summary>\n detail: string;\n\n /// <summary>\n /// The instance\n /// </summary>\n instance: string;\n\n /// <summary>\n /// Additional data about the error\n /// </summary>\n\n [key: string]: any;\n\n constructor(response: ProblemDetails) {\n this.type = response.type;\n this.title = response.title;\n this.status = response.status;\n this.detail = response.detail;\n this.instance = response.instance;\n Object.keys(response)\n .filter(\n x => x !== 'type' && x !== 'title' && x !== 'status' && x !== 'instance'\n )\n .forEach(key => {\n this[key] = response[key];\n });\n }\n}\n","import { ErrorCodeValidationError } from './error-code-validation-error';\nimport { ProblemDetails } from './problem-details';\n\nexport class ErrorCodeValidationProblemDetails extends ProblemDetails {\n errors: ErrorCodeValidationError[];\n\n constructor(response: ProblemDetails) {\n super(response);\n this.errors = (response.errors as ErrorCodeValidationError[]).map(\n err => new ErrorCodeValidationError(err.code, err.description, err.field)\n );\n }\n}\n","import { ProblemDetails } from './problem-details';\n\nexport class KeyValidationProblemDetails extends ProblemDetails {\n /// <summary>\n /// A collection of errors\n /// </summary>\n errors: Record<string, string[]>;\n\n constructor(response: ProblemDetails) {\n super(response);\n this.errors = response.errors;\n }\n}\n","export class MonoCloudResponse<TResult = unknown> {\n status: number;\n\n headers: Record<string, any>;\n\n result: TResult;\n\n constructor(status: number, headers: Record<string, any>, result: TResult) {\n this.status = status;\n this.headers = headers;\n this.result = result;\n }\n}\n","import { MonoCloudResponse } from './monocloud-response';\nimport { PageModel } from './page-model';\n\nexport class MonoCloudPageResponse<\n TResult = unknown,\n> extends MonoCloudResponse<TResult> {\n pageData: PageModel;\n\n constructor(\n status: number,\n headers: Record<string, any>,\n result: TResult,\n pageData: PageModel\n ) {\n super(status, headers, result);\n this.pageData = pageData;\n }\n}\n","export const ValidationExceptionTypes = {\n ValidationError: 'https://httpstatuses.io/422#validation-error',\n IdentityValidationError:\n 'https://httpstatuses.io/422#identity-validation-error',\n};\n","import { ErrorCodeValidationProblemDetails } from '../models/error-code-validation-problem-details';\nimport { KeyValidationProblemDetails } from '../models/key-validation-problem-details';\nimport { ProblemDetails } from '../models/problem-details';\nimport { MonoCloudBadRequestException } from './monocloud-bad-request-exception';\nimport { MonoCloudConflictException } from './monocloud-conflict-exception';\nimport { MonoCloudErrorCodeValidationException } from './monocloud-error-code-validation-exception';\nimport { MonoCloudException } from './monocloud-exception';\nimport { MonoCloudKeyValidationException } from './monocloud-key-validation-exception';\nimport { MonoCloudModelStateException } from './monocloud-model-state-exception';\nimport { MonoCloudNotFoundException } from './monocloud-not-found-exception';\nimport { MonoCloudResourceExhaustedException } from './monocloud-resource-exhausted-exception';\nimport { MonoCloudServerException } from './monocloud-server-exception';\nimport { MonoCloudUnauthorizedException } from './monocloud-unauthorized-exception';\n\n/// <summary>\n/// The MonoCloud Exception Handler\n/// </summary>\nexport class MonoCloudExceptionHandler {\n /// <summary>\n /// Converts the Problem Details returned from the server into an exception\n /// </summary>\n /// <param name=\"problemDetails\">The problem details returned from the server.</param>\n /// <returns></returns>\n public static ThrowProblemErr(problemDetails: ProblemDetails): void {\n switch (problemDetails.status) {\n case 400:\n throw new MonoCloudBadRequestException(problemDetails);\n case 401:\n throw new MonoCloudUnauthorizedException(problemDetails);\n case 403:\n throw new MonoCloudUnauthorizedException(problemDetails);\n case 404:\n throw new MonoCloudNotFoundException(problemDetails);\n case 409:\n throw new MonoCloudConflictException(problemDetails);\n case 422:\n if (problemDetails instanceof ErrorCodeValidationProblemDetails) {\n throw new MonoCloudErrorCodeValidationException(problemDetails);\n }\n\n if (problemDetails instanceof KeyValidationProblemDetails) {\n throw new MonoCloudKeyValidationException(problemDetails);\n }\n\n throw new MonoCloudModelStateException(problemDetails);\n case 429:\n throw new MonoCloudResourceExhaustedException(problemDetails);\n case 500:\n throw new MonoCloudServerException(problemDetails);\n default:\n throw new MonoCloudException(\n problemDetails.title ?? 'An Unknown Error Occured'\n );\n }\n }\n\n /// <summary>\n /// Converts the error returned from the server into an exception\n /// </summary>\n /// <param name=\"statusCode\">The response status code.</param>\n /// <param name=\"message\">The error message returned from the server.</param>\n /// <returns></returns>\n public static ThrowErr(statusCode: number, message?: string): void {\n switch (statusCode) {\n case 400:\n throw new MonoCloudBadRequestException(message ?? 'Bad Request');\n case 401:\n throw new MonoCloudUnauthorizedException(message ?? 'Unauthorized');\n case 403:\n throw new MonoCloudUnauthorizedException(message ?? 'Forbidden');\n case 404:\n throw new MonoCloudNotFoundException(message ?? 'Not Found');\n case 409:\n throw new MonoCloudConflictException(message ?? 'Conflict');\n case 422:\n throw new MonoCloudModelStateException(\n message ?? 'Unprocessable entity'\n );\n case 429:\n throw new MonoCloudResourceExhaustedException(\n message ?? 'Resource Exhausted'\n );\n case 500:\n throw new MonoCloudServerException(message ?? 'Server Error');\n default:\n throw new MonoCloudException(message ?? 'An Unknown Error Occured');\n }\n }\n}\n","import { MonoCloudConfig } from './monocloud-config';\nimport { MonoCloudResponse } from '../models/monocloud-response';\nimport { MonoCloudException } from '../exceptions/monocloud-exception';\nimport { MonoCloudPageResponse } from '../models/monocloud-page-response';\nimport { PageModel } from '../models/page-model';\nimport { ProblemDetails } from '../models/problem-details';\nimport { ValidationExceptionTypes } from '../exceptions/validation-exception-types';\nimport { ErrorCodeValidationProblemDetails } from '../models/error-code-validation-problem-details';\nimport { KeyValidationProblemDetails } from '../models/key-validation-problem-details';\nimport { MonoCloudExceptionHandler } from '../exceptions/monocloud-exception-handler';\nimport { MonoCloudRequest } from '../models/monocloud-request';\nimport { Fetcher } from '../models/fetcher';\n\nexport abstract class MonoCloudClientBase {\n protected fetcher: Fetcher;\n\n constructor(configuration: MonoCloudConfig, fetcher?: Fetcher) {\n if (fetcher) {\n this.fetcher = fetcher;\n } else {\n if (!configuration) {\n throw new MonoCloudException('Configuration is required');\n }\n\n if (!configuration.domain) {\n throw new MonoCloudException('Tenant Domain is required');\n }\n\n if (!configuration.apiKey) {\n throw new MonoCloudException('Api Key is required');\n }\n\n const headers: Record<string, string> = {\n 'X-API-KEY': configuration.apiKey,\n 'Content-Type': 'application/json',\n };\n\n const baseUrl = `${this.sanitizeUrl(configuration.domain)}/api/`;\n\n this.fetcher = async (\n input: string | URL | globalThis.Request,\n init?: RequestInit\n ): Promise<Response> => {\n const url = new URL(input, baseUrl);\n\n const signal = AbortSignal.timeout(\n configuration.config?.timeout ?? 10000\n );\n signal.throwIfAborted();\n\n const resp = await fetch(url.toString(), { ...init, headers, signal });\n\n return resp;\n };\n }\n }\n\n protected async processRequest<T = unknown>(\n request: MonoCloudRequest\n ): Promise<MonoCloudResponse<T>> {\n try {\n const url = this.buildUrl(request.url, request.queryParams);\n\n const response = await this.fetcher(url, {\n method: request.method,\n body: request.body ? JSON.stringify(request.body) : undefined,\n });\n\n if (!response.ok) {\n await this.HandleErrorResponse(response);\n }\n\n const headers: Record<string, any> = {};\n\n response.headers.forEach((value, key) => {\n headers[key] = value;\n });\n\n const resp = response.body ? await response.text() : null;\n\n return new MonoCloudResponse<T>(\n response.status,\n headers,\n (resp?.length ? JSON.parse(resp) : null) as T\n );\n } catch (e: any) {\n if (e instanceof MonoCloudException) {\n throw e;\n }\n\n if (e.name === 'TimeoutError') {\n throw new MonoCloudException(e.message);\n }\n\n throw new MonoCloudException('Something went wrong.');\n }\n }\n\n protected async processPaginatedRequest<T = unknown>(\n request: MonoCloudRequest\n ): Promise<MonoCloudPageResponse<T>> {\n try {\n const url = this.buildUrl(request.url, request.queryParams);\n\n const response = await this.fetcher(url, {\n method: request.method,\n body: request.body ? JSON.stringify(request.body) : undefined,\n });\n\n if (!response.ok) {\n await this.HandleErrorResponse(response);\n }\n\n const headers: Record<string, any> = {};\n\n response.headers.forEach((value, key) => {\n headers[key] = value;\n });\n\n const paginationData = this.resolvePaginationHeader(response.headers);\n\n return new MonoCloudPageResponse<T>(\n response.status,\n headers,\n (response.body ? await response.json() : null) as T,\n paginationData\n );\n } catch (e: any) {\n if (e instanceof MonoCloudException) {\n throw e;\n }\n\n if (e.name === 'TimeoutError') {\n throw new MonoCloudException(e.message);\n }\n\n throw new MonoCloudException('Something went wrong.');\n }\n }\n\n private async HandleErrorResponse(response: Response): Promise<void> {\n const contentType = response.headers.get('content-type');\n if (contentType?.startsWith('application/problem+json')) {\n const body = await response.json();\n let result = body\n ? new ProblemDetails(body as ProblemDetails)\n : undefined;\n\n if (result?.type === ValidationExceptionTypes.IdentityValidationError) {\n result = new ErrorCodeValidationProblemDetails(result);\n }\n\n if (result?.type === ValidationExceptionTypes.ValidationError) {\n result = new KeyValidationProblemDetails(result);\n }\n\n if (!result) {\n throw new MonoCloudException('Invalid body');\n }\n\n MonoCloudExceptionHandler.ThrowProblemErr(result);\n }\n\n const respStrng = await response.text();\n MonoCloudExceptionHandler.ThrowErr(\n response.status,\n respStrng && respStrng !== '' ? respStrng : response.statusText\n );\n }\n\n private sanitizeUrl(url: string): string {\n let u = url;\n if (!u.startsWith('https://')) {\n u = `https://${u}`;\n }\n\n if (u.endsWith('/')) {\n u = u.substring(0, u.length - 1);\n }\n\n return u;\n }\n\n private resolvePaginationHeader(headers: Headers): PageModel {\n const paginationHeader = headers.get('x-pagination');\n const pageData = paginationHeader\n ? JSON.parse(paginationHeader)\n : undefined;\n\n return {\n page_size: pageData?.page_size ?? 0,\n current_page: pageData?.current_page ?? 0,\n total_count: pageData?.total_count ?? 0,\n has_previous: pageData?.has_previous ?? false,\n has_next: pageData?.has_next ?? false,\n };\n }\n\n private buildUrl(\n url: string,\n queryParams?: Record<string, string | number | boolean>\n ): string {\n let urlStr = url;\n\n if (urlStr.startsWith('/')) {\n urlStr = urlStr.substring(1, urlStr.length);\n }\n\n if (!queryParams) {\n return urlStr;\n }\n\n urlStr += '?';\n\n Object.keys(queryParams).forEach(key => {\n urlStr += `${key}=${encodeURIComponent(queryParams[key])}&`;\n });\n\n urlStr = urlStr.substring(0, urlStr.length - 1);\n\n return urlStr;\n }\n}\n"],"mappings":";;AAGA,IAAa,qBAAb,cAAwC,MAAM;;;;ACA9C,IAAa,4BAAb,cAA+C,mBAAmB;CAyBhE,YAAY,GAAG,MAA8D;EAC3E,IAAIA;EACJ,IAAIC;AAEJ,MAAI,MAAM,QAAQ,KAAK,CACrB,KAAI,KAAK,WAAW,GAAG;AACrB,oBAAiB,KAAK;AACtB,kBAAe,KAAK;aACX,KAAK,WAAW,KAAK,OAAO,KAAK,OAAO,SACjD,gBAAe,KAAK;WACX,KAAK,WAAW,KAAK,OAAO,KAAK,OAAO,UAAU;AAC3D,oBAAiB,KAAK;AACtB,kBAAe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GjB,MAAI,EAAE,SAAS,IAAI,CACjB,KAAI,EAAE,UAAU,GAAG,EAAE,SAAS,EAAE;AAGlC,SAAO;;CAGT,AAAQ,wBAAwB,SAA6B;;EAC3D,MAAM,mBAAmB,QAAQ,IAAI,eAAe;EACpD,MAAM,WAAW,mBACb,KAAK,MAAM,iBAAiB,GAC5B;AAEJ,SAAO;GACL,sFAAW,SAAU,8EAAa;GAClC,2FAAc,SAAU,qFAAgB;GACxC,0FAAa,SAAU,oFAAe;GACtC,2FAAc,SAAU,qFAAgB;GACxC,oFAAU,SAAU,2EAAY;GACjC;;CAGH,AAAQ,SACN,KACA,aACQ;EACR,IAAI,SAAS;AAEb,MAAI,OAAO,WAAW,IAAI,CACxB,UAAS,OAAO,UAAU,GAAG,OAAO,OAAO;AAG7C,MAAI,CAAC,YACH,QAAO;AAGT,YAAU;AAEV,SAAO,KAAK,YAAY,CAAC,SAAQ,QAAO;AACtC,aAAU,GAAG,IAAI,GAAG,mBAAmB,YAAY,KAAK,CAAC;IACzD;AAEF,WAAS,OAAO,UAAU,GAAG,OAAO,SAAS,EAAE;AAE/C,SAAO"}
|
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,159 @@
|
|
|
1
|
+
//#region src/exceptions/monocloud-exception.d.ts
|
|
2
|
+
declare class MonoCloudException extends Error {}
|
|
3
|
+
//#endregion
|
|
4
|
+
//#region src/models/problem-details.d.ts
|
|
5
|
+
declare class ProblemDetails {
|
|
6
|
+
type: string;
|
|
7
|
+
title: string;
|
|
8
|
+
status: number;
|
|
9
|
+
detail: string;
|
|
10
|
+
instance: string;
|
|
11
|
+
[key: string]: any;
|
|
12
|
+
constructor(response: ProblemDetails);
|
|
13
|
+
}
|
|
14
|
+
//#endregion
|
|
15
|
+
//#region src/exceptions/monocloud-request-exception.d.ts
|
|
16
|
+
declare class MonoCloudRequestException extends MonoCloudException {
|
|
17
|
+
response?: ProblemDetails;
|
|
18
|
+
constructor(response: ProblemDetails);
|
|
19
|
+
constructor(message: string);
|
|
20
|
+
constructor(response: ProblemDetails, message: string);
|
|
21
|
+
}
|
|
22
|
+
//#endregion
|
|
23
|
+
//#region src/exceptions/monocloud-bad-request-exception.d.ts
|
|
24
|
+
declare class MonoCloudBadRequestException extends MonoCloudRequestException {
|
|
25
|
+
constructor(response: ProblemDetails);
|
|
26
|
+
constructor(message: string);
|
|
27
|
+
}
|
|
28
|
+
//#endregion
|
|
29
|
+
//#region src/exceptions/monocloud-conflict-exception.d.ts
|
|
30
|
+
declare class MonoCloudConflictException extends MonoCloudRequestException {
|
|
31
|
+
constructor(response: ProblemDetails);
|
|
32
|
+
constructor(message: string);
|
|
33
|
+
}
|
|
34
|
+
//#endregion
|
|
35
|
+
//#region src/models/error-code-validation-error.d.ts
|
|
36
|
+
declare class ErrorCodeValidationError {
|
|
37
|
+
code: string;
|
|
38
|
+
description: string;
|
|
39
|
+
field?: string;
|
|
40
|
+
constructor(code: string, description: string, field?: string);
|
|
41
|
+
}
|
|
42
|
+
//#endregion
|
|
43
|
+
//#region src/models/error-code-validation-problem-details.d.ts
|
|
44
|
+
declare class ErrorCodeValidationProblemDetails extends ProblemDetails {
|
|
45
|
+
errors: ErrorCodeValidationError[];
|
|
46
|
+
constructor(response: ProblemDetails);
|
|
47
|
+
}
|
|
48
|
+
//#endregion
|
|
49
|
+
//#region src/exceptions/monocloud-error-code-validation-exception.d.ts
|
|
50
|
+
declare class MonoCloudErrorCodeValidationException extends MonoCloudRequestException {
|
|
51
|
+
errors: ErrorCodeValidationError[];
|
|
52
|
+
constructor(response: ErrorCodeValidationProblemDetails);
|
|
53
|
+
}
|
|
54
|
+
//#endregion
|
|
55
|
+
//#region src/exceptions/monocloud-forbidden-exception.d.ts
|
|
56
|
+
declare class MonoCloudForbiddenException extends MonoCloudRequestException {
|
|
57
|
+
constructor(response: ProblemDetails);
|
|
58
|
+
constructor(message: string);
|
|
59
|
+
}
|
|
60
|
+
//#endregion
|
|
61
|
+
//#region src/models/key-validation-problem-details.d.ts
|
|
62
|
+
declare class KeyValidationProblemDetails extends ProblemDetails {
|
|
63
|
+
errors: Record<string, string[]>;
|
|
64
|
+
constructor(response: ProblemDetails);
|
|
65
|
+
}
|
|
66
|
+
//#endregion
|
|
67
|
+
//#region src/exceptions/monocloud-key-validation-exception.d.ts
|
|
68
|
+
declare class MonoCloudKeyValidationException extends MonoCloudRequestException {
|
|
69
|
+
errors: Record<string, string[]>;
|
|
70
|
+
constructor(response: KeyValidationProblemDetails);
|
|
71
|
+
}
|
|
72
|
+
//#endregion
|
|
73
|
+
//#region src/exceptions/monocloud-model-state-exception.d.ts
|
|
74
|
+
declare class MonoCloudModelStateException extends MonoCloudRequestException {
|
|
75
|
+
constructor(response: ProblemDetails);
|
|
76
|
+
constructor(message: string);
|
|
77
|
+
}
|
|
78
|
+
//#endregion
|
|
79
|
+
//#region src/exceptions/monocloud-not-found-exception.d.ts
|
|
80
|
+
declare class MonoCloudNotFoundException extends MonoCloudRequestException {
|
|
81
|
+
constructor(response: ProblemDetails);
|
|
82
|
+
constructor(message: string);
|
|
83
|
+
}
|
|
84
|
+
//#endregion
|
|
85
|
+
//#region src/exceptions/monocloud-resource-exhausted-exception.d.ts
|
|
86
|
+
declare class MonoCloudResourceExhaustedException extends MonoCloudRequestException {
|
|
87
|
+
constructor(response: ProblemDetails);
|
|
88
|
+
constructor(message: string);
|
|
89
|
+
}
|
|
90
|
+
//#endregion
|
|
91
|
+
//#region src/exceptions/monocloud-server-exception.d.ts
|
|
92
|
+
declare class MonoCloudServerException extends MonoCloudRequestException {
|
|
93
|
+
constructor(response: ProblemDetails);
|
|
94
|
+
constructor(message: string);
|
|
95
|
+
}
|
|
96
|
+
//#endregion
|
|
97
|
+
//#region src/exceptions/monocloud-unauthorized-exception.d.ts
|
|
98
|
+
declare class MonoCloudUnauthorizedException extends MonoCloudRequestException {
|
|
99
|
+
constructor(response: ProblemDetails);
|
|
100
|
+
constructor(message: string);
|
|
101
|
+
}
|
|
102
|
+
//#endregion
|
|
103
|
+
//#region src/base/monocloud-config.d.ts
|
|
104
|
+
interface MonoCloudConfig {
|
|
105
|
+
domain: string;
|
|
106
|
+
apiKey: string;
|
|
107
|
+
config?: {
|
|
108
|
+
timeout?: number;
|
|
109
|
+
};
|
|
110
|
+
}
|
|
111
|
+
//#endregion
|
|
112
|
+
//#region src/models/monocloud-response.d.ts
|
|
113
|
+
declare class MonoCloudResponse<TResult = unknown> {
|
|
114
|
+
status: number;
|
|
115
|
+
headers: Record<string, any>;
|
|
116
|
+
result: TResult;
|
|
117
|
+
constructor(status: number, headers: Record<string, any>, result: TResult);
|
|
118
|
+
}
|
|
119
|
+
//#endregion
|
|
120
|
+
//#region src/models/page-model.d.ts
|
|
121
|
+
interface PageModel {
|
|
122
|
+
page_size: number;
|
|
123
|
+
current_page: number;
|
|
124
|
+
total_count: number;
|
|
125
|
+
has_previous: boolean;
|
|
126
|
+
has_next: boolean;
|
|
127
|
+
}
|
|
128
|
+
//#endregion
|
|
129
|
+
//#region src/models/monocloud-page-response.d.ts
|
|
130
|
+
declare class MonoCloudPageResponse<TResult = unknown> extends MonoCloudResponse<TResult> {
|
|
131
|
+
pageData: PageModel;
|
|
132
|
+
constructor(status: number, headers: Record<string, any>, result: TResult, pageData: PageModel);
|
|
133
|
+
}
|
|
134
|
+
//#endregion
|
|
135
|
+
//#region src/models/monocloud-request.d.ts
|
|
136
|
+
interface MonoCloudRequest {
|
|
137
|
+
url: string;
|
|
138
|
+
method: 'GET' | 'POST' | 'PUT' | 'PATCH' | 'DELETE';
|
|
139
|
+
queryParams?: Record<string, string>;
|
|
140
|
+
body?: object;
|
|
141
|
+
}
|
|
142
|
+
//#endregion
|
|
143
|
+
//#region src/models/fetcher.d.ts
|
|
144
|
+
type Fetcher = (input: string | URL | globalThis.Request, init?: RequestInit) => Promise<Response>;
|
|
145
|
+
//#endregion
|
|
146
|
+
//#region src/base/monocloud-client-base.d.ts
|
|
147
|
+
declare abstract class MonoCloudClientBase {
|
|
148
|
+
protected fetcher: Fetcher;
|
|
149
|
+
constructor(configuration: MonoCloudConfig, fetcher?: Fetcher);
|
|
150
|
+
protected processRequest<T = unknown>(request: MonoCloudRequest): Promise<MonoCloudResponse<T>>;
|
|
151
|
+
protected processPaginatedRequest<T = unknown>(request: MonoCloudRequest): Promise<MonoCloudPageResponse<T>>;
|
|
152
|
+
private HandleErrorResponse;
|
|
153
|
+
private sanitizeUrl;
|
|
154
|
+
private resolvePaginationHeader;
|
|
155
|
+
private buildUrl;
|
|
156
|
+
}
|
|
157
|
+
//#endregion
|
|
158
|
+
export { type ErrorCodeValidationError, ErrorCodeValidationProblemDetails, type Fetcher, KeyValidationProblemDetails, MonoCloudBadRequestException, MonoCloudClientBase, type MonoCloudConfig, MonoCloudConflictException, MonoCloudErrorCodeValidationException, MonoCloudException, MonoCloudForbiddenException, MonoCloudKeyValidationException, MonoCloudModelStateException, MonoCloudNotFoundException, MonoCloudPageResponse, type MonoCloudRequest, MonoCloudRequestException, MonoCloudResourceExhaustedException, MonoCloudResponse, MonoCloudServerException, MonoCloudUnauthorizedException, ProblemDetails };
|
|
159
|
+
//# sourceMappingURL=index.d.cts.map
|
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,159 @@
|
|
|
1
|
+
//#region src/exceptions/monocloud-exception.d.ts
|
|
2
|
+
declare class MonoCloudException extends Error {}
|
|
3
|
+
//#endregion
|
|
4
|
+
//#region src/models/problem-details.d.ts
|
|
5
|
+
declare class ProblemDetails {
|
|
6
|
+
type: string;
|
|
7
|
+
title: string;
|
|
8
|
+
status: number;
|
|
9
|
+
detail: string;
|
|
10
|
+
instance: string;
|
|
11
|
+
[key: string]: any;
|
|
12
|
+
constructor(response: ProblemDetails);
|
|
13
|
+
}
|
|
14
|
+
//#endregion
|
|
15
|
+
//#region src/exceptions/monocloud-request-exception.d.ts
|
|
16
|
+
declare class MonoCloudRequestException extends MonoCloudException {
|
|
17
|
+
response?: ProblemDetails;
|
|
18
|
+
constructor(response: ProblemDetails);
|
|
19
|
+
constructor(message: string);
|
|
20
|
+
constructor(response: ProblemDetails, message: string);
|
|
21
|
+
}
|
|
22
|
+
//#endregion
|
|
23
|
+
//#region src/exceptions/monocloud-bad-request-exception.d.ts
|
|
24
|
+
declare class MonoCloudBadRequestException extends MonoCloudRequestException {
|
|
25
|
+
constructor(response: ProblemDetails);
|
|
26
|
+
constructor(message: string);
|
|
27
|
+
}
|
|
28
|
+
//#endregion
|
|
29
|
+
//#region src/exceptions/monocloud-conflict-exception.d.ts
|
|
30
|
+
declare class MonoCloudConflictException extends MonoCloudRequestException {
|
|
31
|
+
constructor(response: ProblemDetails);
|
|
32
|
+
constructor(message: string);
|
|
33
|
+
}
|
|
34
|
+
//#endregion
|
|
35
|
+
//#region src/models/error-code-validation-error.d.ts
|
|
36
|
+
declare class ErrorCodeValidationError {
|
|
37
|
+
code: string;
|
|
38
|
+
description: string;
|
|
39
|
+
field?: string;
|
|
40
|
+
constructor(code: string, description: string, field?: string);
|
|
41
|
+
}
|
|
42
|
+
//#endregion
|
|
43
|
+
//#region src/models/error-code-validation-problem-details.d.ts
|
|
44
|
+
declare class ErrorCodeValidationProblemDetails extends ProblemDetails {
|
|
45
|
+
errors: ErrorCodeValidationError[];
|
|
46
|
+
constructor(response: ProblemDetails);
|
|
47
|
+
}
|
|
48
|
+
//#endregion
|
|
49
|
+
//#region src/exceptions/monocloud-error-code-validation-exception.d.ts
|
|
50
|
+
declare class MonoCloudErrorCodeValidationException extends MonoCloudRequestException {
|
|
51
|
+
errors: ErrorCodeValidationError[];
|
|
52
|
+
constructor(response: ErrorCodeValidationProblemDetails);
|
|
53
|
+
}
|
|
54
|
+
//#endregion
|
|
55
|
+
//#region src/exceptions/monocloud-forbidden-exception.d.ts
|
|
56
|
+
declare class MonoCloudForbiddenException extends MonoCloudRequestException {
|
|
57
|
+
constructor(response: ProblemDetails);
|
|
58
|
+
constructor(message: string);
|
|
59
|
+
}
|
|
60
|
+
//#endregion
|
|
61
|
+
//#region src/models/key-validation-problem-details.d.ts
|
|
62
|
+
declare class KeyValidationProblemDetails extends ProblemDetails {
|
|
63
|
+
errors: Record<string, string[]>;
|
|
64
|
+
constructor(response: ProblemDetails);
|
|
65
|
+
}
|
|
66
|
+
//#endregion
|
|
67
|
+
//#region src/exceptions/monocloud-key-validation-exception.d.ts
|
|
68
|
+
declare class MonoCloudKeyValidationException extends MonoCloudRequestException {
|
|
69
|
+
errors: Record<string, string[]>;
|
|
70
|
+
constructor(response: KeyValidationProblemDetails);
|
|
71
|
+
}
|
|
72
|
+
//#endregion
|
|
73
|
+
//#region src/exceptions/monocloud-model-state-exception.d.ts
|
|
74
|
+
declare class MonoCloudModelStateException extends MonoCloudRequestException {
|
|
75
|
+
constructor(response: ProblemDetails);
|
|
76
|
+
constructor(message: string);
|
|
77
|
+
}
|
|
78
|
+
//#endregion
|
|
79
|
+
//#region src/exceptions/monocloud-not-found-exception.d.ts
|
|
80
|
+
declare class MonoCloudNotFoundException extends MonoCloudRequestException {
|
|
81
|
+
constructor(response: ProblemDetails);
|
|
82
|
+
constructor(message: string);
|
|
83
|
+
}
|
|
84
|
+
//#endregion
|
|
85
|
+
//#region src/exceptions/monocloud-resource-exhausted-exception.d.ts
|
|
86
|
+
declare class MonoCloudResourceExhaustedException extends MonoCloudRequestException {
|
|
87
|
+
constructor(response: ProblemDetails);
|
|
88
|
+
constructor(message: string);
|
|
89
|
+
}
|
|
90
|
+
//#endregion
|
|
91
|
+
//#region src/exceptions/monocloud-server-exception.d.ts
|
|
92
|
+
declare class MonoCloudServerException extends MonoCloudRequestException {
|
|
93
|
+
constructor(response: ProblemDetails);
|
|
94
|
+
constructor(message: string);
|
|
95
|
+
}
|
|
96
|
+
//#endregion
|
|
97
|
+
//#region src/exceptions/monocloud-unauthorized-exception.d.ts
|
|
98
|
+
declare class MonoCloudUnauthorizedException extends MonoCloudRequestException {
|
|
99
|
+
constructor(response: ProblemDetails);
|
|
100
|
+
constructor(message: string);
|
|
101
|
+
}
|
|
102
|
+
//#endregion
|
|
103
|
+
//#region src/base/monocloud-config.d.ts
|
|
104
|
+
interface MonoCloudConfig {
|
|
105
|
+
domain: string;
|
|
106
|
+
apiKey: string;
|
|
107
|
+
config?: {
|
|
108
|
+
timeout?: number;
|
|
109
|
+
};
|
|
110
|
+
}
|
|
111
|
+
//#endregion
|
|
112
|
+
//#region src/models/monocloud-response.d.ts
|
|
113
|
+
declare class MonoCloudResponse<TResult = unknown> {
|
|
114
|
+
status: number;
|
|
115
|
+
headers: Record<string, any>;
|
|
116
|
+
result: TResult;
|
|
117
|
+
constructor(status: number, headers: Record<string, any>, result: TResult);
|
|
118
|
+
}
|
|
119
|
+
//#endregion
|
|
120
|
+
//#region src/models/page-model.d.ts
|
|
121
|
+
interface PageModel {
|
|
122
|
+
page_size: number;
|
|
123
|
+
current_page: number;
|
|
124
|
+
total_count: number;
|
|
125
|
+
has_previous: boolean;
|
|
126
|
+
has_next: boolean;
|
|
127
|
+
}
|
|
128
|
+
//#endregion
|
|
129
|
+
//#region src/models/monocloud-page-response.d.ts
|
|
130
|
+
declare class MonoCloudPageResponse<TResult = unknown> extends MonoCloudResponse<TResult> {
|
|
131
|
+
pageData: PageModel;
|
|
132
|
+
constructor(status: number, headers: Record<string, any>, result: TResult, pageData: PageModel);
|
|
133
|
+
}
|
|
134
|
+
//#endregion
|
|
135
|
+
//#region src/models/monocloud-request.d.ts
|
|
136
|
+
interface MonoCloudRequest {
|
|
137
|
+
url: string;
|
|
138
|
+
method: 'GET' | 'POST' | 'PUT' | 'PATCH' | 'DELETE';
|
|
139
|
+
queryParams?: Record<string, string>;
|
|
140
|
+
body?: object;
|
|
141
|
+
}
|
|
142
|
+
//#endregion
|
|
143
|
+
//#region src/models/fetcher.d.ts
|
|
144
|
+
type Fetcher = (input: string | URL | globalThis.Request, init?: RequestInit) => Promise<Response>;
|
|
145
|
+
//#endregion
|
|
146
|
+
//#region src/base/monocloud-client-base.d.ts
|
|
147
|
+
declare abstract class MonoCloudClientBase {
|
|
148
|
+
protected fetcher: Fetcher;
|
|
149
|
+
constructor(configuration: MonoCloudConfig, fetcher?: Fetcher);
|
|
150
|
+
protected processRequest<T = unknown>(request: MonoCloudRequest): Promise<MonoCloudResponse<T>>;
|
|
151
|
+
protected processPaginatedRequest<T = unknown>(request: MonoCloudRequest): Promise<MonoCloudPageResponse<T>>;
|
|
152
|
+
private HandleErrorResponse;
|
|
153
|
+
private sanitizeUrl;
|
|
154
|
+
private resolvePaginationHeader;
|
|
155
|
+
private buildUrl;
|
|
156
|
+
}
|
|
157
|
+
//#endregion
|
|
158
|
+
export { type ErrorCodeValidationError, ErrorCodeValidationProblemDetails, type Fetcher, KeyValidationProblemDetails, MonoCloudBadRequestException, MonoCloudClientBase, type MonoCloudConfig, MonoCloudConflictException, MonoCloudErrorCodeValidationException, MonoCloudException, MonoCloudForbiddenException, MonoCloudKeyValidationException, MonoCloudModelStateException, MonoCloudNotFoundException, MonoCloudPageResponse, type MonoCloudRequest, MonoCloudRequestException, MonoCloudResourceExhaustedException, MonoCloudResponse, MonoCloudServerException, MonoCloudUnauthorizedException, ProblemDetails };
|
|
159
|
+
//# sourceMappingURL=index.d.mts.map
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,332 @@
|
|
|
1
|
+
//#region src/exceptions/monocloud-exception.ts
|
|
2
|
+
var MonoCloudException = class extends Error {};
|
|
3
|
+
|
|
4
|
+
//#endregion
|
|
5
|
+
//#region src/exceptions/monocloud-request-exception.ts
|
|
6
|
+
var MonoCloudRequestException = class extends MonoCloudException {
|
|
7
|
+
constructor(...args) {
|
|
8
|
+
let problemDetails;
|
|
9
|
+
let errorMessage;
|
|
10
|
+
if (Array.isArray(args)) if (args.length === 2) {
|
|
11
|
+
problemDetails = args[0];
|
|
12
|
+
errorMessage = args[1];
|
|
13
|
+
} else if (args.length === 1 && typeof args[0] === "string") errorMessage = args[0];
|
|
14
|
+
else if (args.length === 1 && typeof args[0] === "object") {
|
|
15
|
+
problemDetails = args[0];
|
|
16
|
+
errorMessage = args[0].title;
|
|
17
|
+
} else throw new Error("Invalid arguments in constructor");
|
|
18
|
+
if (!errorMessage) throw new Error("Invalid error message");
|
|
19
|
+
super(errorMessage);
|
|
20
|
+
this.response = problemDetails;
|
|
21
|
+
}
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
//#endregion
|
|
25
|
+
//#region src/exceptions/monocloud-bad-request-exception.ts
|
|
26
|
+
var MonoCloudBadRequestException = class extends MonoCloudRequestException {
|
|
27
|
+
constructor(arg) {
|
|
28
|
+
if (typeof arg === "string") super(arg);
|
|
29
|
+
else super(arg);
|
|
30
|
+
}
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
//#endregion
|
|
34
|
+
//#region src/exceptions/monocloud-conflict-exception.ts
|
|
35
|
+
var MonoCloudConflictException = class extends MonoCloudRequestException {
|
|
36
|
+
constructor(arg) {
|
|
37
|
+
if (typeof arg === "string") super(arg);
|
|
38
|
+
else super(arg);
|
|
39
|
+
}
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
//#endregion
|
|
43
|
+
//#region src/exceptions/monocloud-error-code-validation-exception.ts
|
|
44
|
+
var MonoCloudErrorCodeValidationException = class extends MonoCloudRequestException {
|
|
45
|
+
constructor(response) {
|
|
46
|
+
super(response, `${response.title} : ${JSON.stringify(response.errors, void 0, 2)}`);
|
|
47
|
+
this.errors = response.errors;
|
|
48
|
+
}
|
|
49
|
+
};
|
|
50
|
+
|
|
51
|
+
//#endregion
|
|
52
|
+
//#region src/exceptions/monocloud-forbidden-exception.ts
|
|
53
|
+
var MonoCloudForbiddenException = class extends MonoCloudRequestException {
|
|
54
|
+
constructor(arg) {
|
|
55
|
+
if (typeof arg === "string") super(arg);
|
|
56
|
+
else super(arg);
|
|
57
|
+
}
|
|
58
|
+
};
|
|
59
|
+
|
|
60
|
+
//#endregion
|
|
61
|
+
//#region src/exceptions/monocloud-key-validation-exception.ts
|
|
62
|
+
var MonoCloudKeyValidationException = class extends MonoCloudRequestException {
|
|
63
|
+
constructor(response) {
|
|
64
|
+
super(response, `${response.title} : ${JSON.stringify(response.errors, void 0, 2)}`);
|
|
65
|
+
this.errors = response.errors;
|
|
66
|
+
}
|
|
67
|
+
};
|
|
68
|
+
|
|
69
|
+
//#endregion
|
|
70
|
+
//#region src/exceptions/monocloud-model-state-exception.ts
|
|
71
|
+
var MonoCloudModelStateException = class extends MonoCloudRequestException {
|
|
72
|
+
constructor(arg) {
|
|
73
|
+
if (typeof arg === "string") super(arg);
|
|
74
|
+
else super(arg);
|
|
75
|
+
}
|
|
76
|
+
};
|
|
77
|
+
|
|
78
|
+
//#endregion
|
|
79
|
+
//#region src/exceptions/monocloud-not-found-exception.ts
|
|
80
|
+
var MonoCloudNotFoundException = class extends MonoCloudRequestException {
|
|
81
|
+
constructor(arg) {
|
|
82
|
+
if (typeof arg === "string") super(arg);
|
|
83
|
+
else super(arg);
|
|
84
|
+
}
|
|
85
|
+
};
|
|
86
|
+
|
|
87
|
+
//#endregion
|
|
88
|
+
//#region src/exceptions/monocloud-resource-exhausted-exception.ts
|
|
89
|
+
var MonoCloudResourceExhaustedException = class extends MonoCloudRequestException {
|
|
90
|
+
constructor(arg) {
|
|
91
|
+
if (typeof arg === "string") super(arg);
|
|
92
|
+
else super(arg);
|
|
93
|
+
}
|
|
94
|
+
};
|
|
95
|
+
|
|
96
|
+
//#endregion
|
|
97
|
+
//#region src/exceptions/monocloud-server-exception.ts
|
|
98
|
+
var MonoCloudServerException = class extends MonoCloudRequestException {
|
|
99
|
+
constructor(arg) {
|
|
100
|
+
if (typeof arg === "string") super(arg);
|
|
101
|
+
else super(arg);
|
|
102
|
+
}
|
|
103
|
+
};
|
|
104
|
+
|
|
105
|
+
//#endregion
|
|
106
|
+
//#region src/exceptions/monocloud-unauthorized-exception.ts
|
|
107
|
+
var MonoCloudUnauthorizedException = class extends MonoCloudRequestException {
|
|
108
|
+
constructor(arg) {
|
|
109
|
+
if (typeof arg === "string") super(arg);
|
|
110
|
+
else super(arg);
|
|
111
|
+
}
|
|
112
|
+
};
|
|
113
|
+
|
|
114
|
+
//#endregion
|
|
115
|
+
//#region src/models/error-code-validation-error.ts
|
|
116
|
+
var ErrorCodeValidationError = class {
|
|
117
|
+
constructor(code, description, field) {
|
|
118
|
+
this.code = code;
|
|
119
|
+
this.description = description;
|
|
120
|
+
this.field = field;
|
|
121
|
+
}
|
|
122
|
+
};
|
|
123
|
+
|
|
124
|
+
//#endregion
|
|
125
|
+
//#region src/models/problem-details.ts
|
|
126
|
+
var ProblemDetails = class {
|
|
127
|
+
constructor(response) {
|
|
128
|
+
this.type = response.type;
|
|
129
|
+
this.title = response.title;
|
|
130
|
+
this.status = response.status;
|
|
131
|
+
this.detail = response.detail;
|
|
132
|
+
this.instance = response.instance;
|
|
133
|
+
Object.keys(response).filter((x) => x !== "type" && x !== "title" && x !== "status" && x !== "instance").forEach((key) => {
|
|
134
|
+
this[key] = response[key];
|
|
135
|
+
});
|
|
136
|
+
}
|
|
137
|
+
};
|
|
138
|
+
|
|
139
|
+
//#endregion
|
|
140
|
+
//#region src/models/error-code-validation-problem-details.ts
|
|
141
|
+
var ErrorCodeValidationProblemDetails = class extends ProblemDetails {
|
|
142
|
+
constructor(response) {
|
|
143
|
+
super(response);
|
|
144
|
+
this.errors = response.errors.map((err) => new ErrorCodeValidationError(err.code, err.description, err.field));
|
|
145
|
+
}
|
|
146
|
+
};
|
|
147
|
+
|
|
148
|
+
//#endregion
|
|
149
|
+
//#region src/models/key-validation-problem-details.ts
|
|
150
|
+
var KeyValidationProblemDetails = class extends ProblemDetails {
|
|
151
|
+
constructor(response) {
|
|
152
|
+
super(response);
|
|
153
|
+
this.errors = response.errors;
|
|
154
|
+
}
|
|
155
|
+
};
|
|
156
|
+
|
|
157
|
+
//#endregion
|
|
158
|
+
//#region src/models/monocloud-response.ts
|
|
159
|
+
var MonoCloudResponse = class {
|
|
160
|
+
constructor(status, headers, result) {
|
|
161
|
+
this.status = status;
|
|
162
|
+
this.headers = headers;
|
|
163
|
+
this.result = result;
|
|
164
|
+
}
|
|
165
|
+
};
|
|
166
|
+
|
|
167
|
+
//#endregion
|
|
168
|
+
//#region src/models/monocloud-page-response.ts
|
|
169
|
+
var MonoCloudPageResponse = class extends MonoCloudResponse {
|
|
170
|
+
constructor(status, headers, result, pageData) {
|
|
171
|
+
super(status, headers, result);
|
|
172
|
+
this.pageData = pageData;
|
|
173
|
+
}
|
|
174
|
+
};
|
|
175
|
+
|
|
176
|
+
//#endregion
|
|
177
|
+
//#region src/exceptions/validation-exception-types.ts
|
|
178
|
+
const ValidationExceptionTypes = {
|
|
179
|
+
ValidationError: "https://httpstatuses.io/422#validation-error",
|
|
180
|
+
IdentityValidationError: "https://httpstatuses.io/422#identity-validation-error"
|
|
181
|
+
};
|
|
182
|
+
|
|
183
|
+
//#endregion
|
|
184
|
+
//#region src/exceptions/monocloud-exception-handler.ts
|
|
185
|
+
var MonoCloudExceptionHandler = class {
|
|
186
|
+
static ThrowProblemErr(problemDetails) {
|
|
187
|
+
switch (problemDetails.status) {
|
|
188
|
+
case 400: throw new MonoCloudBadRequestException(problemDetails);
|
|
189
|
+
case 401: throw new MonoCloudUnauthorizedException(problemDetails);
|
|
190
|
+
case 403: throw new MonoCloudUnauthorizedException(problemDetails);
|
|
191
|
+
case 404: throw new MonoCloudNotFoundException(problemDetails);
|
|
192
|
+
case 409: throw new MonoCloudConflictException(problemDetails);
|
|
193
|
+
case 422:
|
|
194
|
+
if (problemDetails instanceof ErrorCodeValidationProblemDetails) throw new MonoCloudErrorCodeValidationException(problemDetails);
|
|
195
|
+
if (problemDetails instanceof KeyValidationProblemDetails) throw new MonoCloudKeyValidationException(problemDetails);
|
|
196
|
+
throw new MonoCloudModelStateException(problemDetails);
|
|
197
|
+
case 429: throw new MonoCloudResourceExhaustedException(problemDetails);
|
|
198
|
+
case 500: throw new MonoCloudServerException(problemDetails);
|
|
199
|
+
default:
|
|
200
|
+
var _problemDetails$title;
|
|
201
|
+
throw new MonoCloudException((_problemDetails$title = problemDetails.title) !== null && _problemDetails$title !== void 0 ? _problemDetails$title : "An Unknown Error Occured");
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
static ThrowErr(statusCode, message) {
|
|
205
|
+
switch (statusCode) {
|
|
206
|
+
case 400: throw new MonoCloudBadRequestException(message !== null && message !== void 0 ? message : "Bad Request");
|
|
207
|
+
case 401: throw new MonoCloudUnauthorizedException(message !== null && message !== void 0 ? message : "Unauthorized");
|
|
208
|
+
case 403: throw new MonoCloudUnauthorizedException(message !== null && message !== void 0 ? message : "Forbidden");
|
|
209
|
+
case 404: throw new MonoCloudNotFoundException(message !== null && message !== void 0 ? message : "Not Found");
|
|
210
|
+
case 409: throw new MonoCloudConflictException(message !== null && message !== void 0 ? message : "Conflict");
|
|
211
|
+
case 422: throw new MonoCloudModelStateException(message !== null && message !== void 0 ? message : "Unprocessable entity");
|
|
212
|
+
case 429: throw new MonoCloudResourceExhaustedException(message !== null && message !== void 0 ? message : "Resource Exhausted");
|
|
213
|
+
case 500: throw new MonoCloudServerException(message !== null && message !== void 0 ? message : "Server Error");
|
|
214
|
+
default: throw new MonoCloudException(message !== null && message !== void 0 ? message : "An Unknown Error Occured");
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
};
|
|
218
|
+
|
|
219
|
+
//#endregion
|
|
220
|
+
//#region src/base/monocloud-client-base.ts
|
|
221
|
+
var MonoCloudClientBase = class {
|
|
222
|
+
constructor(configuration, fetcher) {
|
|
223
|
+
if (fetcher) this.fetcher = fetcher;
|
|
224
|
+
else {
|
|
225
|
+
if (!configuration) throw new MonoCloudException("Configuration is required");
|
|
226
|
+
if (!configuration.domain) throw new MonoCloudException("Tenant Domain is required");
|
|
227
|
+
if (!configuration.apiKey) throw new MonoCloudException("Api Key is required");
|
|
228
|
+
const headers = {
|
|
229
|
+
"X-API-KEY": configuration.apiKey,
|
|
230
|
+
"Content-Type": "application/json"
|
|
231
|
+
};
|
|
232
|
+
const baseUrl = `${this.sanitizeUrl(configuration.domain)}/api/`;
|
|
233
|
+
this.fetcher = async (input, init) => {
|
|
234
|
+
var _configuration$config, _configuration$config2;
|
|
235
|
+
const url = new URL(input, baseUrl);
|
|
236
|
+
const signal = AbortSignal.timeout((_configuration$config = (_configuration$config2 = configuration.config) === null || _configuration$config2 === void 0 ? void 0 : _configuration$config2.timeout) !== null && _configuration$config !== void 0 ? _configuration$config : 1e4);
|
|
237
|
+
signal.throwIfAborted();
|
|
238
|
+
return await fetch(url.toString(), {
|
|
239
|
+
...init,
|
|
240
|
+
headers,
|
|
241
|
+
signal
|
|
242
|
+
});
|
|
243
|
+
};
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
async processRequest(request) {
|
|
247
|
+
try {
|
|
248
|
+
const url = this.buildUrl(request.url, request.queryParams);
|
|
249
|
+
const response = await this.fetcher(url, {
|
|
250
|
+
method: request.method,
|
|
251
|
+
body: request.body ? JSON.stringify(request.body) : void 0
|
|
252
|
+
});
|
|
253
|
+
if (!response.ok) await this.HandleErrorResponse(response);
|
|
254
|
+
const headers = {};
|
|
255
|
+
response.headers.forEach((value, key) => {
|
|
256
|
+
headers[key] = value;
|
|
257
|
+
});
|
|
258
|
+
const resp = response.body ? await response.text() : null;
|
|
259
|
+
return new MonoCloudResponse(response.status, headers, (resp === null || resp === void 0 ? void 0 : resp.length) ? JSON.parse(resp) : null);
|
|
260
|
+
} catch (e) {
|
|
261
|
+
if (e instanceof MonoCloudException) throw e;
|
|
262
|
+
if (e.name === "TimeoutError") throw new MonoCloudException(e.message);
|
|
263
|
+
throw new MonoCloudException("Something went wrong.");
|
|
264
|
+
}
|
|
265
|
+
}
|
|
266
|
+
async processPaginatedRequest(request) {
|
|
267
|
+
try {
|
|
268
|
+
const url = this.buildUrl(request.url, request.queryParams);
|
|
269
|
+
const response = await this.fetcher(url, {
|
|
270
|
+
method: request.method,
|
|
271
|
+
body: request.body ? JSON.stringify(request.body) : void 0
|
|
272
|
+
});
|
|
273
|
+
if (!response.ok) await this.HandleErrorResponse(response);
|
|
274
|
+
const headers = {};
|
|
275
|
+
response.headers.forEach((value, key) => {
|
|
276
|
+
headers[key] = value;
|
|
277
|
+
});
|
|
278
|
+
const paginationData = this.resolvePaginationHeader(response.headers);
|
|
279
|
+
return new MonoCloudPageResponse(response.status, headers, response.body ? await response.json() : null, paginationData);
|
|
280
|
+
} catch (e) {
|
|
281
|
+
if (e instanceof MonoCloudException) throw e;
|
|
282
|
+
if (e.name === "TimeoutError") throw new MonoCloudException(e.message);
|
|
283
|
+
throw new MonoCloudException("Something went wrong.");
|
|
284
|
+
}
|
|
285
|
+
}
|
|
286
|
+
async HandleErrorResponse(response) {
|
|
287
|
+
const contentType = response.headers.get("content-type");
|
|
288
|
+
if (contentType === null || contentType === void 0 ? void 0 : contentType.startsWith("application/problem+json")) {
|
|
289
|
+
const body = await response.json();
|
|
290
|
+
let result = body ? new ProblemDetails(body) : void 0;
|
|
291
|
+
if ((result === null || result === void 0 ? void 0 : result.type) === ValidationExceptionTypes.IdentityValidationError) result = new ErrorCodeValidationProblemDetails(result);
|
|
292
|
+
if ((result === null || result === void 0 ? void 0 : result.type) === ValidationExceptionTypes.ValidationError) result = new KeyValidationProblemDetails(result);
|
|
293
|
+
if (!result) throw new MonoCloudException("Invalid body");
|
|
294
|
+
MonoCloudExceptionHandler.ThrowProblemErr(result);
|
|
295
|
+
}
|
|
296
|
+
const respStrng = await response.text();
|
|
297
|
+
MonoCloudExceptionHandler.ThrowErr(response.status, respStrng && respStrng !== "" ? respStrng : response.statusText);
|
|
298
|
+
}
|
|
299
|
+
sanitizeUrl(url) {
|
|
300
|
+
let u = url;
|
|
301
|
+
if (!u.startsWith("https://")) u = `https://${u}`;
|
|
302
|
+
if (u.endsWith("/")) u = u.substring(0, u.length - 1);
|
|
303
|
+
return u;
|
|
304
|
+
}
|
|
305
|
+
resolvePaginationHeader(headers) {
|
|
306
|
+
var _pageData$page_size, _pageData$current_pag, _pageData$total_count, _pageData$has_previou, _pageData$has_next;
|
|
307
|
+
const paginationHeader = headers.get("x-pagination");
|
|
308
|
+
const pageData = paginationHeader ? JSON.parse(paginationHeader) : void 0;
|
|
309
|
+
return {
|
|
310
|
+
page_size: (_pageData$page_size = pageData === null || pageData === void 0 ? void 0 : pageData.page_size) !== null && _pageData$page_size !== void 0 ? _pageData$page_size : 0,
|
|
311
|
+
current_page: (_pageData$current_pag = pageData === null || pageData === void 0 ? void 0 : pageData.current_page) !== null && _pageData$current_pag !== void 0 ? _pageData$current_pag : 0,
|
|
312
|
+
total_count: (_pageData$total_count = pageData === null || pageData === void 0 ? void 0 : pageData.total_count) !== null && _pageData$total_count !== void 0 ? _pageData$total_count : 0,
|
|
313
|
+
has_previous: (_pageData$has_previou = pageData === null || pageData === void 0 ? void 0 : pageData.has_previous) !== null && _pageData$has_previou !== void 0 ? _pageData$has_previou : false,
|
|
314
|
+
has_next: (_pageData$has_next = pageData === null || pageData === void 0 ? void 0 : pageData.has_next) !== null && _pageData$has_next !== void 0 ? _pageData$has_next : false
|
|
315
|
+
};
|
|
316
|
+
}
|
|
317
|
+
buildUrl(url, queryParams) {
|
|
318
|
+
let urlStr = url;
|
|
319
|
+
if (urlStr.startsWith("/")) urlStr = urlStr.substring(1, urlStr.length);
|
|
320
|
+
if (!queryParams) return urlStr;
|
|
321
|
+
urlStr += "?";
|
|
322
|
+
Object.keys(queryParams).forEach((key) => {
|
|
323
|
+
urlStr += `${key}=${encodeURIComponent(queryParams[key])}&`;
|
|
324
|
+
});
|
|
325
|
+
urlStr = urlStr.substring(0, urlStr.length - 1);
|
|
326
|
+
return urlStr;
|
|
327
|
+
}
|
|
328
|
+
};
|
|
329
|
+
|
|
330
|
+
//#endregion
|
|
331
|
+
export { ErrorCodeValidationProblemDetails, KeyValidationProblemDetails, MonoCloudBadRequestException, MonoCloudClientBase, MonoCloudConflictException, MonoCloudErrorCodeValidationException, MonoCloudException, MonoCloudForbiddenException, MonoCloudKeyValidationException, MonoCloudModelStateException, MonoCloudNotFoundException, MonoCloudPageResponse, MonoCloudRequestException, MonoCloudResourceExhaustedException, MonoCloudResponse, MonoCloudServerException, MonoCloudUnauthorizedException, ProblemDetails };
|
|
332
|
+
//# sourceMappingURL=index.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.mjs","names":["problemDetails: ProblemDetails | undefined","errorMessage: string | undefined","headers: Record<string, string>","headers: Record<string, any>","e: any"],"sources":["../src/exceptions/monocloud-exception.ts","../src/exceptions/monocloud-request-exception.ts","../src/exceptions/monocloud-bad-request-exception.ts","../src/exceptions/monocloud-conflict-exception.ts","../src/exceptions/monocloud-error-code-validation-exception.ts","../src/exceptions/monocloud-forbidden-exception.ts","../src/exceptions/monocloud-key-validation-exception.ts","../src/exceptions/monocloud-model-state-exception.ts","../src/exceptions/monocloud-not-found-exception.ts","../src/exceptions/monocloud-resource-exhausted-exception.ts","../src/exceptions/monocloud-server-exception.ts","../src/exceptions/monocloud-unauthorized-exception.ts","../src/models/error-code-validation-error.ts","../src/models/problem-details.ts","../src/models/error-code-validation-problem-details.ts","../src/models/key-validation-problem-details.ts","../src/models/monocloud-response.ts","../src/models/monocloud-page-response.ts","../src/exceptions/validation-exception-types.ts","../src/exceptions/monocloud-exception-handler.ts","../src/base/monocloud-client-base.ts"],"sourcesContent":["/// <summary>\n/// The MonoCloud Exception\n/// </summary>\nexport class MonoCloudException extends Error {}\n","import { ProblemDetails } from '../models/problem-details';\nimport { MonoCloudException } from './monocloud-exception';\n\nexport class MonoCloudRequestException extends MonoCloudException {\n /// <summary>\n /// The problem details received from the server.\n /// </summary>\n response?: ProblemDetails;\n\n /// <summary>\n /// Initializes the MonoCloudRequestException Class\n /// </summary>\n /// <param name=\"response\">The problem details returned from the server.</param>\n constructor(response: ProblemDetails);\n\n /// <summary>\n /// Initializes the MonoCloudRequestException Class\n /// </summary>\n /// <param name=\"message\">The error message.</param>\n constructor(message: string);\n\n /// <summary>\n /// Initializes the MonoCloudRequestException Class\n /// </summary>\n /// <param name=\"response\">The problem details returned from the server.</param>\n /// <param name=\"message\">The error message.</param>\n constructor(response: ProblemDetails, message: string);\n\n constructor(...args: [ProblemDetails, string] | [ProblemDetails] | [string]) {\n let problemDetails: ProblemDetails | undefined;\n let errorMessage: string | undefined;\n\n if (Array.isArray(args)) {\n if (args.length === 2) {\n problemDetails = args[0];\n errorMessage = args[1];\n } else if (args.length === 1 && typeof args[0] === 'string') {\n errorMessage = args[0];\n } else if (args.length === 1 && typeof args[0] === 'object') {\n problemDetails = args[0];\n errorMessage = args[0].title;\n } else {\n throw new Error('Invalid arguments in constructor');\n }\n }\n\n if (!errorMessage) {\n throw new Error('Invalid error message');\n }\n\n super(errorMessage);\n this.response = problemDetails;\n }\n}\n","import { ProblemDetails } from '../models/problem-details';\nimport { MonoCloudRequestException } from './monocloud-request-exception';\n\n/// <summary>\n/// The MonoCloud Bad Request Exception\n/// </summary>\nexport class MonoCloudBadRequestException extends MonoCloudRequestException {\n /// <summary>\n /// Initializes the MonoCloudBadRequestException Class\n /// </summary>\n /// <param name=\"response\">The problem details returned from the server.</param>\n constructor(response: ProblemDetails);\n\n /// <summary>\n /// Initializes the MonoCloudBadRequestException Class\n /// </summary>\n /// <param name=\"message\">The error message.</param>\n constructor(message: string);\n\n constructor(arg: ProblemDetails | string) {\n if (typeof arg === 'string') {\n super(arg);\n } else {\n super(arg);\n }\n }\n}\n","import { ProblemDetails } from '../models/problem-details';\nimport { MonoCloudRequestException } from './monocloud-request-exception';\n\n/// <summary>\n/// The MonoCloud Conflict Exception\n/// </summary>\nexport class MonoCloudConflictException extends MonoCloudRequestException {\n /// <summary>\n /// Initializes the MonoCloudConflictException Class\n /// </summary>\n /// <param name=\"response\">The problem details returned from the server.</param>\n constructor(response: ProblemDetails);\n\n /// <summary>\n /// Initializes the MonoCloudConflictException Class\n /// </summary>\n /// <param name=\"message\">The error message.</param>\n constructor(message: string);\n\n constructor(arg: ProblemDetails | string) {\n if (typeof arg === 'string') {\n super(arg);\n } else {\n super(arg);\n }\n }\n}\n","import { ErrorCodeValidationError } from '../models/error-code-validation-error';\nimport { ErrorCodeValidationProblemDetails } from '../models/error-code-validation-problem-details';\nimport { MonoCloudRequestException } from './monocloud-request-exception';\n\n/// <summary>\n/// The MonoCloud Error Code Validation Exception\n/// </summary>\nexport class MonoCloudErrorCodeValidationException extends MonoCloudRequestException {\n errors: ErrorCodeValidationError[];\n\n /// <summary>\n /// Initializes the MonoCloudErrorCodeValidationException Class\n /// </summary>\n /// <param name=\"response\">The problem details returned from the server.</param>\n constructor(response: ErrorCodeValidationProblemDetails) {\n super(\n response,\n `${response.title} : ${JSON.stringify(response.errors, undefined, 2)}`\n );\n this.errors = response.errors;\n }\n}\n","import { ProblemDetails } from '../models/problem-details';\nimport { MonoCloudRequestException } from './monocloud-request-exception';\n\n/// <summary>\n/// The MonoCloud Forbidden Exception\n/// </summary>\nexport class MonoCloudForbiddenException extends MonoCloudRequestException {\n /// <summary>\n /// Initializes the MonoCloudForbiddenException Class\n /// </summary>\n /// <param name=\"response\">The problem details returned from the server.</param>\n constructor(response: ProblemDetails);\n\n /// <summary>\n /// Initializes the MonoCloudForbiddenException Class\n /// </summary>\n /// <param name=\"message\">The error message.</param>\n constructor(message: string);\n\n constructor(arg: ProblemDetails | string) {\n if (typeof arg === 'string') {\n super(arg);\n } else {\n super(arg);\n }\n }\n}\n","import { KeyValidationProblemDetails } from '../models/key-validation-problem-details';\nimport { MonoCloudRequestException } from './monocloud-request-exception';\n\n/// <summary>\n/// The MonoCloud Key Validation Exception Exception\n/// </summary>\nexport class MonoCloudKeyValidationException extends MonoCloudRequestException {\n errors: Record<string, string[]>;\n\n /// <summary>\n /// Initializes the MonoCloudKeyValidationException Class\n /// </summary>\n /// <param name=\"response\">The problem details returned from the server.</param>\n constructor(response: KeyValidationProblemDetails) {\n super(\n response,\n `${response.title} : ${JSON.stringify(response.errors, undefined, 2)}`\n );\n this.errors = response.errors;\n }\n}\n","import { ProblemDetails } from '../models/problem-details';\nimport { MonoCloudRequestException } from './monocloud-request-exception';\n\n/// <summary>\n/// The MonoCloud Model State Exception\n/// </summary>\nexport class MonoCloudModelStateException extends MonoCloudRequestException {\n /// <summary>\n /// Initializes the MonoCloudModelStateException Class\n /// </summary>\n /// <param name=\"response\">The problem details returned from the server.</param>\n constructor(response: ProblemDetails);\n\n /// <summary>\n /// Initializes the MonoCloudModelStateException Class\n /// </summary>\n /// <param name=\"message\">The error message.</param>\n constructor(message: string);\n\n constructor(arg: ProblemDetails | string) {\n if (typeof arg === 'string') {\n super(arg);\n } else {\n super(arg);\n }\n }\n}\n","import { ProblemDetails } from '../models/problem-details';\nimport { MonoCloudRequestException } from './monocloud-request-exception';\n\n/// <summary>\n/// The MonoCloud Not Found Exception\n/// </summary>\nexport class MonoCloudNotFoundException extends MonoCloudRequestException {\n /// <summary>\n /// Initializes the MonoCloudNotFoundException Class\n /// </summary>\n /// <param name=\"response\">The problem details returned from the server.</param>\n constructor(response: ProblemDetails);\n\n /// <summary>\n /// Initializes the MonoCloudNotFoundException Class\n /// </summary>\n /// <param name=\"message\">The error message.</param>\n constructor(message: string);\n\n constructor(arg: ProblemDetails | string) {\n if (typeof arg === 'string') {\n super(arg);\n } else {\n super(arg);\n }\n }\n}\n","import { ProblemDetails } from '../models/problem-details';\nimport { MonoCloudRequestException } from './monocloud-request-exception';\n\n/// <summary>\n/// The MonoCloud Resource Exhausted Exception\n/// </summary>\nexport class MonoCloudResourceExhaustedException extends MonoCloudRequestException {\n /// <summary>\n /// Initializes the MonoCloudResourceExhaustedException Class\n /// </summary>\n /// <param name=\"response\">The problem details returned from the server.</param>\n constructor(response: ProblemDetails);\n\n /// <summary>\n /// Initializes the MonoCloudResourceExhaustedException Class\n /// </summary>\n /// <param name=\"message\">The error message.</param>\n constructor(message: string);\n\n constructor(arg: ProblemDetails | string) {\n if (typeof arg === 'string') {\n super(arg);\n } else {\n super(arg);\n }\n }\n}\n","import { ProblemDetails } from '../models/problem-details';\nimport { MonoCloudRequestException } from './monocloud-request-exception';\n\n/// <summary>\n/// The MonoCloud Server Exception\n/// </summary>\nexport class MonoCloudServerException extends MonoCloudRequestException {\n /// <summary>\n /// Initializes the MonoCloudServerException Class\n /// </summary>\n /// <param name=\"response\">The problem details returned from the server.</param>\n constructor(response: ProblemDetails);\n\n /// <summary>\n /// Initializes the MonoCloudServerException Class\n /// </summary>\n /// <param name=\"message\">The error message.</param>\n constructor(message: string);\n\n constructor(arg: ProblemDetails | string) {\n if (typeof arg === 'string') {\n super(arg);\n } else {\n super(arg);\n }\n }\n}\n","import { ProblemDetails } from '../models/problem-details';\nimport { MonoCloudRequestException } from './monocloud-request-exception';\n\n/// <summary>\n/// The MonoCloud Unauthorized Exception\n/// </summary>\nexport class MonoCloudUnauthorizedException extends MonoCloudRequestException {\n /// <summary>\n /// Initializes the MonoCloudUnauthorizedException Class\n /// </summary>\n /// <param name=\"response\">The problem details returned from the server.</param>\n constructor(response: ProblemDetails);\n\n /// <summary>\n /// Initializes the MonoCloudUnauthorizedException Class\n /// </summary>\n /// <param name=\"message\">The error message.</param>\n constructor(message: string);\n\n constructor(arg: ProblemDetails | string) {\n if (typeof arg === 'string') {\n super(arg);\n } else {\n super(arg);\n }\n }\n}\n","export class ErrorCodeValidationError {\n /// <summary>\n /// The error code.\n /// </summary>\n code: string;\n\n /// <summary>\n /// Brief explanation of the error.\n /// </summary>\n description: string;\n\n /// <summary>\n /// The field the error belongs to.\n /// </summary>\n field?: string;\n\n constructor(code: string, description: string, field?: string) {\n this.code = code;\n this.description = description;\n this.field = field;\n }\n}\n","export class ProblemDetails {\n /// <summary>\n /// The type of error\n /// </summary>\n type: string;\n\n /// <summary>\n /// The title of the error\n /// </summary>\n title: string;\n\n /// <summary>\n /// The status code representing the error\n /// </summary>\n status: number;\n\n /// <summary>\n /// The error details\n /// </summary>\n detail: string;\n\n /// <summary>\n /// The instance\n /// </summary>\n instance: string;\n\n /// <summary>\n /// Additional data about the error\n /// </summary>\n\n [key: string]: any;\n\n constructor(response: ProblemDetails) {\n this.type = response.type;\n this.title = response.title;\n this.status = response.status;\n this.detail = response.detail;\n this.instance = response.instance;\n Object.keys(response)\n .filter(\n x => x !== 'type' && x !== 'title' && x !== 'status' && x !== 'instance'\n )\n .forEach(key => {\n this[key] = response[key];\n });\n }\n}\n","import { ErrorCodeValidationError } from './error-code-validation-error';\nimport { ProblemDetails } from './problem-details';\n\nexport class ErrorCodeValidationProblemDetails extends ProblemDetails {\n errors: ErrorCodeValidationError[];\n\n constructor(response: ProblemDetails) {\n super(response);\n this.errors = (response.errors as ErrorCodeValidationError[]).map(\n err => new ErrorCodeValidationError(err.code, err.description, err.field)\n );\n }\n}\n","import { ProblemDetails } from './problem-details';\n\nexport class KeyValidationProblemDetails extends ProblemDetails {\n /// <summary>\n /// A collection of errors\n /// </summary>\n errors: Record<string, string[]>;\n\n constructor(response: ProblemDetails) {\n super(response);\n this.errors = response.errors;\n }\n}\n","export class MonoCloudResponse<TResult = unknown> {\n status: number;\n\n headers: Record<string, any>;\n\n result: TResult;\n\n constructor(status: number, headers: Record<string, any>, result: TResult) {\n this.status = status;\n this.headers = headers;\n this.result = result;\n }\n}\n","import { MonoCloudResponse } from './monocloud-response';\nimport { PageModel } from './page-model';\n\nexport class MonoCloudPageResponse<\n TResult = unknown,\n> extends MonoCloudResponse<TResult> {\n pageData: PageModel;\n\n constructor(\n status: number,\n headers: Record<string, any>,\n result: TResult,\n pageData: PageModel\n ) {\n super(status, headers, result);\n this.pageData = pageData;\n }\n}\n","export const ValidationExceptionTypes = {\n ValidationError: 'https://httpstatuses.io/422#validation-error',\n IdentityValidationError:\n 'https://httpstatuses.io/422#identity-validation-error',\n};\n","import { ErrorCodeValidationProblemDetails } from '../models/error-code-validation-problem-details';\nimport { KeyValidationProblemDetails } from '../models/key-validation-problem-details';\nimport { ProblemDetails } from '../models/problem-details';\nimport { MonoCloudBadRequestException } from './monocloud-bad-request-exception';\nimport { MonoCloudConflictException } from './monocloud-conflict-exception';\nimport { MonoCloudErrorCodeValidationException } from './monocloud-error-code-validation-exception';\nimport { MonoCloudException } from './monocloud-exception';\nimport { MonoCloudKeyValidationException } from './monocloud-key-validation-exception';\nimport { MonoCloudModelStateException } from './monocloud-model-state-exception';\nimport { MonoCloudNotFoundException } from './monocloud-not-found-exception';\nimport { MonoCloudResourceExhaustedException } from './monocloud-resource-exhausted-exception';\nimport { MonoCloudServerException } from './monocloud-server-exception';\nimport { MonoCloudUnauthorizedException } from './monocloud-unauthorized-exception';\n\n/// <summary>\n/// The MonoCloud Exception Handler\n/// </summary>\nexport class MonoCloudExceptionHandler {\n /// <summary>\n /// Converts the Problem Details returned from the server into an exception\n /// </summary>\n /// <param name=\"problemDetails\">The problem details returned from the server.</param>\n /// <returns></returns>\n public static ThrowProblemErr(problemDetails: ProblemDetails): void {\n switch (problemDetails.status) {\n case 400:\n throw new MonoCloudBadRequestException(problemDetails);\n case 401:\n throw new MonoCloudUnauthorizedException(problemDetails);\n case 403:\n throw new MonoCloudUnauthorizedException(problemDetails);\n case 404:\n throw new MonoCloudNotFoundException(problemDetails);\n case 409:\n throw new MonoCloudConflictException(problemDetails);\n case 422:\n if (problemDetails instanceof ErrorCodeValidationProblemDetails) {\n throw new MonoCloudErrorCodeValidationException(problemDetails);\n }\n\n if (problemDetails instanceof KeyValidationProblemDetails) {\n throw new MonoCloudKeyValidationException(problemDetails);\n }\n\n throw new MonoCloudModelStateException(problemDetails);\n case 429:\n throw new MonoCloudResourceExhaustedException(problemDetails);\n case 500:\n throw new MonoCloudServerException(problemDetails);\n default:\n throw new MonoCloudException(\n problemDetails.title ?? 'An Unknown Error Occured'\n );\n }\n }\n\n /// <summary>\n /// Converts the error returned from the server into an exception\n /// </summary>\n /// <param name=\"statusCode\">The response status code.</param>\n /// <param name=\"message\">The error message returned from the server.</param>\n /// <returns></returns>\n public static ThrowErr(statusCode: number, message?: string): void {\n switch (statusCode) {\n case 400:\n throw new MonoCloudBadRequestException(message ?? 'Bad Request');\n case 401:\n throw new MonoCloudUnauthorizedException(message ?? 'Unauthorized');\n case 403:\n throw new MonoCloudUnauthorizedException(message ?? 'Forbidden');\n case 404:\n throw new MonoCloudNotFoundException(message ?? 'Not Found');\n case 409:\n throw new MonoCloudConflictException(message ?? 'Conflict');\n case 422:\n throw new MonoCloudModelStateException(\n message ?? 'Unprocessable entity'\n );\n case 429:\n throw new MonoCloudResourceExhaustedException(\n message ?? 'Resource Exhausted'\n );\n case 500:\n throw new MonoCloudServerException(message ?? 'Server Error');\n default:\n throw new MonoCloudException(message ?? 'An Unknown Error Occured');\n }\n }\n}\n","import { MonoCloudConfig } from './monocloud-config';\nimport { MonoCloudResponse } from '../models/monocloud-response';\nimport { MonoCloudException } from '../exceptions/monocloud-exception';\nimport { MonoCloudPageResponse } from '../models/monocloud-page-response';\nimport { PageModel } from '../models/page-model';\nimport { ProblemDetails } from '../models/problem-details';\nimport { ValidationExceptionTypes } from '../exceptions/validation-exception-types';\nimport { ErrorCodeValidationProblemDetails } from '../models/error-code-validation-problem-details';\nimport { KeyValidationProblemDetails } from '../models/key-validation-problem-details';\nimport { MonoCloudExceptionHandler } from '../exceptions/monocloud-exception-handler';\nimport { MonoCloudRequest } from '../models/monocloud-request';\nimport { Fetcher } from '../models/fetcher';\n\nexport abstract class MonoCloudClientBase {\n protected fetcher: Fetcher;\n\n constructor(configuration: MonoCloudConfig, fetcher?: Fetcher) {\n if (fetcher) {\n this.fetcher = fetcher;\n } else {\n if (!configuration) {\n throw new MonoCloudException('Configuration is required');\n }\n\n if (!configuration.domain) {\n throw new MonoCloudException('Tenant Domain is required');\n }\n\n if (!configuration.apiKey) {\n throw new MonoCloudException('Api Key is required');\n }\n\n const headers: Record<string, string> = {\n 'X-API-KEY': configuration.apiKey,\n 'Content-Type': 'application/json',\n };\n\n const baseUrl = `${this.sanitizeUrl(configuration.domain)}/api/`;\n\n this.fetcher = async (\n input: string | URL | globalThis.Request,\n init?: RequestInit\n ): Promise<Response> => {\n const url = new URL(input, baseUrl);\n\n const signal = AbortSignal.timeout(\n configuration.config?.timeout ?? 10000\n );\n signal.throwIfAborted();\n\n const resp = await fetch(url.toString(), { ...init, headers, signal });\n\n return resp;\n };\n }\n }\n\n protected async processRequest<T = unknown>(\n request: MonoCloudRequest\n ): Promise<MonoCloudResponse<T>> {\n try {\n const url = this.buildUrl(request.url, request.queryParams);\n\n const response = await this.fetcher(url, {\n method: request.method,\n body: request.body ? JSON.stringify(request.body) : undefined,\n });\n\n if (!response.ok) {\n await this.HandleErrorResponse(response);\n }\n\n const headers: Record<string, any> = {};\n\n response.headers.forEach((value, key) => {\n headers[key] = value;\n });\n\n const resp = response.body ? await response.text() : null;\n\n return new MonoCloudResponse<T>(\n response.status,\n headers,\n (resp?.length ? JSON.parse(resp) : null) as T\n );\n } catch (e: any) {\n if (e instanceof MonoCloudException) {\n throw e;\n }\n\n if (e.name === 'TimeoutError') {\n throw new MonoCloudException(e.message);\n }\n\n throw new MonoCloudException('Something went wrong.');\n }\n }\n\n protected async processPaginatedRequest<T = unknown>(\n request: MonoCloudRequest\n ): Promise<MonoCloudPageResponse<T>> {\n try {\n const url = this.buildUrl(request.url, request.queryParams);\n\n const response = await this.fetcher(url, {\n method: request.method,\n body: request.body ? JSON.stringify(request.body) : undefined,\n });\n\n if (!response.ok) {\n await this.HandleErrorResponse(response);\n }\n\n const headers: Record<string, any> = {};\n\n response.headers.forEach((value, key) => {\n headers[key] = value;\n });\n\n const paginationData = this.resolvePaginationHeader(response.headers);\n\n return new MonoCloudPageResponse<T>(\n response.status,\n headers,\n (response.body ? await response.json() : null) as T,\n paginationData\n );\n } catch (e: any) {\n if (e instanceof MonoCloudException) {\n throw e;\n }\n\n if (e.name === 'TimeoutError') {\n throw new MonoCloudException(e.message);\n }\n\n throw new MonoCloudException('Something went wrong.');\n }\n }\n\n private async HandleErrorResponse(response: Response): Promise<void> {\n const contentType = response.headers.get('content-type');\n if (contentType?.startsWith('application/problem+json')) {\n const body = await response.json();\n let result = body\n ? new ProblemDetails(body as ProblemDetails)\n : undefined;\n\n if (result?.type === ValidationExceptionTypes.IdentityValidationError) {\n result = new ErrorCodeValidationProblemDetails(result);\n }\n\n if (result?.type === ValidationExceptionTypes.ValidationError) {\n result = new KeyValidationProblemDetails(result);\n }\n\n if (!result) {\n throw new MonoCloudException('Invalid body');\n }\n\n MonoCloudExceptionHandler.ThrowProblemErr(result);\n }\n\n const respStrng = await response.text();\n MonoCloudExceptionHandler.ThrowErr(\n response.status,\n respStrng && respStrng !== '' ? respStrng : response.statusText\n );\n }\n\n private sanitizeUrl(url: string): string {\n let u = url;\n if (!u.startsWith('https://')) {\n u = `https://${u}`;\n }\n\n if (u.endsWith('/')) {\n u = u.substring(0, u.length - 1);\n }\n\n return u;\n }\n\n private resolvePaginationHeader(headers: Headers): PageModel {\n const paginationHeader = headers.get('x-pagination');\n const pageData = paginationHeader\n ? JSON.parse(paginationHeader)\n : undefined;\n\n return {\n page_size: pageData?.page_size ?? 0,\n current_page: pageData?.current_page ?? 0,\n total_count: pageData?.total_count ?? 0,\n has_previous: pageData?.has_previous ?? false,\n has_next: pageData?.has_next ?? false,\n };\n }\n\n private buildUrl(\n url: string,\n queryParams?: Record<string, string | number | boolean>\n ): string {\n let urlStr = url;\n\n if (urlStr.startsWith('/')) {\n urlStr = urlStr.substring(1, urlStr.length);\n }\n\n if (!queryParams) {\n return urlStr;\n }\n\n urlStr += '?';\n\n Object.keys(queryParams).forEach(key => {\n urlStr += `${key}=${encodeURIComponent(queryParams[key])}&`;\n });\n\n urlStr = urlStr.substring(0, urlStr.length - 1);\n\n return urlStr;\n }\n}\n"],"mappings":";AAGA,IAAa,qBAAb,cAAwC,MAAM;;;;ACA9C,IAAa,4BAAb,cAA+C,mBAAmB;CAyBhE,YAAY,GAAG,MAA8D;EAC3E,IAAIA;EACJ,IAAIC;AAEJ,MAAI,MAAM,QAAQ,KAAK,CACrB,KAAI,KAAK,WAAW,GAAG;AACrB,oBAAiB,KAAK;AACtB,kBAAe,KAAK;aACX,KAAK,WAAW,KAAK,O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Y,SAAS,WACtD;;CAGH,AAAQ,YAAY,KAAqB;EACvC,IAAI,IAAI;AACR,MAAI,CAAC,EAAE,WAAW,WAAW,CAC3B,KAAI,WAAW;AAGjB,MAAI,EAAE,SAAS,IAAI,CACjB,KAAI,EAAE,UAAU,GAAG,EAAE,SAAS,EAAE;AAGlC,SAAO;;CAGT,AAAQ,wBAAwB,SAA6B;;EAC3D,MAAM,mBAAmB,QAAQ,IAAI,eAAe;EACpD,MAAM,WAAW,mBACb,KAAK,MAAM,iBAAiB,GAC5B;AAEJ,SAAO;GACL,sFAAW,SAAU,8EAAa;GAClC,2FAAc,SAAU,qFAAgB;GACxC,0FAAa,SAAU,oFAAe;GACtC,2FAAc,SAAU,qFAAgB;GACxC,oFAAU,SAAU,2EAAY;GACjC;;CAGH,AAAQ,SACN,KACA,aACQ;EACR,IAAI,SAAS;AAEb,MAAI,OAAO,WAAW,IAAI,CACxB,UAAS,OAAO,UAAU,GAAG,OAAO,OAAO;AAG7C,MAAI,CAAC,YACH,QAAO;AAGT,YAAU;AAEV,SAAO,KAAK,YAAY,CAAC,SAAQ,QAAO;AACtC,aAAU,GAAG,IAAI,GAAG,mBAAmB,YAAY,KAAK,CAAC;IACzD;AAEF,WAAS,OAAO,UAAU,GAAG,OAAO,SAAS,EAAE;AAE/C,SAAO"}
|
package/package.json
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@monocloud/management-core",
|
|
3
|
+
"version": "0.1.1",
|
|
4
|
+
"description": "MonoCloud Management JavaScript Core Library",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"monocloud",
|
|
7
|
+
"management",
|
|
8
|
+
"sdk",
|
|
9
|
+
"node",
|
|
10
|
+
"core"
|
|
11
|
+
],
|
|
12
|
+
"homepage": "https://www.monocloud.com",
|
|
13
|
+
"bugs": {
|
|
14
|
+
"url": "https://github.com/monocld/monocloud-management-js/issues"
|
|
15
|
+
},
|
|
16
|
+
"repository": {
|
|
17
|
+
"type": "git",
|
|
18
|
+
"url": "git+https://github.com/monocld/monocloud-management-js.git",
|
|
19
|
+
"directory": "packages/core"
|
|
20
|
+
},
|
|
21
|
+
"license": "MIT",
|
|
22
|
+
"author": {
|
|
23
|
+
"name": "MonoCloud",
|
|
24
|
+
"email": "support@monocloud.com"
|
|
25
|
+
},
|
|
26
|
+
"main": "dist/index.cjs",
|
|
27
|
+
"module": "dist/index.mjs",
|
|
28
|
+
"types": "dist/index.d.mts",
|
|
29
|
+
"files": [
|
|
30
|
+
"dist"
|
|
31
|
+
],
|
|
32
|
+
"devDependencies": {
|
|
33
|
+
"eslint": "9.39.1"
|
|
34
|
+
},
|
|
35
|
+
"engines": {
|
|
36
|
+
"node": ">= 11.0.0"
|
|
37
|
+
},
|
|
38
|
+
"scripts": {
|
|
39
|
+
"build": "tsdown",
|
|
40
|
+
"lint:es": "eslint src",
|
|
41
|
+
"lint:ts": "tsc",
|
|
42
|
+
"lint": "pnpm run lint:es && pnpm run lint:ts"
|
|
43
|
+
}
|
|
44
|
+
}
|