@tsg-dsp/tsg-sdk 0.0.0-stage → 0.21.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/LICENSE +201 -0
- package/README.md +362 -2
- package/dist/.generated/control-plane.d.ts +7825 -0
- package/dist/.generated/sso-bridge.d.ts +3999 -0
- package/dist/.generated/wallet.d.ts +6248 -0
- package/dist/auth/auth.provider.d.ts +49 -0
- package/dist/auth/auth.provider.d.ts.map +1 -0
- package/dist/auth/auth.provider.js +166 -0
- package/dist/auth/auth.provider.js.map +1 -0
- package/dist/auth/auth.types.d.ts +27 -0
- package/dist/auth/auth.types.d.ts.map +1 -0
- package/dist/auth/auth.types.js +2 -0
- package/dist/auth/auth.types.js.map +1 -0
- package/dist/auth/index.d.ts +3 -0
- package/dist/auth/index.d.ts.map +1 -0
- package/dist/auth/index.js +3 -0
- package/dist/auth/index.js.map +1 -0
- package/dist/browser.d.ts +1245 -0
- package/dist/browser.d.ts.map +1 -0
- package/dist/browser.js +228 -0
- package/dist/browser.js.map +1 -0
- package/dist/catalog/catalog.sdk.d.ts +149 -0
- package/dist/catalog/catalog.sdk.d.ts.map +1 -0
- package/dist/catalog/catalog.sdk.js +155 -0
- package/dist/catalog/catalog.sdk.js.map +1 -0
- package/dist/catalog/index.d.ts +2 -0
- package/dist/catalog/index.d.ts.map +1 -0
- package/dist/catalog/index.js +2 -0
- package/dist/catalog/index.js.map +1 -0
- package/dist/client.d.ts +43 -0
- package/dist/client.d.ts.map +1 -0
- package/dist/client.js +109 -0
- package/dist/client.js.map +1 -0
- package/dist/config/index.d.ts +2 -0
- package/dist/config/index.d.ts.map +1 -0
- package/dist/config/index.js +2 -0
- package/dist/config/index.js.map +1 -0
- package/dist/config/sdk.config.d.ts +97 -0
- package/dist/config/sdk.config.d.ts.map +1 -0
- package/dist/config/sdk.config.js +2 -0
- package/dist/config/sdk.config.js.map +1 -0
- package/dist/index.d.ts +18 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +25 -0
- package/dist/index.js.map +1 -0
- package/dist/metadata/index.d.ts +2 -0
- package/dist/metadata/index.d.ts.map +1 -0
- package/dist/metadata/index.js +2 -0
- package/dist/metadata/index.js.map +1 -0
- package/dist/metadata/metadata.sdk.d.ts +114 -0
- package/dist/metadata/metadata.sdk.d.ts.map +1 -0
- package/dist/metadata/metadata.sdk.js +145 -0
- package/dist/metadata/metadata.sdk.js.map +1 -0
- package/dist/negotiations/index.d.ts +2 -0
- package/dist/negotiations/index.d.ts.map +1 -0
- package/dist/negotiations/index.js +2 -0
- package/dist/negotiations/index.js.map +1 -0
- package/dist/negotiations/negotiations.sdk.d.ts +113 -0
- package/dist/negotiations/negotiations.sdk.d.ts.map +1 -0
- package/dist/negotiations/negotiations.sdk.js +146 -0
- package/dist/negotiations/negotiations.sdk.js.map +1 -0
- package/dist/requirements/index.d.ts +2 -0
- package/dist/requirements/index.d.ts.map +1 -0
- package/dist/requirements/index.js +2 -0
- package/dist/requirements/index.js.map +1 -0
- package/dist/requirements/requirements.sdk.d.ts +52 -0
- package/dist/requirements/requirements.sdk.d.ts.map +1 -0
- package/dist/requirements/requirements.sdk.js +123 -0
- package/dist/requirements/requirements.sdk.js.map +1 -0
- package/dist/sso/index.d.ts +3 -0
- package/dist/sso/index.d.ts.map +1 -0
- package/dist/sso/index.js +3 -0
- package/dist/sso/index.js.map +1 -0
- package/dist/sso/sso-management.sdk.d.ts +96 -0
- package/dist/sso/sso-management.sdk.d.ts.map +1 -0
- package/dist/sso/sso-management.sdk.js +152 -0
- package/dist/sso/sso-management.sdk.js.map +1 -0
- package/dist/sso/sso.sdk.d.ts +75 -0
- package/dist/sso/sso.sdk.d.ts.map +1 -0
- package/dist/sso/sso.sdk.js +149 -0
- package/dist/sso/sso.sdk.js.map +1 -0
- package/dist/tenants/tenant-management.sdk.d.ts +48 -0
- package/dist/tenants/tenant-management.sdk.d.ts.map +1 -0
- package/dist/tenants/tenant-management.sdk.js +107 -0
- package/dist/tenants/tenant-management.sdk.js.map +1 -0
- package/dist/transfers/index.d.ts +2 -0
- package/dist/transfers/index.d.ts.map +1 -0
- package/dist/transfers/index.js +2 -0
- package/dist/transfers/index.js.map +1 -0
- package/dist/transfers/transfers.sdk.d.ts +174 -0
- package/dist/transfers/transfers.sdk.d.ts.map +1 -0
- package/dist/transfers/transfers.sdk.js +200 -0
- package/dist/transfers/transfers.sdk.js.map +1 -0
- package/dist/tsg-sdk.d.ts +68 -0
- package/dist/tsg-sdk.d.ts.map +1 -0
- package/dist/tsg-sdk.js +145 -0
- package/dist/tsg-sdk.js.map +1 -0
- package/dist/utils/errors.d.ts +67 -0
- package/dist/utils/errors.d.ts.map +1 -0
- package/dist/utils/errors.js +79 -0
- package/dist/utils/errors.js.map +1 -0
- package/dist/utils/index.d.ts +5 -0
- package/dist/utils/index.d.ts.map +1 -0
- package/dist/utils/index.js +5 -0
- package/dist/utils/index.js.map +1 -0
- package/dist/utils/polling.d.ts +20 -0
- package/dist/utils/polling.d.ts.map +1 -0
- package/dist/utils/polling.js +35 -0
- package/dist/utils/polling.js.map +1 -0
- package/dist/utils/reason.d.ts +16 -0
- package/dist/utils/reason.d.ts.map +1 -0
- package/dist/utils/reason.js +34 -0
- package/dist/utils/reason.js.map +1 -0
- package/dist/utils/shape-validation.d.ts +59 -0
- package/dist/utils/shape-validation.d.ts.map +1 -0
- package/dist/utils/shape-validation.js +85 -0
- package/dist/utils/shape-validation.js.map +1 -0
- package/dist/utils/validate.d.ts +47 -0
- package/dist/utils/validate.d.ts.map +1 -0
- package/dist/utils/validate.js +68 -0
- package/dist/utils/validate.js.map +1 -0
- package/dist/vendor/common-dtos/abac.policy.d.ts +69 -0
- package/dist/vendor/common-dtos/abac.policy.js +5 -0
- package/dist/vendor/common-dtos/audit.types.d.ts +74 -0
- package/dist/vendor/common-dtos/audit.types.js +8 -0
- package/dist/vendor/common-dtos/credential-requirements.d.ts +18 -0
- package/dist/vendor/common-dtos/credential-requirements.dto.d.ts +46 -0
- package/dist/vendor/common-dtos/credential-requirements.dto.js +250 -0
- package/dist/vendor/common-dtos/credential-requirements.js +180 -0
- package/dist/vendor/common-dtos/dataplane.dto.d.ts +5 -0
- package/dist/vendor/common-dtos/dataplane.dto.js +28 -0
- package/dist/vendor/common-dtos/dcp.dto.d.ts +89 -0
- package/dist/vendor/common-dtos/dcp.dto.js +489 -0
- package/dist/vendor/common-dtos/dcql.dto.d.ts +88 -0
- package/dist/vendor/common-dtos/dcql.dto.js +449 -0
- package/dist/vendor/common-dtos/did.schemas.d.ts +39 -0
- package/dist/vendor/common-dtos/did.schemas.js +259 -0
- package/dist/vendor/common-dtos/index.d.ts +19 -0
- package/dist/vendor/common-dtos/index.js +19 -0
- package/dist/vendor/common-dtos/metadata.dto.d.ts +5 -0
- package/dist/vendor/common-dtos/metadata.dto.js +29 -0
- package/dist/vendor/common-dtos/negotiations.dto.d.ts +28 -0
- package/dist/vendor/common-dtos/negotiations.dto.js +204 -0
- package/dist/vendor/common-dtos/permission-resources.d.ts +3 -0
- package/dist/vendor/common-dtos/permission-resources.js +18 -0
- package/dist/vendor/common-dtos/permissions.d.ts +242 -0
- package/dist/vendor/common-dtos/permissions.js +409 -0
- package/dist/vendor/common-dtos/presentation.request.dto.d.ts +8 -0
- package/dist/vendor/common-dtos/presentation.request.dto.js +54 -0
- package/dist/vendor/common-dtos/presentation.status.dto.d.ts +12 -0
- package/dist/vendor/common-dtos/presentation.status.dto.js +82 -0
- package/dist/vendor/common-dtos/presentationdefinition.dto.d.ts +74 -0
- package/dist/vendor/common-dtos/presentationdefinition.dto.js +447 -0
- package/dist/vendor/common-dtos/signatures.dto.d.ts +31 -0
- package/dist/vendor/common-dtos/signatures.dto.js +209 -0
- package/dist/vendor/common-dtos/transfer.dto.d.ts +16 -0
- package/dist/vendor/common-dtos/transfer.dto.js +101 -0
- package/dist/vendor/common-dtos/utils.swagger.d.ts +12 -0
- package/dist/vendor/common-dtos/utils.swagger.js +62 -0
- package/dist/vendor/control-plane-dtos/index.d.ts +4 -0
- package/dist/vendor/control-plane-dtos/index.js +4 -0
- package/dist/vendor/control-plane-dtos/registry/registry.dto.d.ts +4 -0
- package/dist/vendor/control-plane-dtos/registry/registry.dto.js +25 -0
- package/dist/vendor/control-plane-dtos/registry/registry.search.dto.d.ts +28 -0
- package/dist/vendor/control-plane-dtos/registry/registry.search.dto.js +126 -0
- package/dist/vendor/control-plane-dtos/ssi/verifiablePresentations.dto.d.ts +9 -0
- package/dist/vendor/control-plane-dtos/ssi/verifiablePresentations.dto.js +43 -0
- package/dist/vendor/control-plane-dtos/status/status.dto.d.ts +37 -0
- package/dist/vendor/control-plane-dtos/status/status.dto.js +172 -0
- package/dist/vendor/sso-bridge-dtos/clients.dto.d.ts +25 -0
- package/dist/vendor/sso-bridge-dtos/clients.dto.js +147 -0
- package/dist/vendor/sso-bridge-dtos/grants.d.ts +4 -0
- package/dist/vendor/sso-bridge-dtos/grants.js +7 -0
- package/dist/vendor/sso-bridge-dtos/index.d.ts +5 -0
- package/dist/vendor/sso-bridge-dtos/index.js +5 -0
- package/dist/vendor/sso-bridge-dtos/tenant.dto.d.ts +70 -0
- package/dist/vendor/sso-bridge-dtos/tenant.dto.js +1 -0
- package/dist/vendor/sso-bridge-dtos/users.dto.d.ts +42 -0
- package/dist/vendor/sso-bridge-dtos/users.dto.js +173 -0
- package/dist/vendor/tenant-dtos/authorization.dto.d.ts +46 -0
- package/dist/vendor/tenant-dtos/authorization.dto.js +5 -0
- package/dist/vendor/tenant-dtos/consumer.dto.d.ts +29 -0
- package/dist/vendor/tenant-dtos/consumer.dto.js +1 -0
- package/dist/vendor/tenant-dtos/fixtures.d.ts +15 -0
- package/dist/vendor/tenant-dtos/fixtures.js +48 -0
- package/dist/vendor/tenant-dtos/index.d.ts +8 -0
- package/dist/vendor/tenant-dtos/index.js +8 -0
- package/dist/vendor/tenant-dtos/management.dto.d.ts +137 -0
- package/dist/vendor/tenant-dtos/management.dto.js +1 -0
- package/dist/vendor/tenant-dtos/membership.dto.d.ts +22 -0
- package/dist/vendor/tenant-dtos/membership.dto.js +7 -0
- package/dist/vendor/tenant-dtos/principal.dto.d.ts +13 -0
- package/dist/vendor/tenant-dtos/principal.dto.js +6 -0
- package/dist/vendor/tenant-dtos/provisioning.dto.d.ts +47 -0
- package/dist/vendor/tenant-dtos/provisioning.dto.js +1 -0
- package/dist/vendor/tenant-dtos/service-binding.dto.d.ts +9 -0
- package/dist/vendor/tenant-dtos/service-binding.dto.js +1 -0
- package/dist/vendor/tenant-dtos/tenant.dto.d.ts +15 -0
- package/dist/vendor/tenant-dtos/tenant.dto.js +1 -0
- package/dist/vendor/wallet-dtos/clients.dto.d.ts +21 -0
- package/dist/vendor/wallet-dtos/clients.dto.js +13 -0
- package/dist/vendor/wallet-dtos/credentials.dto.d.ts +31 -0
- package/dist/vendor/wallet-dtos/credentials.dto.js +46 -0
- package/dist/vendor/wallet-dtos/index.d.ts +6 -0
- package/dist/vendor/wallet-dtos/index.js +6 -0
- package/dist/vendor/wallet-dtos/issuance.dto.d.ts +237 -0
- package/dist/vendor/wallet-dtos/issuance.dto.js +1139 -0
- package/dist/vendor/wallet-dtos/scopes.dto.d.ts +24 -0
- package/dist/vendor/wallet-dtos/scopes.dto.js +151 -0
- package/dist/vendor/wallet-dtos/status.dto.d.ts +35 -0
- package/dist/vendor/wallet-dtos/status.dto.js +169 -0
- package/dist/vendor/wallet-dtos/utils/schema-converter.d.ts +13 -0
- package/dist/vendor/wallet-dtos/utils/schema-converter.js +42 -0
- package/dist/wallet/index.d.ts +2 -0
- package/dist/wallet/index.d.ts.map +1 -0
- package/dist/wallet/index.js +2 -0
- package/dist/wallet/index.js.map +1 -0
- package/dist/wallet/wallet.sdk.d.ts +285 -0
- package/dist/wallet/wallet.sdk.d.ts.map +1 -0
- package/dist/wallet/wallet.sdk.js +384 -0
- package/dist/wallet/wallet.sdk.js.map +1 -0
- package/package.json +79 -3
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Structured error class thrown by all SDK operations.
|
|
3
|
+
*
|
|
4
|
+
* Every `SdkError` carries a machine-readable {@link SdkErrorCode} that can be
|
|
5
|
+
* used for programmatic error handling, along with an optional HTTP
|
|
6
|
+
* `statusCode` and the original `cause`.
|
|
7
|
+
* @example
|
|
8
|
+
* ```ts
|
|
9
|
+
* import { SdkError, SdkErrorCode } from "@tsg-dsp/tsg-sdk";
|
|
10
|
+
*
|
|
11
|
+
* try {
|
|
12
|
+
* await sdk.catalog.getDataset(id, audience);
|
|
13
|
+
* } catch (err) {
|
|
14
|
+
* if (err instanceof SdkError && err.code === SdkErrorCode.NOT_FOUND) {
|
|
15
|
+
* console.log("Dataset not found");
|
|
16
|
+
* }
|
|
17
|
+
* }
|
|
18
|
+
* ```
|
|
19
|
+
*/
|
|
20
|
+
export declare class SdkError extends Error {
|
|
21
|
+
/** Machine-readable error code. */
|
|
22
|
+
readonly code: SdkErrorCode;
|
|
23
|
+
/** HTTP status code, when the error originated from an HTTP response. */
|
|
24
|
+
readonly statusCode?: number | undefined;
|
|
25
|
+
/** The underlying error that caused this SDK error. */
|
|
26
|
+
readonly cause?: unknown;
|
|
27
|
+
constructor(message: string,
|
|
28
|
+
/** Machine-readable error code. */
|
|
29
|
+
code: SdkErrorCode,
|
|
30
|
+
/** HTTP status code, when the error originated from an HTTP response. */
|
|
31
|
+
statusCode?: number | undefined,
|
|
32
|
+
/** The underlying error that caused this SDK error. */
|
|
33
|
+
cause?: unknown);
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* Machine-readable error codes used by {@link SdkError}.
|
|
37
|
+
*/
|
|
38
|
+
export declare enum SdkErrorCode {
|
|
39
|
+
/** HTTP request failed */
|
|
40
|
+
REQUEST_FAILED = "REQUEST_FAILED",
|
|
41
|
+
/** Authentication failed (token acquisition, invalid credentials) */
|
|
42
|
+
AUTH_FAILED = "AUTH_FAILED",
|
|
43
|
+
/** Resource not found */
|
|
44
|
+
NOT_FOUND = "NOT_FOUND",
|
|
45
|
+
/** Polling timed out before reaching target state */
|
|
46
|
+
POLLING_TIMEOUT = "POLLING_TIMEOUT",
|
|
47
|
+
/** Invalid SDK configuration */
|
|
48
|
+
INVALID_CONFIG = "INVALID_CONFIG",
|
|
49
|
+
/** Feature not configured (e.g. wallet not configured) */
|
|
50
|
+
NOT_CONFIGURED = "NOT_CONFIGURED",
|
|
51
|
+
/** API response did not pass DTO validation */
|
|
52
|
+
VALIDATION_FAILED = "VALIDATION_FAILED"
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* Create an {@link SdkError} from an HTTP error response.
|
|
56
|
+
*
|
|
57
|
+
* Maps HTTP status codes to appropriate {@link SdkErrorCode} values:
|
|
58
|
+
* - `404` → {@link SdkErrorCode.NOT_FOUND}
|
|
59
|
+
* - `401` / `403` → {@link SdkErrorCode.AUTH_FAILED}
|
|
60
|
+
* - All others → {@link SdkErrorCode.REQUEST_FAILED}
|
|
61
|
+
* @param message - Human-readable error message.
|
|
62
|
+
* @param statusCode - The HTTP status code.
|
|
63
|
+
* @param cause - Optional underlying error.
|
|
64
|
+
* @returns A new `SdkError` instance.
|
|
65
|
+
*/
|
|
66
|
+
export declare function createRequestError(message: string, statusCode: number, cause?: unknown): SdkError;
|
|
67
|
+
//# sourceMappingURL=errors.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"errors.d.ts","sourceRoot":"","sources":["../../src/utils/errors.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;GAkBG;AACH,qBAAa,QAAS,SAAQ,KAAK;IAG/B,mCAAmC;aACnB,IAAI,EAAE,YAAY;IAClC,yEAAyE;aACzD,UAAU,CAAC,EAAE,MAAM;IACnC,uDAAuD;aACvC,KAAK,CAAC,EAAE,OAAO;IAPjC,YACE,OAAO,EAAE,MAAM;IACf,mCAAmC;IACnB,IAAI,EAAE,YAAY;IAClC,yEAAyE;IACzD,UAAU,CAAC,EAAE,MAAM,YAAA;IACnC,uDAAuD;IACvC,KAAK,CAAC,EAAE,OAAO,EAIhC;CACF;AAED;;GAEG;AACH,oBAAY,YAAY;IACtB,0BAA0B;IAC1B,cAAc,mBAAmB;IACjC,qEAAqE;IACrE,WAAW,gBAAgB;IAC3B,yBAAyB;IACzB,SAAS,cAAc;IACvB,qDAAqD;IACrD,eAAe,oBAAoB;IACnC,gCAAgC;IAChC,cAAc,mBAAmB;IACjC,0DAA0D;IAC1D,cAAc,mBAAmB;IACjC,+CAA+C;IAC/C,iBAAiB,sBAAsB;CACxC;AAED;;;;;;;;;;;GAWG;AACH,wBAAgB,kBAAkB,CAChC,OAAO,EAAE,MAAM,EACf,UAAU,EAAE,MAAM,EAClB,KAAK,CAAC,EAAE,OAAO,GACd,QAAQ,CAQV"}
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Structured error class thrown by all SDK operations.
|
|
3
|
+
*
|
|
4
|
+
* Every `SdkError` carries a machine-readable {@link SdkErrorCode} that can be
|
|
5
|
+
* used for programmatic error handling, along with an optional HTTP
|
|
6
|
+
* `statusCode` and the original `cause`.
|
|
7
|
+
* @example
|
|
8
|
+
* ```ts
|
|
9
|
+
* import { SdkError, SdkErrorCode } from "@tsg-dsp/tsg-sdk";
|
|
10
|
+
*
|
|
11
|
+
* try {
|
|
12
|
+
* await sdk.catalog.getDataset(id, audience);
|
|
13
|
+
* } catch (err) {
|
|
14
|
+
* if (err instanceof SdkError && err.code === SdkErrorCode.NOT_FOUND) {
|
|
15
|
+
* console.log("Dataset not found");
|
|
16
|
+
* }
|
|
17
|
+
* }
|
|
18
|
+
* ```
|
|
19
|
+
*/
|
|
20
|
+
export class SdkError extends Error {
|
|
21
|
+
code;
|
|
22
|
+
statusCode;
|
|
23
|
+
cause;
|
|
24
|
+
constructor(message,
|
|
25
|
+
/** Machine-readable error code. */
|
|
26
|
+
code,
|
|
27
|
+
/** HTTP status code, when the error originated from an HTTP response. */
|
|
28
|
+
statusCode,
|
|
29
|
+
/** The underlying error that caused this SDK error. */
|
|
30
|
+
cause) {
|
|
31
|
+
super(message);
|
|
32
|
+
this.code = code;
|
|
33
|
+
this.statusCode = statusCode;
|
|
34
|
+
this.cause = cause;
|
|
35
|
+
this.name = "SdkError";
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* Machine-readable error codes used by {@link SdkError}.
|
|
40
|
+
*/
|
|
41
|
+
export var SdkErrorCode;
|
|
42
|
+
(function (SdkErrorCode) {
|
|
43
|
+
/** HTTP request failed */
|
|
44
|
+
SdkErrorCode["REQUEST_FAILED"] = "REQUEST_FAILED";
|
|
45
|
+
/** Authentication failed (token acquisition, invalid credentials) */
|
|
46
|
+
SdkErrorCode["AUTH_FAILED"] = "AUTH_FAILED";
|
|
47
|
+
/** Resource not found */
|
|
48
|
+
SdkErrorCode["NOT_FOUND"] = "NOT_FOUND";
|
|
49
|
+
/** Polling timed out before reaching target state */
|
|
50
|
+
SdkErrorCode["POLLING_TIMEOUT"] = "POLLING_TIMEOUT";
|
|
51
|
+
/** Invalid SDK configuration */
|
|
52
|
+
SdkErrorCode["INVALID_CONFIG"] = "INVALID_CONFIG";
|
|
53
|
+
/** Feature not configured (e.g. wallet not configured) */
|
|
54
|
+
SdkErrorCode["NOT_CONFIGURED"] = "NOT_CONFIGURED";
|
|
55
|
+
/** API response did not pass DTO validation */
|
|
56
|
+
SdkErrorCode["VALIDATION_FAILED"] = "VALIDATION_FAILED";
|
|
57
|
+
})(SdkErrorCode || (SdkErrorCode = {}));
|
|
58
|
+
/**
|
|
59
|
+
* Create an {@link SdkError} from an HTTP error response.
|
|
60
|
+
*
|
|
61
|
+
* Maps HTTP status codes to appropriate {@link SdkErrorCode} values:
|
|
62
|
+
* - `404` → {@link SdkErrorCode.NOT_FOUND}
|
|
63
|
+
* - `401` / `403` → {@link SdkErrorCode.AUTH_FAILED}
|
|
64
|
+
* - All others → {@link SdkErrorCode.REQUEST_FAILED}
|
|
65
|
+
* @param message - Human-readable error message.
|
|
66
|
+
* @param statusCode - The HTTP status code.
|
|
67
|
+
* @param cause - Optional underlying error.
|
|
68
|
+
* @returns A new `SdkError` instance.
|
|
69
|
+
*/
|
|
70
|
+
export function createRequestError(message, statusCode, cause) {
|
|
71
|
+
if (statusCode === 404) {
|
|
72
|
+
return new SdkError(message, SdkErrorCode.NOT_FOUND, statusCode, cause);
|
|
73
|
+
}
|
|
74
|
+
if (statusCode === 401 || statusCode === 403) {
|
|
75
|
+
return new SdkError(message, SdkErrorCode.AUTH_FAILED, statusCode, cause);
|
|
76
|
+
}
|
|
77
|
+
return new SdkError(message, SdkErrorCode.REQUEST_FAILED, statusCode, cause);
|
|
78
|
+
}
|
|
79
|
+
//# sourceMappingURL=errors.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"errors.js","sourceRoot":"","sources":["../../src/utils/errors.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;GAkBG;AACH,MAAM,OAAO,QAAS,SAAQ,KAAK;IAIf,IAAI;IAEJ,UAAU;IAEV,KAAK;IAPvB,YACE,OAAe;IACf,mCAAmC;IACnB,IAAkB;IAClC,yEAAyE;IACzD,UAAmB;IACnC,uDAAuD;IACvC,KAAe;QAE/B,KAAK,CAAC,OAAO,CAAC,CAAC;oBANC,IAAI;0BAEJ,UAAU;qBAEV,KAAK;QAGrB,IAAI,CAAC,IAAI,GAAG,UAAU,CAAC;IACzB,CAAC;CACF;AAED;;GAEG;AACH,MAAM,CAAN,IAAY,YAeX;AAfD,WAAY,YAAY;IACtB,0BAA0B;IAC1B,iDAAiC,CAAA;IACjC,qEAAqE;IACrE,2CAA2B,CAAA;IAC3B,yBAAyB;IACzB,uCAAuB,CAAA;IACvB,qDAAqD;IACrD,mDAAmC,CAAA;IACnC,gCAAgC;IAChC,iDAAiC,CAAA;IACjC,0DAA0D;IAC1D,iDAAiC,CAAA;IACjC,+CAA+C;IAC/C,uDAAuC,CAAA;AACzC,CAAC,EAfW,YAAY,KAAZ,YAAY,QAevB;AAED;;;;;;;;;;;GAWG;AACH,MAAM,UAAU,kBAAkB,CAChC,OAAe,EACf,UAAkB,EAClB,KAAe;IAEf,IAAI,UAAU,KAAK,GAAG,EAAE,CAAC;QACvB,OAAO,IAAI,QAAQ,CAAC,OAAO,EAAE,YAAY,CAAC,SAAS,EAAE,UAAU,EAAE,KAAK,CAAC,CAAC;IAC1E,CAAC;IACD,IAAI,UAAU,KAAK,GAAG,IAAI,UAAU,KAAK,GAAG,EAAE,CAAC;QAC7C,OAAO,IAAI,QAAQ,CAAC,OAAO,EAAE,YAAY,CAAC,WAAW,EAAE,UAAU,EAAE,KAAK,CAAC,CAAC;IAC5E,CAAC;IACD,OAAO,IAAI,QAAQ,CAAC,OAAO,EAAE,YAAY,CAAC,cAAc,EAAE,UAAU,EAAE,KAAK,CAAC,CAAC;AAC/E,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/utils/index.ts"],"names":[],"mappings":"AAAA,cAAc,aAAa,CAAC;AAC5B,cAAc,cAAc,CAAC;AAC7B,cAAc,aAAa,CAAC;AAC5B,cAAc,eAAe,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/utils/index.ts"],"names":[],"mappings":"AAAA,cAAc,aAAa,CAAC;AAC5B,cAAc,cAAc,CAAC;AAC7B,cAAc,aAAa,CAAC;AAC5B,cAAc,eAAe,CAAC"}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Configuration for polling operations used by `waitFor*` methods.
|
|
3
|
+
*/
|
|
4
|
+
export interface PollingOptions {
|
|
5
|
+
/** Polling interval in milliseconds. Defaults to `1000`. */
|
|
6
|
+
intervalMs?: number;
|
|
7
|
+
/** Maximum number of polling attempts before timing out. Defaults to `20`. */
|
|
8
|
+
maxRetries?: number;
|
|
9
|
+
}
|
|
10
|
+
/**
|
|
11
|
+
* Poll an async function until a predicate is satisfied, or max retries are exceeded.
|
|
12
|
+
* @typeParam T - The return type of the polled function.
|
|
13
|
+
* @param fn - Async function to call on each polling attempt.
|
|
14
|
+
* @param predicate - Returns `true` when the desired state is reached.
|
|
15
|
+
* @param options - Polling interval and retry configuration.
|
|
16
|
+
* @returns The result of `fn` when `predicate` returns `true`.
|
|
17
|
+
* @throws Throws an {@link SdkError} with code {@link SdkErrorCode.POLLING_TIMEOUT} when `maxRetries` is exceeded.
|
|
18
|
+
*/
|
|
19
|
+
export declare function pollUntil<T>(fn: () => Promise<T>, predicate: (result: T) => boolean, options?: PollingOptions): Promise<T>;
|
|
20
|
+
//# sourceMappingURL=polling.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"polling.d.ts","sourceRoot":"","sources":["../../src/utils/polling.ts"],"names":[],"mappings":"AAEA;;GAEG;AACH,MAAM,WAAW,cAAc;IAC7B,4DAA4D;IAC5D,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,8EAA8E;IAC9E,UAAU,CAAC,EAAE,MAAM,CAAC;CACrB;AAKD;;;;;;;;GAQG;AACH,wBAAsB,SAAS,CAAC,CAAC,EAC/B,EAAE,EAAE,MAAM,OAAO,CAAC,CAAC,CAAC,EACpB,SAAS,EAAE,CAAC,MAAM,EAAE,CAAC,KAAK,OAAO,EACjC,OAAO,CAAC,EAAE,cAAc,GACvB,OAAO,CAAC,CAAC,CAAC,CAkBZ"}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { SdkError, SdkErrorCode } from "./errors.js";
|
|
2
|
+
const DEFAULT_INTERVAL_MS = 1000;
|
|
3
|
+
const DEFAULT_MAX_RETRIES = 20;
|
|
4
|
+
/**
|
|
5
|
+
* Poll an async function until a predicate is satisfied, or max retries are exceeded.
|
|
6
|
+
* @typeParam T - The return type of the polled function.
|
|
7
|
+
* @param fn - Async function to call on each polling attempt.
|
|
8
|
+
* @param predicate - Returns `true` when the desired state is reached.
|
|
9
|
+
* @param options - Polling interval and retry configuration.
|
|
10
|
+
* @returns The result of `fn` when `predicate` returns `true`.
|
|
11
|
+
* @throws Throws an {@link SdkError} with code {@link SdkErrorCode.POLLING_TIMEOUT} when `maxRetries` is exceeded.
|
|
12
|
+
*/
|
|
13
|
+
export async function pollUntil(fn, predicate, options) {
|
|
14
|
+
const intervalMs = options?.intervalMs ?? DEFAULT_INTERVAL_MS;
|
|
15
|
+
const maxRetries = options?.maxRetries ?? DEFAULT_MAX_RETRIES;
|
|
16
|
+
for (let attempt = 0; attempt < maxRetries; attempt++) {
|
|
17
|
+
const result = await fn();
|
|
18
|
+
if (predicate(result)) {
|
|
19
|
+
return result;
|
|
20
|
+
}
|
|
21
|
+
if (attempt < maxRetries - 1) {
|
|
22
|
+
await delay(intervalMs);
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
throw new SdkError(`Polling timed out after ${maxRetries} attempts`, SdkErrorCode.POLLING_TIMEOUT);
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* Wait for the given number of milliseconds.
|
|
29
|
+
* @param ms - Delay duration in milliseconds.
|
|
30
|
+
* @returns A promise that resolves after the delay.
|
|
31
|
+
*/
|
|
32
|
+
function delay(ms) {
|
|
33
|
+
return new Promise((resolve) => setTimeout(resolve, ms));
|
|
34
|
+
}
|
|
35
|
+
//# sourceMappingURL=polling.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"polling.js","sourceRoot":"","sources":["../../src/utils/polling.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,YAAY,EAAE,MAAM,aAAa,CAAC;AAYrD,MAAM,mBAAmB,GAAG,IAAI,CAAC;AACjC,MAAM,mBAAmB,GAAG,EAAE,CAAC;AAE/B;;;;;;;;GAQG;AACH,MAAM,CAAC,KAAK,UAAU,SAAS,CAC7B,EAAoB,EACpB,SAAiC,EACjC,OAAwB;IAExB,MAAM,UAAU,GAAG,OAAO,EAAE,UAAU,IAAI,mBAAmB,CAAC;IAC9D,MAAM,UAAU,GAAG,OAAO,EAAE,UAAU,IAAI,mBAAmB,CAAC;IAE9D,KAAK,IAAI,OAAO,GAAG,CAAC,EAAE,OAAO,GAAG,UAAU,EAAE,OAAO,EAAE,EAAE,CAAC;QACtD,MAAM,MAAM,GAAG,MAAM,EAAE,EAAE,CAAC;QAC1B,IAAI,SAAS,CAAC,MAAM,CAAC,EAAE,CAAC;YACtB,OAAO,MAAM,CAAC;QAChB,CAAC;QACD,IAAI,OAAO,GAAG,UAAU,GAAG,CAAC,EAAE,CAAC;YAC7B,MAAM,KAAK,CAAC,UAAU,CAAC,CAAC;QAC1B,CAAC;IACH,CAAC;IAED,MAAM,IAAI,QAAQ,CAChB,2BAA2B,UAAU,WAAW,EAChD,YAAY,CAAC,eAAe,CAC7B,CAAC;AACJ,CAAC;AAED;;;;GAIG;AACH,SAAS,KAAK,CAAC,EAAU;IACvB,OAAO,IAAI,OAAO,CAAC,CAAC,OAAO,EAAE,EAAE,CAAC,UAAU,CAAC,OAAO,EAAE,EAAE,CAAC,CAAC,CAAC;AAC3D,CAAC"}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Render one DSP reason value for a human-facing SDK surface.
|
|
3
|
+
*
|
|
4
|
+
* DSP reason items are arbitrary JSON values. TSG's multilingual values are
|
|
5
|
+
* rendered as their text, while peer-specific values remain inspectable JSON.
|
|
6
|
+
* @param reason - The reason value to render.
|
|
7
|
+
* @returns A human-readable representation of the value.
|
|
8
|
+
*/
|
|
9
|
+
export declare function formatReason(reason: unknown): string;
|
|
10
|
+
/**
|
|
11
|
+
* Render all DSP reason values as one human-readable string.
|
|
12
|
+
* @param reason - The reason values to render.
|
|
13
|
+
* @returns The rendered values joined by semicolons, or undefined when absent.
|
|
14
|
+
*/
|
|
15
|
+
export declare function formatReasons(reason?: unknown[]): string | undefined;
|
|
16
|
+
//# sourceMappingURL=reason.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"reason.d.ts","sourceRoot":"","sources":["../../src/utils/reason.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AACH,wBAAgB,YAAY,CAAC,MAAM,EAAE,OAAO,GAAG,MAAM,CAgBpD;AAED;;;;GAIG;AACH,wBAAgB,aAAa,CAAC,MAAM,CAAC,EAAE,OAAO,EAAE,GAAG,MAAM,GAAG,SAAS,CAEpE"}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Render one DSP reason value for a human-facing SDK surface.
|
|
3
|
+
*
|
|
4
|
+
* DSP reason items are arbitrary JSON values. TSG's multilingual values are
|
|
5
|
+
* rendered as their text, while peer-specific values remain inspectable JSON.
|
|
6
|
+
* @param reason - The reason value to render.
|
|
7
|
+
* @returns A human-readable representation of the value.
|
|
8
|
+
*/
|
|
9
|
+
export function formatReason(reason) {
|
|
10
|
+
if (typeof reason === "string") {
|
|
11
|
+
return reason;
|
|
12
|
+
}
|
|
13
|
+
if (reason !== null && typeof reason === "object") {
|
|
14
|
+
const value = reason["@value"];
|
|
15
|
+
if (typeof value === "string") {
|
|
16
|
+
return value;
|
|
17
|
+
}
|
|
18
|
+
const plainValue = reason.value;
|
|
19
|
+
if (typeof plainValue === "string") {
|
|
20
|
+
return plainValue;
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
const serialized = JSON.stringify(reason);
|
|
24
|
+
return serialized === undefined ? String(reason) : serialized;
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* Render all DSP reason values as one human-readable string.
|
|
28
|
+
* @param reason - The reason values to render.
|
|
29
|
+
* @returns The rendered values joined by semicolons, or undefined when absent.
|
|
30
|
+
*/
|
|
31
|
+
export function formatReasons(reason) {
|
|
32
|
+
return reason?.map(formatReason).join("; ");
|
|
33
|
+
}
|
|
34
|
+
//# sourceMappingURL=reason.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"reason.js","sourceRoot":"","sources":["../../src/utils/reason.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AACH,MAAM,UAAU,YAAY,CAAC,MAAe;IAC1C,IAAI,OAAO,MAAM,KAAK,QAAQ,EAAE,CAAC;QAC/B,OAAO,MAAM,CAAC;IAChB,CAAC;IACD,IAAI,MAAM,KAAK,IAAI,IAAI,OAAO,MAAM,KAAK,QAAQ,EAAE,CAAC;QAClD,MAAM,KAAK,GAAI,MAAkC,CAAC,QAAQ,CAAC,CAAC;QAC5D,IAAI,OAAO,KAAK,KAAK,QAAQ,EAAE,CAAC;YAC9B,OAAO,KAAK,CAAC;QACf,CAAC;QACD,MAAM,UAAU,GAAI,MAAkC,CAAC,KAAK,CAAC;QAC7D,IAAI,OAAO,UAAU,KAAK,QAAQ,EAAE,CAAC;YACnC,OAAO,UAAU,CAAC;QACpB,CAAC;IACH,CAAC;IACD,MAAM,UAAU,GAAG,IAAI,CAAC,SAAS,CAAC,MAAM,CAAC,CAAC;IAC1C,OAAO,UAAU,KAAK,SAAS,CAAC,CAAC,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,UAAU,CAAC;AAChE,CAAC;AAED;;;;GAIG;AACH,MAAM,UAAU,aAAa,CAAC,MAAkB;IAC9C,OAAO,MAAM,EAAE,GAAG,CAAC,YAAY,CAAC,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;AAC9C,CAAC"}
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
export type JsonObject = Record<string, unknown>;
|
|
2
|
+
/**
|
|
3
|
+
* Validate an object response with a caller-provided type guard.
|
|
4
|
+
* @param data - Untrusted response body.
|
|
5
|
+
* @param name - Response name used in errors.
|
|
6
|
+
* @param predicate - Structural type guard.
|
|
7
|
+
* @returns The validated response.
|
|
8
|
+
*/
|
|
9
|
+
export declare function requireObjectResponse<T extends object>(data: unknown, name: string, predicate: (value: JsonObject) => value is JsonObject & T): T;
|
|
10
|
+
/**
|
|
11
|
+
* Validate every item in an array response.
|
|
12
|
+
* @param data - Untrusted response body.
|
|
13
|
+
* @param name - Response name used in errors.
|
|
14
|
+
* @param predicate - Structural item type guard.
|
|
15
|
+
* @returns The validated response array.
|
|
16
|
+
*/
|
|
17
|
+
export declare function requireArrayResponse<T>(data: unknown, name: string, predicate: (value: JsonObject) => value is JsonObject & T): T[];
|
|
18
|
+
/**
|
|
19
|
+
* Require an object response without accepting arrays or null.
|
|
20
|
+
* @param data - Untrusted response body.
|
|
21
|
+
* @param name - Response name used in errors.
|
|
22
|
+
* @returns The object response.
|
|
23
|
+
*/
|
|
24
|
+
export declare function requireObject(data: unknown, name: string): JsonObject;
|
|
25
|
+
/**
|
|
26
|
+
* Check whether a value is a JSON object.
|
|
27
|
+
* @param value - Candidate value.
|
|
28
|
+
* @returns Whether the value is a non-null, non-array object.
|
|
29
|
+
*/
|
|
30
|
+
export declare function isObject(value: unknown): value is JsonObject;
|
|
31
|
+
/**
|
|
32
|
+
* Check for a non-empty string property.
|
|
33
|
+
* @param value - Candidate object.
|
|
34
|
+
* @param key - Property name.
|
|
35
|
+
* @returns Whether the property is a non-empty string.
|
|
36
|
+
*/
|
|
37
|
+
export declare function hasString(value: JsonObject, key: string): boolean;
|
|
38
|
+
/**
|
|
39
|
+
* Check for a finite number property.
|
|
40
|
+
* @param value - Candidate object.
|
|
41
|
+
* @param key - Property name.
|
|
42
|
+
* @returns Whether the property is a finite number.
|
|
43
|
+
*/
|
|
44
|
+
export declare function hasNumber(value: JsonObject, key: string): boolean;
|
|
45
|
+
/**
|
|
46
|
+
* Check for a boolean property.
|
|
47
|
+
* @param value - Candidate object.
|
|
48
|
+
* @param key - Property name.
|
|
49
|
+
* @returns Whether the property is boolean.
|
|
50
|
+
*/
|
|
51
|
+
export declare function hasBoolean(value: JsonObject, key: string): boolean;
|
|
52
|
+
/**
|
|
53
|
+
* Check for an array property and narrow its type.
|
|
54
|
+
* @param value - Candidate object.
|
|
55
|
+
* @param key - Property name.
|
|
56
|
+
* @returns Whether the property is an array.
|
|
57
|
+
*/
|
|
58
|
+
export declare function hasArray<K extends string>(value: JsonObject, key: K): value is JsonObject & Record<K, unknown[]>;
|
|
59
|
+
//# sourceMappingURL=shape-validation.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"shape-validation.d.ts","sourceRoot":"","sources":["../../src/utils/shape-validation.ts"],"names":[],"mappings":"AAEA,MAAM,MAAM,UAAU,GAAG,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;AAEjD;;;;;;GAMG;AACH,wBAAgB,qBAAqB,CAAC,CAAC,SAAS,MAAM,EACpD,IAAI,EAAE,OAAO,EACb,IAAI,EAAE,MAAM,EACZ,SAAS,EAAE,CAAC,KAAK,EAAE,UAAU,KAAK,KAAK,IAAI,UAAU,GAAG,CAAC,GACxD,CAAC,CAQH;AAED;;;;;;GAMG;AACH,wBAAgB,oBAAoB,CAAC,CAAC,EACpC,IAAI,EAAE,OAAO,EACb,IAAI,EAAE,MAAM,EACZ,SAAS,EAAE,CAAC,KAAK,EAAE,UAAU,KAAK,KAAK,IAAI,UAAU,GAAG,CAAC,GACxD,CAAC,EAAE,CAWL;AAED;;;;;GAKG;AACH,wBAAgB,aAAa,CAAC,IAAI,EAAE,OAAO,EAAE,IAAI,EAAE,MAAM,GAAG,UAAU,CAQrE;AAED;;;;GAIG;AACH,wBAAgB,QAAQ,CAAC,KAAK,EAAE,OAAO,GAAG,KAAK,IAAI,UAAU,CAE5D;AAED;;;;;GAKG;AACH,wBAAgB,SAAS,CAAC,KAAK,EAAE,UAAU,EAAE,GAAG,EAAE,MAAM,GAAG,OAAO,CAEjE;AAED;;;;;GAKG;AACH,wBAAgB,SAAS,CAAC,KAAK,EAAE,UAAU,EAAE,GAAG,EAAE,MAAM,GAAG,OAAO,CAEjE;AAED;;;;;GAKG;AACH,wBAAgB,UAAU,CAAC,KAAK,EAAE,UAAU,EAAE,GAAG,EAAE,MAAM,GAAG,OAAO,CAElE;AAED;;;;;GAKG;AACH,wBAAgB,QAAQ,CAAC,CAAC,SAAS,MAAM,EACvC,KAAK,EAAE,UAAU,EACjB,GAAG,EAAE,CAAC,GACL,KAAK,IAAI,UAAU,GAAG,MAAM,CAAC,CAAC,EAAE,OAAO,EAAE,CAAC,CAE5C"}
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
import { SdkError, SdkErrorCode } from "./errors.js";
|
|
2
|
+
/**
|
|
3
|
+
* Validate an object response with a caller-provided type guard.
|
|
4
|
+
* @param data - Untrusted response body.
|
|
5
|
+
* @param name - Response name used in errors.
|
|
6
|
+
* @param predicate - Structural type guard.
|
|
7
|
+
* @returns The validated response.
|
|
8
|
+
*/
|
|
9
|
+
export function requireObjectResponse(data, name, predicate) {
|
|
10
|
+
if (!isObject(data) || !predicate(data)) {
|
|
11
|
+
throw new SdkError(`Invalid ${name} response`, SdkErrorCode.VALIDATION_FAILED);
|
|
12
|
+
}
|
|
13
|
+
return data;
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* Validate every item in an array response.
|
|
17
|
+
* @param data - Untrusted response body.
|
|
18
|
+
* @param name - Response name used in errors.
|
|
19
|
+
* @param predicate - Structural item type guard.
|
|
20
|
+
* @returns The validated response array.
|
|
21
|
+
*/
|
|
22
|
+
export function requireArrayResponse(data, name, predicate) {
|
|
23
|
+
if (!Array.isArray(data) ||
|
|
24
|
+
!data.every((item) => isObject(item) && predicate(item))) {
|
|
25
|
+
throw new SdkError(`Invalid ${name} response`, SdkErrorCode.VALIDATION_FAILED);
|
|
26
|
+
}
|
|
27
|
+
return data;
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* Require an object response without accepting arrays or null.
|
|
31
|
+
* @param data - Untrusted response body.
|
|
32
|
+
* @param name - Response name used in errors.
|
|
33
|
+
* @returns The object response.
|
|
34
|
+
*/
|
|
35
|
+
export function requireObject(data, name) {
|
|
36
|
+
if (!isObject(data)) {
|
|
37
|
+
throw new SdkError(`Invalid ${name} response: expected an object`, SdkErrorCode.VALIDATION_FAILED);
|
|
38
|
+
}
|
|
39
|
+
return data;
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* Check whether a value is a JSON object.
|
|
43
|
+
* @param value - Candidate value.
|
|
44
|
+
* @returns Whether the value is a non-null, non-array object.
|
|
45
|
+
*/
|
|
46
|
+
export function isObject(value) {
|
|
47
|
+
return value !== null && typeof value === "object" && !Array.isArray(value);
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* Check for a non-empty string property.
|
|
51
|
+
* @param value - Candidate object.
|
|
52
|
+
* @param key - Property name.
|
|
53
|
+
* @returns Whether the property is a non-empty string.
|
|
54
|
+
*/
|
|
55
|
+
export function hasString(value, key) {
|
|
56
|
+
return typeof value[key] === "string" && value[key].length > 0;
|
|
57
|
+
}
|
|
58
|
+
/**
|
|
59
|
+
* Check for a finite number property.
|
|
60
|
+
* @param value - Candidate object.
|
|
61
|
+
* @param key - Property name.
|
|
62
|
+
* @returns Whether the property is a finite number.
|
|
63
|
+
*/
|
|
64
|
+
export function hasNumber(value, key) {
|
|
65
|
+
return typeof value[key] === "number" && Number.isFinite(value[key]);
|
|
66
|
+
}
|
|
67
|
+
/**
|
|
68
|
+
* Check for a boolean property.
|
|
69
|
+
* @param value - Candidate object.
|
|
70
|
+
* @param key - Property name.
|
|
71
|
+
* @returns Whether the property is boolean.
|
|
72
|
+
*/
|
|
73
|
+
export function hasBoolean(value, key) {
|
|
74
|
+
return typeof value[key] === "boolean";
|
|
75
|
+
}
|
|
76
|
+
/**
|
|
77
|
+
* Check for an array property and narrow its type.
|
|
78
|
+
* @param value - Candidate object.
|
|
79
|
+
* @param key - Property name.
|
|
80
|
+
* @returns Whether the property is an array.
|
|
81
|
+
*/
|
|
82
|
+
export function hasArray(value, key) {
|
|
83
|
+
return Array.isArray(value[key]);
|
|
84
|
+
}
|
|
85
|
+
//# sourceMappingURL=shape-validation.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"shape-validation.js","sourceRoot":"","sources":["../../src/utils/shape-validation.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,YAAY,EAAE,MAAM,aAAa,CAAC;AAIrD;;;;;;GAMG;AACH,MAAM,UAAU,qBAAqB,CACnC,IAAa,EACb,IAAY,EACZ,SAAyD;IAEzD,IAAI,CAAC,QAAQ,CAAC,IAAI,CAAC,IAAI,CAAC,SAAS,CAAC,IAAI,CAAC,EAAE,CAAC;QACxC,MAAM,IAAI,QAAQ,CAChB,WAAW,IAAI,WAAW,EAC1B,YAAY,CAAC,iBAAiB,CAC/B,CAAC;IACJ,CAAC;IACD,OAAO,IAAI,CAAC;AACd,CAAC;AAED;;;;;;GAMG;AACH,MAAM,UAAU,oBAAoB,CAClC,IAAa,EACb,IAAY,EACZ,SAAyD;IAEzD,IACE,CAAC,KAAK,CAAC,OAAO,CAAC,IAAI,CAAC;QACpB,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,QAAQ,CAAC,IAAI,CAAC,IAAI,SAAS,CAAC,IAAI,CAAC,CAAC,EACxD,CAAC;QACD,MAAM,IAAI,QAAQ,CAChB,WAAW,IAAI,WAAW,EAC1B,YAAY,CAAC,iBAAiB,CAC/B,CAAC;IACJ,CAAC;IACD,OAAO,IAAI,CAAC;AACd,CAAC;AAED;;;;;GAKG;AACH,MAAM,UAAU,aAAa,CAAC,IAAa,EAAE,IAAY;IACvD,IAAI,CAAC,QAAQ,CAAC,IAAI,CAAC,EAAE,CAAC;QACpB,MAAM,IAAI,QAAQ,CAChB,WAAW,IAAI,+BAA+B,EAC9C,YAAY,CAAC,iBAAiB,CAC/B,CAAC;IACJ,CAAC;IACD,OAAO,IAAI,CAAC;AACd,CAAC;AAED;;;;GAIG;AACH,MAAM,UAAU,QAAQ,CAAC,KAAc;IACrC,OAAO,KAAK,KAAK,IAAI,IAAI,OAAO,KAAK,KAAK,QAAQ,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,KAAK,CAAC,CAAC;AAC9E,CAAC;AAED;;;;;GAKG;AACH,MAAM,UAAU,SAAS,CAAC,KAAiB,EAAE,GAAW;IACtD,OAAO,OAAO,KAAK,CAAC,GAAG,CAAC,KAAK,QAAQ,IAAI,KAAK,CAAC,GAAG,CAAC,CAAC,MAAM,GAAG,CAAC,CAAC;AACjE,CAAC;AAED;;;;;GAKG;AACH,MAAM,UAAU,SAAS,CAAC,KAAiB,EAAE,GAAW;IACtD,OAAO,OAAO,KAAK,CAAC,GAAG,CAAC,KAAK,QAAQ,IAAI,MAAM,CAAC,QAAQ,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC,CAAC;AACvE,CAAC;AAED;;;;;GAKG;AACH,MAAM,UAAU,UAAU,CAAC,KAAiB,EAAE,GAAW;IACvD,OAAO,OAAO,KAAK,CAAC,GAAG,CAAC,KAAK,SAAS,CAAC;AACzC,CAAC;AAED;;;;;GAKG;AACH,MAAM,UAAU,QAAQ,CACtB,KAAiB,EACjB,GAAM;IAEN,OAAO,KAAK,CAAC,OAAO,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC,CAAC;AACnC,CAAC"}
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import { ContextDto, SerializableClass } from "@tsg-dsp/common-dsp";
|
|
2
|
+
import { type ClassConstructor } from "class-transformer";
|
|
3
|
+
/**
|
|
4
|
+
* Infer the raw DTO type from a JSON-LD serializable class.
|
|
5
|
+
*
|
|
6
|
+
* For example, `DtoOf<Catalog>` resolves to `CatalogDto`.
|
|
7
|
+
*/
|
|
8
|
+
export type DtoOf<T> = T extends SerializableClass<infer DTO> ? DTO : never;
|
|
9
|
+
/**
|
|
10
|
+
* Validate an API response against a DTO class.
|
|
11
|
+
*
|
|
12
|
+
* Creates a class instance from the plain data, runs `class-validator` checks,
|
|
13
|
+
* and throws a descriptive error if validation fails. Returns the transformed
|
|
14
|
+
* class instance so `class-transformer` conversions such as `Date` coercion are
|
|
15
|
+
* preserved for SDK consumers.
|
|
16
|
+
* @typeParam T - The DTO class type.
|
|
17
|
+
* @param dto - The class constructor to validate against.
|
|
18
|
+
* @param data - The plain object received from the API.
|
|
19
|
+
* @returns A validated instance of `T`.
|
|
20
|
+
* @throws Throws an {@link SdkError} with code {@link SdkErrorCode.VALIDATION_FAILED} if validation errors are found.
|
|
21
|
+
*/
|
|
22
|
+
export declare function validateResponse<T extends object>(dto: ClassConstructor<T>, data: object): T;
|
|
23
|
+
/**
|
|
24
|
+
* Validate an array of API responses against a DTO class.
|
|
25
|
+
*
|
|
26
|
+
* Each item is validated individually using {@link validateResponse}.
|
|
27
|
+
* @typeParam T - The DTO class type.
|
|
28
|
+
* @param dto - The class constructor to validate against.
|
|
29
|
+
* @param data - The array of plain objects received from the API.
|
|
30
|
+
* @returns An array of validated instances of `T`.
|
|
31
|
+
* @throws Throws an {@link SdkError} with code {@link SdkErrorCode.VALIDATION_FAILED} if any item fails validation.
|
|
32
|
+
*/
|
|
33
|
+
export declare function validateResponseArray<T extends object>(dto: ClassConstructor<T>, data: object[]): T[];
|
|
34
|
+
/**
|
|
35
|
+
* Deserialize and validate a JSON-LD API response.
|
|
36
|
+
*
|
|
37
|
+
* Uses the `@tsg-dsp/common-dsp` deserializer to convert JSON-LD payloads
|
|
38
|
+
* into typed class instances.
|
|
39
|
+
* @typeParam T - The target serializable class type.
|
|
40
|
+
* @param data - The raw JSON-LD data from the API.
|
|
41
|
+
* @param returnDto - When `true`, returns the raw DTO-shaped data instead of the deserialized class instance.
|
|
42
|
+
* @returns The deserialized class instance, or the raw DTO when `returnDto` is `true`.
|
|
43
|
+
*/
|
|
44
|
+
export declare function validateJsonLD<T extends SerializableClass<ContextDto>>(data: unknown, returnDto: true): Promise<DtoOf<T>>;
|
|
45
|
+
export declare function validateJsonLD<T extends SerializableClass<ContextDto>>(data: unknown, returnDto?: false): Promise<T>;
|
|
46
|
+
export declare function validateJsonLD<T extends SerializableClass<ContextDto>>(data: unknown, returnDto: boolean): Promise<T | DtoOf<T>>;
|
|
47
|
+
//# sourceMappingURL=validate.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"validate.d.ts","sourceRoot":"","sources":["../../src/utils/validate.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,UAAU,EAEV,iBAAiB,EAClB,MAAM,qBAAqB,CAAC;AAC7B,OAAO,EAAE,KAAK,gBAAgB,EAAmB,MAAM,mBAAmB,CAAC;AA6B3E;;;;GAIG;AACH,MAAM,MAAM,KAAK,CAAC,CAAC,IAAI,CAAC,SAAS,iBAAiB,CAAC,MAAM,GAAG,CAAC,GAAG,GAAG,GAAG,KAAK,CAAC;AAE5E;;;;;;;;;;;;GAYG;AACH,wBAAgB,gBAAgB,CAAC,CAAC,SAAS,MAAM,EAC/C,GAAG,EAAE,gBAAgB,CAAC,CAAC,CAAC,EACxB,IAAI,EAAE,MAAM,GACX,CAAC,CAcH;AAED;;;;;;;;;GASG;AACH,wBAAgB,qBAAqB,CAAC,CAAC,SAAS,MAAM,EACpD,GAAG,EAAE,gBAAgB,CAAC,CAAC,CAAC,EACxB,IAAI,EAAE,MAAM,EAAE,GACb,CAAC,EAAE,CAEL;AAED;;;;;;;;;GASG;AACH,wBAAsB,cAAc,CAAC,CAAC,SAAS,iBAAiB,CAAC,UAAU,CAAC,EAC1E,IAAI,EAAE,OAAO,EACb,SAAS,EAAE,IAAI,GACd,OAAO,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,CAAC;AACrB,wBAAsB,cAAc,CAAC,CAAC,SAAS,iBAAiB,CAAC,UAAU,CAAC,EAC1E,IAAI,EAAE,OAAO,EACb,SAAS,CAAC,EAAE,KAAK,GAChB,OAAO,CAAC,CAAC,CAAC,CAAC;AACd,wBAAsB,cAAc,CAAC,CAAC,SAAS,iBAAiB,CAAC,UAAU,CAAC,EAC1E,IAAI,EAAE,OAAO,EACb,SAAS,EAAE,OAAO,GACjB,OAAO,CAAC,CAAC,GAAG,KAAK,CAAC,CAAC,CAAC,CAAC,CAAC"}
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import { deserialize } from "@tsg-dsp/common-dsp";
|
|
2
|
+
import { plainToInstance } from "class-transformer";
|
|
3
|
+
import { validateSync } from "class-validator";
|
|
4
|
+
import { SdkError, SdkErrorCode } from "./errors.js";
|
|
5
|
+
/**
|
|
6
|
+
* Validation error messages for a class-validator ValidationError array.
|
|
7
|
+
*
|
|
8
|
+
* Recursively traverses the ValidationError tree and returns a flat array of
|
|
9
|
+
* messages with the property path prepended to each message.
|
|
10
|
+
* @param errors - The array of ValidationError objects to process.
|
|
11
|
+
* @param parentPath - The property path of the parent object, used for recursion. Defaults to an empty string.
|
|
12
|
+
* @returns An array of strings containing the validation error messages with property paths.
|
|
13
|
+
*/
|
|
14
|
+
function validationMessages(errors, parentPath = "") {
|
|
15
|
+
return errors.flatMap((error) => {
|
|
16
|
+
const path = parentPath
|
|
17
|
+
? `${parentPath}.${error.property}`
|
|
18
|
+
: error.property;
|
|
19
|
+
const ownMessages = Object.values(error.constraints ?? {}).map((message) => `${path}: ${message}`);
|
|
20
|
+
return [...ownMessages, ...validationMessages(error.children ?? [], path)];
|
|
21
|
+
});
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* Validate an API response against a DTO class.
|
|
25
|
+
*
|
|
26
|
+
* Creates a class instance from the plain data, runs `class-validator` checks,
|
|
27
|
+
* and throws a descriptive error if validation fails. Returns the transformed
|
|
28
|
+
* class instance so `class-transformer` conversions such as `Date` coercion are
|
|
29
|
+
* preserved for SDK consumers.
|
|
30
|
+
* @typeParam T - The DTO class type.
|
|
31
|
+
* @param dto - The class constructor to validate against.
|
|
32
|
+
* @param data - The plain object received from the API.
|
|
33
|
+
* @returns A validated instance of `T`.
|
|
34
|
+
* @throws Throws an {@link SdkError} with code {@link SdkErrorCode.VALIDATION_FAILED} if validation errors are found.
|
|
35
|
+
*/
|
|
36
|
+
export function validateResponse(dto, data) {
|
|
37
|
+
const instance = plainToInstance(dto, data);
|
|
38
|
+
const errors = validateSync(instance, {
|
|
39
|
+
whitelist: false,
|
|
40
|
+
forbidUnknownValues: false
|
|
41
|
+
});
|
|
42
|
+
if (errors.length > 0) {
|
|
43
|
+
const messages = validationMessages(errors);
|
|
44
|
+
throw new SdkError(`Invalid ${dto.name} response: ${messages.join("; ") || "validation failed"}`, SdkErrorCode.VALIDATION_FAILED);
|
|
45
|
+
}
|
|
46
|
+
return instance;
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* Validate an array of API responses against a DTO class.
|
|
50
|
+
*
|
|
51
|
+
* Each item is validated individually using {@link validateResponse}.
|
|
52
|
+
* @typeParam T - The DTO class type.
|
|
53
|
+
* @param dto - The class constructor to validate against.
|
|
54
|
+
* @param data - The array of plain objects received from the API.
|
|
55
|
+
* @returns An array of validated instances of `T`.
|
|
56
|
+
* @throws Throws an {@link SdkError} with code {@link SdkErrorCode.VALIDATION_FAILED} if any item fails validation.
|
|
57
|
+
*/
|
|
58
|
+
export function validateResponseArray(dto, data) {
|
|
59
|
+
return data.map((item) => validateResponse(dto, item));
|
|
60
|
+
}
|
|
61
|
+
export async function validateJsonLD(data, returnDto = false) {
|
|
62
|
+
const deserialized = await deserialize(data);
|
|
63
|
+
if (returnDto) {
|
|
64
|
+
return data;
|
|
65
|
+
}
|
|
66
|
+
return deserialized;
|
|
67
|
+
}
|
|
68
|
+
//# sourceMappingURL=validate.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"validate.js","sourceRoot":"","sources":["../../src/utils/validate.ts"],"names":[],"mappings":"AAAA,OAAO,EAEL,WAAW,EAEZ,MAAM,qBAAqB,CAAC;AAC7B,OAAO,EAAyB,eAAe,EAAE,MAAM,mBAAmB,CAAC;AAC3E,OAAO,EAAE,YAAY,EAAwB,MAAM,iBAAiB,CAAC;AAErE,OAAO,EAAE,QAAQ,EAAE,YAAY,EAAE,MAAM,aAAa,CAAC;AAErD;;;;;;;;GAQG;AACH,SAAS,kBAAkB,CACzB,MAAyB,EACzB,UAAU,GAAG,EAAE;IAEf,OAAO,MAAM,CAAC,OAAO,CAAC,CAAC,KAAK,EAAE,EAAE;QAC9B,MAAM,IAAI,GAAG,UAAU;YACrB,CAAC,CAAC,GAAG,UAAU,IAAI,KAAK,CAAC,QAAQ,EAAE;YACnC,CAAC,CAAC,KAAK,CAAC,QAAQ,CAAC;QACnB,MAAM,WAAW,GAAG,MAAM,CAAC,MAAM,CAAC,KAAK,CAAC,WAAW,IAAI,EAAE,CAAC,CAAC,GAAG,CAC5D,CAAC,OAAO,EAAE,EAAE,CAAC,GAAG,IAAI,KAAK,OAAO,EAAE,CACnC,CAAC;QACF,OAAO,CAAC,GAAG,WAAW,EAAE,GAAG,kBAAkB,CAAC,KAAK,CAAC,QAAQ,IAAI,EAAE,EAAE,IAAI,CAAC,CAAC,CAAC;IAC7E,CAAC,CAAC,CAAC;AACL,CAAC;AASD;;;;;;;;;;;;GAYG;AACH,MAAM,UAAU,gBAAgB,CAC9B,GAAwB,EACxB,IAAY;IAEZ,MAAM,QAAQ,GAAG,eAAe,CAAC,GAAG,EAAE,IAAI,CAAC,CAAC;IAC5C,MAAM,MAAM,GAAG,YAAY,CAAC,QAAQ,EAAE;QACpC,SAAS,EAAE,KAAK;QAChB,mBAAmB,EAAE,KAAK;KAC3B,CAAC,CAAC;IACH,IAAI,MAAM,CAAC,MAAM,GAAG,CAAC,EAAE,CAAC;QACtB,MAAM,QAAQ,GAAG,kBAAkB,CAAC,MAAM,CAAC,CAAC;QAC5C,MAAM,IAAI,QAAQ,CAChB,WAAW,GAAG,CAAC,IAAI,cAAc,QAAQ,CAAC,IAAI,CAAC,IAAI,CAAC,IAAI,mBAAmB,EAAE,EAC7E,YAAY,CAAC,iBAAiB,CAC/B,CAAC;IACJ,CAAC;IACD,OAAO,QAAQ,CAAC;AAClB,CAAC;AAED;;;;;;;;;GASG;AACH,MAAM,UAAU,qBAAqB,CACnC,GAAwB,EACxB,IAAc;IAEd,OAAO,IAAI,CAAC,GAAG,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,gBAAgB,CAAC,GAAG,EAAE,IAAI,CAAC,CAAC,CAAC;AACzD,CAAC;AAwBD,MAAM,CAAC,KAAK,UAAU,cAAc,CAClC,IAAa,EACb,SAAS,GAAY,KAAK;IAE1B,MAAM,YAAY,GAAG,MAAM,WAAW,CAAI,IAAI,CAAC,CAAC;IAChD,IAAI,SAAS,EAAE,CAAC;QACd,OAAO,IAAgB,CAAC;IAC1B,CAAC;IACD,OAAO,YAAY,CAAC;AACtB,CAAC"}
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import { Action, PermissionString, RESERVED_SCOPES, Resource } from "./permissions.js";
|
|
2
|
+
export type ActorType = "user" | "service" | "system";
|
|
3
|
+
/** Principal kind; platform/provisioning authority is granted separately. */
|
|
4
|
+
export declare const AUTHENTICATED_ACTOR_TYPES: readonly ["human", "service"];
|
|
5
|
+
export type AuthenticatedActorType = (typeof AUTHENTICATED_ACTOR_TYPES)[number];
|
|
6
|
+
export declare function isAuthenticatedActorType(value: unknown): value is AuthenticatedActorType;
|
|
7
|
+
export interface RequestActor {
|
|
8
|
+
ownershipAliases?: string[];
|
|
9
|
+
sub: string;
|
|
10
|
+
type: ActorType;
|
|
11
|
+
actorType?: AuthenticatedActorType;
|
|
12
|
+
issuer?: string;
|
|
13
|
+
audience?: string | string[];
|
|
14
|
+
tenantId?: string;
|
|
15
|
+
authorizationRevision?: number;
|
|
16
|
+
serviceName?: string;
|
|
17
|
+
permissions?: PermissionString[];
|
|
18
|
+
didId?: string;
|
|
19
|
+
username?: string;
|
|
20
|
+
[key: string]: unknown;
|
|
21
|
+
}
|
|
22
|
+
export interface SubjectAttributes extends RequestActor {
|
|
23
|
+
permissions: PermissionString[];
|
|
24
|
+
}
|
|
25
|
+
export interface DelegationContext {
|
|
26
|
+
originalActor: RequestActor;
|
|
27
|
+
delegationChain: RequestActor[];
|
|
28
|
+
originTimestamp: Date;
|
|
29
|
+
correlationId: string;
|
|
30
|
+
effectivePermissions: PermissionString[];
|
|
31
|
+
}
|
|
32
|
+
export interface ResourceAttributes {
|
|
33
|
+
type: Resource;
|
|
34
|
+
id?: string;
|
|
35
|
+
ownerId?: string;
|
|
36
|
+
ownerIdentifier?: string;
|
|
37
|
+
tenantId?: string;
|
|
38
|
+
[key: string]: unknown;
|
|
39
|
+
}
|
|
40
|
+
export interface EnvironmentAttributes {
|
|
41
|
+
timestamp: Date;
|
|
42
|
+
ipAddress?: string;
|
|
43
|
+
userAgent?: string;
|
|
44
|
+
requestPath?: string;
|
|
45
|
+
requestMethod?: string;
|
|
46
|
+
correlationId?: string;
|
|
47
|
+
[key: string]: unknown;
|
|
48
|
+
}
|
|
49
|
+
export interface RequestContext {
|
|
50
|
+
caller: RequestActor;
|
|
51
|
+
delegation?: DelegationContext;
|
|
52
|
+
isOnBehalfOf: boolean;
|
|
53
|
+
environment: EnvironmentAttributes;
|
|
54
|
+
}
|
|
55
|
+
export interface PolicyContext {
|
|
56
|
+
subject: SubjectAttributes;
|
|
57
|
+
action: Action;
|
|
58
|
+
resource: ResourceAttributes;
|
|
59
|
+
environment: EnvironmentAttributes;
|
|
60
|
+
delegation?: DelegationContext;
|
|
61
|
+
}
|
|
62
|
+
export type EffectiveScope = typeof RESERVED_SCOPES.ALL | typeof RESERVED_SCOPES.OWN | "specific";
|
|
63
|
+
export interface PolicyResult {
|
|
64
|
+
allowed: boolean;
|
|
65
|
+
reason?: string;
|
|
66
|
+
matchedPermission?: PermissionString;
|
|
67
|
+
effectiveScope?: EffectiveScope;
|
|
68
|
+
allowedResourceIds?: string[];
|
|
69
|
+
}
|