inkwire-client 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/README.md ADDED
@@ -0,0 +1,18 @@
1
+ # inkwire-client
2
+
3
+ Node client for publishing posts to an [Inkwire](https://github.com/etugrand/inkwire) receiver.
4
+
5
+ ```js
6
+ import { publish } from "inkwire-client";
7
+
8
+ const result = await publish("https://blog.example.com", "your-api-key", {
9
+ external_id: "my-post-1",
10
+ title: "Hello",
11
+ markdown: "# Hello\n\nFirst post via Inkwire.",
12
+ status: "published",
13
+ });
14
+ ```
15
+
16
+ `publish(baseUrl, apiKey, payload, opts?)` upserts by `external_id`, retries on `429`/5xx with backoff, and throws `InkwireClientError` (`code`, `message`, `httpStatus`) on failure.
17
+
18
+ See the [protocol spec](https://github.com/etugrand/inkwire/blob/master/inkwire/SPEC.md) for the full payload shape.
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,35 @@
1
+ import { it, expect, vi, afterEach } from "vitest";
2
+ import { publish } from "../index";
3
+ const ok = (body) => new Response(JSON.stringify(body), { status: 200, headers: { "inkwire-version": "1" } });
4
+ afterEach(() => vi.restoreAllMocks());
5
+ it("publishes and returns the result", async () => {
6
+ vi.stubGlobal("fetch", vi.fn(async () => ok({ id: "1", external_id: "e", url: "u", slug: "s", status: "published", created: true })));
7
+ const r = await publish("https://site.test", "k", { external_id: "e", title: "T", markdown: "x", status: "published" });
8
+ expect(r).toMatchObject({ id: "1", created: true });
9
+ const call = fetch.mock.calls[0];
10
+ expect(call[1].headers.authorization).toBe("Bearer k");
11
+ expect(call[1].headers["idempotency-key"]).toBe("e");
12
+ });
13
+ it("retries on 500 then succeeds", async () => {
14
+ const fetchMock = vi.fn()
15
+ .mockResolvedValueOnce(new Response("{}", { status: 500 }))
16
+ .mockResolvedValueOnce(ok({ id: "1", external_id: "e", url: "u", slug: "s", status: "draft", created: true }));
17
+ vi.stubGlobal("fetch", fetchMock);
18
+ const r = await publish("https://site.test", "k", { external_id: "e", title: "T", markdown: "x" }, { retries: 2 });
19
+ expect(r.id).toBe("1");
20
+ expect(fetchMock).toHaveBeenCalledTimes(2);
21
+ });
22
+ it("throws typed error on 401 without retrying", async () => {
23
+ const fetchMock = vi.fn(async () => new Response(JSON.stringify({ error: { code: "unauthorized", message: "no" } }), { status: 401 }));
24
+ vi.stubGlobal("fetch", fetchMock);
25
+ await expect(publish("https://site.test", "bad", { external_id: "e", title: "T", markdown: "x" }))
26
+ .rejects.toMatchObject({ code: "unauthorized" });
27
+ expect(fetchMock).toHaveBeenCalledTimes(1);
28
+ });
29
+ it("retries: 0 still makes one attempt and throws typed error", async () => {
30
+ const fetchMock = vi.fn(async () => new Response(JSON.stringify({ error: { code: "server_error", message: "oops" } }), { status: 500 }));
31
+ vi.stubGlobal("fetch", fetchMock);
32
+ await expect(publish("https://site.test", "k", { external_id: "e", title: "T", markdown: "x" }, { retries: 0 }))
33
+ .rejects.toMatchObject({ code: "server_error", httpStatus: 500 });
34
+ expect(fetchMock).toHaveBeenCalledTimes(1);
35
+ });
@@ -0,0 +1,32 @@
1
+ export interface PostPayload {
2
+ external_id: string;
3
+ title: string;
4
+ markdown: string;
5
+ slug?: string;
6
+ excerpt?: string;
7
+ tags?: string[];
8
+ cover_image_url?: string;
9
+ canonical_url?: string;
10
+ author?: {
11
+ name?: string;
12
+ email?: string;
13
+ };
14
+ status?: "draft" | "published";
15
+ published_at?: string;
16
+ }
17
+ export interface PublishResult {
18
+ id: string;
19
+ external_id: string;
20
+ url: string;
21
+ slug: string;
22
+ status: "draft" | "published";
23
+ created: boolean;
24
+ }
25
+ export declare class InkwireClientError extends Error {
26
+ code: string;
27
+ httpStatus: number;
28
+ constructor(code: string, message: string, httpStatus: number);
29
+ }
30
+ export declare function publish(baseUrl: string, apiKey: string, payload: PostPayload, opts?: {
31
+ retries?: number;
32
+ }): Promise<PublishResult>;
package/dist/index.js ADDED
@@ -0,0 +1,43 @@
1
+ export class InkwireClientError extends Error {
2
+ code;
3
+ httpStatus;
4
+ constructor(code, message, httpStatus) {
5
+ super(message);
6
+ this.code = code;
7
+ this.httpStatus = httpStatus;
8
+ }
9
+ }
10
+ const sleep = (ms) => new Promise((r) => setTimeout(r, ms));
11
+ export async function publish(baseUrl, apiKey, payload, opts = {}) {
12
+ const retries = Math.max(1, opts.retries ?? 3);
13
+ const url = `${baseUrl.replace(/\/$/, "")}/api/posts`;
14
+ let lastErr;
15
+ for (let attempt = 1; attempt <= retries; attempt++) {
16
+ const res = await fetch(url, {
17
+ method: "POST",
18
+ headers: {
19
+ "content-type": "application/json",
20
+ authorization: `Bearer ${apiKey}`,
21
+ "idempotency-key": payload.external_id,
22
+ },
23
+ body: JSON.stringify(payload),
24
+ });
25
+ if (res.ok)
26
+ return (await res.json());
27
+ const retriable = res.status === 429 || res.status >= 500;
28
+ let code = "internal", message = `HTTP ${res.status}`;
29
+ try {
30
+ const j = await res.json();
31
+ if (j?.error) {
32
+ code = j.error.code;
33
+ message = j.error.message;
34
+ }
35
+ }
36
+ catch { }
37
+ if (!retriable || attempt === retries)
38
+ throw new InkwireClientError(code, message, res.status);
39
+ lastErr = new InkwireClientError(code, message, res.status);
40
+ await sleep(2 ** (attempt - 1) * 200);
41
+ }
42
+ throw lastErr;
43
+ }
package/package.json ADDED
@@ -0,0 +1,10 @@
1
+ {
2
+ "name": "inkwire-client", "version": "0.1.0", "type": "module",
3
+ "description": "Client SDK for publishing posts to an Inkwire receiver (idempotent, retrying, typed errors).",
4
+ "license": "MIT",
5
+ "repository": { "type": "git", "url": "https://github.com/etugrand/inkwire.git", "directory": "inkwire/clients/node" },
6
+ "main": "dist/index.js", "types": "dist/index.d.ts",
7
+ "files": ["dist"],
8
+ "scripts": { "build": "tsc", "test": "vitest run" },
9
+ "devDependencies": { "vitest": "^2.1.0", "typescript": "^5.5.0" }
10
+ }