Loading codegen/type.ts +39 −0 Original line number Diff line number Diff line Loading @@ -65,6 +65,26 @@ const scalarTypes: Record<string, ScalarType> = { return `${v}["@value"]`; }, }, "http://www.w3.org/2001/XMLSchema#float": { name: "number", typeGuard(v) { return `typeof ${v} === "number" && !Number.isNaN(${v})`; }, encoder(v) { return `{ "@type": "http://www.w3.org/2001/XMLSchema#float", "@value": ${v}, }`; }, dataCheck(v) { return `typeof ${v} === "object" && "@type" in ${v} && ${v}["@type"] === "http://www.w3.org/2001/XMLSchema#float" && "@value" in ${v} && typeof ${v}["@value"] === "number"`; }, decoder(v) { return `${v}["@value"]`; }, }, "http://www.w3.org/2001/XMLSchema#string": { name: "string", typeGuard(v) { Loading Loading @@ -192,6 +212,25 @@ const scalarTypes: Record<string, ScalarType> = { })`; }, }, "fedify:units": { name: '"cm" | "feet" | "inches" | "km" | "m" | "miles"', typeGuard(v) { return `${v} == "cm" || ${v} == "feet" || ${v} == "inches" ` + `|| ${v} == "km" || ${v} == "m" || ${v} == "miles"`; }, encoder(v) { return `{ "@value": ${v} }`; }, dataCheck(v) { return `typeof ${v} === "object" && "@value" in ${v} && (${v}["@value"] == "cm" || ${v}["@value"] == "feet" ` + `|| ${v}["@value"] == "inches" || ${v}["@value"] == "km" ` + `|| ${v}["@value"] == "m" || ${v}["@value"] == "miles")`; }, decoder(v) { return `${v}["@value"]`; }, }, }; export function getTypeName( Loading examples/blog/federation/mod.ts +2 −2 Original line number Diff line number Diff line Loading @@ -21,7 +21,7 @@ import { removeFollower, } from "../models/follower.ts"; import { openKv } from "../models/kv.ts"; import { countPosts, getPosts, toNote } from "../models/post.ts"; import { countPosts, getPosts, toArticle } from "../models/post.ts"; // The `Federation<TContextData>` object is a registry that registers // federation-related callbacks: Loading Loading @@ -97,7 +97,7 @@ federation.setOutboxDispatcher( id: new URL(`/posts/${post.uuid}#activity`, ctx.request.url), actor: ctx.getActorUri(handle), to: new URL("https://www.w3.org/ns/activitystreams#Public"), object: toNote(ctx, blog, post, comments), object: toArticle(ctx, blog, post, comments), }); activities.push(activity); } Loading examples/blog/models/post.ts +5 −5 Original line number Diff line number Diff line import { RequestContext } from "fedify/federation"; import { Collection, CollectionPage, Note } from "fedify/vocab"; import { Article, Collection, CollectionPage } from "fedify/vocab"; import markdownIt from "markdown-it"; import { uuidv7 } from "uuidv7"; import { Blog } from "./blog.ts"; Loading Loading @@ -70,15 +70,15 @@ export function getContentHtml(post: Post): string { return md.render(post.content); } // Represents a post as an ActivityStreams `Note`: export function toNote( // Represents a post as an ActivityStreams `Article`: export function toArticle( context: RequestContext<void>, blog: Blog, post: Post, comments: Comment[], ): Note { ): Article { const url = new URL(`/posts/${post.uuid}`, context.url); return new Note({ return new Article({ id: url, attributedTo: context.getActorUri(blog.handle), to: new URL("https://www.w3.org/ns/activitystreams#Public"), Loading examples/blog/routes/posts/[uuid].tsx +6 −2 Original line number Diff line number Diff line Loading @@ -9,7 +9,11 @@ import { type Comment as CommentModel, getComments, } from "../../models/comment.ts"; import { getPost, type Post as PostModel, toNote } from "../../models/post.ts"; import { getPost, type Post as PostModel, toArticle, } from "../../models/post.ts"; export interface PostPageData { domain: string; Loading Loading @@ -37,7 +41,7 @@ export const handler: Handler<PostPageData> = async (req, ctx) => { accept === "application/ld+json" || accept === "application/json" ) { const fedCtx = federation.createContext(req); const note = toNote(fedCtx, blog, post, comments); const note = toArticle(fedCtx, blog, post, comments); const jsonLd = await note.toJsonLd(fedCtx); return new Response(JSON.stringify(jsonLd), { headers: { Loading examples/blog/routes/posts/index.tsx +2 −2 Original line number Diff line number Diff line Loading @@ -10,7 +10,7 @@ import { countPosts, getPosts, Post, toNote, toArticle, } from "../../models/post.ts"; interface PostsData extends PostFormProps { Loading Loading @@ -90,7 +90,7 @@ export const handler: Handlers<PostsData> = { id: new URL(`/posts/${post.uuid}#activity`, req.url), actor: fedCtx.getActorUri(blog.handle), to: new URL("https://www.w3.org/ns/activitystreams#Public"), object: toNote(fedCtx, blog, post, []), object: toArticle(fedCtx, blog, post, []), }), ); Loading Loading
codegen/type.ts +39 −0 Original line number Diff line number Diff line Loading @@ -65,6 +65,26 @@ const scalarTypes: Record<string, ScalarType> = { return `${v}["@value"]`; }, }, "http://www.w3.org/2001/XMLSchema#float": { name: "number", typeGuard(v) { return `typeof ${v} === "number" && !Number.isNaN(${v})`; }, encoder(v) { return `{ "@type": "http://www.w3.org/2001/XMLSchema#float", "@value": ${v}, }`; }, dataCheck(v) { return `typeof ${v} === "object" && "@type" in ${v} && ${v}["@type"] === "http://www.w3.org/2001/XMLSchema#float" && "@value" in ${v} && typeof ${v}["@value"] === "number"`; }, decoder(v) { return `${v}["@value"]`; }, }, "http://www.w3.org/2001/XMLSchema#string": { name: "string", typeGuard(v) { Loading Loading @@ -192,6 +212,25 @@ const scalarTypes: Record<string, ScalarType> = { })`; }, }, "fedify:units": { name: '"cm" | "feet" | "inches" | "km" | "m" | "miles"', typeGuard(v) { return `${v} == "cm" || ${v} == "feet" || ${v} == "inches" ` + `|| ${v} == "km" || ${v} == "m" || ${v} == "miles"`; }, encoder(v) { return `{ "@value": ${v} }`; }, dataCheck(v) { return `typeof ${v} === "object" && "@value" in ${v} && (${v}["@value"] == "cm" || ${v}["@value"] == "feet" ` + `|| ${v}["@value"] == "inches" || ${v}["@value"] == "km" ` + `|| ${v}["@value"] == "m" || ${v}["@value"] == "miles")`; }, decoder(v) { return `${v}["@value"]`; }, }, }; export function getTypeName( Loading
examples/blog/federation/mod.ts +2 −2 Original line number Diff line number Diff line Loading @@ -21,7 +21,7 @@ import { removeFollower, } from "../models/follower.ts"; import { openKv } from "../models/kv.ts"; import { countPosts, getPosts, toNote } from "../models/post.ts"; import { countPosts, getPosts, toArticle } from "../models/post.ts"; // The `Federation<TContextData>` object is a registry that registers // federation-related callbacks: Loading Loading @@ -97,7 +97,7 @@ federation.setOutboxDispatcher( id: new URL(`/posts/${post.uuid}#activity`, ctx.request.url), actor: ctx.getActorUri(handle), to: new URL("https://www.w3.org/ns/activitystreams#Public"), object: toNote(ctx, blog, post, comments), object: toArticle(ctx, blog, post, comments), }); activities.push(activity); } Loading
examples/blog/models/post.ts +5 −5 Original line number Diff line number Diff line import { RequestContext } from "fedify/federation"; import { Collection, CollectionPage, Note } from "fedify/vocab"; import { Article, Collection, CollectionPage } from "fedify/vocab"; import markdownIt from "markdown-it"; import { uuidv7 } from "uuidv7"; import { Blog } from "./blog.ts"; Loading Loading @@ -70,15 +70,15 @@ export function getContentHtml(post: Post): string { return md.render(post.content); } // Represents a post as an ActivityStreams `Note`: export function toNote( // Represents a post as an ActivityStreams `Article`: export function toArticle( context: RequestContext<void>, blog: Blog, post: Post, comments: Comment[], ): Note { ): Article { const url = new URL(`/posts/${post.uuid}`, context.url); return new Note({ return new Article({ id: url, attributedTo: context.getActorUri(blog.handle), to: new URL("https://www.w3.org/ns/activitystreams#Public"), Loading
examples/blog/routes/posts/[uuid].tsx +6 −2 Original line number Diff line number Diff line Loading @@ -9,7 +9,11 @@ import { type Comment as CommentModel, getComments, } from "../../models/comment.ts"; import { getPost, type Post as PostModel, toNote } from "../../models/post.ts"; import { getPost, type Post as PostModel, toArticle, } from "../../models/post.ts"; export interface PostPageData { domain: string; Loading Loading @@ -37,7 +41,7 @@ export const handler: Handler<PostPageData> = async (req, ctx) => { accept === "application/ld+json" || accept === "application/json" ) { const fedCtx = federation.createContext(req); const note = toNote(fedCtx, blog, post, comments); const note = toArticle(fedCtx, blog, post, comments); const jsonLd = await note.toJsonLd(fedCtx); return new Response(JSON.stringify(jsonLd), { headers: { Loading
examples/blog/routes/posts/index.tsx +2 −2 Original line number Diff line number Diff line Loading @@ -10,7 +10,7 @@ import { countPosts, getPosts, Post, toNote, toArticle, } from "../../models/post.ts"; interface PostsData extends PostFormProps { Loading Loading @@ -90,7 +90,7 @@ export const handler: Handlers<PostsData> = { id: new URL(`/posts/${post.uuid}#activity`, req.url), actor: fedCtx.getActorUri(blog.handle), to: new URL("https://www.w3.org/ns/activitystreams#Public"), object: toNote(fedCtx, blog, post, []), object: toArticle(fedCtx, blog, post, []), }), ); Loading