inkwire-receiver-core 0.1.1 → 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 +2 -1
- package/dist/auth.js +1 -1
- package/dist/handle.d.ts +2 -2
- package/dist/handle.js +11 -5
- package/dist/index.d.ts +5 -5
- package/dist/index.js +4 -4
- package/dist/schema.d.ts +28 -0
- package/dist/schema.js +7 -1
- package/dist/store.d.ts +7 -0
- package/package.json +2 -2
- package/dist/__tests__/handle.test.d.ts +0 -1
- package/dist/__tests__/handle.test.js +0 -56
- package/dist/__tests__/render.test.d.ts +0 -1
- package/dist/__tests__/render.test.js +0 -17
package/README.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# inkwire-receiver-core
|
|
2
2
|
|
|
3
|
-
Shared TypeScript core for building an [Inkwire](https://github.com/etugrand/inkwire) receiver: validate the incoming payload
|
|
3
|
+
Shared TypeScript core for building an [Inkwire](https://github.com/etugrand/inkwire) receiver: validate the incoming payload, derive SEO metadata, render+sanitize markdown to HTML, and upsert by `external_id` against a `PostStore` you provide.
|
|
4
4
|
|
|
5
5
|
```ts
|
|
6
6
|
import { handlePost, MemoryStore } from "inkwire-receiver-core";
|
|
@@ -20,6 +20,7 @@ This package is the shared logic behind the [Express](https://github.com/etugran
|
|
|
20
20
|
|
|
21
21
|
- `handlePost(input)` — the whole request lifecycle: auth, validation, slugging, sanitization, store upsert.
|
|
22
22
|
- `type PostStore` — the interface your storage backend must implement (`findByExternalId`, `findBySlug`, `upsert`).
|
|
23
|
+
- `type SeoMetadata` — effective title, description, image, and indexing values passed to `PostStore.upsert`.
|
|
23
24
|
- `MemoryStore` — an in-memory `PostStore` for local dev/testing.
|
|
24
25
|
- `renderMarkdown`, `slugify` — the sanitization and slug-generation helpers `handlePost` uses internally.
|
|
25
26
|
- `InkwireError` — typed error class mapping to the protocol's error codes.
|
package/dist/auth.js
CHANGED
package/dist/handle.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import type { PostStore } from "./store";
|
|
2
|
-
export type { PostStore } from "./store";
|
|
1
|
+
import type { PostStore } from "./store.js";
|
|
2
|
+
export type { PostStore } from "./store.js";
|
|
3
3
|
export declare function handlePost(input: {
|
|
4
4
|
authHeader?: string;
|
|
5
5
|
rawBody: unknown;
|
package/dist/handle.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import { PostInput } from "./schema";
|
|
2
|
-
import { authorize } from "./auth";
|
|
3
|
-
import { renderMarkdown, slugify } from "./render";
|
|
4
|
-
import { InkwireError } from "./errors";
|
|
1
|
+
import { PostInput } from "./schema.js";
|
|
2
|
+
import { authorize } from "./auth.js";
|
|
3
|
+
import { renderMarkdown, slugify } from "./render.js";
|
|
4
|
+
import { InkwireError } from "./errors.js";
|
|
5
5
|
const HEADERS = { "Inkwire-Version": "1", "content-type": "application/json" };
|
|
6
6
|
export async function handlePost(input) {
|
|
7
7
|
try {
|
|
@@ -25,10 +25,16 @@ export async function handlePost(input) {
|
|
|
25
25
|
slug = existing?.slug ?? await dedupeSlug(input.store, key, slugify(p.title));
|
|
26
26
|
}
|
|
27
27
|
const html = renderMarkdown(p.markdown);
|
|
28
|
+
const seo = {
|
|
29
|
+
title: p.seo?.title ?? p.title,
|
|
30
|
+
description: p.seo?.description ?? (p.excerpt || p.title),
|
|
31
|
+
image_url: p.seo?.image_url ?? p.cover_image_url,
|
|
32
|
+
noindex: p.seo?.noindex ?? false,
|
|
33
|
+
};
|
|
28
34
|
const result = await input.store.upsert(key, {
|
|
29
35
|
external_id: p.external_id, title: p.title, html, markdown: p.markdown, slug,
|
|
30
36
|
excerpt: p.excerpt, tags: p.tags, cover_image_url: p.cover_image_url,
|
|
31
|
-
canonical_url: p.canonical_url, author: p.author, status: p.status,
|
|
37
|
+
canonical_url: p.canonical_url, seo, author: p.author, status: p.status,
|
|
32
38
|
published_at: p.published_at ?? new Date().toISOString(),
|
|
33
39
|
});
|
|
34
40
|
return { status: 200, headers: HEADERS, body: {
|
package/dist/index.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
export { handlePost, type PostStore } from "./handle";
|
|
2
|
-
export { MemoryStore } from "./store";
|
|
3
|
-
export type { StoredPost, UpsertInput } from "./store";
|
|
4
|
-
export { InkwireError } from "./errors";
|
|
5
|
-
export { renderMarkdown, slugify } from "./render";
|
|
1
|
+
export { handlePost, type PostStore } from "./handle.js";
|
|
2
|
+
export { MemoryStore } from "./store.js";
|
|
3
|
+
export type { SeoMetadata, StoredPost, UpsertInput } from "./store.js";
|
|
4
|
+
export { InkwireError } from "./errors.js";
|
|
5
|
+
export { renderMarkdown, slugify } from "./render.js";
|
package/dist/index.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
export { handlePost } from "./handle";
|
|
2
|
-
export { MemoryStore } from "./store";
|
|
3
|
-
export { InkwireError } from "./errors";
|
|
4
|
-
export { renderMarkdown, slugify } from "./render";
|
|
1
|
+
export { handlePost } from "./handle.js";
|
|
2
|
+
export { MemoryStore } from "./store.js";
|
|
3
|
+
export { InkwireError } from "./errors.js";
|
|
4
|
+
export { renderMarkdown, slugify } from "./render.js";
|
package/dist/schema.d.ts
CHANGED
|
@@ -8,6 +8,22 @@ export declare const PostInput: z.ZodObject<{
|
|
|
8
8
|
tags: z.ZodOptional<z.ZodArray<z.ZodString, "many">>;
|
|
9
9
|
cover_image_url: z.ZodOptional<z.ZodEffects<z.ZodString, string, string>>;
|
|
10
10
|
canonical_url: z.ZodOptional<z.ZodEffects<z.ZodString, string, string>>;
|
|
11
|
+
seo: z.ZodOptional<z.ZodObject<{
|
|
12
|
+
title: z.ZodOptional<z.ZodString>;
|
|
13
|
+
description: z.ZodOptional<z.ZodString>;
|
|
14
|
+
image_url: z.ZodOptional<z.ZodEffects<z.ZodString, string, string>>;
|
|
15
|
+
noindex: z.ZodDefault<z.ZodBoolean>;
|
|
16
|
+
}, "strict", z.ZodTypeAny, {
|
|
17
|
+
noindex: boolean;
|
|
18
|
+
title?: string | undefined;
|
|
19
|
+
description?: string | undefined;
|
|
20
|
+
image_url?: string | undefined;
|
|
21
|
+
}, {
|
|
22
|
+
title?: string | undefined;
|
|
23
|
+
description?: string | undefined;
|
|
24
|
+
image_url?: string | undefined;
|
|
25
|
+
noindex?: boolean | undefined;
|
|
26
|
+
}>>;
|
|
11
27
|
author: z.ZodOptional<z.ZodObject<{
|
|
12
28
|
name: z.ZodOptional<z.ZodString>;
|
|
13
29
|
email: z.ZodOptional<z.ZodString>;
|
|
@@ -30,6 +46,12 @@ export declare const PostInput: z.ZodObject<{
|
|
|
30
46
|
tags?: string[] | undefined;
|
|
31
47
|
cover_image_url?: string | undefined;
|
|
32
48
|
canonical_url?: string | undefined;
|
|
49
|
+
seo?: {
|
|
50
|
+
noindex: boolean;
|
|
51
|
+
title?: string | undefined;
|
|
52
|
+
description?: string | undefined;
|
|
53
|
+
image_url?: string | undefined;
|
|
54
|
+
} | undefined;
|
|
33
55
|
author?: {
|
|
34
56
|
name?: string | undefined;
|
|
35
57
|
email?: string | undefined;
|
|
@@ -45,6 +67,12 @@ export declare const PostInput: z.ZodObject<{
|
|
|
45
67
|
tags?: string[] | undefined;
|
|
46
68
|
cover_image_url?: string | undefined;
|
|
47
69
|
canonical_url?: string | undefined;
|
|
70
|
+
seo?: {
|
|
71
|
+
title?: string | undefined;
|
|
72
|
+
description?: string | undefined;
|
|
73
|
+
image_url?: string | undefined;
|
|
74
|
+
noindex?: boolean | undefined;
|
|
75
|
+
} | undefined;
|
|
48
76
|
author?: {
|
|
49
77
|
name?: string | undefined;
|
|
50
78
|
email?: string | undefined;
|
package/dist/schema.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { z } from "zod";
|
|
2
|
-
const httpUrl = z.string().url().refine((u) => /^https
|
|
2
|
+
const httpUrl = z.string().url().refine((u) => /^https?:\/\//.test(u), { message: "must be an http(s) URL" });
|
|
3
3
|
export const PostInput = z.object({
|
|
4
4
|
external_id: z.string().min(1).max(255),
|
|
5
5
|
title: z.string().min(1).max(512),
|
|
@@ -9,6 +9,12 @@ export const PostInput = z.object({
|
|
|
9
9
|
tags: z.array(z.string().max(64)).max(50).optional(),
|
|
10
10
|
cover_image_url: httpUrl.optional(),
|
|
11
11
|
canonical_url: httpUrl.optional(),
|
|
12
|
+
seo: z.object({
|
|
13
|
+
title: z.string().min(1).max(512).optional(),
|
|
14
|
+
description: z.string().min(1).max(1024).optional(),
|
|
15
|
+
image_url: httpUrl.optional(),
|
|
16
|
+
noindex: z.boolean().default(false),
|
|
17
|
+
}).strict().optional(),
|
|
12
18
|
author: z.object({ name: z.string().optional(), email: z.string().email().optional() }).strict().optional(),
|
|
13
19
|
status: z.enum(["draft", "published"]).default("draft"),
|
|
14
20
|
published_at: z.string().datetime({ offset: true }).optional(),
|
package/dist/store.d.ts
CHANGED
|
@@ -4,6 +4,12 @@ export interface StoredPost {
|
|
|
4
4
|
url: string;
|
|
5
5
|
external_id: string;
|
|
6
6
|
}
|
|
7
|
+
export interface SeoMetadata {
|
|
8
|
+
title: string;
|
|
9
|
+
description: string;
|
|
10
|
+
image_url?: string;
|
|
11
|
+
noindex: boolean;
|
|
12
|
+
}
|
|
7
13
|
export interface UpsertInput {
|
|
8
14
|
external_id: string;
|
|
9
15
|
title: string;
|
|
@@ -14,6 +20,7 @@ export interface UpsertInput {
|
|
|
14
20
|
tags?: string[];
|
|
15
21
|
cover_image_url?: string;
|
|
16
22
|
canonical_url?: string;
|
|
23
|
+
seo: SeoMetadata;
|
|
17
24
|
author?: {
|
|
18
25
|
name?: string;
|
|
19
26
|
email?: string;
|
package/package.json
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
{
|
|
2
|
-
"name": "inkwire-receiver-core", "version": "0.
|
|
2
|
+
"name": "inkwire-receiver-core", "version": "0.2.0", "type": "module",
|
|
3
3
|
"description": "Shared TS core (validate, render, sanitize, upsert) for building an Inkwire receiver.",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"repository": { "type": "git", "url": "git+https://github.com/etugrand/inkwire.git", "directory": "inkwire/receivers/core-ts" },
|
|
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
|
"dependencies": { "zod": "^3.23.8", "markdown-it": "^14.1.0", "sanitize-html": "^2.13.1" },
|
|
10
10
|
"devDependencies": { "vitest": "^2.1.0", "typescript": "^5.5.0",
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|
|
@@ -1,56 +0,0 @@
|
|
|
1
|
-
import { it, expect, beforeEach } from "vitest";
|
|
2
|
-
import { handlePost } from "../handle";
|
|
3
|
-
import { MemoryStore } from "../store";
|
|
4
|
-
let store;
|
|
5
|
-
const keys = ["secret-key"];
|
|
6
|
-
const auth = "Bearer secret-key";
|
|
7
|
-
beforeEach(() => { store = new MemoryStore(); });
|
|
8
|
-
it("creates a published post", async () => {
|
|
9
|
-
const r = await handlePost({ authHeader: auth, apiKeys: keys, store,
|
|
10
|
-
rawBody: { external_id: "a", title: "T", markdown: "# H", status: "published" } });
|
|
11
|
-
expect(r.status).toBe(200);
|
|
12
|
-
expect(r.body).toMatchObject({ created: true, status: "published" });
|
|
13
|
-
expect(r.headers["Inkwire-Version"]).toBe("1");
|
|
14
|
-
});
|
|
15
|
-
it("upserts on external_id", async () => {
|
|
16
|
-
const base = { external_id: "a", markdown: "x" };
|
|
17
|
-
await handlePost({ authHeader: auth, apiKeys: keys, store, rawBody: { ...base, title: "One" } });
|
|
18
|
-
const r = await handlePost({ authHeader: auth, apiKeys: keys, store, rawBody: { ...base, title: "Two" } });
|
|
19
|
-
expect(r.body).toMatchObject({ created: false });
|
|
20
|
-
});
|
|
21
|
-
it("defaults status to draft", async () => {
|
|
22
|
-
const r = await handlePost({ authHeader: auth, apiKeys: keys, store,
|
|
23
|
-
rawBody: { external_id: "b", title: "T", markdown: "x" } });
|
|
24
|
-
expect(r.body).toMatchObject({ status: "draft" });
|
|
25
|
-
});
|
|
26
|
-
it("rejects missing title with 400 invalid_payload", async () => {
|
|
27
|
-
const r = await handlePost({ authHeader: auth, apiKeys: keys, store, rawBody: { external_id: "c", markdown: "x" } });
|
|
28
|
-
expect(r.status).toBe(400);
|
|
29
|
-
expect(r.body.error.code).toBe("invalid_payload");
|
|
30
|
-
});
|
|
31
|
-
it("rejects bad auth with 401", async () => {
|
|
32
|
-
const r = await handlePost({ authHeader: "Bearer nope", apiKeys: keys, store,
|
|
33
|
-
rawBody: { external_id: "d", title: "T", markdown: "x" } });
|
|
34
|
-
expect(r.status).toBe(401);
|
|
35
|
-
expect(r.body.error.code).toBe("unauthorized");
|
|
36
|
-
});
|
|
37
|
-
it("dedupes a 255-char slug without exceeding the length cap", async () => {
|
|
38
|
-
const longTitle = "a".repeat(300);
|
|
39
|
-
const first = await handlePost({ authHeader: auth, apiKeys: keys, store,
|
|
40
|
-
rawBody: { external_id: "long-1", title: longTitle, markdown: "x" } });
|
|
41
|
-
const second = await handlePost({ authHeader: auth, apiKeys: keys, store,
|
|
42
|
-
rawBody: { external_id: "long-2", title: longTitle, markdown: "x" } });
|
|
43
|
-
const firstSlug = first.body.slug;
|
|
44
|
-
const secondSlug = second.body.slug;
|
|
45
|
-
expect(firstSlug.length).toBe(255);
|
|
46
|
-
expect(secondSlug.length).toBeLessThanOrEqual(255);
|
|
47
|
-
expect(secondSlug).not.toBe(firstSlug);
|
|
48
|
-
});
|
|
49
|
-
it("409 on explicit slug owned by a different external_id", async () => {
|
|
50
|
-
await handlePost({ authHeader: auth, apiKeys: keys, store,
|
|
51
|
-
rawBody: { external_id: "owner", title: "O", markdown: "x", slug: "taken" } });
|
|
52
|
-
const r = await handlePost({ authHeader: auth, apiKeys: keys, store,
|
|
53
|
-
rawBody: { external_id: "other", title: "X", markdown: "x", slug: "taken" } });
|
|
54
|
-
expect(r.status).toBe(409);
|
|
55
|
-
expect(r.body.error.code).toBe("conflict");
|
|
56
|
-
});
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|
|
@@ -1,17 +0,0 @@
|
|
|
1
|
-
import { describe, it, expect } from "vitest";
|
|
2
|
-
import { renderMarkdown } from "../render";
|
|
3
|
-
describe("renderMarkdown", () => {
|
|
4
|
-
it("renders markdown to html", () => {
|
|
5
|
-
expect(renderMarkdown("# Hi")).toContain("<h1>Hi</h1>");
|
|
6
|
-
});
|
|
7
|
-
it("strips script tags and their contents", () => {
|
|
8
|
-
const html = renderMarkdown("Hi\n\n<script>alert(1)</script>");
|
|
9
|
-
expect(html).not.toContain("<script>");
|
|
10
|
-
expect(html).not.toContain("alert(1)");
|
|
11
|
-
});
|
|
12
|
-
it("strips event handlers and javascript: urls", () => {
|
|
13
|
-
const html = renderMarkdown('<a href="javascript:alert(1)" onclick="x()">z</a>');
|
|
14
|
-
expect(html).not.toContain("onclick");
|
|
15
|
-
expect(html).not.toContain("javascript:");
|
|
16
|
-
});
|
|
17
|
-
});
|