From 5c899b4a5c2c07398151960b2b86d238b49d5bf4 Mon Sep 17 00:00:00 2001 From: Volpeon Date: Sun, 26 Jul 2026 11:57:13 +0200 Subject: Better Strapi integration --- src/deps/strapi.ts | 110 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 110 insertions(+) create mode 100644 src/deps/strapi.ts (limited to 'src/deps/strapi.ts') diff --git a/src/deps/strapi.ts b/src/deps/strapi.ts new file mode 100644 index 0000000..32e49b0 --- /dev/null +++ b/src/deps/strapi.ts @@ -0,0 +1,110 @@ +import type { Pipeline, PipelineStageError } from "corvos"; +import type { StandardSchemaV1, StandardTypedV1 } from "corvos/deps/standard_schema.ts"; + +import { Page } from "corvos"; +import { replaceExtension } from "corvos/core/utils/path.ts"; + +import type { Node } from "./strapi/schema.ts"; + +import { blocksToTSX } from "./strapi/blocks.tsx"; +import { StrapiLoader } from "./strapi/loader.ts"; + +export type { Node } from "./strapi/schema.ts"; +export { nodeSchema, fileAttributeSchema, imageAttributeSchema } from "./strapi/schema.ts"; +export { Blocks } from "./strapi/blocks.tsx"; + +export interface StrapiContent { + type: Type; + schema: StandardSchemaV1; + query?: Record; +} + +type ContentSingle> = { + [Key in keyof T]: T[Key]["type"] extends "single" ? Key : never; +}[keyof T]; + +type ContentCollection> = { + [Key in keyof T]: T[Key]["type"] extends "collection" ? Key : never; +}[keyof T]; + +export interface Options> { + url: string; + token: string; + content: Content; +} + +export function strapi>(options: Options) { + const loader = new StrapiLoader(options.url, options.token); + + function load & string>( + key: Key, + query?: Record, + ): Promise< + { + documentId: string; + publishedAt: Date | null; + updatedAt: Date; + } & StandardTypedV1.InferOutput + >; + function load & string>( + key: Key, + query?: Record, + ): AsyncGenerator< + | PipelineStageError + | ({ + documentId: string; + publishedAt: Date | null; + updatedAt: Date; + } & StandardTypedV1.InferOutput), + void, + unknown + >; + function load( + key: Key, + query?: Record, + ): + | Promise< + { + documentId: string; + publishedAt: Date | null; + updatedAt: Date; + } & StandardTypedV1.InferOutput + > + | AsyncGenerator< + | PipelineStageError + | ({ + documentId: string; + publishedAt: Date | null; + updatedAt: Date; + } & StandardTypedV1.InferOutput), + void, + unknown + > { + if (options.content[key]!.type === "single") { + return loader.loadSingle(key, options.content[key]!.schema, { + ...options.content[key]!.query, + ...query, + }); + } else { + return loader.loadCollection(key, options.content[key]!.schema, { + ...options.content[key]!.query, + ...query, + }); + } + } + + return ( + pipeline: Pipeline, + ) => + pipeline + .process("strapi", ".strapi", function* (page) { + yield new Page( + ".jsxelement", + blocksToTSX(page.content), + page.data as Out[".strapi"][1], + replaceExtension(page.url, ".jsx"), + page, + ); + }) + .helper("strapi", load); +} -- cgit v1.3.1