@crittora/sdk-js 2.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (42) hide show
  1. package/CHANGELOG.md +37 -0
  2. package/README.md +348 -0
  3. package/dist/auth/bearer.d.ts +2 -0
  4. package/dist/auth/bearer.js +11 -0
  5. package/dist/auth/bearer.js.map +1 -0
  6. package/dist/auth/cognito.d.ts +14 -0
  7. package/dist/auth/cognito.js +53 -0
  8. package/dist/auth/cognito.js.map +1 -0
  9. package/dist/auth/types.d.ts +9 -0
  10. package/dist/auth/types.js +3 -0
  11. package/dist/auth/types.js.map +1 -0
  12. package/dist/client.d.ts +18 -0
  13. package/dist/client.js +57 -0
  14. package/dist/client.js.map +1 -0
  15. package/dist/crittora.d.ts +18 -0
  16. package/dist/crittora.js +79 -0
  17. package/dist/crittora.js.map +1 -0
  18. package/dist/errors/authenticationError.d.ts +1 -0
  19. package/dist/errors/authenticationError.js +6 -0
  20. package/dist/errors/authenticationError.js.map +1 -0
  21. package/dist/errors/crittoraErrors.d.ts +1 -0
  22. package/dist/errors/crittoraErrors.js +12 -0
  23. package/dist/errors/crittoraErrors.js.map +1 -0
  24. package/dist/errors.d.ts +33 -0
  25. package/dist/errors.js +59 -0
  26. package/dist/errors.js.map +1 -0
  27. package/dist/index.d.ts +7 -0
  28. package/dist/index.js +24 -0
  29. package/dist/index.js.map +1 -0
  30. package/dist/resources/crypto.d.ts +14 -0
  31. package/dist/resources/crypto.js +102 -0
  32. package/dist/resources/crypto.js.map +1 -0
  33. package/dist/transport/httpTransport.d.ts +16 -0
  34. package/dist/transport/httpTransport.js +158 -0
  35. package/dist/transport/httpTransport.js.map +1 -0
  36. package/dist/types.d.ts +101 -0
  37. package/dist/types.js +3 -0
  38. package/dist/types.js.map +1 -0
  39. package/docs/API.md +225 -0
  40. package/docs/ARCHITECTURE.md +83 -0
  41. package/docs/MIGRATION.md +187 -0
  42. package/package.json +82 -0
@@ -0,0 +1,6 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.AuthenticationError = void 0;
4
+ var errors_1 = require("../errors");
5
+ Object.defineProperty(exports, "AuthenticationError", { enumerable: true, get: function () { return errors_1.AuthError; } });
6
+ //# sourceMappingURL=authenticationError.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"authenticationError.js","sourceRoot":"","sources":["../../src/errors/authenticationError.ts"],"names":[],"mappings":";;;AAAA,oCAA6D;AAApD,6GAAA,SAAS,OAAuB"}
@@ -0,0 +1 @@
1
+ export { AuthError as AuthenticationError, CrittoraError, DecryptError as DecryptionError, EncryptError as EncryptionError, RateLimitError, RequestError, ValidationError, } from "../errors";
@@ -0,0 +1,12 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.ValidationError = exports.RequestError = exports.RateLimitError = exports.EncryptionError = exports.DecryptionError = exports.CrittoraError = exports.AuthenticationError = void 0;
4
+ var errors_1 = require("../errors");
5
+ Object.defineProperty(exports, "AuthenticationError", { enumerable: true, get: function () { return errors_1.AuthError; } });
6
+ Object.defineProperty(exports, "CrittoraError", { enumerable: true, get: function () { return errors_1.CrittoraError; } });
7
+ Object.defineProperty(exports, "DecryptionError", { enumerable: true, get: function () { return errors_1.DecryptError; } });
8
+ Object.defineProperty(exports, "EncryptionError", { enumerable: true, get: function () { return errors_1.EncryptError; } });
9
+ Object.defineProperty(exports, "RateLimitError", { enumerable: true, get: function () { return errors_1.RateLimitError; } });
10
+ Object.defineProperty(exports, "RequestError", { enumerable: true, get: function () { return errors_1.RequestError; } });
11
+ Object.defineProperty(exports, "ValidationError", { enumerable: true, get: function () { return errors_1.ValidationError; } });
12
+ //# sourceMappingURL=crittoraErrors.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"crittoraErrors.js","sourceRoot":"","sources":["../../src/errors/crittoraErrors.ts"],"names":[],"mappings":";;;AAAA,oCAQmB;AAPjB,6GAAA,SAAS,OAAuB;AAChC,uGAAA,aAAa,OAAA;AACb,yGAAA,YAAY,OAAmB;AAC/B,yGAAA,YAAY,OAAmB;AAC/B,wGAAA,cAAc,OAAA;AACd,sGAAA,YAAY,OAAA;AACZ,yGAAA,eAAe,OAAA"}
@@ -0,0 +1,33 @@
1
+ export interface CrittoraErrorOptions {
2
+ code: string;
3
+ status?: number;
4
+ details?: unknown;
5
+ requestId?: string;
6
+ cause?: unknown;
7
+ }
8
+ export declare class CrittoraError extends Error {
9
+ readonly code: string;
10
+ readonly status?: number;
11
+ readonly details?: unknown;
12
+ readonly requestId?: string;
13
+ readonly cause?: unknown;
14
+ constructor(message: string, options: CrittoraErrorOptions);
15
+ }
16
+ export declare class ValidationError extends CrittoraError {
17
+ constructor(message: string, details?: unknown);
18
+ }
19
+ export declare class AuthError extends CrittoraError {
20
+ constructor(message: string, details?: unknown, cause?: unknown);
21
+ }
22
+ export declare class RequestError extends CrittoraError {
23
+ constructor(message: string, options?: Omit<CrittoraErrorOptions, "code">);
24
+ }
25
+ export declare class RateLimitError extends CrittoraError {
26
+ constructor(message: string, options?: Omit<CrittoraErrorOptions, "code">);
27
+ }
28
+ export declare class EncryptError extends CrittoraError {
29
+ constructor(message: string, options?: Omit<CrittoraErrorOptions, "code">);
30
+ }
31
+ export declare class DecryptError extends CrittoraError {
32
+ constructor(message: string, options?: Omit<CrittoraErrorOptions, "code">);
33
+ }
package/dist/errors.js ADDED
@@ -0,0 +1,59 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.DecryptError = exports.EncryptError = exports.RateLimitError = exports.RequestError = exports.AuthError = exports.ValidationError = exports.CrittoraError = void 0;
4
+ class CrittoraError extends Error {
5
+ constructor(message, options) {
6
+ super(message);
7
+ this.name = "CrittoraError";
8
+ this.code = options.code;
9
+ this.status = options.status;
10
+ this.details = options.details;
11
+ this.requestId = options.requestId;
12
+ this.cause = options.cause;
13
+ Object.setPrototypeOf(this, new.target.prototype);
14
+ }
15
+ }
16
+ exports.CrittoraError = CrittoraError;
17
+ class ValidationError extends CrittoraError {
18
+ constructor(message, details) {
19
+ super(message, { code: "VALIDATION_ERROR", details });
20
+ this.name = "ValidationError";
21
+ }
22
+ }
23
+ exports.ValidationError = ValidationError;
24
+ class AuthError extends CrittoraError {
25
+ constructor(message, details, cause) {
26
+ super(message, { code: "AUTH_ERROR", details, cause });
27
+ this.name = "AuthError";
28
+ }
29
+ }
30
+ exports.AuthError = AuthError;
31
+ class RequestError extends CrittoraError {
32
+ constructor(message, options = {}) {
33
+ super(message, { code: "REQUEST_ERROR", ...options });
34
+ this.name = "RequestError";
35
+ }
36
+ }
37
+ exports.RequestError = RequestError;
38
+ class RateLimitError extends CrittoraError {
39
+ constructor(message, options = {}) {
40
+ super(message, { code: "RATE_LIMIT_ERROR", ...options });
41
+ this.name = "RateLimitError";
42
+ }
43
+ }
44
+ exports.RateLimitError = RateLimitError;
45
+ class EncryptError extends CrittoraError {
46
+ constructor(message, options = {}) {
47
+ super(message, { code: "ENCRYPT_ERROR", ...options });
48
+ this.name = "EncryptError";
49
+ }
50
+ }
51
+ exports.EncryptError = EncryptError;
52
+ class DecryptError extends CrittoraError {
53
+ constructor(message, options = {}) {
54
+ super(message, { code: "DECRYPT_ERROR", ...options });
55
+ this.name = "DecryptError";
56
+ }
57
+ }
58
+ exports.DecryptError = DecryptError;
59
+ //# sourceMappingURL=errors.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"errors.js","sourceRoot":"","sources":["../src/errors.ts"],"names":[],"mappings":";;;AAQA,MAAa,aAAc,SAAQ,KAAK;IAOtC,YAAY,OAAe,EAAE,OAA6B;QACxD,KAAK,CAAC,OAAO,CAAC,CAAC;QACf,IAAI,CAAC,IAAI,GAAG,eAAe,CAAC;QAC5B,IAAI,CAAC,IAAI,GAAG,OAAO,CAAC,IAAI,CAAC;QACzB,IAAI,CAAC,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC;QAC7B,IAAI,CAAC,OAAO,GAAG,OAA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,UAA8C,EAAE;QAEhD,KAAK,CAAC,OAAO,EAAE,EAAE,IAAI,EAAE,eAAe,EAAE,GAAG,OAAO,EAAE,CAAC,CAAC;QACtD,IAAI,CAAC,IAAI,GAAG,cAAc,CAAC;IAC7B,CAAC;CACF;AARD,oCAQC;AAED,MAAa,YAAa,SAAQ,aAAa;IAC7C,YACE,OAAe,EACf,UAA8C,EAAE;QAEhD,KAAK,CAAC,OAAO,EAAE,EAAE,IAAI,EAAE,eAAe,EAAE,GAAG,OAAO,EAAE,CAAC,CAAC;QACtD,IAAI,CAAC,IAAI,GAAG,cAAc,CAAC;IAC7B,CAAC;CACF;AARD,oCAQC"}
@@ -0,0 +1,7 @@
1
+ export * from "./client";
2
+ export * from "./crittora";
3
+ export * from "./types";
4
+ export * from "./errors";
5
+ export * from "./auth/types";
6
+ export * from "./auth/bearer";
7
+ export * from "./auth/cognito";
package/dist/index.js ADDED
@@ -0,0 +1,24 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ __exportStar(require("./client"), exports);
18
+ __exportStar(require("./crittora"), exports);
19
+ __exportStar(require("./types"), exports);
20
+ __exportStar(require("./errors"), exports);
21
+ __exportStar(require("./auth/types"), exports);
22
+ __exportStar(require("./auth/bearer"), exports);
23
+ __exportStar(require("./auth/cognito"), exports);
24
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,2CAAyB;AACzB,6CAA2B;AAC3B,0CAAwB;AACxB,2CAAyB;AACzB,+CAA6B;AAC7B,gDAA8B;AAC9B,iDAA+B"}
@@ -0,0 +1,14 @@
1
+ import { DecryptInput, DecryptResult, DecryptVerifyInput, DecryptVerifyResult, EncryptInput, EncryptResult } from "../types";
2
+ import { AuthProvider } from "../auth/types";
3
+ import { HttpTransport } from "../transport/httpTransport";
4
+ export declare class CryptoResource {
5
+ private readonly transport;
6
+ private readonly authProvider?;
7
+ constructor(transport: HttpTransport, authProvider?: AuthProvider | undefined);
8
+ encrypt(input: EncryptInput): Promise<EncryptResult>;
9
+ decrypt(input: DecryptInput): Promise<DecryptResult>;
10
+ decryptVerify(input: DecryptVerifyInput): Promise<DecryptVerifyResult>;
11
+ private serializePermissions;
12
+ private wrapEncryptError;
13
+ private wrapDecryptError;
14
+ }
@@ -0,0 +1,102 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.CryptoResource = void 0;
4
+ const errors_1 = require("../errors");
5
+ class CryptoResource {
6
+ constructor(transport, authProvider) {
7
+ this.transport = transport;
8
+ this.authProvider = authProvider;
9
+ }
10
+ async encrypt(input) {
11
+ try {
12
+ const result = await this.transport.request({
13
+ path: "/encrypt",
14
+ body: {
15
+ data: input.data,
16
+ requested_actions: ["e"],
17
+ ...(input.permissions && {
18
+ permissions: this.serializePermissions(input.permissions),
19
+ }),
20
+ },
21
+ }, this.authProvider);
22
+ return {
23
+ encryptedData: result.encrypted_data,
24
+ transactionId: result.transactionId,
25
+ };
26
+ }
27
+ catch (error) {
28
+ throw this.wrapEncryptError("Encryption failed", error);
29
+ }
30
+ }
31
+ async decrypt(input) {
32
+ try {
33
+ const result = await this.transport.request({
34
+ path: "/decrypt",
35
+ body: {
36
+ encrypted_data: input.encryptedData,
37
+ ...(input.permissions && {
38
+ permissions: this.serializePermissions(input.permissions),
39
+ }),
40
+ },
41
+ }, this.authProvider);
42
+ return {
43
+ decryptedData: result.decrypted_data,
44
+ transactionId: result.transactionId,
45
+ };
46
+ }
47
+ catch (error) {
48
+ throw this.wrapDecryptError("Decryption failed", error);
49
+ }
50
+ }
51
+ async decryptVerify(input) {
52
+ try {
53
+ const result = await this.transport.request({
54
+ path: "/decrypt-verify",
55
+ body: {
56
+ encrypted_data: input.encryptedData,
57
+ ...(input.permissions && {
58
+ permissions: this.serializePermissions(input.permissions),
59
+ }),
60
+ },
61
+ }, this.authProvider);
62
+ return {
63
+ decryptedData: result.decrypted_data,
64
+ isValidSignature: result.is_valid_signature,
65
+ transactionId: result.transactionId,
66
+ };
67
+ }
68
+ catch (error) {
69
+ throw this.wrapDecryptError("Decrypt-verify failed", error);
70
+ }
71
+ }
72
+ serializePermissions(permissions) {
73
+ return (permissions ?? []).map((permission) => ({
74
+ partner_id: permission.partnerId,
75
+ permissions: permission.actions,
76
+ }));
77
+ }
78
+ wrapEncryptError(message, error) {
79
+ if (error instanceof errors_1.RequestError) {
80
+ return new errors_1.EncryptError(message, {
81
+ status: error.status,
82
+ details: error.details,
83
+ requestId: error.requestId,
84
+ cause: error,
85
+ });
86
+ }
87
+ return new errors_1.EncryptError(message, { cause: error });
88
+ }
89
+ wrapDecryptError(message, error) {
90
+ if (error instanceof errors_1.RequestError) {
91
+ return new errors_1.DecryptError(message, {
92
+ status: error.status,
93
+ details: error.details,
94
+ requestId: error.requestId,
95
+ cause: error,
96
+ });
97
+ }
98
+ return new errors_1.DecryptError(message, { cause: error });
99
+ }
100
+ }
101
+ exports.CryptoResource = CryptoResource;
102
+ //# sourceMappingURL=crypto.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"crypto.js","sourceRoot":"","sources":["../../src/resources/crypto.ts"],"names":[],"mappings":";;;AAcA,sCAAqE;AAErE,MAA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BACtB,SAAS,EAAE,KAAK,CAAC,SAAS;gBAC1B,KAAK,EAAE,KAAK;aACb,CAAC,CAAC;QACL,CAAC;QAED,OAAO,IAAI,qBAAY,CAAC,OAAO,EAAE,EAAE,KAAK,EAAE,KAAK,EAAE,CAAC,CAAC;IACrD,CAAC;IAEO,gBAAgB,CAAC,OAAe,EAAE,KAAc;QACtD,IAAI,KAAK,YAAY,qBAAY,EAAE,CAAC;YAClC,OAAO,IAAI,qBAAY,CAAC,OAAO,EAAE;gBAC/B,MAAM,EAAE,KAAK,CAAC,MAAM;gBACpB,OAAO,EAAE,KAAK,CAAC,OAAO;gBACtB,SAAS,EAAE,KAAK,CAAC,SAAS;gBAC1B,KAAK,EAAE,KAAK;aACb,CAAC,CAAC;QACL,CAAC;QAED,OAAO,IAAI,qBAAY,CAAC,OAAO,EAAE,EAAE,KAAK,EAAE,KAAK,EAAE,CAAC,CAAC;IACrD,CAAC;CACF;AApHD,wCAoHC"}
@@ -0,0 +1,16 @@
1
+ import { AuthProvider } from "../auth/types";
2
+ import { CrittoraClientOptions, RequestContext, RequestOptions } from "../types";
3
+ export declare class HttpTransport {
4
+ private readonly context;
5
+ private readonly fetchImpl;
6
+ private readonly retryOptions;
7
+ constructor(context: RequestContext, options: Pick<CrittoraClientOptions, "fetch" | "retry">);
8
+ request<T>(request: RequestOptions, authProvider?: AuthProvider): Promise<T>;
9
+ private executeRequest;
10
+ private buildHeaders;
11
+ private unwrapBody;
12
+ private parseBody;
13
+ private extractErrorMessage;
14
+ private shouldRetry;
15
+ private delay;
16
+ }
@@ -0,0 +1,158 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.HttpTransport = void 0;
4
+ const errors_1 = require("../errors");
5
+ const DEFAULT_RETRY_OPTIONS = {
6
+ maxAttempts: 1,
7
+ backoffMs: 250,
8
+ retryOn: [429, 500, 502, 503, 504],
9
+ };
10
+ class HttpTransport {
11
+ constructor(context, options) {
12
+ this.context = context;
13
+ if (!options.fetch && typeof globalThis.fetch !== "function") {
14
+ throw new errors_1.ValidationError("No fetch implementation available. Use Node 18+ or pass `fetch` in the client options.");
15
+ }
16
+ this.fetchImpl = options.fetch ?? globalThis.fetch;
17
+ this.retryOptions = {
18
+ ...DEFAULT_RETRY_OPTIONS,
19
+ ...(options.retry ?? {}),
20
+ };
21
+ }
22
+ async request(request, authProvider) {
23
+ let lastError;
24
+ for (let attempt = 1; attempt <= this.retryOptions.maxAttempts; attempt += 1) {
25
+ try {
26
+ return await this.executeRequest(request, authProvider);
27
+ }
28
+ catch (error) {
29
+ lastError = error;
30
+ if (!this.shouldRetry(error, attempt)) {
31
+ throw error;
32
+ }
33
+ await this.delay(this.retryOptions.backoffMs * attempt);
34
+ }
35
+ }
36
+ throw lastError;
37
+ }
38
+ async executeRequest(request, authProvider) {
39
+ const controller = new AbortController();
40
+ const timeout = setTimeout(() => controller.abort(), this.context.timeoutMs);
41
+ try {
42
+ const headers = await this.buildHeaders(request.headers, authProvider);
43
+ const response = await this.fetchImpl(`${this.context.baseUrl}${request.path}`, {
44
+ method: request.method ?? "POST",
45
+ headers,
46
+ body: request.body === undefined ? undefined : JSON.stringify(request.body),
47
+ signal: request.signal ?? controller.signal,
48
+ });
49
+ const rawText = await response.text();
50
+ const parsedBody = this.parseBody(rawText);
51
+ const requestId = response.headers.get("x-request-id") ?? undefined;
52
+ if (!response.ok) {
53
+ const message = this.extractErrorMessage(parsedBody, response.status);
54
+ const errorOptions = {
55
+ status: response.status,
56
+ details: parsedBody,
57
+ requestId,
58
+ };
59
+ if (response.status === 429) {
60
+ throw new errors_1.RateLimitError(message, errorOptions);
61
+ }
62
+ throw new errors_1.RequestError(message, errorOptions);
63
+ }
64
+ return this.unwrapBody(parsedBody);
65
+ }
66
+ catch (error) {
67
+ if (error instanceof Error && error.name === "AbortError") {
68
+ throw new errors_1.RequestError("Request timed out", {
69
+ details: { timeoutMs: this.context.timeoutMs },
70
+ cause: error,
71
+ });
72
+ }
73
+ if (error instanceof errors_1.RequestError || error instanceof errors_1.RateLimitError) {
74
+ throw error;
75
+ }
76
+ throw new errors_1.RequestError("Request failed", { cause: error });
77
+ }
78
+ finally {
79
+ clearTimeout(timeout);
80
+ }
81
+ }
82
+ async buildHeaders(requestHeaders, authProvider) {
83
+ const headers = {
84
+ "Content-Type": "application/json",
85
+ ...this.context.headers,
86
+ ...requestHeaders,
87
+ };
88
+ if (this.context.userAgent) {
89
+ headers["User-Agent"] = this.context.userAgent;
90
+ }
91
+ if (this.context.credentials?.apiKey) {
92
+ headers["api_key"] = this.context.credentials.apiKey;
93
+ }
94
+ if (this.context.credentials?.accessKey) {
95
+ headers["access_key"] = this.context.credentials.accessKey;
96
+ }
97
+ if (this.context.credentials?.secretKey) {
98
+ headers["secret_key"] = this.context.credentials.secretKey;
99
+ }
100
+ const authorization = await authProvider?.getAuthorizationHeader();
101
+ if (authorization) {
102
+ headers.Authorization = authorization;
103
+ }
104
+ return headers;
105
+ }
106
+ unwrapBody(parsedBody) {
107
+ if (parsedBody &&
108
+ typeof parsedBody === "object" &&
109
+ "body" in parsedBody &&
110
+ typeof parsedBody.body === "string") {
111
+ return this.parseBody(parsedBody.body);
112
+ }
113
+ return parsedBody;
114
+ }
115
+ parseBody(rawText) {
116
+ if (!rawText) {
117
+ return undefined;
118
+ }
119
+ try {
120
+ return JSON.parse(rawText);
121
+ }
122
+ catch {
123
+ return rawText;
124
+ }
125
+ }
126
+ extractErrorMessage(body, status) {
127
+ if (typeof body === "string" && body.trim()) {
128
+ return body;
129
+ }
130
+ if (body && typeof body === "object") {
131
+ const candidate = body;
132
+ const message = candidate.message ??
133
+ candidate.error ??
134
+ candidate.error_description ??
135
+ candidate.detail;
136
+ if (typeof message === "string" && message.trim()) {
137
+ return message;
138
+ }
139
+ }
140
+ return `Request failed with status ${status}`;
141
+ }
142
+ shouldRetry(error, attempt) {
143
+ if (attempt >= this.retryOptions.maxAttempts) {
144
+ return false;
145
+ }
146
+ if (error instanceof errors_1.RequestError &&
147
+ typeof error.status === "number" &&
148
+ this.retryOptions.retryOn.includes(error.status)) {
149
+ return true;
150
+ }
151
+ return error instanceof errors_1.RateLimitError;
152
+ }
153
+ async delay(ms) {
154
+ await new Promise((resolve) => setTimeout(resolve, ms));
155
+ }
156
+ }
157
+ exports.HttpTransport = HttpTransport;
158
+ //# sourceMappingURL=httpTransport.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"httpTransport.js","sourceRoot":"","sources":["../../src/transport/httpTransport.ts"],"names":[],"mappings":";;;AAOA,sCAA0E;AAE1E,MAAM,qBAAqB,GAA2B;IACpD,WAAW,EAAE,CAAC;IACd,SAAS,EAAE,GAAG;IACd,OAAO,EAAE,CAAC,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE,GAAG,CAAC;CACnC,CAAC;AAEF,MAAa,aAAa;IAIxB,YACmB,OAAuB,EACxC,OAAuD;QADtC,YAAO,GAAP,OAAO,CAAgB;QAGxC,IAAI,CAAC,OAAO,CAAC,KAAK,IAAI,OAAO,UAAU,CAAC,KAAK,KAAK,UAAU,EAAE,CAAC;YAC7D,MAAM,IAAI,wBAAe,CACvB,wFAAwF,CACzF,CAAC;QACJ,CAAC;QAED,IAAI,CAAC,SAAS,GAAG,OAAO,CAAC,KAAK,IAAI,UAAU,CAAC,KAAK,CAAC;QACnD,IAAI,CAAC,YAAY,GAAG;YAClB,GAAG,qBAAqB;YACxB,GAAG,CAAC,OAAO,CAAC,KAAK,IAAI,EAAE,CAAC;SACzB,CAAC;IACJ,CAAC;IAED,KAAK,CAAC,OAAO,CACX,OAAuB,EACvB,YAA2B;QAE3B,IAAI,SAAkB,CAAC;QAEvB,KACE,IAAI,OAAO,GAAG,CAAC,EACf,OAAO,IAAI,IAAI,CAAC,YAAY,CAAC,WAAW,EACxC,OAAO,IAAI,CAAC,EACZ,CAAC;YACD,IAAI,CAAC;gBACH,OAAO,MAAM,IAAI,CAAC,cAAc,CAAI,OAAO,EAAE,YAAY,CAAC,C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E,CAAC,UAAU,CAAC,OAAO,EAAE,EAAE,CAAC,CAAC,CAAC;IAC1D,CAAC;CACF;AA7MD,sCA6MC"}
@@ -0,0 +1,101 @@
1
+ export type FetchLike = typeof globalThis.fetch;
2
+ export interface ApiCredentials {
3
+ apiKey: string;
4
+ accessKey?: string;
5
+ secretKey?: string;
6
+ }
7
+ export interface RetryOptions {
8
+ maxAttempts?: number;
9
+ backoffMs?: number;
10
+ retryOn?: number[];
11
+ }
12
+ export interface BearerAuthConfig {
13
+ type: "bearer";
14
+ token: string;
15
+ }
16
+ export interface Permission {
17
+ partnerId: string;
18
+ actions: string[];
19
+ }
20
+ export interface EncryptInput {
21
+ data: string;
22
+ permissions?: Permission[];
23
+ }
24
+ export interface EncryptResult {
25
+ encryptedData: string;
26
+ transactionId?: string;
27
+ }
28
+ export interface DecryptInput {
29
+ encryptedData: string;
30
+ permissions?: Permission[];
31
+ }
32
+ export interface DecryptResult {
33
+ decryptedData: string;
34
+ transactionId?: string;
35
+ }
36
+ export interface DecryptVerifyInput {
37
+ encryptedData: string;
38
+ permissions?: Permission[];
39
+ }
40
+ export interface DecryptVerifyResult {
41
+ decryptedData: string;
42
+ isValidSignature: boolean;
43
+ transactionId?: string;
44
+ }
45
+ export interface AuthTokens {
46
+ idToken: string;
47
+ accessToken: string;
48
+ refreshToken: string;
49
+ }
50
+ export interface CognitoAuthConfig {
51
+ userPoolId: string;
52
+ clientId: string;
53
+ username?: string;
54
+ password?: string;
55
+ }
56
+ export interface CrittoraClientOptions {
57
+ baseUrl?: string;
58
+ credentials?: ApiCredentials;
59
+ auth?: BearerAuthConfig | import("./auth/types").AuthProvider;
60
+ fetch?: FetchLike;
61
+ timeoutMs?: number;
62
+ retry?: RetryOptions;
63
+ headers?: Record<string, string>;
64
+ userAgent?: string;
65
+ }
66
+ export interface RequestOptions {
67
+ path: string;
68
+ method?: "POST";
69
+ body?: unknown;
70
+ headers?: Record<string, string>;
71
+ signal?: AbortSignal;
72
+ }
73
+ export interface RequestContext {
74
+ baseUrl: string;
75
+ credentials?: ApiCredentials;
76
+ headers?: Record<string, string>;
77
+ timeoutMs: number;
78
+ userAgent?: string;
79
+ }
80
+ export interface WirePermission {
81
+ partner_id: string;
82
+ permissions: string[];
83
+ }
84
+ export interface WireEncryptResult {
85
+ encrypted_data: string;
86
+ transactionId?: string;
87
+ }
88
+ export interface WireDecryptResult {
89
+ decrypted_data: string;
90
+ transactionId?: string;
91
+ }
92
+ export interface WireDecryptVerifyResult {
93
+ decrypted_data: string;
94
+ is_valid_signature: boolean;
95
+ transactionId?: string;
96
+ }
97
+ export interface LegacyAuthResponse {
98
+ IdToken: string;
99
+ AccessToken: string;
100
+ RefreshToken: string;
101
+ }
package/dist/types.js ADDED
@@ -0,0 +1,3 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ //# sourceMappingURL=types.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.js","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":""}