@usenadia/node 0.1.0 → 0.2.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/README.md CHANGED
@@ -7,33 +7,60 @@ manage channels, and drive the Instagram comment/story → DM machinery.
7
7
  pnpm add @usenadia/node
8
8
  ```
9
9
 
10
+ <!-- smoke:start -->
10
11
  ```ts
11
12
  import { Nadia, NadiaError } from '@usenadia/node';
12
13
 
13
- const nadia = new Nadia(process.env.USENADIA_API_KEY!);
14
+ const nadia = new Nadia(process.env.NADIA_API_KEY ?? '');
14
15
 
15
16
  const channels = await nadia.integrations();
17
+ const instagram = channels.find((c) => c.identifier === 'instagram-standalone');
18
+ if (!instagram) throw new Error('Connect an Instagram account first.');
16
19
 
17
20
  try {
18
- await nadia.post({
19
- type: 'schedule',
20
- date: '2026-10-01T09:00:00.000Z',
21
+ const [{ postId }] = await nadia.post({
22
+ type: 'draft', // or 'schedule' / 'now'
23
+ date: '2030-01-15T09:00:00.000Z',
21
24
  shortLink: false,
22
25
  tags: [],
23
26
  posts: [
24
27
  {
25
- integration: { id: channels[0].id },
28
+ integration: { id: instagram.id },
26
29
  value: [{ content: 'Hello from the SDK', image: [] }],
27
- settings: {},
30
+ // Each network has its own settings; Instagram wants `post_type`.
31
+ settings: { __type: 'instagram-standalone', post_type: 'post' },
28
32
  },
29
33
  ],
30
34
  });
35
+
36
+ await nadia.deletePost(postId);
31
37
  } catch (err) {
32
38
  if (err instanceof NadiaError) {
33
39
  console.error(err.status, err.message); // 402 "You have reached your limit"
34
40
  }
41
+ throw err;
35
42
  }
36
43
  ```
44
+ <!-- smoke:end -->
45
+
46
+ This block is run as is against the real API by the smoke test
47
+ (`test/smoke.test.mjs`): if it stops working, the release stops.
48
+
49
+ ## Uploading a file
50
+
51
+ ```ts
52
+ const media = await nadia.upload('./reel.mp4'); // a path, or upload(blob, 'reel.mp4')
53
+ media.path; // what a post takes as image[].path
54
+ ```
55
+
56
+ An MP4, JPEG, PNG, WebP or GIF goes in parts of 20 MB straight to the
57
+ storage, four at a time: a part cut by the network is sent again, and so are
58
+ the parts the server says are missing, before it confirms. Up to 500 MB.
59
+ A server without the upload in parts, or another type (HEIC, MOV, which the
60
+ server converts), gets the whole file in one request, as before. In one
61
+ request the server takes an image up to 10 MB, a MOV up to 1 GB, and an MP4
62
+ up to 25 MB only (`413 FILE_TOO_LARGE` beyond: a recent server takes it in
63
+ parts).
37
64
 
38
65
  ## The content board
39
66
 
@@ -88,10 +115,58 @@ await nadia.request((o) =>
88
115
  );
89
116
  ```
90
117
 
118
+ ## Webhooks
119
+
120
+ UseNadia signs every delivery: `X-Nadia-Signature: sha256=<hex>`, an
121
+ HMAC-SHA256 of `` `${X-Nadia-Timestamp}.${raw body}` `` with the secret
122
+ returned when the webhook was created. `parseWebhook` checks it (in constant
123
+ time), refuses a timestamp more than 5 minutes off, and returns the parsed
124
+ delivery — or throws a `NadiaWebhookError` whose `reason` says why
125
+ (`missing_header`, `bad_signature`, `stale`, `bad_body`).
126
+
127
+ **Read the raw body.** A body parsed then re-serialized is not byte-identical
128
+ and will not verify.
129
+
130
+ ```ts
131
+ import express from 'express';
132
+ import { parseWebhook, NadiaWebhookError } from '@usenadia/node';
133
+
134
+ const app = express();
135
+
136
+ app.post('/nadia', express.raw({ type: 'application/json' }), (req, res) => {
137
+ try {
138
+ const delivery = parseWebhook({
139
+ secret: process.env.NADIA_WEBHOOK_SECRET!,
140
+ headers: req.headers,
141
+ body: req.body, // a Buffer, thanks to express.raw()
142
+ });
143
+ if (delivery.event === 'post.published') {
144
+ // delivery.data …
145
+ }
146
+ res.sendStatus(200);
147
+ } catch (err) {
148
+ if (err instanceof NadiaWebhookError) return res.status(400).send(err.reason);
149
+ throw err;
150
+ }
151
+ });
152
+ ```
153
+
154
+ Fastify: register a `content-type` parser with `{ parseAs: 'buffer' }` and
155
+ pass `request.headers` and the buffer the same way. With the Fetch API
156
+ (Next.js route handlers, Hono, Workers with `nodejs_compat`):
157
+ `parseWebhook({ secret, headers: request.headers, body: await request.text() })`.
158
+
159
+ `verifyWebhookSignature({ secret, signature, timestamp, body, tolerance? })`
160
+ is the primitive underneath: it returns a boolean and checks the timestamp
161
+ only when `tolerance` (seconds) is given. Both are tested against the
162
+ fixture the server signs with
163
+ (`apps/server/contract/signed-delivery.json`).
164
+
91
165
  ## Errors
92
166
 
93
167
  Every response with a status ≥ 400 throws a `NadiaError` carrying `status`,
94
- `body` and the API's `msg`. `err.isTransient` is true for 429 and 5xx — those
168
+ `body` and the API's message (`msg`, or the list of fields a validation
169
+ error names). `err.isTransient` is true for 429 and 5xx — those
95
170
  are retried automatically (twice by default, `new Nadia(key, { retries: 0 })`
96
171
  to turn it off).
97
172
 
@@ -109,12 +184,29 @@ both fixes:
109
184
 
110
185
  The default base URL is `https://app.usenadia.com/api`.
111
186
 
112
- ## Regenerating
187
+ ## Regenerating and testing
113
188
 
114
189
  ```bash
115
190
  pnpm run generate # from apps/sdk/node — reads ../../../openapi.public.json
116
191
  pnpm run build
192
+ pnpm test # offline, stubbed transport
193
+ NADIA_API_KEY=… pnpm run test:smoke # real API: one draft in 2030, deleted
117
194
  ```
118
195
 
119
- `openapi.public.json` itself is produced by `pnpm openapi:dump` at the repo
120
- root and guarded by CI. Plan: `docs/roadmap/19-sdk-public-api.md`.
196
+ ## Releasing
197
+
198
+ A tag publishes; nothing is published from a laptop.
199
+
200
+ ```bash
201
+ # 1. bump "version" in apps/sdk/node/package.json, commit, push main
202
+ # 2. tag it — the tag must equal the version
203
+ git tag node-v0.2.0 && git push origin node-v0.2.0
204
+ ```
205
+
206
+ `.github/workflows/publish-npm.yml` refuses a tag that does not match
207
+ `package.json`, builds, runs the offline tests **and the smoke test against
208
+ the real API**, and publishes only if all are green (npm Trusted Publisher,
209
+ no token).
210
+
211
+ `openapi.public.json` itself is held by hand at the repo root; the server's
212
+ tests hold every public answer to it (`pnpm openapi:check`). Plan: `docs/roadmap/done/19-sdk-public-api.md`.