@scaleway/sdk 2.0.0-alpha.13 → 2.0.0-alpha.15

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 (215) hide show
  1. package/dist/api/account/index.js +4 -0
  2. package/dist/api/account/v2/api.gen.js +147 -0
  3. package/dist/api/account/v2/index.gen.js +4 -0
  4. package/dist/api/account/v2/marshalling.gen.js +39 -0
  5. package/dist/api/account/v3/api.gen.js +129 -0
  6. package/dist/api/account/v3/index.gen.js +6 -0
  7. package/dist/api/account/v3/marshalling.gen.js +39 -0
  8. package/dist/api/account/v3/validation-rules.gen.js +39 -0
  9. package/dist/api/applesilicon/index.js +2 -0
  10. package/dist/api/applesilicon/v1alpha1/api.gen.js +220 -0
  11. package/dist/api/applesilicon/v1alpha1/content.gen.js +7 -0
  12. package/dist/api/applesilicon/v1alpha1/index.gen.js +5 -0
  13. package/dist/api/applesilicon/v1alpha1/marshalling.gen.js +113 -0
  14. package/dist/api/baremetal/index.js +2 -0
  15. package/dist/api/baremetal/v1/api.gen.js +523 -0
  16. package/dist/api/baremetal/v1/api.utils.js +27 -0
  17. package/dist/api/baremetal/v1/content.gen.js +13 -0
  18. package/dist/api/baremetal/v1/index.js +5 -0
  19. package/dist/api/baremetal/v1/marshalling.gen.js +406 -0
  20. package/dist/api/baremetal/v1/validation-rules.gen.js +109 -0
  21. package/dist/api/billing/index.js +2 -0
  22. package/dist/api/billing/v2alpha1/api.gen.js +80 -0
  23. package/dist/api/billing/v2alpha1/index.gen.js +4 -0
  24. package/dist/api/billing/v2alpha1/marshalling.gen.js +53 -0
  25. package/dist/api/block/index.js +2 -0
  26. package/dist/api/block/v1alpha1/api.gen.js +249 -0
  27. package/dist/api/block/v1alpha1/content.gen.js +13 -0
  28. package/dist/api/block/v1alpha1/index.gen.js +7 -0
  29. package/dist/api/block/v1alpha1/marshalling.gen.js +182 -0
  30. package/dist/api/block/v1alpha1/validation-rules.gen.js +52 -0
  31. package/dist/api/cockpit/index.js +2 -0
  32. package/dist/api/cockpit/v1beta1/api.gen.js +543 -0
  33. package/dist/api/cockpit/v1beta1/content.gen.js +7 -0
  34. package/dist/api/cockpit/v1beta1/index.gen.js +5 -0
  35. package/dist/api/cockpit/v1beta1/marshalling.gen.js +271 -0
  36. package/dist/api/container/index.js +2 -0
  37. package/dist/api/container/v1beta1/api.gen.js +549 -0
  38. package/dist/api/container/v1beta1/content.gen.js +22 -0
  39. package/dist/api/container/v1beta1/index.gen.js +7 -0
  40. package/dist/api/container/v1beta1/marshalling.gen.js +358 -0
  41. package/dist/api/container/v1beta1/validation-rules.gen.js +44 -0
  42. package/dist/api/document_db/index.js +2 -0
  43. package/dist/api/document_db/v1beta1/api.gen.js +830 -0
  44. package/dist/api/document_db/v1beta1/content.gen.js +19 -0
  45. package/dist/api/document_db/v1beta1/index.gen.js +7 -0
  46. package/dist/api/document_db/v1beta1/marshalling.gen.js +636 -0
  47. package/dist/api/document_db/v1beta1/validation-rules.gen.js +10 -0
  48. package/dist/api/domain/index.js +2 -0
  49. package/dist/api/domain/v2beta1/api.gen.js +867 -0
  50. package/dist/api/domain/v2beta1/content.gen.js +28 -0
  51. package/dist/api/domain/v2beta1/index.gen.js +5 -0
  52. package/dist/api/domain/v2beta1/marshalling.gen.js +1172 -0
  53. package/dist/api/flexibleip/index.js +2 -0
  54. package/dist/api/flexibleip/v1alpha1/api.gen.js +195 -0
  55. package/dist/api/flexibleip/v1alpha1/content.gen.js +10 -0
  56. package/dist/api/flexibleip/v1alpha1/index.gen.js +7 -0
  57. package/dist/api/flexibleip/v1alpha1/marshalling.gen.js +97 -0
  58. package/dist/api/flexibleip/v1alpha1/validation-rules.gen.js +14 -0
  59. package/dist/api/function/index.js +2 -0
  60. package/dist/api/function/v1beta1/api.gen.js +560 -0
  61. package/dist/api/function/v1beta1/content.gen.js +22 -0
  62. package/dist/api/function/v1beta1/index.gen.js +7 -0
  63. package/dist/api/function/v1beta1/marshalling.gen.js +396 -0
  64. package/dist/api/function/v1beta1/validation-rules.gen.js +44 -0
  65. package/dist/api/iam/index.js +2 -0
  66. package/dist/api/iam/v1alpha1/api.gen.js +783 -0
  67. package/dist/api/iam/v1alpha1/index.gen.js +6 -0
  68. package/dist/api/iam/v1alpha1/marshalling.gen.js +384 -0
  69. package/dist/api/iam/v1alpha1/validation-rules.gen.js +207 -0
  70. package/dist/api/instance/index.js +2 -0
  71. package/dist/api/instance/v1/api.gen.js +1003 -0
  72. package/dist/api/instance/v1/api.utils.js +369 -0
  73. package/dist/api/instance/v1/content.gen.js +31 -0
  74. package/dist/api/instance/v1/index.js +2 -0
  75. package/dist/api/instance/v1/marshalling.gen.js +1415 -0
  76. package/dist/api/instance/v1/marshalling.utils.js +63 -0
  77. package/dist/api/iot/index.js +2 -0
  78. package/dist/api/iot/v1/api.gen.js +573 -0
  79. package/dist/api/iot/v1/content.gen.js +7 -0
  80. package/dist/api/iot/v1/index.gen.js +5 -0
  81. package/dist/api/iot/v1/marshalling.gen.js +439 -0
  82. package/dist/api/ipam/index.js +2 -0
  83. package/dist/api/ipam/v1/api.gen.js +113 -0
  84. package/dist/api/ipam/v1/index.gen.js +6 -0
  85. package/dist/api/ipam/v1/marshalling.gen.js +77 -0
  86. package/dist/api/ipam/v1/validation-rules.gen.js +11 -0
  87. package/dist/api/ipfs/index.js +2 -0
  88. package/dist/api/ipfs/v1alpha1/api.gen.js +314 -0
  89. package/dist/api/ipfs/v1alpha1/content.gen.js +10 -0
  90. package/dist/api/ipfs/v1alpha1/index.gen.js +5 -0
  91. package/dist/api/ipfs/v1alpha1/marshalling.gen.js +180 -0
  92. package/dist/api/k8s/index.js +2 -0
  93. package/dist/api/k8s/v1/api.gen.js +454 -0
  94. package/dist/api/k8s/v1/api.utils.js +13 -0
  95. package/dist/api/k8s/v1/content.gen.js +13 -0
  96. package/dist/api/k8s/v1/index.js +4 -0
  97. package/dist/api/k8s/v1/marshalling.gen.js +421 -0
  98. package/dist/api/k8s/v1/validation-rules.gen.js +101 -0
  99. package/dist/api/lb/index.js +2 -0
  100. package/dist/api/lb/v1/api.gen.js +1669 -0
  101. package/dist/api/lb/v1/api.utils.js +81 -0
  102. package/dist/api/lb/v1/content.gen.js +16 -0
  103. package/dist/api/lb/v1/index.js +2 -0
  104. package/dist/api/lb/v1/marshalling.gen.js +1039 -0
  105. package/dist/api/marketplace/index.js +4 -0
  106. package/dist/api/marketplace/v1/api.gen.js +60 -0
  107. package/dist/api/marketplace/v1/index.gen.js +4 -0
  108. package/dist/api/marketplace/v1/marshalling.gen.js +92 -0
  109. package/dist/api/marketplace/v2/api.gen.js +162 -0
  110. package/dist/api/marketplace/v2/index.gen.js +4 -0
  111. package/dist/api/marketplace/v2/marshalling.gen.js +94 -0
  112. package/dist/api/mnq/index.js +4 -0
  113. package/dist/api/mnq/v1alpha1/api.gen.js +197 -0
  114. package/dist/api/mnq/v1alpha1/index.gen.js +4 -0
  115. package/dist/api/mnq/v1alpha1/marshalling.gen.js +126 -0
  116. package/dist/api/mnq/v1beta1/api.gen.js +529 -0
  117. package/dist/api/mnq/v1beta1/index.gen.js +6 -0
  118. package/dist/api/mnq/v1beta1/marshalling.gen.js +212 -0
  119. package/dist/api/mnq/v1beta1/validation-rules.gen.js +77 -0
  120. package/dist/api/rdb/index.js +2 -0
  121. package/dist/api/rdb/v1/api.gen.js +972 -0
  122. package/dist/api/rdb/v1/content.gen.js +22 -0
  123. package/dist/api/rdb/v1/index.gen.js +7 -0
  124. package/dist/api/rdb/v1/marshalling.gen.js +694 -0
  125. package/dist/api/rdb/v1/validation-rules.gen.js +10 -0
  126. package/dist/api/redis/index.js +2 -0
  127. package/dist/api/redis/v1/api.gen.js +389 -0
  128. package/dist/api/redis/v1/content.gen.js +7 -0
  129. package/dist/api/redis/v1/index.gen.js +5 -0
  130. package/dist/api/redis/v1/marshalling.gen.js +283 -0
  131. package/dist/api/registry/index.js +2 -0
  132. package/dist/api/registry/v1/api.gen.js +252 -0
  133. package/dist/api/registry/v1/content.gen.js +13 -0
  134. package/dist/api/registry/v1/index.gen.js +5 -0
  135. package/dist/api/registry/v1/marshalling.gen.js +108 -0
  136. package/dist/api/secret/index.js +2 -0
  137. package/dist/api/secret/v1alpha1/api.gen.js +415 -0
  138. package/dist/api/secret/v1alpha1/index.gen.js +4 -0
  139. package/dist/api/secret/v1alpha1/marshalling.gen.js +150 -0
  140. package/dist/api/tem/index.js +2 -0
  141. package/dist/api/tem/v1alpha1/api.gen.js +229 -0
  142. package/dist/api/tem/v1alpha1/content.gen.js +10 -0
  143. package/dist/api/tem/v1alpha1/index.gen.js +5 -0
  144. package/dist/api/tem/v1alpha1/marshalling.gen.js +168 -0
  145. package/dist/api/test/index.js +2 -0
  146. package/dist/api/test/v1/api.gen.js +152 -0
  147. package/dist/api/test/v1/content.gen.js +7 -0
  148. package/dist/api/test/v1/index.gen.js +5 -0
  149. package/dist/api/test/v1/marshalling.gen.js +81 -0
  150. package/dist/api/vpc/index.js +4 -0
  151. package/dist/api/vpc/v1/api.gen.js +110 -0
  152. package/dist/api/vpc/v1/index.gen.js +4 -0
  153. package/dist/api/vpc/v1/marshalling.gen.js +44 -0
  154. package/dist/api/vpc/v2/api.gen.js +276 -0
  155. package/dist/api/vpc/v2/index.gen.js +4 -0
  156. package/dist/api/vpc/v2/marshalling.gen.js +137 -0
  157. package/dist/api/vpcgw/index.js +2 -0
  158. package/dist/api/vpcgw/v1/api.gen.js +669 -0
  159. package/dist/api/vpcgw/v1/content.gen.js +10 -0
  160. package/dist/api/vpcgw/v1/index.gen.js +5 -0
  161. package/dist/api/vpcgw/v1/marshalling.gen.js +359 -0
  162. package/dist/api/webhosting/index.js +2 -0
  163. package/dist/api/webhosting/v1alpha1/api.gen.js +158 -0
  164. package/dist/api/webhosting/v1alpha1/content.gen.js +7 -0
  165. package/dist/api/webhosting/v1alpha1/index.gen.js +7 -0
  166. package/dist/api/webhosting/v1alpha1/marshalling.gen.js +146 -0
  167. package/dist/api/webhosting/v1alpha1/validation-rules.gen.js +14 -0
  168. package/dist/helpers/is-browser.js +5 -0
  169. package/dist/helpers/is-response.js +10 -0
  170. package/dist/helpers/json.js +11 -0
  171. package/dist/helpers/marshalling.js +130 -0
  172. package/dist/index.cjs +26484 -0
  173. package/dist/index.d.ts +31184 -0
  174. package/dist/index.js +71 -0
  175. package/dist/internal/async/interval-retrier.js +105 -0
  176. package/dist/internal/async/sleep.js +13 -0
  177. package/dist/internal/interceptors/composer.js +46 -0
  178. package/dist/internal/interceptors/helpers.js +32 -0
  179. package/dist/internal/logger/console-logger.js +38 -0
  180. package/dist/internal/logger/index.js +41 -0
  181. package/dist/internal/logger/level-resolver.js +13 -0
  182. package/dist/internal/validations/string-validation.js +38 -0
  183. package/dist/node_modules/.pnpm/@scaleway_random-name@4.0.2/node_modules/@scaleway/random-name/dist/index.js +13 -0
  184. package/dist/scw/api.js +12 -0
  185. package/dist/scw/auth.js +68 -0
  186. package/dist/scw/client-ini-factory.js +174 -0
  187. package/dist/scw/client-ini-profile.js +54 -0
  188. package/dist/scw/client-settings.js +78 -0
  189. package/dist/scw/client.js +98 -0
  190. package/dist/scw/constants.js +4 -0
  191. package/dist/scw/custom-marshalling.js +131 -0
  192. package/dist/scw/errors/error-parser.js +107 -0
  193. package/dist/scw/errors/non-standard/invalid-request-mapper.js +36 -0
  194. package/dist/scw/errors/non-standard/unknown-resource-mapper.js +27 -0
  195. package/dist/scw/errors/scw-error.js +66 -0
  196. package/dist/scw/errors/standard/already-exists-error.js +26 -0
  197. package/dist/scw/errors/standard/denied-authentication-error.js +52 -0
  198. package/dist/scw/errors/standard/index.js +13 -0
  199. package/dist/scw/errors/standard/invalid-arguments-error.js +67 -0
  200. package/dist/scw/errors/standard/out-of-stock-error.js +22 -0
  201. package/dist/scw/errors/standard/permissions-denied-error.js +48 -0
  202. package/dist/scw/errors/standard/precondition-failed-error.js +42 -0
  203. package/dist/scw/errors/standard/quotas-exceeded-error.js +68 -0
  204. package/dist/scw/errors/standard/resource-expired-error.js +26 -0
  205. package/dist/scw/errors/standard/resource-locked-error.js +25 -0
  206. package/dist/scw/errors/standard/resource-not-found-error.js +25 -0
  207. package/dist/scw/errors/standard/too-many-requests-error.js +70 -0
  208. package/dist/scw/errors/standard/transient-state-error.js +26 -0
  209. package/dist/scw/errors/types.js +23 -0
  210. package/dist/scw/fetch/build-fetcher.js +72 -0
  211. package/dist/scw/fetch/http-dumper.js +54 -0
  212. package/dist/scw/fetch/http-interceptors.js +99 -0
  213. package/dist/scw/fetch/resource-paginator.js +84 -0
  214. package/dist/scw/fetch/response-parser.js +72 -0
  215. package/package.json +2 -2
@@ -0,0 +1,54 @@
1
+ import { isAccessKey, isSecretKey } from '../internal/validations/string-validation.js';
2
+
3
+ /**
4
+ * Holds access key and secret key.
5
+ *
6
+ * @public
7
+ */
8
+
9
+ /**
10
+ * Holds default values of a Scaleway profile.
11
+ *
12
+ * @public
13
+ */
14
+
15
+ /**
16
+ * Holds values of a Scaleway profile.
17
+ *
18
+ * @public
19
+ */
20
+
21
+ /**
22
+ * Verifies that the payload contains both the accessKey and the secretKey.
23
+ *
24
+ * @param obj - The secrets
25
+ * @returns Whether the secrets are not empty.
26
+ *
27
+ * @internal
28
+ */
29
+ const hasAuthenticationSecrets = obj => typeof obj.accessKey === 'string' && obj.accessKey !== '' && typeof obj.secretKey === 'string' && obj.secretKey !== '';
30
+
31
+ /**
32
+ * Asserts the format of secrets.
33
+ *
34
+ * @param obj - The secrets
35
+ * @returns Whether the secrets use a valid format
36
+ *
37
+ * @throws Error
38
+ * Thrown if either the accessKey or the secretKey has en invalid format.
39
+ *
40
+ * @internal
41
+ */
42
+ function assertValidAuthenticationSecrets(obj) {
43
+ if (!(obj.accessKey && obj.secretKey)) {
44
+ throw new Error(`Invalid secrets, accessKey & secretKey must be defined. See https://www.scaleway.com/en/docs/identity-and-access-management/iam/how-to/create-api-keys/`);
45
+ }
46
+ if (!isAccessKey(obj.accessKey)) {
47
+ throw new Error(`Invalid access key format '${obj.accessKey}', expected SCWXXXXXXXXXXXXXXXXX format. See https://www.scaleway.com/en/docs/identity-and-access-management/iam/how-to/create-api-keys/`);
48
+ }
49
+ if (!isSecretKey(obj.secretKey)) {
50
+ throw new Error(`Invalid secret key format '${obj.secretKey}', expected a UUID: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx. See https://www.scaleway.com/en/docs/identity-and-access-management/iam/how-to/create-api-keys/`);
51
+ }
52
+ }
53
+
54
+ export { assertValidAuthenticationSecrets, hasAuthenticationSecrets };
@@ -0,0 +1,78 @@
1
+ import { isOrganizationId, isProjectId, isRegion, isZone, isURL } from '../internal/validations/string-validation.js';
2
+
3
+ /**
4
+ * Holds default values of settings.
5
+ *
6
+ * @public
7
+ */
8
+
9
+ /**
10
+ * Settings hold the values of all client options.
11
+ *
12
+ * @public
13
+ */
14
+
15
+ /**
16
+ * Validates the content of a {@link Settings} object.
17
+ *
18
+ * @throws Error
19
+ * Thrown if {@link Settings} aren't valid.
20
+ *
21
+ * @internal
22
+ */
23
+ const assertValidSettings = obj => {
24
+ // Default Organization ID.
25
+ if (obj.defaultOrganizationId !== undefined) {
26
+ if (typeof obj.defaultOrganizationId !== 'string' || obj.defaultOrganizationId.length === 0) {
27
+ throw new Error('Default organization ID cannot be empty');
28
+ }
29
+ if (!isOrganizationId(obj.defaultOrganizationId)) {
30
+ throw new Error(`Invalid organization ID format '${obj.defaultOrganizationId}', expected a UUID: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx`);
31
+ }
32
+ }
33
+
34
+ // Default Project ID.
35
+ if (obj.defaultProjectId !== undefined) {
36
+ if (typeof obj.defaultProjectId !== 'string' || obj.defaultProjectId.length === 0) {
37
+ throw new Error('Default project ID cannot be empty');
38
+ }
39
+ if (!isProjectId(obj.defaultProjectId)) {
40
+ throw new Error(`Invalid project ID format '${obj.defaultProjectId}', expected a UUID: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx`);
41
+ }
42
+ }
43
+
44
+ // Default Region.
45
+ if (obj.defaultRegion && !isRegion(obj.defaultRegion)) {
46
+ throw new Error(`Invalid default region format '${obj.defaultRegion}'`);
47
+ }
48
+
49
+ // Default Zone.
50
+ if (obj.defaultZone && !isZone(obj.defaultZone)) {
51
+ throw new Error(`Invalid default zone format '${obj.defaultZone}'`);
52
+ }
53
+
54
+ // API URL.
55
+ if (!isURL(obj.apiURL)) {
56
+ throw new Error(`Invalid URL ${obj.apiURL}`);
57
+ }
58
+ if (obj.apiURL.endsWith('/')) {
59
+ throw new Error(`Invalid URL ${obj.apiURL}: it should not have a trailing slash`);
60
+ }
61
+
62
+ // HTTP Client.
63
+ if (typeof obj.httpClient !== typeof fetch) {
64
+ throw new Error(`Invalid HTTP Client`);
65
+ }
66
+
67
+ // Default Page Size.
68
+ if (obj.defaultPageSize !== undefined && (typeof obj.defaultPageSize !== 'number' || Number.isNaN(obj.defaultPageSize) || obj.defaultPageSize <= 0)) {
69
+ throw new Error(`Invalid defaultPageSize ${obj.defaultPageSize}: it should be a number above 0`);
70
+ }
71
+
72
+ // User Agent.
73
+ if (typeof obj.userAgent !== 'string') {
74
+ throw new Error(`Invalid User-Agent`);
75
+ }
76
+ };
77
+
78
+ export { assertValidSettings };
@@ -0,0 +1,98 @@
1
+ import { getLogger } from '../internal/logger/index.js';
2
+ import { withLegacyInterceptors, withProfile } from './client-ini-factory.js';
3
+ import { assertValidSettings } from './client-settings.js';
4
+ import { version, userAgent } from './constants.js';
5
+ import { buildFetcher } from './fetch/build-fetcher.js';
6
+
7
+ /** Default {@link Settings} values. */
8
+ const DEFAULT_SETTINGS = {
9
+ apiURL: 'https://api.scaleway.com',
10
+ httpClient: fetch,
11
+ interceptors: [],
12
+ userAgent
13
+ };
14
+
15
+ /**
16
+ * Scaleway client.
17
+ */
18
+
19
+ /**
20
+ * Creates a Scaleway client with advanced options.
21
+ * You can either use existing factories
22
+ * (like `withProfile`, `withUserAgentSuffix`, etc)
23
+ * or write your own using the interface `ClientConfig`.
24
+ *
25
+ * @example
26
+ * Creates a client with factories:
27
+ * ```
28
+ * createAdvancedClient(
29
+ * (obj: Settings) => ({
30
+ * ...obj,
31
+ * defaultPageSize: 100 ,
32
+ * httpClient: myFetchWrapper,
33
+ * }),
34
+ * withUserAgentSuffix('bot-name/1.0'),
35
+ * )
36
+ * ```
37
+ *
38
+ * @throws Error
39
+ * Thrown if the setup fails.
40
+ *
41
+ * @public
42
+ */
43
+ const createAdvancedClient = function () {
44
+ for (var _len = arguments.length, configs = new Array(_len), _key = 0; _key < _len; _key++) {
45
+ configs[_key] = arguments[_key];
46
+ }
47
+ const settings = configs.concat([withLegacyInterceptors()]).reduce((currentSettings, config) => config(currentSettings), DEFAULT_SETTINGS);
48
+ assertValidSettings(settings);
49
+ getLogger().info(`init Scaleway SDK version ${version}`);
50
+ return {
51
+ fetch: buildFetcher(settings, settings.httpClient),
52
+ settings
53
+ };
54
+ };
55
+
56
+ /**
57
+ * Creates a Scaleway client with a profile.
58
+ *
59
+ * @example
60
+ * Creates a client with credentials & default values (see https://www.scaleway.com/en/docs/identity-and-access-management/iam/how-to/create-api-keys/):
61
+ * ```
62
+ * import { createClient } from '@scaleway/sdk'
63
+ *
64
+ * createClient({
65
+ * accessKey: 'SCWXXXXXXXXXXXXXXXXX',
66
+ * secretKey: 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx',
67
+ * defaultProjectId: 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx',
68
+ * defaultRegion: 'fr-par',
69
+ * defaultZone: 'fr-par-1',
70
+ * })
71
+ * ```
72
+ *
73
+ * @example
74
+ * Creates a client by loading values from the environment (see https://www.scaleway.com/en/docs/identity-and-access-management/iam/how-to/create-api-keys/)
75
+ * or the config file created by CLI `scw init` (see https://www.scaleway.com/en/cli/):
76
+ * ```
77
+ * import { loadProfileFromConfigurationFile } from '@scaleway/configuration-loader'
78
+ * import { createClient } from '@scaleway/sdk'
79
+ *
80
+ * createClient({
81
+ * ...await loadProfileFromConfigurationFile(),
82
+ * defaultZone: 'fr-par-3',
83
+ * })
84
+ * ```
85
+ *
86
+ * @throws Error
87
+ * Thrown if the setup fails.
88
+ *
89
+ * @public
90
+ */
91
+ const createClient = function (profile) {
92
+ if (profile === void 0) {
93
+ profile = {};
94
+ }
95
+ return createAdvancedClient(withProfile(profile));
96
+ };
97
+
98
+ export { createAdvancedClient, createClient };
@@ -0,0 +1,4 @@
1
+ const version = 'v2.0.0-alpha.14';
2
+ const userAgent = `scaleway-sdk-js/${version}`;
3
+
4
+ export { userAgent, version };
@@ -0,0 +1,131 @@
1
+ import { isJSONObject } from '../helpers/json.js';
2
+ import { unmarshalDate, unmarshalArrayOfObject } from '../helpers/marshalling.js';
3
+
4
+ /**
5
+ * Unmarshals {@link Money}
6
+ *
7
+ * @internal
8
+ */
9
+ const unmarshalMoney = data => {
10
+ if (!isJSONObject(data)) {
11
+ throw new TypeError(`Unmarshalling the type 'Money' failed as data isn't a dictionary.`);
12
+ }
13
+ return {
14
+ currencyCode: data.currency_code,
15
+ nanos: data.nanos,
16
+ units: data.units
17
+ };
18
+ };
19
+
20
+ /**
21
+ * Unmarshals {@link ServiceInfo}.
22
+ *
23
+ * @internal
24
+ */
25
+ const unmarshalServiceInfo = data => {
26
+ if (!isJSONObject(data)) {
27
+ throw new TypeError(`Unmarshalling the type 'ServiceInfo' failed as data isn't a dictionary.`);
28
+ }
29
+ return {
30
+ description: data.description,
31
+ documentationUrl: data.documentation_url,
32
+ name: data.name,
33
+ version: data.version
34
+ };
35
+ };
36
+
37
+ /**
38
+ * Unmarshals {@link ScwFile}.
39
+ *
40
+ * @internal
41
+ */
42
+ const unmarshalScwFile = data => {
43
+ if (!isJSONObject(data)) {
44
+ throw new TypeError(`Unmarshalling the type 'ScwFile' failed as data isn't a dictionary.`);
45
+ }
46
+ return {
47
+ content: data.content,
48
+ contentType: data.content_type,
49
+ name: data.name
50
+ };
51
+ };
52
+
53
+ /**
54
+ * Unmarshals {@link TimeSeriesPoint}
55
+ *
56
+ * @remarks To optimize the size of this message,
57
+ * the JSON is compressed in an array instead of a dictionary.
58
+ * Example: `["2019-08-08T15:00:00Z", 0.2]`.
59
+ *
60
+ * @internal
61
+ */
62
+ const unmarshalTimeSeriesPoint = data => {
63
+ if (!Array.isArray(data)) {
64
+ throw new TypeError(`Unmarshalling the type 'TimeSeriesPoint' failed as data isn't an array.`);
65
+ }
66
+ return {
67
+ timestamp: unmarshalDate(data[0]),
68
+ value: data[1]
69
+ };
70
+ };
71
+
72
+ /**
73
+ * Unmarshals {@link TimeSeries}
74
+ *
75
+ * @internal
76
+ */
77
+ const unmarshalTimeSeries = data => {
78
+ if (!isJSONObject(data)) {
79
+ throw new TypeError(`Unmarshalling the type 'TimeSeries' failed as data isn't a dictionary.`);
80
+ }
81
+ return {
82
+ metadata: data.metadata,
83
+ name: data.name,
84
+ points: unmarshalArrayOfObject(data.points, unmarshalTimeSeriesPoint)
85
+ };
86
+ };
87
+
88
+ /**
89
+ * Marshals {@link ScwFile}.
90
+ *
91
+ * @internal
92
+ */
93
+ const marshalScwFile = obj => ({
94
+ content: obj.content,
95
+ content_type: obj.contentType,
96
+ name: obj.name
97
+ });
98
+
99
+ /**
100
+ * Marshals {@link Money}
101
+ *
102
+ * @internal
103
+ */
104
+ const marshalMoney = obj => ({
105
+ currency_code: obj.currencyCode,
106
+ nanos: obj.nanos,
107
+ units: obj.units
108
+ });
109
+
110
+ /**
111
+ * Marshals {@link TimeSeriesPoint}
112
+ *
113
+ * @internal
114
+ */
115
+ const marshalTimeSeriesPoint = obj => ({
116
+ timestamp: obj.timestamp?.toISOString(),
117
+ value: obj.value
118
+ });
119
+
120
+ /**
121
+ * Marshals {@link TimeSeries}
122
+ *
123
+ * @internal
124
+ */
125
+ const marshalTimeSeries = obj => ({
126
+ metadata: obj.metadata,
127
+ name: obj.name,
128
+ points: obj.points.map(elt => marshalTimeSeriesPoint(elt))
129
+ });
130
+
131
+ export { marshalMoney, marshalScwFile, marshalTimeSeries, marshalTimeSeriesPoint, unmarshalMoney, unmarshalScwFile, unmarshalServiceInfo, unmarshalTimeSeries, unmarshalTimeSeriesPoint };
@@ -0,0 +1,107 @@
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
+
17
+ /**
18
+ * Unmarshals a standard error from raw body.
19
+ *
20
+ * @param type - The error type
21
+ * @param status - The status code
22
+ * @param body - The error response
23
+ * @returns The standard error if found
24
+ *
25
+ * @internal
26
+ */
27
+ const unmarshalStandardError = (type, status, body) => {
28
+ let error;
29
+ switch (type) {
30
+ case 'denied_authentication':
31
+ error = DeniedAuthenticationError;
32
+ break;
33
+ case 'invalid_arguments':
34
+ error = InvalidArgumentsError;
35
+ break;
36
+ case 'out_of_stock':
37
+ error = OutOfStockError;
38
+ break;
39
+ case 'permissions_denied':
40
+ error = PermissionsDeniedError;
41
+ break;
42
+ case 'precondition_failed':
43
+ error = PreconditionFailedError;
44
+ break;
45
+ case 'quotas_exceeded':
46
+ error = QuotasExceededError;
47
+ break;
48
+ case 'expired':
49
+ error = ResourceExpiredError;
50
+ break;
51
+ case 'not_found':
52
+ error = ResourceNotFoundError;
53
+ break;
54
+ case 'locked':
55
+ error = ResourceLockedError;
56
+ break;
57
+ case 'transient_state':
58
+ error = TransientStateError;
59
+ break;
60
+ case 'already_exists':
61
+ error = AlreadyExistsError;
62
+ break;
63
+ case 'too_many_requests':
64
+ error = TooManyRequestsError;
65
+ break;
66
+ default:
67
+ return null;
68
+ }
69
+ return error.fromJSON(status, body);
70
+ };
71
+
72
+ /**
73
+ * Unmarshals a non-standard error from raw body.
74
+ *
75
+ * @param type - The error type
76
+ * @param status - The status code
77
+ * @param body - The error response
78
+ * @returns The non-standard error if found
79
+ *
80
+ * @internal
81
+ */
82
+ const unmarshalNonStandardError = (type, status, body) => {
83
+ switch (type) {
84
+ case 'unknown_resource':
85
+ return UnknownResourceMapper.fromJSON(status, body);
86
+ case 'invalid_request_error':
87
+ return InvalidRequestMapper.fromJSON(status, body);
88
+ default:
89
+ return null;
90
+ }
91
+ };
92
+
93
+ /**
94
+ * Parses Scaleway error from raw body.
95
+ *
96
+ * @param status - The status code
97
+ * @param body - The error response
98
+ * @returns The resolved error
99
+ *
100
+ * @internal
101
+ */
102
+ const parseScalewayError = (status, body) => {
103
+ const parsableError = typeof body.type === 'string' && (unmarshalStandardError(body.type, status, body) ?? unmarshalNonStandardError(body.type, status, body));
104
+ return parsableError || new ScalewayError(status, body);
105
+ };
106
+
107
+ export { parseScalewayError };
@@ -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
+
6
+ /**
7
+ * InvalidRequest error is only returned by the instance API.
8
+ *
9
+ * @public
10
+ */
11
+ class InvalidRequestMapper {
12
+ static fromJSON(status, obj) {
13
+ if (typeof obj.message === 'string' && obj.message.toLowerCase().includes('quota exceeded for this resource')) {
14
+ return new QuotasExceededError(status, obj, [{
15
+ current: 0,
16
+ quota: 0,
17
+ resource: typeof obj.resource === 'string' ? obj.resource : ''
18
+ }]);
19
+ }
20
+ const fields = obj.fields && isRecordOfStringArray(obj.fields) ? obj.fields : {};
21
+ const fieldsMessages = Object.entries(fields);
22
+ if (fieldsMessages.length) {
23
+ return new InvalidArgumentsError(status, obj, fieldsMessages.map(_ref => {
24
+ let [argumentName, messages] = _ref;
25
+ return messages.map(helpMessage => ({
26
+ argumentName,
27
+ helpMessage,
28
+ reason: 'constraint'
29
+ }));
30
+ }).flat());
31
+ }
32
+ return new ScalewayError(status, obj);
33
+ }
34
+ }
35
+
36
+ export { InvalidRequestMapper };
@@ -0,0 +1,27 @@
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
+
5
+ /**
6
+ * UnknownResource error is only returned by the instance API.
7
+ *
8
+ * @public
9
+ */
10
+ class UnknownResourceMapper {
11
+ static fromJSON(status, obj) {
12
+ // Split the message
13
+ // Note: some errors uses ' and not "
14
+ // Examples: `"111..." not found` or `Security Group '111...' not found`
15
+ const messageParts = typeof obj.message === 'string' ? obj.message.split(/"|'/) : [];
16
+ if (messageParts.length === 3 && isUUID(messageParts[1])) {
17
+ return new ResourceNotFoundError(status, obj,
18
+ // transform `Security group ` to `security_group`
19
+ // `.replaceAll()` may be too recent to use yet.
20
+ // that's why we're using `.split(' ').join('_')` for now.
21
+ messageParts[0].trim().toLowerCase().split(' ').join('_'), messageParts[1]);
22
+ }
23
+ return new ScalewayError(status, obj);
24
+ }
25
+ }
26
+
27
+ export { UnknownResourceMapper };
@@ -0,0 +1,66 @@
1
+ import { isJSONObject } from '../../helpers/json.js';
2
+ import { isRecordOfStringArray } from './types.js';
3
+
4
+ /**
5
+ * Builds the default message for {@link ScalewayError}.
6
+ *
7
+ * @param status - The response code
8
+ * @param body - The response body
9
+ * @returns The error message
10
+ *
11
+ * @internal
12
+ */
13
+ const buildDefaultMessage = (status, body) => {
14
+ const message = [`http error ${status}`];
15
+ if (typeof body === 'string') {
16
+ message.push(body);
17
+ } else if (isJSONObject(body)) {
18
+ if (typeof body.resource === 'string') {
19
+ message.push(`resource ${body.resource}`);
20
+ }
21
+ if (typeof body.message === 'string') {
22
+ message.push(body.message);
23
+ }
24
+ if (body.fields && isRecordOfStringArray(body.fields)) {
25
+ message.push(Object.entries(body.fields).map(_ref => {
26
+ let [name, list] = _ref;
27
+ return `${name} (${list.join(', ')})`;
28
+ }).join(', '));
29
+ }
30
+ }
31
+ return message.join(': ');
32
+ };
33
+
34
+ /**
35
+ * Scaleway error.
36
+ *
37
+ * @public
38
+ */
39
+ class ScalewayError extends Error {
40
+ /** The message originating from the payload. */
41
+
42
+ constructor( /** The response status. */
43
+ status, /** The response payload. */
44
+ body, /** The augmented message. */
45
+ message) {
46
+ if (message === void 0) {
47
+ message = buildDefaultMessage(status, body);
48
+ }
49
+ super(message); // 'Error' breaks prototype chain here
50
+ this.status = status;
51
+ this.body = body;
52
+ this.message = message;
53
+ this.name = 'ScalewayError';
54
+ this.rawMessage = typeof body === 'object' && typeof body.message === 'string' ? body.message : undefined;
55
+ Object.setPrototypeOf(this, new.target.prototype); // restore prototype chain
56
+ }
57
+
58
+ static fromJSON(status, obj) {
59
+ return new ScalewayError(status, obj);
60
+ }
61
+ toString() {
62
+ return `${this.name}: ${this.message}`;
63
+ }
64
+ }
65
+
66
+ export { ScalewayError };
@@ -0,0 +1,26 @@
1
+ import { ScalewayError } from '../scw-error.js';
2
+
3
+ /**
4
+ * AlreadyExists error is used when a resource already exists.
5
+ *
6
+ * @public
7
+ */
8
+ class AlreadyExistsError extends ScalewayError {
9
+ constructor(status, body, resource, resourceId, helpMessage) {
10
+ super(status, body, `resource ${resource} with ID ${resourceId} already exists: ${helpMessage}`);
11
+ this.status = status;
12
+ this.body = body;
13
+ this.resource = resource;
14
+ this.resourceId = resourceId;
15
+ this.helpMessage = helpMessage;
16
+ this.name = 'AlreadyExistsError';
17
+ }
18
+ static fromJSON(status, obj) {
19
+ if (typeof obj.resource !== 'string' || typeof obj.resource_id !== 'string' || typeof obj.help_message !== 'string') {
20
+ return null;
21
+ }
22
+ return new AlreadyExistsError(status, obj, obj.resource, obj.resource_id, obj.help_message);
23
+ }
24
+ }
25
+
26
+ export { AlreadyExistsError };
@@ -0,0 +1,52 @@
1
+ import { ScalewayError } from '../scw-error.js';
2
+
3
+ /**
4
+ * Build the default message for {@link DeniedAuthenticationError}.
5
+ *
6
+ * @param method - The authentication method
7
+ * @param reason - The deny reason
8
+ * @returns The error message
9
+ *
10
+ * @internal
11
+ */
12
+ const buildMessage = (method, reason) => {
13
+ let reasonDesc;
14
+ switch (reason) {
15
+ case 'invalid_argument':
16
+ reasonDesc = `invalid ${method} format or empty value`;
17
+ break;
18
+ case 'not_found':
19
+ reasonDesc = `${method} does not exist`;
20
+ break;
21
+ case 'expired':
22
+ reasonDesc = `${method} is expired`;
23
+ break;
24
+ default:
25
+ reasonDesc = `unknown reason for ${method}`;
26
+ }
27
+ return `denied authentication: ${reasonDesc}`;
28
+ };
29
+
30
+ /**
31
+ * DeniedAuthentication error is used by the API Gateway auth service to deny a request.
32
+ *
33
+ * @public
34
+ */
35
+ class DeniedAuthenticationError extends ScalewayError {
36
+ constructor(status, body, method, reason) {
37
+ super(status, body, buildMessage(method, reason));
38
+ this.status = status;
39
+ this.body = body;
40
+ this.method = method;
41
+ this.reason = reason;
42
+ this.name = 'DeniedAuthenticationError';
43
+ }
44
+ static fromJSON(status, obj) {
45
+ if (typeof obj.method !== 'string' || typeof obj.reason !== 'string') {
46
+ return null;
47
+ }
48
+ return new DeniedAuthenticationError(status, obj, obj.method, obj.reason);
49
+ }
50
+ }
51
+
52
+ export { DeniedAuthenticationError };