inkwire-client 0.1.0 → 0.3.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 +1 -1
- package/dist/index.d.ts +15 -2
- package/dist/index.js +2 -1
- package/package.json +3 -3
- package/dist/__tests__/client.test.d.ts +0 -1
- package/dist/__tests__/client.test.js +0 -35
package/README.md
CHANGED
|
@@ -13,6 +13,6 @@ const result = await publish("https://blog.example.com", "your-api-key", {
|
|
|
13
13
|
});
|
|
14
14
|
```
|
|
15
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.
|
|
16
|
+
`publish(baseUrl, apiKey, payload, opts?)` upserts by `external_id`, retries on `429`/5xx with backoff, and throws `InkwireClientError` (`code`, `message`, `httpStatus`) on failure. Pass `opts.fetch` to send through your own fetch, for example an SSRF-guarded one when the site URL comes from your users.
|
|
17
17
|
|
|
18
18
|
See the [protocol spec](https://github.com/etugrand/inkwire/blob/master/inkwire/SPEC.md) for the full payload shape.
|
package/dist/index.d.ts
CHANGED
|
@@ -1,3 +1,9 @@
|
|
|
1
|
+
export interface SeoPayload {
|
|
2
|
+
title?: string;
|
|
3
|
+
description?: string;
|
|
4
|
+
image_url?: string;
|
|
5
|
+
noindex?: boolean;
|
|
6
|
+
}
|
|
1
7
|
export interface PostPayload {
|
|
2
8
|
external_id: string;
|
|
3
9
|
title: string;
|
|
@@ -7,6 +13,7 @@ export interface PostPayload {
|
|
|
7
13
|
tags?: string[];
|
|
8
14
|
cover_image_url?: string;
|
|
9
15
|
canonical_url?: string;
|
|
16
|
+
seo?: SeoPayload;
|
|
10
17
|
author?: {
|
|
11
18
|
name?: string;
|
|
12
19
|
email?: string;
|
|
@@ -27,6 +34,12 @@ export declare class InkwireClientError extends Error {
|
|
|
27
34
|
httpStatus: number;
|
|
28
35
|
constructor(code: string, message: string, httpStatus: number);
|
|
29
36
|
}
|
|
30
|
-
export
|
|
37
|
+
export interface PublishOptions {
|
|
31
38
|
retries?: number;
|
|
32
|
-
|
|
39
|
+
/**
|
|
40
|
+
* The fetch to send with (default: global fetch). A server that publishes to user-supplied
|
|
41
|
+
* site URLs should pass an SSRF-guarded fetch that refuses internal hosts on every redirect hop.
|
|
42
|
+
*/
|
|
43
|
+
fetch?: (url: string, init: RequestInit) => Promise<Response>;
|
|
44
|
+
}
|
|
45
|
+
export declare function publish(baseUrl: string, apiKey: string, payload: PostPayload, opts?: PublishOptions): Promise<PublishResult>;
|
package/dist/index.js
CHANGED
|
@@ -10,10 +10,11 @@ export class InkwireClientError extends Error {
|
|
|
10
10
|
const sleep = (ms) => new Promise((r) => setTimeout(r, ms));
|
|
11
11
|
export async function publish(baseUrl, apiKey, payload, opts = {}) {
|
|
12
12
|
const retries = Math.max(1, opts.retries ?? 3);
|
|
13
|
+
const send = opts.fetch ?? fetch;
|
|
13
14
|
const url = `${baseUrl.replace(/\/$/, "")}/api/posts`;
|
|
14
15
|
let lastErr;
|
|
15
16
|
for (let attempt = 1; attempt <= retries; attempt++) {
|
|
16
|
-
const res = await
|
|
17
|
+
const res = await send(url, {
|
|
17
18
|
method: "POST",
|
|
18
19
|
headers: {
|
|
19
20
|
"content-type": "application/json",
|
package/package.json
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
{
|
|
2
|
-
"name": "inkwire-client", "version": "0.
|
|
2
|
+
"name": "inkwire-client", "version": "0.3.0", "type": "module",
|
|
3
3
|
"description": "Client SDK for publishing posts to an Inkwire receiver (idempotent, retrying, typed errors).",
|
|
4
4
|
"license": "MIT",
|
|
5
|
-
"repository": { "type": "git", "url": "https://github.com/etugrand/inkwire.git", "directory": "inkwire/clients/node" },
|
|
5
|
+
"repository": { "type": "git", "url": "git+https://github.com/etugrand/inkwire.git", "directory": "inkwire/clients/node" },
|
|
6
6
|
"main": "dist/index.js", "types": "dist/index.d.ts",
|
|
7
|
-
"files": ["dist"],
|
|
7
|
+
"files": ["dist/*.js", "dist/*.d.ts"],
|
|
8
8
|
"scripts": { "build": "tsc", "test": "vitest run" },
|
|
9
9
|
"devDependencies": { "vitest": "^2.1.0", "typescript": "^5.5.0" }
|
|
10
10
|
}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|
|
@@ -1,35 +0,0 @@
|
|
|
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
|
-
});
|