@zilfu/sdk 0.1.0 → 0.1.1

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 Zilfu
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 CHANGED
@@ -1,51 +1,143 @@
1
1
  # @zilfu/sdk
2
2
 
3
- Official TypeScript SDK for the [Zilfu](https://zilfu.com) API. Type-safe client generated from the live OpenAPI spec — works in browser, Node 18+, and edge runtimes (Cloudflare Workers, Vercel Edge).
3
+ Official TypeScript SDK for the [Zilfu](https://zilfu.app) API.
4
4
 
5
5
  ## Install
6
6
 
7
- ```bash
7
+ ```sh
8
8
  pnpm add @zilfu/sdk
9
9
  # or
10
10
  npm install @zilfu/sdk
11
11
  ```
12
12
 
13
- ## Usage
13
+ Requires Node 18.17+ (or any runtime with global `fetch`).
14
14
 
15
- Get a personal access token from your Zilfu account settings, then:
15
+ ## Quickstart
16
16
 
17
17
  ```ts
18
- import { createZilfuClient } from '@zilfu/sdk';
18
+ import { createZilfuClient } from "@zilfu/sdk";
19
19
 
20
20
  const api = createZilfuClient({
21
- baseUrl: 'https://zilfu.com/api',
21
+ baseUrl: "https://zilfu.app/api",
22
22
  token: process.env.ZILFU_TOKEN!,
23
23
  });
24
24
 
25
- const { data: spaces } = await api.spaces.list();
26
- const { data: post } = await api.posts.create({
27
- path: { space: '1' },
28
- body: { space_id: 1, items: [{ account_id: 1, social: 'threads', main: { content: 'hi' } }] },
25
+ const spaces = await api.spaces.list();
26
+ ```
27
+
28
+ ## Auth
29
+
30
+ Mint a personal-access token at **Settings → API tokens** (or via `POST /api/api-tokens`). Pass it as a string or as a function for refresh logic:
31
+
32
+ ```ts
33
+ const api = createZilfuClient({
34
+ token: async () => await getTokenFromVault(),
29
35
  });
30
36
  ```
31
37
 
32
- Available namespaces: `accounts`, `bio`, `bioBlocks`, `clusters`, `media`, `posts`, `queue`, `slots`, `spaces`, `subscription`, `tokens`, `webhooks`. Each exposes typed `list / get / create / update / delete` plus resource-specific verbs (`activate`, `boards`, `reorder`, `uploadAvatar`, etc.).
38
+ The `Authorization: Bearer <token>` header is added automatically to every request.
33
39
 
34
- ### Custom fetch (edge runtimes)
40
+ ## Examples
35
41
 
36
42
  ```ts
37
- createZilfuClient({
38
- baseUrl: 'https://zilfu.com',
43
+ // List posts in a space
44
+ await api.posts.list({ path: { space: 1 } });
45
+
46
+ // Create a multi-account post (a "cluster")
47
+ await api.posts.create({
48
+ path: { space: 1 },
49
+ body: {
50
+ space_id: 1,
51
+ items: [{ account_id: 1, social: "threads", main: { content: "Hello" } }],
52
+ },
53
+ });
54
+
55
+ // Update an entire cluster
56
+ await api.clusters.update({
57
+ path: { space: 1, cluster_id: "abc-123" },
58
+ body: { /* same shape as create */ },
59
+ });
60
+
61
+ // Reorder a bio link block
62
+ await api.bio.blocks.reorder({
63
+ path: { space: 1, block: 7 },
64
+ body: { sort_order: 2 },
65
+ });
66
+
67
+ // Upload media (FormData)
68
+ const fd = new FormData();
69
+ fd.append("file", file);
70
+ await api.media.create({ body: fd });
71
+ ```
72
+
73
+ ## Errors
74
+
75
+ Non-2xx responses throw a typed `ZilfuApiError`:
76
+
77
+ ```ts
78
+ import { ZilfuApiError } from "@zilfu/sdk";
79
+
80
+ try {
81
+ await api.posts.create({ /* ... */ });
82
+ } catch (e) {
83
+ if (e instanceof ZilfuApiError) {
84
+ console.error(e.status, e.errors); // Laravel validation: { field: ["msg"] }
85
+ }
86
+ }
87
+ ```
88
+
89
+ ## Types
90
+
91
+ ```ts
92
+ import type {
93
+ PostResource,
94
+ SpaceResource,
95
+ AccountResource,
96
+ WebhookResource,
97
+ } from "@zilfu/sdk";
98
+ ```
99
+
100
+ All request/response shapes are exported as named types.
101
+
102
+ ## Custom fetch
103
+
104
+ ```ts
105
+ import { fetch as undiciFetch } from "undici";
106
+
107
+ const api = createZilfuClient({
39
108
  token,
40
- fetch: globalThis.fetch.bind(globalThis),
109
+ fetch: undiciFetch,
41
110
  });
42
111
  ```
43
112
 
44
- ## Development
113
+ ## Regenerating from the API spec
114
+
115
+ The package is generated from the live OpenAPI spec at `https://zilfu.app/docs/api.json` (Scramble).
116
+
117
+ ```sh
118
+ pnpm sync:spec # fetches openapi.json
119
+ pnpm generate # regenerates src/generated
120
+ pnpm typecheck # verifies the smoke file still compiles
121
+ pnpm build
122
+ ```
123
+
124
+ Override the spec source for staging or local Herd:
125
+
126
+ ```sh
127
+ SCRAMBLE_SPEC_URL=http://zilfu.test/docs/api.json pnpm sync:spec
128
+ ```
129
+
130
+ The generated `src/generated/` directory is committed; PRs that bump the spec show a reviewable diff of API surface changes.
131
+
132
+ A daily GitHub Actions cron (`.github/workflows/sync-spec.yml`) does this automatically and opens a PR if anything changed — review and merge, then cut a release with:
45
133
 
46
- ```bash
47
- pnpm sync # export openapi.json from the Laravel app
48
- pnpm generate # regenerate src/generated/ from openapi.json
49
- pnpm build # bundle dist/
50
- pnpm typecheck
134
+ ```sh
135
+ pnpm version <patch|minor|major>
136
+ git push --follow-tags
51
137
  ```
138
+
139
+ The release workflow (`.github/workflows/release.yml`) publishes to npm with provenance and creates a GitHub release on every `v*` tag.
140
+
141
+ ## License
142
+
143
+ MIT
package/dist/index.cjs CHANGED
@@ -823,7 +823,7 @@ var HeyApiRegistry = class {
823
823
  get(key) {
824
824
  const instance = this.instances.get(key ?? this.defaultKey);
825
825
  if (!instance) {
826
- throw new Error(`No SDK client found. Create one with "new ZilfuClient()" to fix this error.`);
826
+ throw new Error(`No SDK client found. Create one with "new ZilfuApi()" to fix this error.`);
827
827
  }
828
828
  return instance;
829
829
  }
@@ -893,7 +893,7 @@ var Accounts = class extends HeyApiClient {
893
893
  });
894
894
  }
895
895
  };
896
- var Tokens = class extends HeyApiClient {
896
+ var ApiTokens = class extends HeyApiClient {
897
897
  /**
898
898
  * Create an API token
899
899
  *
@@ -923,7 +923,7 @@ var Tokens = class extends HeyApiClient {
923
923
  });
924
924
  }
925
925
  };
926
- var BioBlocks = class extends HeyApiClient {
926
+ var Blocks = class extends HeyApiClient {
927
927
  list(options) {
928
928
  return (options.client ?? this.client).get({
929
929
  security: [{ scheme: "bearer", type: "http" }],
@@ -1014,6 +1014,19 @@ var Bio = class extends HeyApiClient {
1014
1014
  }
1015
1015
  });
1016
1016
  }
1017
+ _blocks;
1018
+ get blocks() {
1019
+ return this._blocks ??= new Blocks({ client: this.client });
1020
+ }
1021
+ };
1022
+ var Health = class extends HeyApiClient {
1023
+ check(options) {
1024
+ return (options?.client ?? this.client).get({
1025
+ security: [{ scheme: "bearer", type: "http" }],
1026
+ url: "/health",
1027
+ ...options
1028
+ });
1029
+ }
1017
1030
  };
1018
1031
  var Media = class extends HeyApiClient {
1019
1032
  /**
@@ -1332,28 +1345,28 @@ var Webhooks = class extends HeyApiClient {
1332
1345
  });
1333
1346
  }
1334
1347
  };
1335
- var ZilfuClient = class _ZilfuClient extends HeyApiClient {
1348
+ var ZilfuApi = class _ZilfuApi extends HeyApiClient {
1336
1349
  static __registry = new HeyApiRegistry();
1337
1350
  constructor(args) {
1338
1351
  super(args);
1339
- _ZilfuClient.__registry.set(this, args?.key);
1352
+ _ZilfuApi.__registry.set(this, args?.key);
1340
1353
  }
1341
1354
  _accounts;
1342
1355
  get accounts() {
1343
1356
  return this._accounts ??= new Accounts({ client: this.client });
1344
1357
  }
1345
- _tokens;
1346
- get tokens() {
1347
- return this._tokens ??= new Tokens({ client: this.client });
1348
- }
1349
- _bioBlocks;
1350
- get bioBlocks() {
1351
- return this._bioBlocks ??= new BioBlocks({ client: this.client });
1358
+ _apiTokens;
1359
+ get apiTokens() {
1360
+ return this._apiTokens ??= new ApiTokens({ client: this.client });
1352
1361
  }
1353
1362
  _bio;
1354
1363
  get bio() {
1355
1364
  return this._bio ??= new Bio({ client: this.client });
1356
1365
  }
1366
+ _health;
1367
+ get health() {
1368
+ return this._health ??= new Health({ client: this.client });
1369
+ }
1357
1370
  _media;
1358
1371
  get media() {
1359
1372
  return this._media ??= new Media({ client: this.client });
@@ -1388,31 +1401,68 @@ var ZilfuClient = class _ZilfuClient extends HeyApiClient {
1388
1401
  }
1389
1402
  };
1390
1403
 
1404
+ // src/errors.ts
1405
+ var ZilfuApiError = class extends Error {
1406
+ status;
1407
+ statusText;
1408
+ url;
1409
+ body;
1410
+ errors;
1411
+ constructor(init) {
1412
+ const message = extractMessage(init);
1413
+ super(message);
1414
+ this.name = "ZilfuApiError";
1415
+ this.status = init.status;
1416
+ this.statusText = init.statusText;
1417
+ this.url = init.url;
1418
+ this.body = init.body;
1419
+ this.errors = extractValidationErrors(init.body);
1420
+ }
1421
+ };
1422
+ function extractMessage(init) {
1423
+ const body = init.body;
1424
+ if (body && typeof body === "object" && "message" in body) {
1425
+ const m = body.message;
1426
+ if (typeof m === "string" && m.length > 0) {
1427
+ return m;
1428
+ }
1429
+ }
1430
+ return `Zilfu API ${init.status}${init.statusText ? ` ${init.statusText}` : ""}`;
1431
+ }
1432
+ function extractValidationErrors(body) {
1433
+ if (!body || typeof body !== "object" || !("errors" in body)) {
1434
+ return void 0;
1435
+ }
1436
+ const errors = body.errors;
1437
+ if (!errors || typeof errors !== "object") {
1438
+ return void 0;
1439
+ }
1440
+ return errors;
1441
+ }
1442
+
1391
1443
  // src/client.ts
1392
- function createZilfuClient(options) {
1393
- const { baseUrl, token, fetch: fetchImpl } = options;
1394
- client.setConfig({
1395
- baseUrl,
1396
- auth: token,
1397
- ...fetchImpl ? { fetch: fetchImpl } : {}
1444
+ function createZilfuClient(opts) {
1445
+ const client2 = createClient(
1446
+ createConfig({
1447
+ baseUrl: opts.baseUrl ?? "https://zilfu.app/api",
1448
+ fetch: opts.fetch,
1449
+ headers: { accept: "application/json", ...opts.headers },
1450
+ auth: opts.token,
1451
+ throwOnError: true
1452
+ })
1453
+ );
1454
+ client2.interceptors.error.use((error, response) => {
1455
+ return new ZilfuApiError({
1456
+ status: response?.status ?? 0,
1457
+ statusText: response?.statusText,
1458
+ url: response?.url,
1459
+ body: error
1460
+ });
1398
1461
  });
1399
- return new ZilfuClient();
1462
+ return new ZilfuApi({ client: client2 });
1400
1463
  }
1401
1464
 
1402
- exports.Accounts = Accounts;
1403
- exports.Bio = Bio;
1404
- exports.BioBlocks = BioBlocks;
1405
- exports.Clusters = Clusters;
1406
- exports.Media = Media;
1407
- exports.Posts = Posts;
1408
- exports.Queue = Queue;
1409
- exports.Slots = Slots;
1410
- exports.Spaces = Spaces;
1411
- exports.Subscription = Subscription;
1412
- exports.Tokens = Tokens;
1413
- exports.Webhooks = Webhooks;
1414
- exports.ZilfuClient = ZilfuClient;
1415
- exports.client = client;
1465
+ exports.ZilfuApiError = ZilfuApiError;
1416
1466
  exports.createZilfuClient = createZilfuClient;
1417
1467
  //# sourceMappingURL=index.cjs.map
1418
1468
  //# sourceMappingURL=index.cjs.map