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); }