@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
package/docs/API.md ADDED
@@ -0,0 +1,225 @@
1
+ # API Reference
2
+
3
+ This document describes the public SDK surface exposed by `@crittora/sdk-js`.
4
+
5
+ ## Exports
6
+
7
+ Primary exports:
8
+
9
+ - `CrittoraClient`
10
+ - `Crittora` (legacy compatibility shim)
11
+ - `bearerToken`
12
+ - `cognitoAuthProvider`
13
+ - `CrittoraError`
14
+ - `ValidationError`
15
+ - `AuthError`
16
+ - `RequestError`
17
+ - `RateLimitError`
18
+ - `EncryptError`
19
+ - `DecryptError`
20
+
21
+ ## CrittoraClient
22
+
23
+ ### Constructor
24
+
25
+ ```ts
26
+ new CrittoraClient(options?: CrittoraClientOptions)
27
+ ```
28
+
29
+ ### Options
30
+
31
+ ```ts
32
+ type CrittoraClientOptions = {
33
+ baseUrl?: string;
34
+ credentials?: ApiCredentials;
35
+ auth?: BearerAuthConfig | AuthProvider;
36
+ fetch?: typeof globalThis.fetch;
37
+ timeoutMs?: number;
38
+ retry?: RetryOptions;
39
+ headers?: Record<string, string>;
40
+ userAgent?: string;
41
+ };
42
+ ```
43
+
44
+ ### Methods
45
+
46
+ #### `encrypt(input)`
47
+
48
+ ```ts
49
+ encrypt(input: EncryptInput): Promise<EncryptResult>
50
+ ```
51
+
52
+ Input:
53
+
54
+ ```ts
55
+ type EncryptInput = {
56
+ data: string;
57
+ permissions?: Permission[];
58
+ };
59
+ ```
60
+
61
+ Result:
62
+
63
+ ```ts
64
+ type EncryptResult = {
65
+ encryptedData: string;
66
+ transactionId?: string;
67
+ };
68
+ ```
69
+
70
+ #### `decrypt(input)`
71
+
72
+ ```ts
73
+ decrypt(input: DecryptInput): Promise<DecryptResult>
74
+ ```
75
+
76
+ Input:
77
+
78
+ ```ts
79
+ type DecryptInput = {
80
+ encryptedData: string;
81
+ permissions?: Permission[];
82
+ };
83
+ ```
84
+
85
+ Result:
86
+
87
+ ```ts
88
+ type DecryptResult = {
89
+ decryptedData: string;
90
+ transactionId?: string;
91
+ };
92
+ ```
93
+
94
+ #### `decryptVerify(input)`
95
+
96
+ ```ts
97
+ decryptVerify(input: DecryptVerifyInput): Promise<DecryptVerifyResult>
98
+ ```
99
+
100
+ Input:
101
+
102
+ ```ts
103
+ type DecryptVerifyInput = {
104
+ encryptedData: string;
105
+ permissions?: Permission[];
106
+ };
107
+ ```
108
+
109
+ Result:
110
+
111
+ ```ts
112
+ type DecryptVerifyResult = {
113
+ decryptedData: string;
114
+ isValidSignature: boolean;
115
+ transactionId?: string;
116
+ };
117
+ ```
118
+
119
+ #### `withAuth(auth)`
120
+
121
+ ```ts
122
+ withAuth(auth: AuthProvider | BearerAuthConfig): CrittoraClient
123
+ ```
124
+
125
+ Creates a new client instance with the same transport and credential configuration, but with a different auth source.
126
+
127
+ #### `auth`
128
+
129
+ ```ts
130
+ get auth(): AuthProvider | undefined
131
+ ```
132
+
133
+ Exposes the resolved auth provider associated with the client instance.
134
+
135
+ ## Auth
136
+
137
+ ### `bearerToken(token)`
138
+
139
+ ```ts
140
+ bearerToken(token: string): AuthProvider
141
+ ```
142
+
143
+ Creates an auth provider that always returns `Authorization: Bearer <token>`.
144
+
145
+ ### `cognitoAuthProvider(config)`
146
+
147
+ ```ts
148
+ cognitoAuthProvider(config: CognitoAuthConfig): CognitoAuthProvider
149
+ ```
150
+
151
+ Configuration:
152
+
153
+ ```ts
154
+ type CognitoAuthConfig = {
155
+ userPoolId: string;
156
+ clientId: string;
157
+ username?: string;
158
+ password?: string;
159
+ };
160
+ ```
161
+
162
+ Methods:
163
+
164
+ ```ts
165
+ getAuthorizationHeader(): Promise<string | undefined>
166
+ login(credentials?: { username: string; password: string }): Promise<AuthTokens>
167
+ ```
168
+
169
+ Result:
170
+
171
+ ```ts
172
+ type AuthTokens = {
173
+ idToken: string;
174
+ accessToken: string;
175
+ refreshToken: string;
176
+ };
177
+ ```
178
+
179
+ ## Legacy API
180
+
181
+ ### `Crittora`
182
+
183
+ The `Crittora` class is preserved for compatibility with existing consumers.
184
+
185
+ Methods:
186
+
187
+ ```ts
188
+ authenticate(username: string, password: string): Promise<LegacyAuthResponse>
189
+ encrypt(idToken: string, data: string, permissions?: string[]): Promise<string>
190
+ decrypt(idToken: string, encryptedData: string, permissions?: string[]): Promise<string>
191
+ decryptVerify(
192
+ idToken: string,
193
+ encryptedData: string,
194
+ permissions?: string[]
195
+ ): Promise<{ decrypted_data: string; is_valid_signature: boolean }>
196
+ ```
197
+
198
+ This interface is transitional. New integrations should use `CrittoraClient`.
199
+
200
+ ## Errors
201
+
202
+ ### `CrittoraError`
203
+
204
+ Base SDK error with:
205
+
206
+ ```ts
207
+ type CrittoraError = Error & {
208
+ code: string;
209
+ status?: number;
210
+ requestId?: string;
211
+ details?: unknown;
212
+ cause?: unknown;
213
+ }
214
+ ```
215
+
216
+ ### Specializations
217
+
218
+ - `ValidationError`
219
+ - `AuthError`
220
+ - `RequestError`
221
+ - `RateLimitError`
222
+ - `EncryptError`
223
+ - `DecryptError`
224
+
225
+ Use these types for programmatic handling rather than parsing error messages.
@@ -0,0 +1,83 @@
1
+ # Architecture Notes
2
+
3
+ This document explains the SDK structure and the design decisions behind the current implementation.
4
+
5
+ ## Goals
6
+
7
+ The SDK is designed to be:
8
+
9
+ - explicit in configuration
10
+ - safe to run in multi-tenant or multi-environment processes
11
+ - adaptable to different auth models
12
+ - operationally predictable under failure
13
+
14
+ ## Layering
15
+
16
+ The package is split into a few clear layers:
17
+
18
+ ### Client
19
+
20
+ [`src/client.ts`](../src/client.ts)
21
+
22
+ `CrittoraClient` owns client configuration and exposes the public resource methods. It does not own process-global state.
23
+
24
+ ### Auth
25
+
26
+ [`src/auth/`](../src/auth)
27
+
28
+ Auth providers are responsible for supplying `Authorization` headers. The client accepts either a bearer config or a concrete `AuthProvider`.
29
+
30
+ Built-in options:
31
+
32
+ - `bearerToken(...)`
33
+ - `cognitoAuthProvider(...)`
34
+
35
+ ### Transport
36
+
37
+ [`src/transport/httpTransport.ts`](../src/transport/httpTransport.ts)
38
+
39
+ The transport layer is responsible for:
40
+
41
+ - request construction
42
+ - timeout enforcement
43
+ - retries
44
+ - response parsing
45
+ - error normalization
46
+
47
+ It accepts both standard JSON responses and legacy API Gateway style payloads that wrap JSON in a `body` string.
48
+
49
+ ### Resources
50
+
51
+ [`src/resources/crypto.ts`](../src/resources/crypto.ts)
52
+
53
+ Resource classes translate public SDK models into backend wire payloads and map transport failures into operation-specific SDK errors.
54
+
55
+ ## Model translation
56
+
57
+ Public SDK models use camelCase because they are intended for direct application use in JavaScript and TypeScript.
58
+
59
+ Backend payloads still use snake_case. The SDK keeps that translation internal so wire-format concerns do not leak into consumer code.
60
+
61
+ Examples:
62
+
63
+ - `encryptedData` -> `encrypted_data`
64
+ - `partnerId` -> `partner_id`
65
+ - `isValidSignature` -> `is_valid_signature`
66
+
67
+ ## Error strategy
68
+
69
+ The SDK exposes one base error and several specializations so callers can respond at the right layer:
70
+
71
+ - validation failures
72
+ - auth failures
73
+ - transport or HTTP failures
74
+ - rate limiting
75
+ - operation-level failures
76
+
77
+ Errors preserve status, details, and request identifiers where available.
78
+
79
+ ## Compatibility strategy
80
+
81
+ The legacy `Crittora` class remains in the package as a migration bridge. It adapts older positional-call semantics to the new client internally.
82
+
83
+ This avoids a forced flag day while still moving the primary API toward a better SDK contract.
@@ -0,0 +1,187 @@
1
+ # Migration Guide
2
+
3
+ This guide covers migration from the legacy `Crittora` API to `CrittoraClient`.
4
+
5
+ ## Why migrate
6
+
7
+ The v2 client gives you:
8
+
9
+ - isolated client instances
10
+ - explicit environment and credential wiring
11
+ - typed object-based inputs
12
+ - cleaner auth composition
13
+ - structured error handling
14
+
15
+ ## Before and after
16
+
17
+ ### Construction
18
+
19
+ Before:
20
+
21
+ ```ts
22
+ import { Crittora } from "@crittora/sdk-js";
23
+
24
+ const sdk = new Crittora();
25
+ ```
26
+
27
+ After:
28
+
29
+ ```ts
30
+ import { CrittoraClient } from "@crittora/sdk-js";
31
+
32
+ const client = new CrittoraClient({
33
+ baseUrl: "https://api.crittoraapis.com",
34
+ credentials: {
35
+ apiKey: process.env.CRITTORA_API_KEY!,
36
+ accessKey: process.env.CRITTORA_ACCESS_KEY!,
37
+ secretKey: process.env.CRITTORA_SECRET_KEY!,
38
+ },
39
+ });
40
+ ```
41
+
42
+ ### Authentication
43
+
44
+ Before:
45
+
46
+ ```ts
47
+ const { IdToken } = await sdk.authenticate(username, password);
48
+ ```
49
+
50
+ After:
51
+
52
+ ```ts
53
+ import { cognitoAuthProvider } from "@crittora/sdk-js";
54
+
55
+ const auth = cognitoAuthProvider({
56
+ userPoolId: "us-east-1_Tmljk4Uiw",
57
+ clientId: "5cvaao4qgphfp38g433vi5e82u",
58
+ });
59
+
60
+ const { idToken } = await auth.login({ username, password });
61
+ ```
62
+
63
+ ### Encrypt
64
+
65
+ Before:
66
+
67
+ ```ts
68
+ const encrypted = await sdk.encrypt(idToken, data, ["read"]);
69
+ ```
70
+
71
+ After:
72
+
73
+ ```ts
74
+ const encrypted = await client
75
+ .withAuth({ type: "bearer", token: idToken })
76
+ .encrypt({
77
+ data,
78
+ permissions: [
79
+ {
80
+ partnerId: "default",
81
+ actions: ["read"],
82
+ },
83
+ ],
84
+ });
85
+ ```
86
+
87
+ ### Decrypt
88
+
89
+ Before:
90
+
91
+ ```ts
92
+ const decrypted = await sdk.decrypt(idToken, encryptedData);
93
+ ```
94
+
95
+ After:
96
+
97
+ ```ts
98
+ const decrypted = await client
99
+ .withAuth({ type: "bearer", token: idToken })
100
+ .decrypt({
101
+ encryptedData,
102
+ });
103
+ ```
104
+
105
+ ### Decrypt and verify
106
+
107
+ Before:
108
+
109
+ ```ts
110
+ const result = await sdk.decryptVerify(idToken, encryptedData);
111
+ console.log(result.decrypted_data, result.is_valid_signature);
112
+ ```
113
+
114
+ After:
115
+
116
+ ```ts
117
+ const result = await client
118
+ .withAuth({ type: "bearer", token: idToken })
119
+ .decryptVerify({
120
+ encryptedData,
121
+ });
122
+
123
+ console.log(result.decryptedData, result.isValidSignature);
124
+ ```
125
+
126
+ ## Type changes
127
+
128
+ Main public naming changes:
129
+
130
+ - `encrypted_data` -> `encryptedData`
131
+ - `decrypted_data` -> `decryptedData`
132
+ - `is_valid_signature` -> `isValidSignature`
133
+
134
+ Permission changes:
135
+
136
+ Before:
137
+
138
+ ```ts
139
+ ["read", "write"]
140
+ ```
141
+
142
+ After:
143
+
144
+ ```ts
145
+ [
146
+ {
147
+ partnerId: "default",
148
+ actions: ["read", "write"],
149
+ },
150
+ ]
151
+ ```
152
+
153
+ ## Error handling changes
154
+
155
+ Before, callers often had to catch broad errors.
156
+
157
+ After, callers can branch on typed SDK errors:
158
+
159
+ ```ts
160
+ import { DecryptError, RateLimitError, RequestError } from "@crittora/sdk-js";
161
+
162
+ try {
163
+ await client.decrypt({ encryptedData });
164
+ } catch (error) {
165
+ if (error instanceof RateLimitError) {
166
+ // handle throttling
167
+ } else if (error instanceof DecryptError) {
168
+ // handle decrypt failure
169
+ } else if (error instanceof RequestError) {
170
+ // handle transport or non-2xx responses
171
+ } else {
172
+ throw error;
173
+ }
174
+ }
175
+ ```
176
+
177
+ ## Transitional strategy
178
+
179
+ Recommended migration sequence:
180
+
181
+ 1. Replace `new Crittora()` with `new CrittoraClient(...)`.
182
+ 2. Move token acquisition into an auth provider or your own auth layer.
183
+ 3. Convert positional method calls to object inputs.
184
+ 4. Update call sites to camelCase response fields.
185
+ 5. Replace generic error handling with typed error handling.
186
+
187
+ The legacy `Crittora` export remains available for staged migrations, but new code should not be added against it.
package/package.json ADDED
@@ -0,0 +1,82 @@
1
+ {
2
+ "name": "@crittora/sdk-js",
3
+ "version": "2.0.0",
4
+ "description": "Crittora JavaScript SDK",
5
+ "main": "./dist/index.js",
6
+ "module": "./dist/index.js",
7
+ "types": "./dist/index.d.ts",
8
+ "files": [
9
+ "dist",
10
+ "CHANGELOG.md",
11
+ "docs",
12
+ "LICENSE",
13
+ "README.md"
14
+ ],
15
+ "scripts": {
16
+ "test": "jest",
17
+ "clean": "rimraf dist",
18
+ "build": "NODE_ENV=production tsc",
19
+ "prepare": "NODE_ENV=production npm run build",
20
+ "prepublishOnly": "npm run verify",
21
+ "prepack": "NODE_ENV=production npm run build",
22
+ "lint": "eslint . --ext .ts",
23
+ "verify": "npm run clean && npm run build && npm run test",
24
+ "release": "npm run verify && npm version patch && npm publish",
25
+ "release:patch": "npm run verify && npm version patch && npm publish",
26
+ "release:minor": "npm run verify && npm version minor && npm publish",
27
+ "release:major": "npm run verify && npm version major && npm publish"
28
+ },
29
+ "keywords": [
30
+ "encryption",
31
+ "signing",
32
+ "sdk",
33
+ "cryptography",
34
+ "crittora"
35
+ ],
36
+ "author": "Erik Rowan <erik@crittora.com>, Gerardo I. Ornelas <gerardo@crittora.com>",
37
+ "license": "MIT",
38
+ "type": "commonjs",
39
+ "exports": {
40
+ ".": {
41
+ "types": "./dist/index.d.ts",
42
+ "require": "./dist/index.js",
43
+ "default": "./dist/index.js"
44
+ },
45
+ "./auth-cognito": {
46
+ "types": "./dist/auth/cognito.d.ts",
47
+ "require": "./dist/auth/cognito.js",
48
+ "default": "./dist/auth/cognito.js"
49
+ },
50
+ "./errors": {
51
+ "types": "./dist/errors.d.ts",
52
+ "require": "./dist/errors.js",
53
+ "default": "./dist/errors.js"
54
+ }
55
+ },
56
+ "dependencies": {
57
+ "amazon-cognito-identity-js": "^6.3.12"
58
+ },
59
+ "devDependencies": {
60
+ "@types/jest": "^29.5.12",
61
+ "@types/node": "^20.14.10",
62
+ "jest": "^29.7.0",
63
+ "prettier": "^2.8.0",
64
+ "rimraf": "^5.0.0",
65
+ "ts-jest": "^29.2.2",
66
+ "typescript": "^5.5.3"
67
+ },
68
+ "repository": {
69
+ "type": "git",
70
+ "url": "git+https://github.com/Crittora/crittora-sdk-js.git"
71
+ },
72
+ "bugs": {
73
+ "url": "https://github.com/Crittora/crittora-sdk-js/issues"
74
+ },
75
+ "homepage": "https://github.com/Crittora/crittora-sdk-js#readme",
76
+ "publishConfig": {
77
+ "access": "public"
78
+ },
79
+ "engines": {
80
+ "node": ">=18.0.0"
81
+ }
82
+ }