@scaleway/sdk-client 1.3.3 → 2.2.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/dist/_virtual/_rolldown/runtime.js +11 -0
- package/dist/helpers/is-browser.js +1 -3
- package/dist/helpers/is-response.js +8 -3
- package/dist/helpers/json.js +47 -41
- package/dist/helpers/marshalling.d.ts +0 -8
- package/dist/helpers/marshalling.js +89 -64
- package/dist/index.js +6 -47
- package/dist/internal/async/interval-retrier.js +64 -48
- package/dist/internal/async/sleep.js +10 -4
- package/dist/internal/interceptors/composer.js +34 -23
- package/dist/internal/interceptors/helpers.js +22 -9
- package/dist/internal/interceptors/types.d.ts +7 -0
- package/dist/internal/logger/console-logger.js +27 -22
- package/dist/internal/logger/index.js +23 -7
- package/dist/internal/logger/level-resolver.js +9 -12
- package/dist/internal/validations/string-validation.js +20 -21
- package/dist/internals.js +8 -0
- package/dist/package.js +32 -0
- package/dist/scw/api.js +10 -7
- package/dist/scw/auth.d.ts +7 -0
- package/dist/scw/auth.js +60 -17
- package/dist/scw/client-ini-factory.js +127 -57
- package/dist/scw/client-ini-profile.js +23 -19
- package/dist/scw/client-settings.js +25 -49
- package/dist/scw/client.js +76 -25
- package/dist/scw/constants.js +3 -8
- package/dist/scw/custom-marshalling.js +147 -123
- package/dist/scw/custom-types.js +11 -10
- package/dist/scw/errors/error-parser.js +84 -62
- package/dist/scw/errors/non-standard/invalid-request-mapper.d.ts +1 -3
- package/dist/scw/errors/non-standard/invalid-request-mapper.js +21 -32
- package/dist/scw/errors/non-standard/unknown-resource-mapper.d.ts +1 -3
- package/dist/scw/errors/non-standard/unknown-resource-mapper.js +10 -19
- package/dist/scw/errors/scw-error.js +42 -39
- package/dist/scw/errors/standard/already-exists-error.js +20 -29
- package/dist/scw/errors/standard/denied-authentication-error.js +43 -34
- package/dist/scw/errors/standard/index.js +20 -18
- package/dist/scw/errors/standard/invalid-arguments-error.js +51 -50
- package/dist/scw/errors/standard/out-of-stock-error.js +18 -15
- package/dist/scw/errors/standard/permissions-denied-error.js +30 -26
- package/dist/scw/errors/standard/precondition-failed-error.js +32 -29
- package/dist/scw/errors/standard/quotas-exceeded-error.js +43 -38
- package/dist/scw/errors/standard/resource-expired-error.js +20 -29
- package/dist/scw/errors/standard/resource-locked-error.js +19 -18
- package/dist/scw/errors/standard/resource-not-found-error.js +19 -22
- package/dist/scw/errors/standard/too-many-requests-error.js +41 -54
- package/dist/scw/errors/standard/transient-state-error.js +20 -29
- package/dist/scw/errors/types.js +12 -12
- package/dist/scw/fetch/build-fetcher.js +49 -53
- package/dist/scw/fetch/http-dumper.js +50 -16
- package/dist/scw/fetch/http-interceptors.d.ts +8 -3
- package/dist/scw/fetch/http-interceptors.js +52 -34
- package/dist/scw/fetch/resource-paginator.js +52 -28
- package/dist/scw/fetch/response-parser.js +48 -49
- package/dist/scw/locality.js +12 -14
- package/dist/vendor/base64/index.js +34 -37
- package/package.json +2 -4
- package/dist/helpers/is-browser.cjs +0 -4
- package/dist/helpers/is-response.cjs +0 -4
- package/dist/helpers/json.cjs +0 -47
- package/dist/helpers/marshalling.cjs +0 -76
- package/dist/index.cjs +0 -58
- package/dist/internal/async/interval-retrier.cjs +0 -53
- package/dist/internal/async/sleep.cjs +0 -6
- package/dist/internal/interceptors/composer.cjs +0 -25
- package/dist/internal/interceptors/helpers.cjs +0 -12
- package/dist/internal/logger/console-logger.cjs +0 -24
- package/dist/internal/logger/index.cjs +0 -12
- package/dist/internal/logger/level-resolver.cjs +0 -13
- package/dist/internal/validations/string-validation.cjs +0 -30
- package/dist/package.json.cjs +0 -8
- package/dist/package.json.js +0 -8
- package/dist/scw/api.cjs +0 -8
- package/dist/scw/auth.cjs +0 -25
- package/dist/scw/client-ini-factory.cjs +0 -66
- package/dist/scw/client-ini-profile.cjs +0 -23
- package/dist/scw/client-settings.cjs +0 -51
- package/dist/scw/client.cjs +0 -31
- package/dist/scw/constants.cjs +0 -11
- package/dist/scw/custom-marshalling.cjs +0 -152
- package/dist/scw/custom-types.cjs +0 -11
- package/dist/scw/errors/error-parser.cjs +0 -76
- package/dist/scw/errors/non-standard/invalid-request-mapper.cjs +0 -36
- package/dist/scw/errors/non-standard/unknown-resource-mapper.cjs +0 -23
- package/dist/scw/errors/scw-error.cjs +0 -43
- package/dist/scw/errors/standard/already-exists-error.cjs +0 -31
- package/dist/scw/errors/standard/denied-authentication-error.cjs +0 -37
- package/dist/scw/errors/standard/index.cjs +0 -28
- package/dist/scw/errors/standard/invalid-arguments-error.cjs +0 -54
- package/dist/scw/errors/standard/out-of-stock-error.cjs +0 -17
- package/dist/scw/errors/standard/permissions-denied-error.cjs +0 -29
- package/dist/scw/errors/standard/precondition-failed-error.cjs +0 -32
- package/dist/scw/errors/standard/quotas-exceeded-error.cjs +0 -43
- package/dist/scw/errors/standard/resource-expired-error.cjs +0 -31
- package/dist/scw/errors/standard/resource-locked-error.cjs +0 -20
- package/dist/scw/errors/standard/resource-not-found-error.cjs +0 -24
- package/dist/scw/errors/standard/too-many-requests-error.cjs +0 -58
- package/dist/scw/errors/standard/transient-state-error.cjs +0 -31
- package/dist/scw/errors/types.cjs +0 -15
- package/dist/scw/fetch/build-fetcher.cjs +0 -60
- package/dist/scw/fetch/http-dumper.cjs +0 -20
- package/dist/scw/fetch/http-interceptors.cjs +0 -43
- package/dist/scw/fetch/resource-paginator.cjs +0 -36
- package/dist/scw/fetch/response-parser.cjs +0 -55
- package/dist/scw/locality.cjs +0 -16
- package/dist/vendor/base64/index.cjs +0 -46
|
@@ -1,24 +1,29 @@
|
|
|
1
1
|
import { LevelResolver, shouldLog } from "./level-resolver.js";
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
|
|
2
|
+
/**
|
|
3
|
+
* A Logger using console output.
|
|
4
|
+
*
|
|
5
|
+
* @param logLevel - The logger level name
|
|
6
|
+
* @param prefix - An optional logger message prefix
|
|
7
|
+
* @param output - The output to print logs, using by default the global console object
|
|
8
|
+
*
|
|
9
|
+
* @internal
|
|
10
|
+
*/
|
|
11
|
+
var ConsoleLogger = class {
|
|
12
|
+
level;
|
|
13
|
+
constructor(logLevel, prefix = "", output = console) {
|
|
14
|
+
this.logLevel = logLevel;
|
|
15
|
+
this.prefix = prefix;
|
|
16
|
+
this.output = output;
|
|
17
|
+
this.level = LevelResolver[this.logLevel];
|
|
18
|
+
}
|
|
19
|
+
makeMethod(method) {
|
|
20
|
+
return (message) => {
|
|
21
|
+
if (shouldLog(this.level, method)) this.output[method](this.prefix ? `${this.prefix} ${message}` : message);
|
|
22
|
+
};
|
|
23
|
+
}
|
|
24
|
+
debug = this.makeMethod("debug");
|
|
25
|
+
error = this.makeMethod("error");
|
|
26
|
+
info = this.makeMethod("info");
|
|
27
|
+
warn = this.makeMethod("warn");
|
|
24
28
|
};
|
|
29
|
+
export { ConsoleLogger };
|
|
@@ -1,12 +1,28 @@
|
|
|
1
1
|
import { ConsoleLogger } from "./console-logger.js";
|
|
2
|
-
|
|
2
|
+
var sdkLogger = new ConsoleLogger("silent");
|
|
3
|
+
/**
|
|
4
|
+
* Sets a logger to be used within the SDK.
|
|
5
|
+
*
|
|
6
|
+
* @param logger - The Logger instance
|
|
7
|
+
*
|
|
8
|
+
* @public
|
|
9
|
+
*/
|
|
3
10
|
const setLogger = (logger) => {
|
|
4
|
-
|
|
11
|
+
sdkLogger = logger;
|
|
5
12
|
};
|
|
13
|
+
/**
|
|
14
|
+
* Sets the logger to console logger with given logLevel (log is disabled by default).
|
|
15
|
+
*
|
|
16
|
+
* @param logLevel - The Log level (default to 'warn')
|
|
17
|
+
* @param prefix - A Log message prefix (default to 'scaleway-sdk-js:')
|
|
18
|
+
*
|
|
19
|
+
* @public
|
|
20
|
+
*/
|
|
6
21
|
const enableConsoleLogger = (logLevel = "warn", prefix = "scaleway-sdk-js:") => setLogger(new ConsoleLogger(logLevel, prefix));
|
|
22
|
+
/**
|
|
23
|
+
* Returns the active SDK logger.
|
|
24
|
+
*
|
|
25
|
+
* @internal
|
|
26
|
+
*/
|
|
7
27
|
const getLogger = () => sdkLogger;
|
|
8
|
-
export {
|
|
9
|
-
enableConsoleLogger,
|
|
10
|
-
getLogger,
|
|
11
|
-
setLogger
|
|
12
|
-
};
|
|
28
|
+
export { enableConsoleLogger, getLogger, setLogger };
|
|
@@ -1,13 +1,10 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
}
|
|
1
|
+
let LevelResolver = /* @__PURE__ */ function(LevelResolver) {
|
|
2
|
+
LevelResolver[LevelResolver["silent"] = 0] = "silent";
|
|
3
|
+
LevelResolver[LevelResolver["error"] = 1] = "error";
|
|
4
|
+
LevelResolver[LevelResolver["warn"] = 2] = "warn";
|
|
5
|
+
LevelResolver[LevelResolver["info"] = 3] = "info";
|
|
6
|
+
LevelResolver[LevelResolver["debug"] = 4] = "debug";
|
|
7
|
+
return LevelResolver;
|
|
8
|
+
}({});
|
|
9
9
|
const shouldLog = (currentLevel, level) => LevelResolver[level] <= currentLevel;
|
|
10
|
-
export {
|
|
11
|
-
LevelResolver,
|
|
12
|
-
shouldLog
|
|
13
|
-
};
|
|
10
|
+
export { LevelResolver, shouldLog };
|
|
@@ -1,30 +1,29 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
1
|
+
var isAccessKeyRegex = /^SCW[A-Z0-9]{17}$/i;
|
|
2
|
+
var isRegionRegex = /^[a-z]{2}-[a-z]{3}$/i;
|
|
3
|
+
var isUUIDRegex = /^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}$/i;
|
|
4
|
+
var isZoneRegex = /^[a-z]{2}-[a-z]{3}-[1-9]$/i;
|
|
5
|
+
/** Returns true if the given string has a valid UUID format. */
|
|
5
6
|
const isUUID = (str) => isUUIDRegex.test(str);
|
|
7
|
+
/** Returns true if the given string has a valid Scaleway access key format. */
|
|
6
8
|
const isAccessKey = (str) => isAccessKeyRegex.test(str);
|
|
9
|
+
/** Returns true if the given string has a valid Scaleway secret key format. */
|
|
7
10
|
const isSecretKey = (str) => isUUID(str);
|
|
11
|
+
/** Returns true if the given string has a valid Scaleway organization ID format. */
|
|
8
12
|
const isOrganizationId = (str) => isUUID(str);
|
|
13
|
+
/** Returns true if the given string has a valid Scaleway project ID format. */
|
|
9
14
|
const isProjectId = (str) => isUUID(str);
|
|
15
|
+
/** Returns true if the given string has a valid region format. */
|
|
10
16
|
const isRegion = (str) => isRegionRegex.test(str);
|
|
17
|
+
/** Returns true if the given string has a valid zone format. */
|
|
11
18
|
const isZone = (str) => isZoneRegex.test(str);
|
|
19
|
+
/** Returns true if the given string has a valid URL format and starts by `http(s):`. */
|
|
12
20
|
const isURL = (str) => {
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
};
|
|
21
|
-
export {
|
|
22
|
-
isAccessKey,
|
|
23
|
-
isOrganizationId,
|
|
24
|
-
isProjectId,
|
|
25
|
-
isRegion,
|
|
26
|
-
isSecretKey,
|
|
27
|
-
isURL,
|
|
28
|
-
isUUID,
|
|
29
|
-
isZone
|
|
21
|
+
let url;
|
|
22
|
+
try {
|
|
23
|
+
url = new URL(str);
|
|
24
|
+
} catch {
|
|
25
|
+
return false;
|
|
26
|
+
}
|
|
27
|
+
return url.protocol === "http:" || url.protocol === "https:";
|
|
30
28
|
};
|
|
29
|
+
export { isAccessKey, isOrganizationId, isProjectId, isRegion, isSecretKey, isURL, isUUID, isZone };
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import "./helpers/json.js";
|
|
2
|
+
import "./helpers/marshalling.js";
|
|
3
|
+
import "./internal/async/interval-retrier.js";
|
|
4
|
+
import "./internal/interceptors/helpers.js";
|
|
5
|
+
import "./scw/api.js";
|
|
6
|
+
import "./scw/auth.js";
|
|
7
|
+
import "./scw/custom-marshalling.js";
|
|
8
|
+
import "./scw/fetch/resource-paginator.js";
|
package/dist/package.js
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
var package_default = {
|
|
2
|
+
name: "@scaleway/sdk-client",
|
|
3
|
+
version: "2.1.0",
|
|
4
|
+
license: "Apache-2.0",
|
|
5
|
+
description: "Scaleway SDK Client",
|
|
6
|
+
keywords: [
|
|
7
|
+
"scaleway",
|
|
8
|
+
"cloud",
|
|
9
|
+
"sdk",
|
|
10
|
+
"client"
|
|
11
|
+
],
|
|
12
|
+
scripts: {
|
|
13
|
+
"typecheck": "tsc --noEmit",
|
|
14
|
+
"type:generate": "tsc --declaration -p tsconfig.build.json",
|
|
15
|
+
"build": "vite build --config ../../vite.config.ts && pnpm run type:generate && tsc-alias -p tsconfig.build.json",
|
|
16
|
+
"build:profile": "npx vite-bundle-visualizer -c ../../vite.config.ts"
|
|
17
|
+
},
|
|
18
|
+
files: ["dist"],
|
|
19
|
+
publishConfig: { "access": "public" },
|
|
20
|
+
repository: {
|
|
21
|
+
"type": "git",
|
|
22
|
+
"url": "git+https://github.com/scaleway/scaleway-sdk-js",
|
|
23
|
+
"directory": "packages/client"
|
|
24
|
+
},
|
|
25
|
+
engines: { "node": ">=20.19.6" },
|
|
26
|
+
type: "module",
|
|
27
|
+
exports: { ".": {
|
|
28
|
+
"types": "./dist/index.d.ts",
|
|
29
|
+
"default": "./dist/index.js"
|
|
30
|
+
} }
|
|
31
|
+
};
|
|
32
|
+
export { package_default as default };
|
package/dist/scw/api.js
CHANGED
|
@@ -1,8 +1,11 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
1
|
+
/**
|
|
2
|
+
* Abstract class to instantiate API from a {@link Client}.
|
|
3
|
+
*
|
|
4
|
+
* @internal
|
|
5
|
+
*/
|
|
6
|
+
var API = class {
|
|
7
|
+
constructor(client) {
|
|
8
|
+
this.client = client;
|
|
9
|
+
}
|
|
8
10
|
};
|
|
11
|
+
export { API };
|
package/dist/scw/auth.d.ts
CHANGED
|
@@ -1,3 +1,10 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Authentication utilities for Scaleway SDK.
|
|
3
|
+
*
|
|
4
|
+
* Note: This file uses interface call signatures instead of shorthand function types
|
|
5
|
+
* to maintain clear, documented public API signatures that are more readable in IDE
|
|
6
|
+
* tooltips and generated documentation.
|
|
7
|
+
*/
|
|
1
8
|
import type { RequestInterceptor } from '../internal/interceptors/types.js';
|
|
2
9
|
import type { AuthenticationSecrets } from './client-ini-profile.js';
|
|
3
10
|
interface TokenAccessor {
|
package/dist/scw/auth.js
CHANGED
|
@@ -1,25 +1,68 @@
|
|
|
1
1
|
import { addAsyncHeaderInterceptor, addHeaderInterceptor } from "../internal/interceptors/helpers.js";
|
|
2
2
|
import { assertValidAuthenticationSecrets } from "./client-ini-profile.js";
|
|
3
|
-
import {
|
|
3
|
+
import { AUTH_HEADER_KEY, SESSION_HEADER_KEY } from "./constants.js";
|
|
4
|
+
/**
|
|
5
|
+
* Authentication utilities for Scaleway SDK.
|
|
6
|
+
*
|
|
7
|
+
* Note: This file uses interface call signatures instead of shorthand function types
|
|
8
|
+
* to maintain clear, documented public API signatures that are more readable in IDE
|
|
9
|
+
* tooltips and generated documentation.
|
|
10
|
+
*/
|
|
11
|
+
/**
|
|
12
|
+
* Authenticates with a session token.
|
|
13
|
+
*
|
|
14
|
+
* @param getToken - The token accessor
|
|
15
|
+
* @returns The request interceptor
|
|
16
|
+
*
|
|
17
|
+
* @deprecated Please use addAsyncHeaderInterceptor instead.
|
|
18
|
+
*
|
|
19
|
+
* @internal
|
|
20
|
+
*/
|
|
4
21
|
const authenticateWithSessionToken = (getToken) => addAsyncHeaderInterceptor(SESSION_HEADER_KEY, getToken);
|
|
22
|
+
/**
|
|
23
|
+
* Authenticates with a secrets.
|
|
24
|
+
*
|
|
25
|
+
* @param getToken - The secrets
|
|
26
|
+
* @returns The request interceptor
|
|
27
|
+
*
|
|
28
|
+
* @throws Error
|
|
29
|
+
* Thrown if the secrets are invalid.
|
|
30
|
+
*
|
|
31
|
+
* @internal
|
|
32
|
+
*/
|
|
5
33
|
const authenticateWithSecrets = (secrets) => {
|
|
6
|
-
|
|
7
|
-
|
|
34
|
+
assertValidAuthenticationSecrets(secrets);
|
|
35
|
+
return addHeaderInterceptor(AUTH_HEADER_KEY, secrets.secretKey);
|
|
8
36
|
};
|
|
37
|
+
/**
|
|
38
|
+
* Obfuscates a token.
|
|
39
|
+
*
|
|
40
|
+
* @param key - The token
|
|
41
|
+
* @returns The obfuscated token
|
|
42
|
+
*
|
|
43
|
+
* @internal
|
|
44
|
+
*/
|
|
9
45
|
const obfuscateToken = (key) => `${key.substring(0, 5)}xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx`;
|
|
46
|
+
/**
|
|
47
|
+
* Obfuscates an UUID.
|
|
48
|
+
*
|
|
49
|
+
* @param key - The UUID
|
|
50
|
+
* @returns The obfuscated UUID
|
|
51
|
+
*
|
|
52
|
+
* @internal
|
|
53
|
+
*/
|
|
10
54
|
const obfuscateUUID = (key) => `${key.substring(0, 8)}-xxxx-xxxx-xxxx-xxxxxxxxxxxx`;
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
obfuscateToken,
|
|
24
|
-
obfuscateUUID
|
|
55
|
+
/**
|
|
56
|
+
* Obfuscates headers entry.
|
|
57
|
+
*
|
|
58
|
+
* @param array - The header entry
|
|
59
|
+
* @returns The obfuscated entry
|
|
60
|
+
*
|
|
61
|
+
* @internal
|
|
62
|
+
*/
|
|
63
|
+
const obfuscateAuthHeadersEntry = ([name, value]) => {
|
|
64
|
+
if (name === "x-session-token") return [name, obfuscateToken(value)];
|
|
65
|
+
if (name === "x-auth-token") return [name, obfuscateUUID(value)];
|
|
66
|
+
return [name, value];
|
|
25
67
|
};
|
|
68
|
+
export { authenticateWithSecrets, authenticateWithSessionToken, obfuscateAuthHeadersEntry };
|
|
@@ -1,66 +1,136 @@
|
|
|
1
|
-
import { authenticateWithSecrets } from "./auth.js";
|
|
2
1
|
import { hasAuthenticationSecrets } from "./client-ini-profile.js";
|
|
2
|
+
import { authenticateWithSecrets } from "./auth.js";
|
|
3
|
+
/**
|
|
4
|
+
* Instantiates the SDK from a configuration {@link Profile}.
|
|
5
|
+
*
|
|
6
|
+
* @param profile - The profile
|
|
7
|
+
* @returns A factory {@link ClientConfig}
|
|
8
|
+
*
|
|
9
|
+
* @remarks This method should be used in conjunction with the initializer `createAdvancedClient`.
|
|
10
|
+
*
|
|
11
|
+
* @public
|
|
12
|
+
*/
|
|
3
13
|
const withProfile = (profile) => (settings) => {
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
newSettings.defaultProjectId = profile.defaultProjectId;
|
|
13
|
-
}
|
|
14
|
-
if (profile.defaultRegion) {
|
|
15
|
-
newSettings.defaultRegion = profile.defaultRegion;
|
|
16
|
-
}
|
|
17
|
-
if (profile.defaultZone) {
|
|
18
|
-
newSettings.defaultZone = profile.defaultZone;
|
|
19
|
-
}
|
|
20
|
-
if (hasAuthenticationSecrets(profile)) {
|
|
21
|
-
newSettings.interceptors = [
|
|
22
|
-
{
|
|
23
|
-
request: authenticateWithSecrets(profile)
|
|
24
|
-
},
|
|
25
|
-
...newSettings.interceptors
|
|
26
|
-
];
|
|
27
|
-
}
|
|
28
|
-
return newSettings;
|
|
14
|
+
const newSettings = { ...settings };
|
|
15
|
+
if (profile.apiURL) newSettings.apiURL = profile.apiURL;
|
|
16
|
+
if (profile.defaultOrganizationId) newSettings.defaultOrganizationId = profile.defaultOrganizationId;
|
|
17
|
+
if (profile.defaultProjectId) newSettings.defaultProjectId = profile.defaultProjectId;
|
|
18
|
+
if (profile.defaultRegion) newSettings.defaultRegion = profile.defaultRegion;
|
|
19
|
+
if (profile.defaultZone) newSettings.defaultZone = profile.defaultZone;
|
|
20
|
+
if (hasAuthenticationSecrets(profile)) newSettings.interceptors = [{ request: authenticateWithSecrets(profile) }, ...newSettings.interceptors];
|
|
21
|
+
return newSettings;
|
|
29
22
|
};
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
23
|
+
/**
|
|
24
|
+
* Instantiates the SDK with a different HTTP client.
|
|
25
|
+
*
|
|
26
|
+
* @param httpClient - A fetch compatible HTTP client
|
|
27
|
+
* @returns A factory {@link ClientConfig}
|
|
28
|
+
*
|
|
29
|
+
* @remarks This method should be used in conjunction with the initializer `createAdvancedClient`.
|
|
30
|
+
*
|
|
31
|
+
* @public
|
|
32
|
+
*/
|
|
33
|
+
const withHTTPClient = (httpClient) => (settings) => ({
|
|
34
|
+
...settings,
|
|
35
|
+
httpClient
|
|
36
|
+
});
|
|
37
|
+
/**
|
|
38
|
+
* Instantiates the SDK with a default page size.
|
|
39
|
+
*
|
|
40
|
+
* @param defaultPageSize - The default page size
|
|
41
|
+
* @returns A factory {@link ClientConfig}
|
|
42
|
+
*
|
|
43
|
+
* @remarks This method should be used in conjunction with the initializer `createAdvancedClient`.
|
|
44
|
+
*
|
|
45
|
+
* @public
|
|
46
|
+
*/
|
|
47
|
+
const withDefaultPageSize = (defaultPageSize) => (settings) => ({
|
|
48
|
+
...settings,
|
|
49
|
+
defaultPageSize
|
|
50
|
+
});
|
|
51
|
+
/**
|
|
52
|
+
* Instantiates the SDK with a different default user agent.
|
|
53
|
+
*
|
|
54
|
+
* @param userAgent - The default user agent
|
|
55
|
+
* @returns A factory {@link ClientConfig}
|
|
56
|
+
*
|
|
57
|
+
* @remarks This method should be used in conjunction with the initializer `createAdvancedClient`.
|
|
58
|
+
*
|
|
59
|
+
* @public
|
|
60
|
+
*/
|
|
61
|
+
const withUserAgent = (userAgent) => (settings) => ({
|
|
62
|
+
...settings,
|
|
63
|
+
userAgent
|
|
64
|
+
});
|
|
65
|
+
/**
|
|
66
|
+
* Instantiates the SDK with an additional user agent.
|
|
67
|
+
*
|
|
68
|
+
* @param userAgent - The suffix to append to default user agent
|
|
69
|
+
* @returns A factory {@link ClientConfig}
|
|
70
|
+
*
|
|
71
|
+
* @remarks This method should be used in conjunction with the initializer `createAdvancedClient`.
|
|
72
|
+
*
|
|
73
|
+
* @public
|
|
74
|
+
*/
|
|
33
75
|
const withUserAgentSuffix = (userAgent) => (settings) => ({
|
|
34
|
-
|
|
35
|
-
|
|
76
|
+
...settings,
|
|
77
|
+
userAgent: settings.userAgent ? `${settings.userAgent} ${userAgent}` : userAgent
|
|
36
78
|
});
|
|
79
|
+
/**
|
|
80
|
+
* Instantiates the SDK with additional interceptors.
|
|
81
|
+
*
|
|
82
|
+
* @param interceptors - The additional {@link NetworkInterceptors} interceptors
|
|
83
|
+
* @returns A factory {@link ClientConfig}
|
|
84
|
+
*
|
|
85
|
+
* @remarks
|
|
86
|
+
* It doesn't override the existing interceptors, but instead push more to the list.
|
|
87
|
+
* This method should be used in conjunction with the initializer `createAdvancedClient`.
|
|
88
|
+
*
|
|
89
|
+
* @example
|
|
90
|
+
* ```
|
|
91
|
+
* withAdditionalInterceptors([
|
|
92
|
+
* {
|
|
93
|
+
* request: ({ request }) => {
|
|
94
|
+
* console.log(`Do something with ${JSON.stringify(request)}`)
|
|
95
|
+
* return request
|
|
96
|
+
* },
|
|
97
|
+
* response: ({ response }) => {
|
|
98
|
+
* console.log(`Do something with ${JSON.stringify(response)}`)
|
|
99
|
+
* return response
|
|
100
|
+
* },
|
|
101
|
+
* responseError: async ({
|
|
102
|
+
* request,
|
|
103
|
+
* error,
|
|
104
|
+
* }: {
|
|
105
|
+
* request: Request
|
|
106
|
+
* error: unknown
|
|
107
|
+
* }) => {
|
|
108
|
+
* console.log(
|
|
109
|
+
* `Do something with ${JSON.stringify(request)} and ${JSON.stringify(
|
|
110
|
+
* error,
|
|
111
|
+
* )}`,
|
|
112
|
+
* )
|
|
113
|
+
* throw error // or return Promise.resolve(someData)
|
|
114
|
+
* },
|
|
115
|
+
* },
|
|
116
|
+
* ])
|
|
117
|
+
* ```
|
|
118
|
+
*
|
|
119
|
+
* @public
|
|
120
|
+
*/
|
|
37
121
|
const withAdditionalInterceptors = (interceptors) => (settings) => ({
|
|
38
|
-
|
|
39
|
-
|
|
122
|
+
...settings,
|
|
123
|
+
interceptors: settings.interceptors.concat(interceptors)
|
|
40
124
|
});
|
|
125
|
+
/**
|
|
126
|
+
* Instantiates the SDK with legacy interceptors.
|
|
127
|
+
*/
|
|
41
128
|
const withLegacyInterceptors = () => (settings) => {
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
})),
|
|
49
|
-
(settings.responseInterceptors ?? []).map((obj) => ({
|
|
50
|
-
response: obj
|
|
51
|
-
}))
|
|
52
|
-
);
|
|
53
|
-
return {
|
|
54
|
-
...settings,
|
|
55
|
-
interceptors: allInterceptors
|
|
56
|
-
};
|
|
57
|
-
};
|
|
58
|
-
export {
|
|
59
|
-
withAdditionalInterceptors,
|
|
60
|
-
withDefaultPageSize,
|
|
61
|
-
withHTTPClient,
|
|
62
|
-
withLegacyInterceptors,
|
|
63
|
-
withProfile,
|
|
64
|
-
withUserAgent,
|
|
65
|
-
withUserAgentSuffix
|
|
129
|
+
if (!settings.requestInterceptors && !settings.responseInterceptors) return settings;
|
|
130
|
+
const allInterceptors = settings.interceptors.concat((settings.requestInterceptors ?? []).map((obj) => ({ request: obj })), (settings.responseInterceptors ?? []).map((obj) => ({ response: obj })));
|
|
131
|
+
return {
|
|
132
|
+
...settings,
|
|
133
|
+
interceptors: allInterceptors
|
|
134
|
+
};
|
|
66
135
|
};
|
|
136
|
+
export { withAdditionalInterceptors, withDefaultPageSize, withHTTPClient, withLegacyInterceptors, withProfile, withUserAgent, withUserAgentSuffix };
|
|
@@ -1,23 +1,27 @@
|
|
|
1
1
|
import { isAccessKey, isSecretKey } from "../internal/validations/string-validation.js";
|
|
2
|
+
/**
|
|
3
|
+
* Verifies that the payload contains both the accessKey and the secretKey.
|
|
4
|
+
*
|
|
5
|
+
* @param obj - The secrets
|
|
6
|
+
* @returns Whether the secrets are not empty.
|
|
7
|
+
*
|
|
8
|
+
* @internal
|
|
9
|
+
*/
|
|
2
10
|
const hasAuthenticationSecrets = (obj) => typeof obj.accessKey === "string" && obj.accessKey !== "" && typeof obj.secretKey === "string" && obj.secretKey !== "";
|
|
11
|
+
/**
|
|
12
|
+
* Asserts the format of secrets.
|
|
13
|
+
*
|
|
14
|
+
* @param obj - The secrets
|
|
15
|
+
* @returns Whether the secrets use a valid format
|
|
16
|
+
*
|
|
17
|
+
* @throws Error
|
|
18
|
+
* Thrown if either the accessKey or the secretKey has en invalid format.
|
|
19
|
+
*
|
|
20
|
+
* @internal
|
|
21
|
+
*/
|
|
3
22
|
function assertValidAuthenticationSecrets(obj) {
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
);
|
|
8
|
-
}
|
|
9
|
-
if (!isAccessKey(obj.accessKey)) {
|
|
10
|
-
throw new Error(
|
|
11
|
-
`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/`
|
|
12
|
-
);
|
|
13
|
-
}
|
|
14
|
-
if (!isSecretKey(obj.secretKey)) {
|
|
15
|
-
throw new Error(
|
|
16
|
-
`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/`
|
|
17
|
-
);
|
|
18
|
-
}
|
|
23
|
+
if (!(obj.accessKey && obj.secretKey)) 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/`);
|
|
24
|
+
if (!isAccessKey(obj.accessKey)) 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/`);
|
|
25
|
+
if (!isSecretKey(obj.secretKey)) 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/`);
|
|
19
26
|
}
|
|
20
|
-
export {
|
|
21
|
-
assertValidAuthenticationSecrets,
|
|
22
|
-
hasAuthenticationSecrets
|
|
23
|
-
};
|
|
27
|
+
export { assertValidAuthenticationSecrets, hasAuthenticationSecrets };
|
|
@@ -1,51 +1,27 @@
|
|
|
1
|
-
import { isOrganizationId, isProjectId, isRegion,
|
|
1
|
+
import { isOrganizationId, isProjectId, isRegion, isURL, isZone } from "../internal/validations/string-validation.js";
|
|
2
|
+
/**
|
|
3
|
+
* Validates the content of a {@link Settings} object.
|
|
4
|
+
*
|
|
5
|
+
* @throws Error
|
|
6
|
+
* Thrown if {@link Settings} aren't valid.
|
|
7
|
+
*
|
|
8
|
+
* @internal
|
|
9
|
+
*/
|
|
2
10
|
const assertValidSettings = (obj) => {
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
throw new Error(
|
|
19
|
-
`Invalid project ID format '${obj.defaultProjectId}', expected a UUID: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx`
|
|
20
|
-
);
|
|
21
|
-
}
|
|
22
|
-
}
|
|
23
|
-
if (obj.defaultRegion && !isRegion(obj.defaultRegion)) {
|
|
24
|
-
throw new Error(`Invalid default region format '${obj.defaultRegion}'`);
|
|
25
|
-
}
|
|
26
|
-
if (obj.defaultZone && !isZone(obj.defaultZone)) {
|
|
27
|
-
throw new Error(`Invalid default zone format '${obj.defaultZone}'`);
|
|
28
|
-
}
|
|
29
|
-
if (obj.apiURL && !isURL(obj.apiURL)) {
|
|
30
|
-
throw new Error(`Invalid URL ${obj.apiURL}`);
|
|
31
|
-
}
|
|
32
|
-
if (obj.apiURL?.endsWith("/")) {
|
|
33
|
-
throw new Error(
|
|
34
|
-
`Invalid URL ${obj.apiURL}: it should not have a trailing slash`
|
|
35
|
-
);
|
|
36
|
-
}
|
|
37
|
-
if (typeof obj.httpClient !== typeof fetch) {
|
|
38
|
-
throw new Error(`Invalid HTTP Client`);
|
|
39
|
-
}
|
|
40
|
-
if (obj.defaultPageSize !== void 0 && (typeof obj.defaultPageSize !== "number" || Number.isNaN(obj.defaultPageSize) || obj.defaultPageSize <= 0)) {
|
|
41
|
-
throw new Error(
|
|
42
|
-
`Invalid defaultPageSize ${obj.defaultPageSize}: it should be a number above 0`
|
|
43
|
-
);
|
|
44
|
-
}
|
|
45
|
-
if (typeof obj.userAgent !== "string") {
|
|
46
|
-
throw new Error(`Invalid User-Agent`);
|
|
47
|
-
}
|
|
48
|
-
};
|
|
49
|
-
export {
|
|
50
|
-
assertValidSettings
|
|
11
|
+
if (obj.defaultOrganizationId !== void 0) {
|
|
12
|
+
if (typeof obj.defaultOrganizationId !== "string" || obj.defaultOrganizationId.length === 0) throw new Error("Default organization ID cannot be empty");
|
|
13
|
+
if (!isOrganizationId(obj.defaultOrganizationId)) throw new Error(`Invalid organization ID format '${obj.defaultOrganizationId}', expected a UUID: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx`);
|
|
14
|
+
}
|
|
15
|
+
if (obj.defaultProjectId !== void 0) {
|
|
16
|
+
if (typeof obj.defaultProjectId !== "string" || obj.defaultProjectId.length === 0) throw new Error("Default project ID cannot be empty");
|
|
17
|
+
if (!isProjectId(obj.defaultProjectId)) throw new Error(`Invalid project ID format '${obj.defaultProjectId}', expected a UUID: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx`);
|
|
18
|
+
}
|
|
19
|
+
if (obj.defaultRegion && !isRegion(obj.defaultRegion)) throw new Error(`Invalid default region format '${obj.defaultRegion}'`);
|
|
20
|
+
if (obj.defaultZone && !isZone(obj.defaultZone)) throw new Error(`Invalid default zone format '${obj.defaultZone}'`);
|
|
21
|
+
if (obj.apiURL && !isURL(obj.apiURL)) throw new Error(`Invalid URL ${obj.apiURL}`);
|
|
22
|
+
if (obj.apiURL?.endsWith("/")) throw new Error(`Invalid URL ${obj.apiURL}: it should not have a trailing slash`);
|
|
23
|
+
if (typeof obj.httpClient !== typeof fetch) throw new Error(`Invalid HTTP Client`);
|
|
24
|
+
if (obj.defaultPageSize !== void 0 && (typeof obj.defaultPageSize !== "number" || Number.isNaN(obj.defaultPageSize) || obj.defaultPageSize <= 0)) throw new Error(`Invalid defaultPageSize ${obj.defaultPageSize}: it should be a number above 0`);
|
|
25
|
+
if (typeof obj.userAgent !== "string") throw new Error(`Invalid User-Agent`);
|
|
51
26
|
};
|
|
27
|
+
export { assertValidSettings };
|