@scaleway/sdk 0.1.0-beta.9 → 1.0.0
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/README.md +1 -1
- package/dist/api/account/index.js +2 -2
- package/dist/api/account/v2/api.gen.js +5 -18
- package/dist/api/account/v2/index.gen.js +3 -0
- package/dist/api/account/v2/marshalling.gen.js +0 -2
- package/dist/api/account/v2alpha1/api.gen.js +3 -13
- package/dist/api/account/v2alpha1/marshalling.gen.js +0 -5
- package/dist/api/applesilicon/index.js +2 -2
- package/dist/api/applesilicon/v1alpha1/api.gen.js +7 -27
- package/dist/api/applesilicon/v1alpha1/index.gen.js +4 -0
- package/dist/api/applesilicon/v1alpha1/marshalling.gen.js +1 -14
- package/dist/api/baremetal/v1/api.gen.js +47 -49
- package/dist/api/baremetal/v1/api.utils.js +2 -10
- package/dist/api/baremetal/v1/content.gen.js +6 -3
- package/dist/api/baremetal/v1/index.js +4 -1
- package/dist/api/baremetal/v1/marshalling.gen.js +44 -45
- package/dist/api/baremetal/v1/validation-rules.gen.js +109 -0
- package/dist/api/container/index.js +2 -2
- package/dist/api/container/v1beta1/api.gen.js +5 -51
- package/dist/api/container/v1beta1/content.gen.js +4 -4
- package/dist/api/container/v1beta1/{index.js → index.gen.js} +3 -1
- package/dist/api/container/v1beta1/marshalling.gen.js +12 -26
- package/dist/api/domain/index.js +2 -2
- package/dist/api/domain/v2beta1/api.gen.js +55 -85
- package/dist/api/domain/v2beta1/content.gen.js +22 -1
- package/dist/api/domain/v2beta1/index.gen.js +4 -0
- package/dist/api/domain/v2beta1/marshalling.gen.js +47 -165
- package/dist/api/flexibleip/index.js +2 -2
- package/dist/api/flexibleip/v1alpha1/api.gen.js +5 -22
- package/dist/api/flexibleip/v1alpha1/content.gen.js +1 -1
- package/dist/api/flexibleip/v1alpha1/index.gen.js +6 -0
- package/dist/api/flexibleip/v1alpha1/marshalling.gen.js +0 -7
- package/dist/api/flexibleip/v1alpha1/validation-rules.gen.js +14 -0
- package/dist/api/function/index.js +2 -2
- package/dist/api/function/v1beta1/api.gen.js +54 -57
- package/dist/api/function/v1beta1/content.gen.js +8 -5
- package/dist/api/function/v1beta1/index.gen.js +4 -0
- package/dist/api/function/v1beta1/marshalling.gen.js +125 -33
- package/dist/api/iam/index.js +2 -2
- package/dist/api/iam/v1alpha1/api.gen.js +15 -62
- package/dist/api/iam/v1alpha1/index.gen.js +5 -0
- package/dist/api/iam/v1alpha1/marshalling.gen.js +7 -29
- package/dist/api/iam/v1alpha1/validation-rules.gen.js +184 -0
- package/dist/api/instance/v1/api.gen.js +38 -118
- package/dist/api/instance/v1/api.utils.js +59 -51
- package/dist/api/instance/v1/content.gen.js +7 -7
- package/dist/api/instance/v1/marshalling.gen.js +46 -168
- package/dist/api/instance/v1/marshalling.utils.js +63 -0
- package/dist/api/iot/index.js +2 -2
- package/dist/api/iot/v1/api.gen.js +5 -56
- package/dist/api/iot/v1/index.gen.js +4 -0
- package/dist/api/iot/v1/marshalling.gen.js +2 -51
- package/dist/api/k8s/index.js +2 -2
- package/dist/api/k8s/v1/api.gen.js +17 -43
- package/dist/api/k8s/v1/content.gen.js +2 -2
- package/dist/api/k8s/v1/index.gen.js +6 -0
- package/dist/api/k8s/v1/marshalling.gen.js +18 -36
- package/dist/api/k8s/v1/validation-rules.gen.js +92 -0
- package/dist/api/lb/v1/api.gen.js +8 -163
- package/dist/api/lb/v1/api.utils.js +3 -9
- package/dist/api/lb/v1/content.gen.js +3 -3
- package/dist/api/lb/v1/marshalling.gen.js +74 -101
- package/dist/api/marketplace/index.js +4 -2
- package/dist/api/marketplace/v1/api.gen.js +3 -13
- package/dist/api/marketplace/v1/index.gen.js +3 -0
- package/dist/api/marketplace/v1/marshalling.gen.js +0 -13
- package/dist/api/marketplace/v2/api.gen.js +85 -0
- package/dist/api/marketplace/v2/index.gen.js +3 -0
- package/dist/api/marketplace/v2/marshalling.gen.js +92 -0
- package/dist/api/mnq/index.js +2 -2
- package/dist/api/mnq/v1alpha1/api.gen.js +7 -25
- package/dist/api/mnq/v1alpha1/index.gen.js +3 -0
- package/dist/api/mnq/v1alpha1/marshalling.gen.js +7 -46
- package/dist/api/rdb/index.js +2 -2
- package/dist/api/rdb/v1/api.gen.js +15 -89
- package/dist/api/rdb/v1/content.gen.js +10 -4
- package/dist/api/rdb/v1/index.gen.js +4 -0
- package/dist/api/rdb/v1/marshalling.gen.js +25 -81
- package/dist/api/redis/index.js +2 -2
- package/dist/api/redis/{v1alpha1 → v1}/api.gen.js +45 -77
- package/dist/api/redis/{v1alpha1 → v1}/content.gen.js +1 -1
- package/dist/api/redis/v1/index.gen.js +4 -0
- package/dist/api/redis/{v1alpha1 → v1}/marshalling.gen.js +42 -59
- package/dist/api/registry/index.js +2 -2
- package/dist/api/registry/v1/api.gen.js +5 -29
- package/dist/api/registry/v1/content.gen.js +2 -2
- package/dist/api/registry/v1/{index.js → index.gen.js} +3 -1
- package/dist/api/registry/v1/marshalling.gen.js +2 -8
- package/dist/api/secret/index.js +2 -0
- package/dist/api/secret/v1alpha1/api.gen.js +135 -0
- package/dist/api/secret/v1alpha1/index.gen.js +3 -0
- package/dist/api/secret/v1alpha1/marshalling.gen.js +82 -0
- package/dist/api/tem/index.js +2 -0
- package/dist/api/tem/v1alpha1/api.gen.js +109 -0
- package/dist/api/tem/v1alpha1/content.gen.js +10 -0
- package/dist/api/tem/v1alpha1/index.gen.js +4 -0
- package/dist/api/tem/v1alpha1/marshalling.gen.js +132 -0
- package/dist/api/test/index.js +2 -2
- package/dist/api/test/v1/api.gen.js +6 -20
- package/dist/api/test/v1/index.gen.js +4 -0
- package/dist/api/test/v1/marshalling.gen.js +2 -5
- package/dist/api/vpc/index.js +2 -2
- package/dist/api/vpc/v1/api.gen.js +6 -17
- package/dist/api/vpc/v1/index.gen.js +3 -0
- package/dist/api/vpc/v1/marshalling.gen.js +1 -3
- package/dist/api/vpcgw/index.js +2 -2
- package/dist/api/vpcgw/v1/api.gen.js +5 -64
- package/dist/api/vpcgw/v1/content.gen.js +2 -2
- package/dist/api/vpcgw/v1/{index.js → index.gen.js} +3 -1
- package/dist/api/vpcgw/v1/marshalling.gen.js +3 -24
- package/dist/helpers/json.js +1 -2
- package/dist/helpers/marshalling.js +7 -20
- package/dist/index.cjs +3864 -2945
- package/dist/index.d.ts +7212 -4332
- package/dist/index.js +10 -6
- package/dist/internal/async/interval-retrier.js +10 -16
- package/dist/internal/interceptors/interceptor.js +3 -1
- package/dist/internal/interceptors/request.js +1 -3
- package/dist/internal/logger/console-logger.js +3 -5
- package/dist/internal/logger/index.js +3 -5
- package/dist/internal/logger/level-resolver.js +0 -3
- package/dist/internal/validations/string-validation.js +8 -10
- package/dist/node_modules/.pnpm/@scaleway_random-name@4.0.1/node_modules/@scaleway/random-name/dist/index.js +13 -0
- package/dist/scw/api.js +3 -4
- package/dist/scw/auth.js +3 -5
- package/dist/scw/client-ini-factory.js +32 -15
- package/dist/scw/client-ini-profile.js +10 -4
- package/dist/scw/client-settings.js +32 -15
- package/dist/scw/client.js +5 -6
- package/dist/scw/constants.js +1 -1
- package/dist/scw/custom-marshalling.js +8 -14
- package/dist/scw/errors/error-parser.js +2 -21
- package/dist/scw/errors/non-standard/invalid-request-mapper.js +17 -15
- package/dist/scw/errors/non-standard/unknown-resource-mapper.js +7 -6
- package/dist/scw/errors/scw-error.js +20 -20
- package/dist/scw/errors/standard/already-exists-error.js +3 -4
- package/dist/scw/errors/standard/denied-authentication-error.js +4 -11
- package/dist/scw/errors/standard/invalid-arguments-error.js +1 -10
- package/dist/scw/errors/standard/out-of-stock-error.js +0 -3
- package/dist/scw/errors/standard/permissions-denied-error.js +1 -4
- package/dist/scw/errors/standard/precondition-failed-error.js +4 -8
- package/dist/scw/errors/standard/quotas-exceeded-error.js +14 -14
- package/dist/scw/errors/standard/resource-expired-error.js +3 -4
- package/dist/scw/errors/standard/resource-locked-error.js +3 -4
- package/dist/scw/errors/standard/resource-not-found-error.js +3 -4
- package/dist/scw/errors/standard/too-many-requests-error.js +3 -16
- package/dist/scw/errors/standard/transient-state-error.js +3 -4
- package/dist/scw/errors/types.js +0 -3
- package/dist/scw/fetch/build-fetcher.js +1 -9
- package/dist/scw/fetch/http-dumper.js +4 -7
- package/dist/scw/fetch/http-interceptors.js +9 -12
- package/dist/scw/fetch/resource-paginator.js +18 -16
- package/dist/scw/fetch/response-parser.js +17 -23
- package/node_modules/@scaleway/random-name/CHANGELOG.md +42 -63
- package/node_modules/@scaleway/random-name/dist/index.js +5 -246
- package/node_modules/@scaleway/random-name/package.json +6 -5
- package/package.json +4 -5
- package/dist/api/account/v2/index.js +0 -1
- package/dist/api/applesilicon/v1alpha1/index.js +0 -2
- package/dist/api/domain/v2beta1/index.js +0 -2
- package/dist/api/flexibleip/v1alpha1/index.js +0 -2
- package/dist/api/function/v1beta1/index.js +0 -2
- package/dist/api/iam/v1alpha1/index.js +0 -1
- package/dist/api/iot/v1/index.js +0 -2
- package/dist/api/k8s/v1/index.js +0 -2
- package/dist/api/marketplace/v1/index.js +0 -1
- package/dist/api/mnq/v1alpha1/index.js +0 -1
- package/dist/api/rdb/v1/index.js +0 -2
- package/dist/api/redis/v1alpha1/index.js +0 -2
- package/dist/api/test/v1/index.js +0 -1
- package/dist/api/vpc/v1/index.js +0 -1
- package/dist/node_modules/.pnpm/@scaleway_random-name@3.0.2/node_modules/@scaleway/random-name/dist/index.js +0 -254
- package/node_modules/@scaleway/random-name/dist/index.browser.js +0 -256
|
@@ -5,7 +5,6 @@ import { ScalewayError } from '../scw-error.js';
|
|
|
5
5
|
*
|
|
6
6
|
* @public
|
|
7
7
|
*/
|
|
8
|
-
|
|
9
8
|
class ResourceExpiredError extends ScalewayError {
|
|
10
9
|
constructor(status, body, resource, resourceId, expiredSince) {
|
|
11
10
|
super(status, body, `resource ${resource} with ID ${resourceId} expired since ${expiredSince.toISOString()}`);
|
|
@@ -16,12 +15,12 @@ class ResourceExpiredError extends ScalewayError {
|
|
|
16
15
|
this.expiredSince = expiredSince;
|
|
17
16
|
this.name = 'ResourceExpiredError';
|
|
18
17
|
}
|
|
19
|
-
|
|
20
18
|
static fromJSON(status, obj) {
|
|
21
|
-
if (typeof obj.resource !== 'string' || typeof obj.resource_id !== 'string' || typeof obj.expired_since !== 'string')
|
|
19
|
+
if (typeof obj.resource !== 'string' || typeof obj.resource_id !== 'string' || typeof obj.expired_since !== 'string') {
|
|
20
|
+
return null;
|
|
21
|
+
}
|
|
22
22
|
return new ResourceExpiredError(status, obj, obj.resource, obj.resource_id, new Date(obj.expired_since));
|
|
23
23
|
}
|
|
24
|
-
|
|
25
24
|
}
|
|
26
25
|
|
|
27
26
|
export { ResourceExpiredError };
|
|
@@ -5,7 +5,6 @@ import { ScalewayError } from '../scw-error.js';
|
|
|
5
5
|
*
|
|
6
6
|
* @public
|
|
7
7
|
*/
|
|
8
|
-
|
|
9
8
|
class ResourceLockedError extends ScalewayError {
|
|
10
9
|
constructor(status, body, resource, resourceId) {
|
|
11
10
|
super(status, body, `resource ${resource} with ID ${resourceId} is locked`);
|
|
@@ -15,12 +14,12 @@ class ResourceLockedError extends ScalewayError {
|
|
|
15
14
|
this.resourceId = resourceId;
|
|
16
15
|
this.name = 'ResourceLockedError';
|
|
17
16
|
}
|
|
18
|
-
|
|
19
17
|
static fromJSON(status, obj) {
|
|
20
|
-
if (typeof obj.resource !== 'string' || typeof obj.resource_id !== 'string')
|
|
18
|
+
if (typeof obj.resource !== 'string' || typeof obj.resource_id !== 'string') {
|
|
19
|
+
return null;
|
|
20
|
+
}
|
|
21
21
|
return new ResourceLockedError(status, obj, obj.resource, obj.resource_id);
|
|
22
22
|
}
|
|
23
|
-
|
|
24
23
|
}
|
|
25
24
|
|
|
26
25
|
export { ResourceLockedError };
|
|
@@ -5,7 +5,6 @@ import { ScalewayError } from '../scw-error.js';
|
|
|
5
5
|
*
|
|
6
6
|
* @public
|
|
7
7
|
*/
|
|
8
|
-
|
|
9
8
|
class ResourceNotFoundError extends ScalewayError {
|
|
10
9
|
constructor(status, body, resource, resourceId) {
|
|
11
10
|
super(status, body, `resource ${resource} with ID ${resourceId} is not found`);
|
|
@@ -15,12 +14,12 @@ class ResourceNotFoundError extends ScalewayError {
|
|
|
15
14
|
this.resourceId = resourceId;
|
|
16
15
|
this.name = 'ResourceNotFoundError';
|
|
17
16
|
}
|
|
18
|
-
|
|
19
17
|
static fromJSON(status, obj) {
|
|
20
|
-
if (typeof obj.resource !== 'string' || typeof obj.resource_id !== 'string')
|
|
18
|
+
if (typeof obj.resource !== 'string' || typeof obj.resource_id !== 'string') {
|
|
19
|
+
return null;
|
|
20
|
+
}
|
|
21
21
|
return new ResourceNotFoundError(status, obj, obj.resource, obj.resource_id);
|
|
22
22
|
}
|
|
23
|
-
|
|
24
23
|
}
|
|
25
24
|
|
|
26
25
|
export { ResourceNotFoundError };
|
|
@@ -14,7 +14,6 @@ import { ScalewayError } from '../scw-error.js';
|
|
|
14
14
|
*/
|
|
15
15
|
const buildMessage = (helpMessage, limit, resetSeconds, resetAt) => {
|
|
16
16
|
const details = [];
|
|
17
|
-
|
|
18
17
|
if (limit) {
|
|
19
18
|
if (limit.windowSeconds) {
|
|
20
19
|
details.push(`quota is ${limit.quota} for ${limit.windowSeconds}s`);
|
|
@@ -22,37 +21,29 @@ const buildMessage = (helpMessage, limit, resetSeconds, resetAt) => {
|
|
|
22
21
|
details.push(`quota is ${limit.quota}`);
|
|
23
22
|
}
|
|
24
23
|
}
|
|
25
|
-
|
|
26
24
|
if (resetSeconds) {
|
|
27
25
|
details.push(`resets in ${resetSeconds}s`);
|
|
28
26
|
} else if (resetAt) {
|
|
29
27
|
details.push(`resets at ${resetAt.toISOString()}`);
|
|
30
28
|
}
|
|
31
|
-
|
|
32
29
|
let output = `too many requests`;
|
|
33
|
-
|
|
34
30
|
if (details.length > 0) {
|
|
35
31
|
output += ` (${details.join(', ')})`;
|
|
36
32
|
}
|
|
37
|
-
|
|
38
33
|
if (helpMessage.length > 0) {
|
|
39
34
|
output += `: ${helpMessage}`;
|
|
40
35
|
}
|
|
41
|
-
|
|
42
36
|
return output;
|
|
43
37
|
};
|
|
38
|
+
|
|
44
39
|
/**
|
|
45
40
|
* TooManyRequestsError error happens when fetching too many times a resource.
|
|
46
41
|
*
|
|
47
42
|
* @public
|
|
48
43
|
*/
|
|
49
|
-
|
|
50
|
-
|
|
51
44
|
class TooManyRequestsError extends ScalewayError {
|
|
52
|
-
constructor(status, body, helpMessage, limit,
|
|
53
|
-
/** The
|
|
54
|
-
resetSeconds,
|
|
55
|
-
/** The timestamp when the quota resets */
|
|
45
|
+
constructor(status, body, helpMessage, limit, /** The number of seconds until the quota resets */
|
|
46
|
+
resetSeconds, /** The timestamp when the quota resets */
|
|
56
47
|
resetAt) {
|
|
57
48
|
super(status, body, buildMessage(helpMessage, limit, resetSeconds, resetAt));
|
|
58
49
|
this.status = status;
|
|
@@ -63,21 +54,17 @@ class TooManyRequestsError extends ScalewayError {
|
|
|
63
54
|
this.resetAt = resetAt;
|
|
64
55
|
this.name = 'TooManyRequestsError';
|
|
65
56
|
}
|
|
66
|
-
|
|
67
57
|
static fromJSON(status, obj) {
|
|
68
58
|
if (typeof obj.help_message !== 'string') return null;
|
|
69
59
|
let limit;
|
|
70
|
-
|
|
71
60
|
if (isJSONObject(obj.limit) && typeof obj.limit.quota === 'number') {
|
|
72
61
|
limit = {
|
|
73
62
|
quota: obj.limit.quota,
|
|
74
63
|
windowSeconds: typeof obj.limit.window_seconds === 'number' ? obj.limit.window_seconds : undefined
|
|
75
64
|
};
|
|
76
65
|
}
|
|
77
|
-
|
|
78
66
|
return new TooManyRequestsError(status, obj, obj.help_message, limit, typeof obj.reset_seconds === 'number' ? obj.reset_seconds : undefined, typeof obj.reset_at === 'string' ? new Date(obj.reset_at) : undefined);
|
|
79
67
|
}
|
|
80
|
-
|
|
81
68
|
}
|
|
82
69
|
|
|
83
70
|
export { TooManyRequestsError };
|
|
@@ -5,7 +5,6 @@ import { ScalewayError } from '../scw-error.js';
|
|
|
5
5
|
*
|
|
6
6
|
* @public
|
|
7
7
|
*/
|
|
8
|
-
|
|
9
8
|
class TransientStateError extends ScalewayError {
|
|
10
9
|
constructor(status, body, resource, resourceId, currentState) {
|
|
11
10
|
super(status, body, `resource ${resource} with ID ${resourceId} is in a transient state: ${currentState}`);
|
|
@@ -16,12 +15,12 @@ class TransientStateError extends ScalewayError {
|
|
|
16
15
|
this.currentState = currentState;
|
|
17
16
|
this.name = 'TransientStateError';
|
|
18
17
|
}
|
|
19
|
-
|
|
20
18
|
static fromJSON(status, obj) {
|
|
21
|
-
if (typeof obj.resource !== 'string' || typeof obj.resource_id !== 'string' || typeof obj.current_state !== 'string')
|
|
19
|
+
if (typeof obj.resource !== 'string' || typeof obj.resource_id !== 'string' || typeof obj.current_state !== 'string') {
|
|
20
|
+
return null;
|
|
21
|
+
}
|
|
22
22
|
return new TransientStateError(status, obj, obj.resource, obj.resource_id, obj.current_state);
|
|
23
23
|
}
|
|
24
|
-
|
|
25
24
|
}
|
|
26
25
|
|
|
27
26
|
export { TransientStateError };
|
package/dist/scw/errors/types.js
CHANGED
|
@@ -8,18 +8,15 @@ import { isJSONObject } from '../../helpers/json.js';
|
|
|
8
8
|
*
|
|
9
9
|
* @internal
|
|
10
10
|
*/
|
|
11
|
-
|
|
12
11
|
const isRecordOfStringArray = obj => {
|
|
13
12
|
if (!isJSONObject(obj)) {
|
|
14
13
|
return false;
|
|
15
14
|
}
|
|
16
|
-
|
|
17
15
|
for (const elt of Object.values(obj)) {
|
|
18
16
|
if (!Array.isArray(elt) || Object.values(elt).find(x => typeof x !== 'string') !== undefined) {
|
|
19
17
|
return false;
|
|
20
18
|
}
|
|
21
19
|
}
|
|
22
|
-
|
|
23
20
|
return true;
|
|
24
21
|
};
|
|
25
22
|
|
|
@@ -17,11 +17,9 @@ const buildRequest = (request, settings) => {
|
|
|
17
17
|
let {
|
|
18
18
|
path
|
|
19
19
|
} = request;
|
|
20
|
-
|
|
21
20
|
if (request.urlParams instanceof URLSearchParams) {
|
|
22
21
|
path = path.concat(`?${request.urlParams.toString()}`);
|
|
23
22
|
}
|
|
24
|
-
|
|
25
23
|
return new Request(`${settings.apiURL}${path}`, {
|
|
26
24
|
body: request.body,
|
|
27
25
|
headers: {
|
|
@@ -34,9 +32,7 @@ const buildRequest = (request, settings) => {
|
|
|
34
32
|
method: request.method
|
|
35
33
|
});
|
|
36
34
|
};
|
|
37
|
-
|
|
38
35
|
const asIs = response => response;
|
|
39
|
-
|
|
40
36
|
/**
|
|
41
37
|
* Builds a resource fetcher.
|
|
42
38
|
*
|
|
@@ -48,18 +44,14 @@ const asIs = response => response;
|
|
|
48
44
|
*/
|
|
49
45
|
const buildFetcher = (settings, httpClient) => {
|
|
50
46
|
let requestNumber = 0;
|
|
51
|
-
|
|
52
47
|
const prepareRequest = requestId => composeInterceptors([...settings.requestInterceptors, logRequest(requestId, obfuscateInterceptor(obfuscateAuthHeadersEntry))]);
|
|
53
|
-
|
|
54
48
|
const prepareResponse = requestId => composeInterceptors([...settings.responseInterceptors, logResponse(requestId)]);
|
|
55
|
-
|
|
56
49
|
return async function (request, unwrapper) {
|
|
57
50
|
if (unwrapper === void 0) {
|
|
58
51
|
unwrapper = asIs;
|
|
59
52
|
}
|
|
60
|
-
|
|
61
53
|
const requestId = `${requestNumber += 1}`;
|
|
62
|
-
return Promise.resolve(buildRequest(request, settings)).then(prepareRequest(requestId)).then(httpClient).then(prepareResponse(requestId)).then(responseParser(unwrapper));
|
|
54
|
+
return Promise.resolve(buildRequest(request, settings)).then(prepareRequest(requestId)).then(httpClient).then(prepareResponse(requestId)).then(responseParser(unwrapper, request.responseType ?? 'json'));
|
|
63
55
|
};
|
|
64
56
|
};
|
|
65
57
|
|
|
@@ -7,6 +7,7 @@
|
|
|
7
7
|
* @internal
|
|
8
8
|
*/
|
|
9
9
|
const toPascalCase = str => str.replace(/\w+/g, word => `${word[0].toUpperCase()}${word.slice(1).toLowerCase()}`);
|
|
10
|
+
|
|
10
11
|
/**
|
|
11
12
|
* Converts a Headers entry to string.
|
|
12
13
|
*
|
|
@@ -15,12 +16,11 @@ const toPascalCase = str => str.replace(/\w+/g, word => `${word[0].toUpperCase()
|
|
|
15
16
|
*
|
|
16
17
|
* @internal
|
|
17
18
|
*/
|
|
18
|
-
|
|
19
|
-
|
|
20
19
|
const serializeHeadersEntry = _ref => {
|
|
21
20
|
let [name, value] = _ref;
|
|
22
21
|
return `${toPascalCase(name)}: ${value}`;
|
|
23
22
|
};
|
|
23
|
+
|
|
24
24
|
/**
|
|
25
25
|
* Converts Headers to safe to log strings (with obfuscated auth secrets).
|
|
26
26
|
*
|
|
@@ -29,9 +29,8 @@ const serializeHeadersEntry = _ref => {
|
|
|
29
29
|
*
|
|
30
30
|
* @internal
|
|
31
31
|
*/
|
|
32
|
-
|
|
33
|
-
|
|
34
32
|
const serializeHeaders = headers => Array.from(headers.entries(), serializeHeadersEntry);
|
|
33
|
+
|
|
35
34
|
/**
|
|
36
35
|
* Dumps a Request into a readable string.
|
|
37
36
|
*
|
|
@@ -40,9 +39,8 @@ const serializeHeaders = headers => Array.from(headers.entries(), serializeHeade
|
|
|
40
39
|
*
|
|
41
40
|
* @internal
|
|
42
41
|
*/
|
|
43
|
-
|
|
44
|
-
|
|
45
42
|
const dumpRequest = async request => [`${request.method.toUpperCase()}: ${request.url}`, ...serializeHeaders(request.headers), await request.clone().text()].join('\r\n');
|
|
43
|
+
|
|
46
44
|
/**
|
|
47
45
|
* Dumps a Response into a readable string.
|
|
48
46
|
*
|
|
@@ -51,7 +49,6 @@ const dumpRequest = async request => [`${request.method.toUpperCase()}: ${reques
|
|
|
51
49
|
*
|
|
52
50
|
* @internal
|
|
53
51
|
*/
|
|
54
|
-
|
|
55
52
|
const dumpResponse = async response => [`HTTP ${response.status} ${response.ok ? 'OK' : 'NOK'}`, ...serializeHeaders(response.headers), await response.clone().text()].join('\r\n');
|
|
56
53
|
|
|
57
54
|
export { dumpRequest, dumpResponse };
|
|
@@ -19,16 +19,14 @@ class ObfuscatedRequest extends Request {
|
|
|
19
19
|
this.request = request;
|
|
20
20
|
this.obfuscate = obfuscate;
|
|
21
21
|
}
|
|
22
|
-
|
|
23
22
|
get headers() {
|
|
24
23
|
return new Headers(Array.from(this.request.headers, this.obfuscate));
|
|
25
24
|
}
|
|
26
|
-
|
|
27
25
|
clone() {
|
|
28
26
|
return new ObfuscatedRequest(this.request, this.obfuscate);
|
|
29
27
|
}
|
|
30
|
-
|
|
31
28
|
}
|
|
29
|
+
|
|
32
30
|
/**
|
|
33
31
|
* Creates an interceptor to obfuscate the requests.
|
|
34
32
|
*
|
|
@@ -37,11 +35,9 @@ class ObfuscatedRequest extends Request {
|
|
|
37
35
|
*
|
|
38
36
|
* @internal
|
|
39
37
|
*/
|
|
40
|
-
|
|
41
|
-
|
|
42
38
|
const obfuscateInterceptor = obfuscate => request => new ObfuscatedRequest(request, obfuscate);
|
|
43
|
-
|
|
44
39
|
const identity = instance => instance;
|
|
40
|
+
|
|
45
41
|
/**
|
|
46
42
|
* Creates an interceptor to log the requests.
|
|
47
43
|
*
|
|
@@ -51,20 +47,20 @@ const identity = instance => instance;
|
|
|
51
47
|
*
|
|
52
48
|
* @internal
|
|
53
49
|
*/
|
|
54
|
-
|
|
55
|
-
|
|
56
50
|
const logRequest = function (identifier, obfuscate) {
|
|
57
51
|
if (obfuscate === void 0) {
|
|
58
52
|
obfuscate = identity;
|
|
59
53
|
}
|
|
60
|
-
|
|
61
54
|
return async request => {
|
|
62
|
-
if (shouldLog(LevelResolver[getLogger().logLevel], 'debug'))
|
|
55
|
+
if (shouldLog(LevelResolver[getLogger().logLevel], 'debug')) {
|
|
56
|
+
getLogger().debug(`--------------- Scaleway SDK REQUEST ${identifier} ---------------
|
|
63
57
|
${await dumpRequest(await obfuscate(request))}
|
|
64
58
|
---------------------------------------------------------`);
|
|
59
|
+
}
|
|
65
60
|
return request;
|
|
66
61
|
};
|
|
67
62
|
};
|
|
63
|
+
|
|
68
64
|
/**
|
|
69
65
|
* Creates an interceptor to log the responses.
|
|
70
66
|
*
|
|
@@ -73,11 +69,12 @@ ${await dumpRequest(await obfuscate(request))}
|
|
|
73
69
|
*
|
|
74
70
|
* @internal
|
|
75
71
|
*/
|
|
76
|
-
|
|
77
72
|
const logResponse = identifier => async response => {
|
|
78
|
-
if (shouldLog(LevelResolver[getLogger().logLevel], 'debug'))
|
|
73
|
+
if (shouldLog(LevelResolver[getLogger().logLevel], 'debug')) {
|
|
74
|
+
getLogger().debug(`--------------- Scaleway SDK RESPONSE ${identifier} ---------------
|
|
79
75
|
${await dumpResponse(response)}
|
|
80
76
|
---------------------------------------------------------`);
|
|
77
|
+
}
|
|
81
78
|
return response;
|
|
82
79
|
};
|
|
83
80
|
|
|
@@ -1,15 +1,14 @@
|
|
|
1
1
|
const extract = key => result => result[key];
|
|
2
|
-
|
|
3
2
|
function* pages(key, fetcher, request, firstPage) {
|
|
4
|
-
if (!Array.isArray(firstPage[key]))
|
|
3
|
+
if (!Array.isArray(firstPage[key])) {
|
|
4
|
+
throw new Error(`Property ${key} is not a list in paginated result`);
|
|
5
|
+
}
|
|
5
6
|
const getList = extract(key);
|
|
6
7
|
let page = request.page || 1;
|
|
7
|
-
|
|
8
8
|
if (page === 1) {
|
|
9
9
|
yield Promise.resolve(getList(firstPage));
|
|
10
10
|
page += 1;
|
|
11
11
|
}
|
|
12
|
-
|
|
13
12
|
const {
|
|
14
13
|
length
|
|
15
14
|
} = firstPage[key];
|
|
@@ -17,14 +16,15 @@ function* pages(key, fetcher, request, firstPage) {
|
|
|
17
16
|
const {
|
|
18
17
|
totalCount
|
|
19
18
|
} = firstPage;
|
|
20
|
-
|
|
21
19
|
while (page <= Math.floor((totalCount + length - 1) / length)) {
|
|
22
|
-
yield fetcher({
|
|
20
|
+
yield fetcher({
|
|
21
|
+
...request,
|
|
23
22
|
page
|
|
24
23
|
}).then(getList);
|
|
25
24
|
page += 1;
|
|
26
25
|
}
|
|
27
26
|
}
|
|
27
|
+
|
|
28
28
|
/**
|
|
29
29
|
* Fetches a paginated resource.
|
|
30
30
|
*
|
|
@@ -34,15 +34,19 @@ function* pages(key, fetcher, request, firstPage) {
|
|
|
34
34
|
* @param initial - The first page
|
|
35
35
|
* @returns An async generator of resources arrays
|
|
36
36
|
*/
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
37
|
+
function fetchPaginated(key, fetcher, request, initial) {
|
|
38
|
+
try {
|
|
39
|
+
if (initial === void 0) {
|
|
40
|
+
initial = fetcher(request);
|
|
41
|
+
}
|
|
42
|
+
return async function* () {
|
|
43
|
+
yield* pages(key, fetcher, request, await initial);
|
|
44
|
+
}();
|
|
45
|
+
} catch (e) {
|
|
46
|
+
return Promise.reject(e);
|
|
42
47
|
}
|
|
43
|
-
|
|
44
|
-
yield* pages(key, fetcher, request, await initial);
|
|
45
48
|
}
|
|
49
|
+
|
|
46
50
|
/**
|
|
47
51
|
* Fetches all paginated resource.
|
|
48
52
|
*
|
|
@@ -52,14 +56,13 @@ async function* fetchPaginated(key, fetcher, request, initial) {
|
|
|
52
56
|
* @param initial - The first page
|
|
53
57
|
* @returns A resources array Promise
|
|
54
58
|
*/
|
|
55
|
-
|
|
56
59
|
const fetchAll = async function (key, fetcher, request, initial) {
|
|
57
60
|
if (initial === void 0) {
|
|
58
61
|
initial = fetcher(request);
|
|
59
62
|
}
|
|
60
|
-
|
|
61
63
|
return (await Promise.all(Array.from(pages(key, fetcher, request, await initial)))).flat();
|
|
62
64
|
};
|
|
65
|
+
|
|
63
66
|
/**
|
|
64
67
|
* Enriches a listing method with helpers.
|
|
65
68
|
*
|
|
@@ -68,7 +71,6 @@ const fetchAll = async function (key, fetcher, request, initial) {
|
|
|
68
71
|
* @param request - A request with pagination options
|
|
69
72
|
* @returns A resource Promise with the pagination helpers
|
|
70
73
|
*/
|
|
71
|
-
|
|
72
74
|
const enrichForPagination = (key, fetcher, request) => {
|
|
73
75
|
const firstPage = fetcher(request);
|
|
74
76
|
return Object.assign(firstPage, {
|
|
@@ -4,33 +4,29 @@ import { ScalewayError } from '../errors/scw-error.js';
|
|
|
4
4
|
|
|
5
5
|
const X_TOTAL_COUNT_HEADER_KEY = 'x-total-count';
|
|
6
6
|
const TOTAL_COUNT_RES_KEY = 'total_count';
|
|
7
|
+
|
|
7
8
|
/**
|
|
8
9
|
* Fixes the totalCount property for old APIs.
|
|
9
10
|
*
|
|
10
11
|
* @internal
|
|
11
12
|
*/
|
|
12
|
-
|
|
13
13
|
const fixLegacyTotalCount = (obj, headers) => {
|
|
14
14
|
const headerVal = headers.get(X_TOTAL_COUNT_HEADER_KEY);
|
|
15
|
-
|
|
16
15
|
if (!headerVal) {
|
|
17
16
|
return obj;
|
|
18
17
|
}
|
|
19
|
-
|
|
20
18
|
const totalCount = parseInt(headerVal, 10);
|
|
21
|
-
|
|
22
|
-
if (Number.isNaN(totalCount) === true) {
|
|
19
|
+
if (Number.isNaN(totalCount)) {
|
|
23
20
|
return obj;
|
|
24
21
|
}
|
|
25
|
-
|
|
26
22
|
if (isJSONObject(obj) && !(TOTAL_COUNT_RES_KEY in obj)) {
|
|
27
23
|
return Object.assign(obj, {
|
|
28
24
|
[TOTAL_COUNT_RES_KEY]: totalCount
|
|
29
25
|
});
|
|
30
26
|
}
|
|
31
|
-
|
|
32
27
|
return obj;
|
|
33
28
|
};
|
|
29
|
+
|
|
34
30
|
/**
|
|
35
31
|
* Makes response parser.
|
|
36
32
|
*
|
|
@@ -48,27 +44,25 @@ const fixLegacyTotalCount = (obj, headers) => {
|
|
|
48
44
|
*
|
|
49
45
|
* @internal
|
|
50
46
|
*/
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
47
|
+
const responseParser = (unmarshaller, responseType) => async response => {
|
|
48
|
+
if (!(response instanceof Response)) {
|
|
49
|
+
throw new TypeError('Invalid response object');
|
|
50
|
+
}
|
|
55
51
|
if (response.ok) {
|
|
56
52
|
if (response.status === 204) return unmarshaller(undefined);
|
|
57
53
|
const contentType = response.headers.get('Content-Type');
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
throw new ScalewayError(response.status, `invalid content type ${contentType ?? ''}`.trim());
|
|
54
|
+
try {
|
|
55
|
+
if (responseType === 'json' && contentType === 'application/json') {
|
|
56
|
+
return unmarshaller(fixLegacyTotalCount(await response.json(), response.headers));
|
|
57
|
+
}
|
|
58
|
+
if (responseType === 'blob') {
|
|
59
|
+
return unmarshaller(await response.blob());
|
|
60
|
+
}
|
|
61
|
+
return unmarshaller(await response.text());
|
|
62
|
+
} catch (err) {
|
|
63
|
+
throw new ScalewayError(response.status, `could not parse '${contentType ?? ''}' response${err instanceof Error ? `: ${err.message}` : ''}`);
|
|
69
64
|
}
|
|
70
65
|
}
|
|
71
|
-
|
|
72
66
|
const error = await response.clone().json().catch(() => response.text());
|
|
73
67
|
if (isJSONObject(error)) throw parseScalewayError(response.status, error);
|
|
74
68
|
throw new ScalewayError(response.status, typeof error === 'string' ? error : 'cannot read error response body');
|