@spreadspace/embed 0.1.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 ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 SpreadSpace
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,128 @@
1
+ # @spreadspace/embed
2
+
3
+ Server-side Node.js SDK for [SpreadSpace](https://spreadspace.app) embeds —
4
+ mints the short-lived embed sessions that authorize the browser widget, and
5
+ verifies SpreadSpace webhooks. For general API access from TypeScript, use
6
+ [`@spreadspace/sdk`](https://www.npmjs.com/package/@spreadspace/sdk).
7
+
8
+ ```bash
9
+ npm install @spreadspace/embed
10
+ # or
11
+ pnpm add @spreadspace/embed
12
+ ```
13
+
14
+ Server-side use only. Don't ship this package to a browser — API keys must
15
+ never appear in client code. For browser embedding, use the
16
+ `<SpreadSpaceReview />` widget with a short-lived embed token minted via this
17
+ SDK.
18
+
19
+ ## Quickstart
20
+
21
+ ### 1. Mint an embed token
22
+
23
+ ```ts
24
+ import { SpreadSpaceClient } from '@spreadspace/embed';
25
+
26
+ const client = new SpreadSpaceClient({
27
+ apiKey: process.env.SPREADSPACE_API_KEY!, // 'ss_live_...' or 'ss_test_...'
28
+ });
29
+
30
+ const session = await client.embed.sessions.create({
31
+ loan_id: 'loan_abc',
32
+ });
33
+
34
+ // Pass session.embed_token to the browser. It is scoped to this single loan
35
+ // and expires automatically.
36
+ ```
37
+
38
+ ### 2. Verify a webhook
39
+
40
+ ```ts
41
+ import { verifyAndParseWebhook } from '@spreadspace/embed/webhooks';
42
+
43
+ app.post('/webhooks/spreadspace', async (req, res) => {
44
+ const event = verifyAndParseWebhook(
45
+ req.rawBody, // the exact bytes received — not re-stringified JSON
46
+ req.headers['spreadspace-signature'] as string,
47
+ process.env.SPREADSPACE_WEBHOOK_SECRET!,
48
+ );
49
+
50
+ switch (event.type) {
51
+ case 'extraction.ready':
52
+ // event.data is typed as ExtractionReadyPayload
53
+ await markExtractionReady(event.data.loan_id, event.data.extraction_id);
54
+ break;
55
+ case 'job.completed':
56
+ // event.data is typed as JobCompletedPayload
57
+ break;
58
+ }
59
+ res.status(200).end();
60
+ });
61
+ ```
62
+
63
+ ### 3. Iterate over loans
64
+
65
+ ```ts
66
+ for await (const loan of client.loans.list({ borrower_id: 'br_xyz' })) {
67
+ console.log(loan.id, loan.borrower_id);
68
+ }
69
+ ```
70
+
71
+ The list iterator handles cursor pagination automatically — no manual
72
+ `next_cursor` plumbing.
73
+
74
+ ## What you get
75
+
76
+ - **Typed client** for every endpoint, organized into Stripe-style
77
+ resources (`client.loans`, `client.borrowers`, `client.documents`, etc.).
78
+ - **Automatic retries** on `429` and `5xx` responses with exponential
79
+ backoff and full jitter; honors `Retry-After`.
80
+ - **Auto-generated `Idempotency-Key`** on every non-GET request so retries
81
+ are safe by default. Override or suppress per-request.
82
+ - **Auto-pinned `SpreadSpace-Version`** header — the SDK ships with the API
83
+ surface it was built against.
84
+ - **Webhook signature verifier** — byte-for-byte compatible with the
85
+ server-side signer, available as a tree-shakeable
86
+ `@spreadspace/embed/webhooks` import for receiver Lambdas.
87
+ - **Async iterators** for paginated list endpoints (`for await`,
88
+ `.toArray()`, or `.pages()` for batch processing).
89
+ - **Typed errors** —
90
+ `SpreadSpaceError` / `RateLimitError` / `PermissionError` / etc. — for
91
+ `instanceof`-based handling.
92
+
93
+ ## Configuration
94
+
95
+ ```ts
96
+ new SpreadSpaceClient({
97
+ apiKey: 'ss_live_...',
98
+ baseUrl: 'https://api.spreadspace.app', // override for staging / local
99
+ apiVersion: '2026-05-03', // override the SDK-pinned version
100
+ timeout: 30_000, // ms per HTTP attempt
101
+ maxRetries: 3, // retry budget for 429 / 5xx
102
+ });
103
+ ```
104
+
105
+ ## Errors
106
+
107
+ ```ts
108
+ import { SpreadSpaceClient, RateLimitError, PermissionError } from '@spreadspace/embed';
109
+
110
+ try {
111
+ await client.borrowers.retrieve('br_xyz');
112
+ } catch (err) {
113
+ if (err instanceof PermissionError && err.type === 'pii_claim_required') {
114
+ // err.details.borrower_id, err.details.claim_endpoint are set
115
+ } else if (err instanceof RateLimitError) {
116
+ // SDK already retried `maxRetries` times — the integrator may want to
117
+ // back off harder or surface to the caller.
118
+ }
119
+ }
120
+ ```
121
+
122
+ Every thrown `SpreadSpaceError` carries `requestId` (the server-generated
123
+ `X-Request-ID`), `type` (the canonical error-type string from the API
124
+ envelope), and `statusCode`. Quote `requestId` in support tickets.
125
+
126
+ ## License
127
+
128
+ MIT.