@scaleway/sdk-client 1.1.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.
Files changed (149) hide show
  1. package/LICENSE +191 -0
  2. package/README.md +36 -0
  3. package/dist/bridge.d.ts +12 -0
  4. package/dist/helpers/is-browser.cjs +4 -0
  5. package/dist/helpers/is-browser.d.ts +1 -0
  6. package/dist/helpers/is-browser.js +4 -0
  7. package/dist/helpers/is-response.cjs +4 -0
  8. package/dist/helpers/is-response.d.ts +8 -0
  9. package/dist/helpers/is-response.js +4 -0
  10. package/dist/helpers/json.cjs +7 -0
  11. package/dist/helpers/json.d.ts +13 -0
  12. package/dist/helpers/json.js +7 -0
  13. package/dist/helpers/marshalling.cjs +76 -0
  14. package/dist/helpers/marshalling.d.ts +60 -0
  15. package/dist/helpers/marshalling.js +76 -0
  16. package/dist/index.cjs +52 -0
  17. package/dist/index.d.ts +15 -0
  18. package/dist/index.js +52 -0
  19. package/dist/internal/async/interval-retrier.cjs +53 -0
  20. package/dist/internal/async/interval-retrier.d.ts +111 -0
  21. package/dist/internal/async/interval-retrier.js +53 -0
  22. package/dist/internal/async/sleep.cjs +6 -0
  23. package/dist/internal/async/sleep.d.ts +9 -0
  24. package/dist/internal/async/sleep.js +6 -0
  25. package/dist/internal/interceptors/composer.cjs +25 -0
  26. package/dist/internal/interceptors/composer.d.ts +25 -0
  27. package/dist/internal/interceptors/composer.js +25 -0
  28. package/dist/internal/interceptors/helpers.cjs +12 -0
  29. package/dist/internal/interceptors/helpers.d.ts +21 -0
  30. package/dist/internal/interceptors/helpers.js +12 -0
  31. package/dist/internal/interceptors/types.d.ts +89 -0
  32. package/dist/internal/logger/console-logger.cjs +24 -0
  33. package/dist/internal/logger/console-logger.d.ts +23 -0
  34. package/dist/internal/logger/console-logger.js +24 -0
  35. package/dist/internal/logger/index.cjs +12 -0
  36. package/dist/internal/logger/index.d.ts +25 -0
  37. package/dist/internal/logger/index.js +12 -0
  38. package/dist/internal/logger/level-resolver.cjs +13 -0
  39. package/dist/internal/logger/level-resolver.d.ts +9 -0
  40. package/dist/internal/logger/level-resolver.js +13 -0
  41. package/dist/internal/logger/logger.d.ts +13 -0
  42. package/dist/internal/validations/string-validation.cjs +30 -0
  43. package/dist/internal/validations/string-validation.d.ts +18 -0
  44. package/dist/internal/validations/string-validation.js +30 -0
  45. package/dist/internals.d.ts +10 -0
  46. package/dist/package.json.cjs +67 -0
  47. package/dist/package.json.js +67 -0
  48. package/dist/scw/api.cjs +8 -0
  49. package/dist/scw/api.d.ts +10 -0
  50. package/dist/scw/api.js +8 -0
  51. package/dist/scw/auth.cjs +26 -0
  52. package/dist/scw/auth.d.ts +57 -0
  53. package/dist/scw/auth.js +26 -0
  54. package/dist/scw/client-ini-factory.cjs +66 -0
  55. package/dist/scw/client-ini-factory.d.ts +111 -0
  56. package/dist/scw/client-ini-factory.js +66 -0
  57. package/dist/scw/client-ini-profile.cjs +23 -0
  58. package/dist/scw/client-ini-profile.d.ts +82 -0
  59. package/dist/scw/client-ini-profile.js +23 -0
  60. package/dist/scw/client-settings.cjs +51 -0
  61. package/dist/scw/client-settings.d.ts +59 -0
  62. package/dist/scw/client-settings.js +51 -0
  63. package/dist/scw/client.cjs +31 -0
  64. package/dist/scw/client.d.ts +71 -0
  65. package/dist/scw/client.js +31 -0
  66. package/dist/scw/constants.cjs +7 -0
  67. package/dist/scw/constants.d.ts +2 -0
  68. package/dist/scw/constants.js +7 -0
  69. package/dist/scw/custom-marshalling.cjs +126 -0
  70. package/dist/scw/custom-marshalling.d.ts +80 -0
  71. package/dist/scw/custom-marshalling.js +126 -0
  72. package/dist/scw/custom-types.cjs +11 -0
  73. package/dist/scw/custom-types.d.ts +74 -0
  74. package/dist/scw/custom-types.js +11 -0
  75. package/dist/scw/errors/error-parser.cjs +76 -0
  76. package/dist/scw/errors/error-parser.d.ts +12 -0
  77. package/dist/scw/errors/error-parser.js +76 -0
  78. package/dist/scw/errors/non-standard/invalid-request-mapper.cjs +36 -0
  79. package/dist/scw/errors/non-standard/invalid-request-mapper.d.ts +12 -0
  80. package/dist/scw/errors/non-standard/invalid-request-mapper.js +36 -0
  81. package/dist/scw/errors/non-standard/unknown-resource-mapper.cjs +23 -0
  82. package/dist/scw/errors/non-standard/unknown-resource-mapper.d.ts +11 -0
  83. package/dist/scw/errors/non-standard/unknown-resource-mapper.js +23 -0
  84. package/dist/scw/errors/scw-error-from-json.d.ts +10 -0
  85. package/dist/scw/errors/scw-error.cjs +43 -0
  86. package/dist/scw/errors/scw-error.d.ts +25 -0
  87. package/dist/scw/errors/scw-error.js +43 -0
  88. package/dist/scw/errors/standard/already-exists-error.cjs +31 -0
  89. package/dist/scw/errors/standard/already-exists-error.d.ts +16 -0
  90. package/dist/scw/errors/standard/already-exists-error.js +31 -0
  91. package/dist/scw/errors/standard/denied-authentication-error.cjs +37 -0
  92. package/dist/scw/errors/standard/denied-authentication-error.d.ts +15 -0
  93. package/dist/scw/errors/standard/denied-authentication-error.js +37 -0
  94. package/dist/scw/errors/standard/index.cjs +28 -0
  95. package/dist/scw/errors/standard/index.d.ts +17 -0
  96. package/dist/scw/errors/standard/index.js +28 -0
  97. package/dist/scw/errors/standard/invalid-arguments-error.cjs +54 -0
  98. package/dist/scw/errors/standard/invalid-arguments-error.d.ts +24 -0
  99. package/dist/scw/errors/standard/invalid-arguments-error.js +54 -0
  100. package/dist/scw/errors/standard/out-of-stock-error.cjs +17 -0
  101. package/dist/scw/errors/standard/out-of-stock-error.d.ts +14 -0
  102. package/dist/scw/errors/standard/out-of-stock-error.js +17 -0
  103. package/dist/scw/errors/standard/permissions-denied-error.cjs +29 -0
  104. package/dist/scw/errors/standard/permissions-denied-error.d.ts +23 -0
  105. package/dist/scw/errors/standard/permissions-denied-error.js +29 -0
  106. package/dist/scw/errors/standard/precondition-failed-error.cjs +32 -0
  107. package/dist/scw/errors/standard/precondition-failed-error.d.ts +15 -0
  108. package/dist/scw/errors/standard/precondition-failed-error.js +32 -0
  109. package/dist/scw/errors/standard/quotas-exceeded-error.cjs +43 -0
  110. package/dist/scw/errors/standard/quotas-exceeded-error.d.ts +34 -0
  111. package/dist/scw/errors/standard/quotas-exceeded-error.js +43 -0
  112. package/dist/scw/errors/standard/resource-expired-error.cjs +31 -0
  113. package/dist/scw/errors/standard/resource-expired-error.d.ts +16 -0
  114. package/dist/scw/errors/standard/resource-expired-error.js +31 -0
  115. package/dist/scw/errors/standard/resource-locked-error.cjs +20 -0
  116. package/dist/scw/errors/standard/resource-locked-error.d.ts +15 -0
  117. package/dist/scw/errors/standard/resource-locked-error.js +20 -0
  118. package/dist/scw/errors/standard/resource-not-found-error.cjs +24 -0
  119. package/dist/scw/errors/standard/resource-not-found-error.d.ts +15 -0
  120. package/dist/scw/errors/standard/resource-not-found-error.js +24 -0
  121. package/dist/scw/errors/standard/too-many-requests-error.cjs +58 -0
  122. package/dist/scw/errors/standard/too-many-requests-error.d.ts +32 -0
  123. package/dist/scw/errors/standard/too-many-requests-error.js +58 -0
  124. package/dist/scw/errors/standard/transient-state-error.cjs +31 -0
  125. package/dist/scw/errors/standard/transient-state-error.d.ts +16 -0
  126. package/dist/scw/errors/standard/transient-state-error.js +31 -0
  127. package/dist/scw/errors/types.cjs +15 -0
  128. package/dist/scw/errors/types.d.ts +9 -0
  129. package/dist/scw/errors/types.js +15 -0
  130. package/dist/scw/fetch/build-fetcher.cjs +60 -0
  131. package/dist/scw/fetch/build-fetcher.d.ts +23 -0
  132. package/dist/scw/fetch/build-fetcher.js +60 -0
  133. package/dist/scw/fetch/http-dumper.cjs +20 -0
  134. package/dist/scw/fetch/http-dumper.d.ts +18 -0
  135. package/dist/scw/fetch/http-dumper.js +20 -0
  136. package/dist/scw/fetch/http-interceptors.cjs +43 -0
  137. package/dist/scw/fetch/http-interceptors.d.ts +38 -0
  138. package/dist/scw/fetch/http-interceptors.js +43 -0
  139. package/dist/scw/fetch/resource-paginator.cjs +36 -0
  140. package/dist/scw/fetch/resource-paginator.d.ts +47 -0
  141. package/dist/scw/fetch/resource-paginator.js +36 -0
  142. package/dist/scw/fetch/response-parser.cjs +55 -0
  143. package/dist/scw/fetch/response-parser.d.ts +25 -0
  144. package/dist/scw/fetch/response-parser.js +55 -0
  145. package/dist/scw/fetch/types.d.ts +16 -0
  146. package/dist/scw/locality.d.ts +2 -0
  147. package/dist/vendor/base64/index.cjs +42 -0
  148. package/dist/vendor/base64/index.js +42 -0
  149. package/package.json +37 -0
@@ -0,0 +1,126 @@
1
+ import { isJSONObject } from "../helpers/json.js";
2
+ import { unmarshalDate, unmarshalArrayOfObject } from "../helpers/marshalling.js";
3
+ import { fromByteArray } from "../vendor/base64/index.js";
4
+ import { Decimal } from "./custom-types.js";
5
+ const unmarshalMoney = (data) => {
6
+ if (!isJSONObject(data)) {
7
+ throw new TypeError(
8
+ `Unmarshalling the type 'Money' failed as data isn't a dictionary.`
9
+ );
10
+ }
11
+ return {
12
+ currencyCode: data.currency_code,
13
+ nanos: data.nanos,
14
+ units: data.units
15
+ };
16
+ };
17
+ const unmarshalServiceInfo = (data) => {
18
+ if (!isJSONObject(data)) {
19
+ throw new TypeError(
20
+ `Unmarshalling the type 'ServiceInfo' failed as data isn't a dictionary.`
21
+ );
22
+ }
23
+ return {
24
+ description: data.description,
25
+ documentationUrl: data.documentation_url,
26
+ name: data.name,
27
+ version: data.version
28
+ };
29
+ };
30
+ const unmarshalScwFile = (data) => {
31
+ if (!isJSONObject(data)) {
32
+ throw new TypeError(
33
+ `Unmarshalling the type 'ScwFile' failed as data isn't a dictionary.`
34
+ );
35
+ }
36
+ return {
37
+ content: data.content,
38
+ contentType: data.content_type,
39
+ name: data.name
40
+ };
41
+ };
42
+ const unmarshalTimeSeriesPoint = (data) => {
43
+ if (!Array.isArray(data)) {
44
+ throw new TypeError(
45
+ `Unmarshalling the type 'TimeSeriesPoint' failed as data isn't an array.`
46
+ );
47
+ }
48
+ return {
49
+ timestamp: unmarshalDate(data[0]),
50
+ value: data[1]
51
+ };
52
+ };
53
+ const unmarshalTimeSeries = (data) => {
54
+ if (!isJSONObject(data)) {
55
+ throw new TypeError(
56
+ `Unmarshalling the type 'TimeSeries' failed as data isn't a dictionary.`
57
+ );
58
+ }
59
+ return {
60
+ metadata: data.metadata,
61
+ name: data.name,
62
+ points: unmarshalArrayOfObject(data.points, unmarshalTimeSeriesPoint)
63
+ };
64
+ };
65
+ const unmarshalDecimal = (data) => {
66
+ if (!(typeof data === "object")) {
67
+ throw new TypeError(
68
+ `Unmarshalling the type 'Decimal' failed as data isn't an object.`
69
+ );
70
+ }
71
+ if (data === null) {
72
+ return null;
73
+ }
74
+ if (!("value" in data)) {
75
+ throw new TypeError(
76
+ `Unmarshalling the type 'Decimal' failed as data object does not have a 'value' key.`
77
+ );
78
+ }
79
+ if (!(typeof data.value === "string")) {
80
+ throw new TypeError(
81
+ `Unmarshalling the type 'Decimal' failed as 'value' is not a string.`
82
+ );
83
+ }
84
+ return new Decimal(data.value);
85
+ };
86
+ const marshalScwFile = (obj) => ({
87
+ content: obj.content,
88
+ content_type: obj.contentType,
89
+ name: obj.name
90
+ });
91
+ const marshalBlobToScwFile = async (blob) => ({
92
+ content: fromByteArray(new Uint8Array(await blob.arrayBuffer())),
93
+ content_type: blob.type,
94
+ name: "file"
95
+ });
96
+ const marshalMoney = (obj) => ({
97
+ currency_code: obj.currencyCode,
98
+ nanos: obj.nanos,
99
+ units: obj.units
100
+ });
101
+ const marshalTimeSeriesPoint = (obj) => ({
102
+ timestamp: obj.timestamp?.toISOString(),
103
+ value: obj.value
104
+ });
105
+ const marshalTimeSeries = (obj) => ({
106
+ metadata: obj.metadata,
107
+ name: obj.name,
108
+ points: obj.points.map((elt) => marshalTimeSeriesPoint(elt))
109
+ });
110
+ const marshalDecimal = (obj) => ({
111
+ value: obj.toString()
112
+ });
113
+ export {
114
+ marshalBlobToScwFile,
115
+ marshalDecimal,
116
+ marshalMoney,
117
+ marshalScwFile,
118
+ marshalTimeSeries,
119
+ marshalTimeSeriesPoint,
120
+ unmarshalDecimal,
121
+ unmarshalMoney,
122
+ unmarshalScwFile,
123
+ unmarshalServiceInfo,
124
+ unmarshalTimeSeries,
125
+ unmarshalTimeSeriesPoint
126
+ };
@@ -0,0 +1,11 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
3
+ class Decimal {
4
+ str;
5
+ constructor(v) {
6
+ this.str = v;
7
+ }
8
+ toString = () => this.str;
9
+ marshal = () => ({ value: this.str });
10
+ }
11
+ exports.Decimal = Decimal;
@@ -0,0 +1,74 @@
1
+ /**
2
+ * Represents API metadata.
3
+ *
4
+ * @remarks These metadata are only here for debugging. Do not rely on these values.
5
+ *
6
+ * @internal
7
+ */
8
+ export interface ServiceInfo {
9
+ /** Name of the API */
10
+ name: string;
11
+ /** Human readable description for the API. */
12
+ description: string;
13
+ /** Version of the API. */
14
+ version: string;
15
+ /** Web url where the documentation of the API can be found. */
16
+ documentationUrl?: string;
17
+ }
18
+ /**
19
+ * Represents an amount of money with its currency type.
20
+ */
21
+ export interface Money {
22
+ /** 3-letter currency code defined in ISO 4217. */
23
+ currencyCode: string;
24
+ /**
25
+ * Whole units of the amount.
26
+ *
27
+ * For example if `currencyCode` is `"USD"`, then 1 unit is one US dollar.
28
+ */
29
+ units: number;
30
+ /**
31
+ * Number of nano (10^-9) units of the amount.
32
+ *
33
+ * The value must be between -999,999,999 and +999,999,999 inclusive.
34
+ * If `units` is positive, `nanos` must be positive or zero.
35
+ * If `units` is zero, `nanos` can be positive, zero, or negative.
36
+ * If `units` is negative, `nanos` must be negative or zero.
37
+ * For example $-1.75 is represented as `units`=-1 and `nanos`=-750,000,000.
38
+ */
39
+ nanos: number;
40
+ }
41
+ /** Represents a point in a TimeSeries. */
42
+ export interface TimeSeriesPoint {
43
+ /** Date of the point. */
44
+ timestamp?: Date;
45
+ /** Value of the point. */
46
+ value: number;
47
+ }
48
+ /** Represents a time series that could be used for graph purposes. */
49
+ export interface TimeSeries {
50
+ /** Name of the metric. */
51
+ name: string;
52
+ /** Points contains all the points that composed the series. */
53
+ points: TimeSeriesPoint[];
54
+ /** Metadata contains some string metadata related to a metric. */
55
+ metadata: Record<string, string>;
56
+ }
57
+ /** Represents a Scaleway file. */
58
+ export interface ScwFile {
59
+ /** Name of the file. */
60
+ name: string;
61
+ /** Content-type of the file. */
62
+ contentType: string;
63
+ /** Content of the file in base64. */
64
+ content: string;
65
+ }
66
+ /** A representation of a decimal value, such as 2.5.
67
+ * Comparable to language-native decimal formats, such as Java's BigDecimal or Python's decimal.Decimal.
68
+ * Lookup protobuf google.type.Decimal for details */
69
+ export declare class Decimal {
70
+ private readonly str;
71
+ constructor(v: string);
72
+ toString: () => string;
73
+ marshal: () => object;
74
+ }
@@ -0,0 +1,11 @@
1
+ class Decimal {
2
+ str;
3
+ constructor(v) {
4
+ this.str = v;
5
+ }
6
+ toString = () => this.str;
7
+ marshal = () => ({ value: this.str });
8
+ }
9
+ export {
10
+ Decimal
11
+ };
@@ -0,0 +1,76 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
3
+ const invalidRequestMapper = require("./non-standard/invalid-request-mapper.cjs");
4
+ const unknownResourceMapper = require("./non-standard/unknown-resource-mapper.cjs");
5
+ const scwError = require("./scw-error.cjs");
6
+ const alreadyExistsError = require("./standard/already-exists-error.cjs");
7
+ const deniedAuthenticationError = require("./standard/denied-authentication-error.cjs");
8
+ const invalidArgumentsError = require("./standard/invalid-arguments-error.cjs");
9
+ const outOfStockError = require("./standard/out-of-stock-error.cjs");
10
+ const permissionsDeniedError = require("./standard/permissions-denied-error.cjs");
11
+ const preconditionFailedError = require("./standard/precondition-failed-error.cjs");
12
+ const quotasExceededError = require("./standard/quotas-exceeded-error.cjs");
13
+ const resourceExpiredError = require("./standard/resource-expired-error.cjs");
14
+ const resourceLockedError = require("./standard/resource-locked-error.cjs");
15
+ const resourceNotFoundError = require("./standard/resource-not-found-error.cjs");
16
+ const tooManyRequestsError = require("./standard/too-many-requests-error.cjs");
17
+ const transientStateError = require("./standard/transient-state-error.cjs");
18
+ const unmarshalStandardError = (type, status, body) => {
19
+ let error;
20
+ switch (type) {
21
+ case "denied_authentication":
22
+ error = deniedAuthenticationError.DeniedAuthenticationError;
23
+ break;
24
+ case "invalid_arguments":
25
+ error = invalidArgumentsError.InvalidArgumentsError;
26
+ break;
27
+ case "out_of_stock":
28
+ error = outOfStockError.OutOfStockError;
29
+ break;
30
+ case "permissions_denied":
31
+ error = permissionsDeniedError.PermissionsDeniedError;
32
+ break;
33
+ case "precondition_failed":
34
+ error = preconditionFailedError.PreconditionFailedError;
35
+ break;
36
+ case "quotas_exceeded":
37
+ error = quotasExceededError.QuotasExceededError;
38
+ break;
39
+ case "expired":
40
+ error = resourceExpiredError.ResourceExpiredError;
41
+ break;
42
+ case "not_found":
43
+ error = resourceNotFoundError.ResourceNotFoundError;
44
+ break;
45
+ case "locked":
46
+ error = resourceLockedError.ResourceLockedError;
47
+ break;
48
+ case "transient_state":
49
+ error = transientStateError.TransientStateError;
50
+ break;
51
+ case "already_exists":
52
+ error = alreadyExistsError.AlreadyExistsError;
53
+ break;
54
+ case "too_many_requests":
55
+ error = tooManyRequestsError.TooManyRequestsError;
56
+ break;
57
+ default:
58
+ return null;
59
+ }
60
+ return error.fromJSON(status, body);
61
+ };
62
+ const unmarshalNonStandardError = (type, status, body) => {
63
+ switch (type) {
64
+ case "unknown_resource":
65
+ return unknownResourceMapper.UnknownResourceMapper.fromJSON(status, body);
66
+ case "invalid_request_error":
67
+ return invalidRequestMapper.InvalidRequestMapper.fromJSON(status, body);
68
+ default:
69
+ return null;
70
+ }
71
+ };
72
+ const parseScalewayError = (status, body) => {
73
+ const parsableError = typeof body.type === "string" && (unmarshalStandardError(body.type, status, body) ?? unmarshalNonStandardError(body.type, status, body));
74
+ return parsableError || new scwError.ScalewayError(status, body);
75
+ };
76
+ exports.parseScalewayError = parseScalewayError;
@@ -0,0 +1,12 @@
1
+ import type { JSONObject } from '../../helpers/json.js';
2
+ import { ScalewayError } from './scw-error.js';
3
+ /**
4
+ * Parses Scaleway error from raw body.
5
+ *
6
+ * @param status - The status code
7
+ * @param body - The error response
8
+ * @returns The resolved error
9
+ *
10
+ * @internal
11
+ */
12
+ export declare const parseScalewayError: (status: number, body: Readonly<JSONObject>) => ScalewayError;
@@ -0,0 +1,76 @@
1
+ import { InvalidRequestMapper } from "./non-standard/invalid-request-mapper.js";
2
+ import { UnknownResourceMapper } from "./non-standard/unknown-resource-mapper.js";
3
+ import { ScalewayError } from "./scw-error.js";
4
+ import { AlreadyExistsError } from "./standard/already-exists-error.js";
5
+ import { DeniedAuthenticationError } from "./standard/denied-authentication-error.js";
6
+ import { InvalidArgumentsError } from "./standard/invalid-arguments-error.js";
7
+ import { OutOfStockError } from "./standard/out-of-stock-error.js";
8
+ import { PermissionsDeniedError } from "./standard/permissions-denied-error.js";
9
+ import { PreconditionFailedError } from "./standard/precondition-failed-error.js";
10
+ import { QuotasExceededError } from "./standard/quotas-exceeded-error.js";
11
+ import { ResourceExpiredError } from "./standard/resource-expired-error.js";
12
+ import { ResourceLockedError } from "./standard/resource-locked-error.js";
13
+ import { ResourceNotFoundError } from "./standard/resource-not-found-error.js";
14
+ import { TooManyRequestsError } from "./standard/too-many-requests-error.js";
15
+ import { TransientStateError } from "./standard/transient-state-error.js";
16
+ const unmarshalStandardError = (type, status, body) => {
17
+ let error;
18
+ switch (type) {
19
+ case "denied_authentication":
20
+ error = DeniedAuthenticationError;
21
+ break;
22
+ case "invalid_arguments":
23
+ error = InvalidArgumentsError;
24
+ break;
25
+ case "out_of_stock":
26
+ error = OutOfStockError;
27
+ break;
28
+ case "permissions_denied":
29
+ error = PermissionsDeniedError;
30
+ break;
31
+ case "precondition_failed":
32
+ error = PreconditionFailedError;
33
+ break;
34
+ case "quotas_exceeded":
35
+ error = QuotasExceededError;
36
+ break;
37
+ case "expired":
38
+ error = ResourceExpiredError;
39
+ break;
40
+ case "not_found":
41
+ error = ResourceNotFoundError;
42
+ break;
43
+ case "locked":
44
+ error = ResourceLockedError;
45
+ break;
46
+ case "transient_state":
47
+ error = TransientStateError;
48
+ break;
49
+ case "already_exists":
50
+ error = AlreadyExistsError;
51
+ break;
52
+ case "too_many_requests":
53
+ error = TooManyRequestsError;
54
+ break;
55
+ default:
56
+ return null;
57
+ }
58
+ return error.fromJSON(status, body);
59
+ };
60
+ const unmarshalNonStandardError = (type, status, body) => {
61
+ switch (type) {
62
+ case "unknown_resource":
63
+ return UnknownResourceMapper.fromJSON(status, body);
64
+ case "invalid_request_error":
65
+ return InvalidRequestMapper.fromJSON(status, body);
66
+ default:
67
+ return null;
68
+ }
69
+ };
70
+ const parseScalewayError = (status, body) => {
71
+ const parsableError = typeof body.type === "string" && (unmarshalStandardError(body.type, status, body) ?? unmarshalNonStandardError(body.type, status, body));
72
+ return parsableError || new ScalewayError(status, body);
73
+ };
74
+ export {
75
+ parseScalewayError
76
+ };
@@ -0,0 +1,36 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
3
+ const scwError = require("../scw-error.cjs");
4
+ const invalidArgumentsError = require("../standard/invalid-arguments-error.cjs");
5
+ const quotasExceededError = require("../standard/quotas-exceeded-error.cjs");
6
+ const types = require("../types.cjs");
7
+ class InvalidRequestMapper {
8
+ static fromJSON(status, obj) {
9
+ if (typeof obj.message === "string" && obj.message.toLowerCase().includes("quota exceeded for this resource")) {
10
+ return new quotasExceededError.QuotasExceededError(status, obj, [
11
+ {
12
+ current: 0,
13
+ quota: 0,
14
+ resource: typeof obj.resource === "string" ? obj.resource : ""
15
+ }
16
+ ]);
17
+ }
18
+ const fields = obj.fields && types.isRecordOfStringArray(obj.fields) ? obj.fields : {};
19
+ const fieldsMessages = Object.entries(fields);
20
+ if (fieldsMessages.length) {
21
+ return new invalidArgumentsError.InvalidArgumentsError(
22
+ status,
23
+ obj,
24
+ fieldsMessages.map(
25
+ ([argumentName, messages]) => messages.map((helpMessage) => ({
26
+ argumentName,
27
+ helpMessage,
28
+ reason: "constraint"
29
+ }))
30
+ ).flat()
31
+ );
32
+ }
33
+ return new scwError.ScalewayError(status, obj);
34
+ }
35
+ }
36
+ exports.InvalidRequestMapper = InvalidRequestMapper;
@@ -0,0 +1,12 @@
1
+ import type { JSONObject } from '../../../helpers/json.js';
2
+ import { ScalewayError } from '../scw-error.js';
3
+ import { InvalidArgumentsError } from '../standard/invalid-arguments-error.js';
4
+ import { QuotasExceededError } from '../standard/quotas-exceeded-error.js';
5
+ /**
6
+ * InvalidRequest error is only returned by the instance API.
7
+ *
8
+ * @public
9
+ */
10
+ export declare class InvalidRequestMapper {
11
+ static fromJSON(status: number, obj: Readonly<JSONObject>): QuotasExceededError | InvalidArgumentsError | ScalewayError;
12
+ }
@@ -0,0 +1,36 @@
1
+ import { ScalewayError } from "../scw-error.js";
2
+ import { InvalidArgumentsError } from "../standard/invalid-arguments-error.js";
3
+ import { QuotasExceededError } from "../standard/quotas-exceeded-error.js";
4
+ import { isRecordOfStringArray } from "../types.js";
5
+ class InvalidRequestMapper {
6
+ static fromJSON(status, obj) {
7
+ if (typeof obj.message === "string" && obj.message.toLowerCase().includes("quota exceeded for this resource")) {
8
+ return new QuotasExceededError(status, obj, [
9
+ {
10
+ current: 0,
11
+ quota: 0,
12
+ resource: typeof obj.resource === "string" ? obj.resource : ""
13
+ }
14
+ ]);
15
+ }
16
+ const fields = obj.fields && isRecordOfStringArray(obj.fields) ? obj.fields : {};
17
+ const fieldsMessages = Object.entries(fields);
18
+ if (fieldsMessages.length) {
19
+ return new InvalidArgumentsError(
20
+ status,
21
+ obj,
22
+ fieldsMessages.map(
23
+ ([argumentName, messages]) => messages.map((helpMessage) => ({
24
+ argumentName,
25
+ helpMessage,
26
+ reason: "constraint"
27
+ }))
28
+ ).flat()
29
+ );
30
+ }
31
+ return new ScalewayError(status, obj);
32
+ }
33
+ }
34
+ export {
35
+ InvalidRequestMapper
36
+ };
@@ -0,0 +1,23 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
3
+ const stringValidation = require("../../../internal/validations/string-validation.cjs");
4
+ const scwError = require("../scw-error.cjs");
5
+ const resourceNotFoundError = require("../standard/resource-not-found-error.cjs");
6
+ class UnknownResourceMapper {
7
+ static fromJSON(status, obj) {
8
+ const messageParts = typeof obj.message === "string" ? obj.message.split(/"|'/) : [];
9
+ if (messageParts.length === 3 && stringValidation.isUUID(messageParts[1])) {
10
+ return new resourceNotFoundError.ResourceNotFoundError(
11
+ status,
12
+ obj,
13
+ // transform `Security group ` to `security_group`
14
+ // `.replaceAll()` may be too recent to use yet.
15
+ // that's why we're using `.split(' ').join('_')` for now.
16
+ messageParts[0].trim().toLowerCase().split(" ").join("_"),
17
+ messageParts[1]
18
+ );
19
+ }
20
+ return new scwError.ScalewayError(status, obj);
21
+ }
22
+ }
23
+ exports.UnknownResourceMapper = UnknownResourceMapper;
@@ -0,0 +1,11 @@
1
+ import type { JSONObject } from '../../../helpers/json.js';
2
+ import { ScalewayError } from '../scw-error.js';
3
+ import { ResourceNotFoundError } from '../standard/resource-not-found-error.js';
4
+ /**
5
+ * UnknownResource error is only returned by the instance API.
6
+ *
7
+ * @public
8
+ */
9
+ export declare class UnknownResourceMapper {
10
+ static fromJSON(status: number, obj: Readonly<JSONObject>): ResourceNotFoundError | ScalewayError;
11
+ }
@@ -0,0 +1,23 @@
1
+ import { isUUID } from "../../../internal/validations/string-validation.js";
2
+ import { ScalewayError } from "../scw-error.js";
3
+ import { ResourceNotFoundError } from "../standard/resource-not-found-error.js";
4
+ class UnknownResourceMapper {
5
+ static fromJSON(status, obj) {
6
+ const messageParts = typeof obj.message === "string" ? obj.message.split(/"|'/) : [];
7
+ if (messageParts.length === 3 && isUUID(messageParts[1])) {
8
+ return new ResourceNotFoundError(
9
+ status,
10
+ obj,
11
+ // transform `Security group ` to `security_group`
12
+ // `.replaceAll()` may be too recent to use yet.
13
+ // that's why we're using `.split(' ').join('_')` for now.
14
+ messageParts[0].trim().toLowerCase().split(" ").join("_"),
15
+ messageParts[1]
16
+ );
17
+ }
18
+ return new ScalewayError(status, obj);
19
+ }
20
+ }
21
+ export {
22
+ UnknownResourceMapper
23
+ };
@@ -0,0 +1,10 @@
1
+ import type { JSONObject } from '../../helpers/json.js';
2
+ import type { ScalewayError } from './scw-error.js';
3
+ /**
4
+ * Interface with static method that initialize {@link ScalewayError} from JSON.
5
+ *
6
+ * @internal
7
+ */
8
+ export interface ScalewayErrorFromJSONInitializer {
9
+ fromJSON(status: number, obj: Readonly<JSONObject>): ScalewayError | null;
10
+ }
@@ -0,0 +1,43 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
3
+ const json = require("../../helpers/json.cjs");
4
+ const types = require("./types.cjs");
5
+ const buildDefaultMessage = (status, body) => {
6
+ const message = [`http error ${status}`];
7
+ if (typeof body === "string") {
8
+ message.push(body);
9
+ } else if (json.isJSONObject(body)) {
10
+ if (typeof body.resource === "string") {
11
+ message.push(`resource ${body.resource}`);
12
+ }
13
+ if (typeof body.message === "string") {
14
+ message.push(body.message);
15
+ }
16
+ if (body.fields && types.isRecordOfStringArray(body.fields)) {
17
+ message.push(
18
+ Object.entries(body.fields).map(([name, list]) => `${name} (${list.join(", ")})`).join(", ")
19
+ );
20
+ }
21
+ }
22
+ return message.join(": ");
23
+ };
24
+ class ScalewayError extends Error {
25
+ constructor(status, body, message = buildDefaultMessage(status, body)) {
26
+ super(message);
27
+ this.status = status;
28
+ this.body = body;
29
+ this.message = message;
30
+ this.name = "ScalewayError";
31
+ this.rawMessage = typeof body === "object" && typeof body.message === "string" ? body.message : void 0;
32
+ Object.setPrototypeOf(this, new.target.prototype);
33
+ }
34
+ /** The message originating from the payload. */
35
+ rawMessage;
36
+ static fromJSON(status, obj) {
37
+ return new ScalewayError(status, obj);
38
+ }
39
+ toString() {
40
+ return `${this.name}: ${this.message}`;
41
+ }
42
+ }
43
+ exports.ScalewayError = ScalewayError;
@@ -0,0 +1,25 @@
1
+ import type { JSONObject } from '../../helpers/json.js';
2
+ /**
3
+ * Scaleway error.
4
+ *
5
+ * @public
6
+ */
7
+ export declare class ScalewayError extends Error {
8
+ /** The response status. */
9
+ readonly status: number;
10
+ /** The response payload. */
11
+ readonly body: JSONObject | string;
12
+ /** The augmented message. */
13
+ readonly message: string;
14
+ /** The message originating from the payload. */
15
+ rawMessage: string | undefined;
16
+ constructor(
17
+ /** The response status. */
18
+ status: number,
19
+ /** The response payload. */
20
+ body: JSONObject | string,
21
+ /** The augmented message. */
22
+ message?: string);
23
+ static fromJSON(status: number, obj: Readonly<JSONObject>): ScalewayError | null;
24
+ toString(): string;
25
+ }
@@ -0,0 +1,43 @@
1
+ import { isJSONObject } from "../../helpers/json.js";
2
+ import { isRecordOfStringArray } from "./types.js";
3
+ const buildDefaultMessage = (status, body) => {
4
+ const message = [`http error ${status}`];
5
+ if (typeof body === "string") {
6
+ message.push(body);
7
+ } else if (isJSONObject(body)) {
8
+ if (typeof body.resource === "string") {
9
+ message.push(`resource ${body.resource}`);
10
+ }
11
+ if (typeof body.message === "string") {
12
+ message.push(body.message);
13
+ }
14
+ if (body.fields && isRecordOfStringArray(body.fields)) {
15
+ message.push(
16
+ Object.entries(body.fields).map(([name, list]) => `${name} (${list.join(", ")})`).join(", ")
17
+ );
18
+ }
19
+ }
20
+ return message.join(": ");
21
+ };
22
+ class ScalewayError extends Error {
23
+ constructor(status, body, message = buildDefaultMessage(status, body)) {
24
+ super(message);
25
+ this.status = status;
26
+ this.body = body;
27
+ this.message = message;
28
+ this.name = "ScalewayError";
29
+ this.rawMessage = typeof body === "object" && typeof body.message === "string" ? body.message : void 0;
30
+ Object.setPrototypeOf(this, new.target.prototype);
31
+ }
32
+ /** The message originating from the payload. */
33
+ rawMessage;
34
+ static fromJSON(status, obj) {
35
+ return new ScalewayError(status, obj);
36
+ }
37
+ toString() {
38
+ return `${this.name}: ${this.message}`;
39
+ }
40
+ }
41
+ export {
42
+ ScalewayError
43
+ };