@nestuge/sdk 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 Nestuge
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,203 @@
1
+ # @nestuge/sdk
2
+
3
+ Official TypeScript/JavaScript SDK for the [Nestuge](https://nestuge.com) Developer API.
4
+ Fully typed, zero runtime dependencies, and runs anywhere `fetch` does — Node.js ≥ 18, Bun, Deno, browsers, and edge runtimes.
5
+
6
+ ## Installation
7
+
8
+ ```bash
9
+ npm install @nestuge/sdk
10
+ # or
11
+ pnpm add @nestuge/sdk
12
+ # or
13
+ yarn add @nestuge/sdk
14
+ ```
15
+
16
+ ## Quickstart
17
+
18
+ Create an API key in your Nestuge dashboard under **Settings → Developer**, then:
19
+
20
+ ```ts
21
+ import Nestuge from '@nestuge/sdk';
22
+
23
+ const nestuge = new Nestuge({ apiKey: process.env.NESTUGE_API_KEY! });
24
+
25
+ // Verify your key works
26
+ const { brandID, mode, scopes } = await nestuge.ping();
27
+
28
+ // List your customers
29
+ const { items } = await nestuge.customers.list({ limit: 50 });
30
+ ```
31
+
32
+ Keys look like `nk_live_…` (production) or `nk_test_…` (test mode — requests validate and return previews without persisting live data).
33
+
34
+ ## Configuration
35
+
36
+ ```ts
37
+ const nestuge = new Nestuge({
38
+ apiKey: 'nk_live_…', // required
39
+ baseUrl: 'https://nestuge.com', // default; use https://stg.nestuge.com for staging
40
+ maxRetries: 2, // extra attempts on 429/5xx/network errors
41
+ timeoutMs: 30_000, // per-attempt timeout
42
+ fetch: customFetch, // optional fetch override (proxies, testing)
43
+ });
44
+ ```
45
+
46
+ ## Resources
47
+
48
+ | Resource | Methods | Required scope |
49
+ |---|---|---|
50
+ | `nestuge.ping()` | — | none |
51
+ | `nestuge.customers` | `list`, `retrieve`, `iterate` | `customers:read` |
52
+ | `nestuge.products` | `list`, `retrieve`, `create`, `iterate` | `products:read` / `products:write` |
53
+ | `nestuge.purchases` | `list`, `retrieve`, `iterate` | `purchases:read` |
54
+ | `nestuge.transactions` | `list`, `retrieve`, `iterate` | `transactions:read` |
55
+ | `nestuge.events` | `list`, `retrieve`, `create`, `iterate` | `events:read` / `events:write` |
56
+ | `nestuge.tickets` | `retrieve`, `checkIn` | `purchases:read` / `purchases:write` |
57
+ | `nestuge.webhooks` | `create`, `list`, `retrieve`, `update`, `del`, `test`, `iterate`, `verifySignature`, `constructEvent` | `webhooks:write` |
58
+ | `nestuge.webhooks.deliveries` | `list`, `retrieve`, `resend`, `iterate` | `webhooks:write` |
59
+
60
+ ### Pagination
61
+
62
+ `list()` returns one page: `{ items, hasMore, nextCursor }`. Pass `nextCursor` back as `cursor` for the next page — or let `iterate()` do it for you:
63
+
64
+ ```ts
65
+ // Manual paging
66
+ let page = await nestuge.purchases.list({ limit: 100 });
67
+ while (page.hasMore) {
68
+ page = await nestuge.purchases.list({ limit: 100, cursor: page.nextCursor! });
69
+ }
70
+
71
+ // Auto paging
72
+ for await (const purchase of nestuge.purchases.iterate()) {
73
+ console.log(purchase.id, purchase.title);
74
+ }
75
+ ```
76
+
77
+ ### Creating a product
78
+
79
+ ```ts
80
+ const product = await nestuge.products.create({
81
+ type: 'event',
82
+ title: 'Live Masterclass',
83
+ description: 'A 2-hour deep dive.',
84
+ imageUrl: 'https://example.com/cover.png',
85
+ price: { amount: 5000, currency: 'NGN' },
86
+ event: {
87
+ eventType: 'Zoom',
88
+ dateTime: '2026-08-01T18:00:00.000Z',
89
+ },
90
+ });
91
+ console.log(product.url); // shareable sales page
92
+ ```
93
+
94
+ ### Retrieving a product
95
+
96
+ `retrieve()` returns a `ProductDetail` — the product plus its pricing `tiers` (payment plans) and `items` (content). `list()` returns the lighter `Product` shape without them.
97
+
98
+ ```ts
99
+ const product = await nestuge.products.retrieve('reg_…');
100
+
101
+ for (const tier of product.tiers) {
102
+ console.log(tier.name, tier.price.amount, tier.price.currency, tier.perks);
103
+ }
104
+ for (const item of product.items) {
105
+ console.log(item.title, item.type); // descriptive only — deliverables are never exposed
106
+ }
107
+ ```
108
+
109
+ ### Checking in an event ticket
110
+
111
+ ```ts
112
+ const ticket = await nestuge.tickets.checkIn('tkt_…');
113
+ console.log(ticket.isCheckedIn, ticket.checkinTime);
114
+ ```
115
+
116
+ ## Webhooks
117
+
118
+ Register an endpoint and store the signing secret — it is returned **once** and can never be retrieved again:
119
+
120
+ ```ts
121
+ const endpoint = await nestuge.webhooks.create({
122
+ url: 'https://example.com/webhooks/nestuge',
123
+ events: ['transaction.succeeded', 'member.joined'],
124
+ });
125
+ await saveSecret(endpoint.signingSecret);
126
+ ```
127
+
128
+ Verify and handle incoming events (Express example — note the **raw** body):
129
+
130
+ ```ts
131
+ import express from 'express';
132
+ import { constructEvent, WebhookVerificationError } from '@nestuge/sdk';
133
+
134
+ const app = express();
135
+
136
+ app.post('/webhooks/nestuge', express.raw({ type: 'application/json' }), async (req, res) => {
137
+ try {
138
+ const event = await constructEvent({
139
+ payload: req.body.toString('utf8'),
140
+ signature: req.header('X-Nestuge-Signature') ?? '',
141
+ secret: process.env.NESTUGE_WEBHOOK_SECRET!,
142
+ });
143
+
144
+ switch (event.type) {
145
+ case 'transaction.succeeded':
146
+ console.log(`Payment of ${event.data.amount} ${event.data.currency}`); // data: Transaction
147
+ break;
148
+ case 'member.joined':
149
+ console.log(`${event.data.name} joined hub ${event.data.hubID}`); // data: Customer
150
+ break;
151
+ }
152
+ res.sendStatus(200);
153
+ } catch (err) {
154
+ if (err instanceof WebhookVerificationError) return res.sendStatus(400);
155
+ throw err;
156
+ }
157
+ });
158
+ ```
159
+
160
+ `event` is a discriminated union — narrowing on `event.type` types `event.data` as a `Transaction` (`transaction.*`, `payout.settled`), `Purchase` (`subscription.*`), or `Customer` (`member.*`).
161
+
162
+ Send yourself a sample event any time:
163
+
164
+ ```ts
165
+ await nestuge.webhooks.test({ eventType: 'transaction.succeeded' });
166
+ ```
167
+
168
+ ## Error handling
169
+
170
+ Every failed request throws a typed subclass of `NestugeError`:
171
+
172
+ ```ts
173
+ import { NotFoundError, PermissionError, RateLimitError } from '@nestuge/sdk';
174
+
175
+ try {
176
+ await nestuge.products.retrieve('reg_missing');
177
+ } catch (err) {
178
+ if (err instanceof NotFoundError) {
179
+ // 404 — doesn't exist or belongs to another brand
180
+ } else if (err instanceof PermissionError) {
181
+ // 403 — key is missing a scope, e.g. "products:read"
182
+ } else if (err instanceof RateLimitError) {
183
+ // 429 — err.retryAfter = seconds until the window resets
184
+ }
185
+ }
186
+ ```
187
+
188
+ | Error | When |
189
+ |---|---|
190
+ | `ValidationError` | 400 — bad body or params |
191
+ | `AuthenticationError` | 401 — missing/invalid/revoked key |
192
+ | `PermissionError` | 403 — key lacks a required scope |
193
+ | `NotFoundError` | 404 — not found (or another brand's resource) |
194
+ | `RateLimitError` | 429 — over 120 req/min (`retryAfter` in seconds) |
195
+ | `ServerError` | 5xx |
196
+ | `ConnectionError` | network failure or timeout |
197
+ | `WebhookVerificationError` | webhook signature check failed |
198
+
199
+ Retries on 429/5xx/network errors are automatic (default 2, exponential backoff, honors `Retry-After`).
200
+
201
+ ## License
202
+
203
+ MIT