1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
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 extends "single" | "collection" = "single" | "collection"> {
type: Type;
schema: StandardSchemaV1;
query?: Record<string, string>;
}
type ContentSingle<T extends Record<string, StrapiContent>> = {
[Key in keyof T]: T[Key]["type"] extends "single" ? Key : never;
}[keyof T];
type ContentCollection<T extends Record<string, StrapiContent>> = {
[Key in keyof T]: T[Key]["type"] extends "collection" ? Key : never;
}[keyof T];
export interface Options<Content extends Record<string, StrapiContent>> {
url: string;
token: string;
content: Content;
}
export function strapi<Content extends Record<string, StrapiContent>>(options: Options<Content>) {
const loader = new StrapiLoader(options.url, options.token);
function load<const Key extends ContentSingle<Content> & string>(
key: Key,
query?: Record<string, string>,
): Promise<
{
documentId: string;
publishedAt: Date | null;
updatedAt: Date;
} & StandardTypedV1.InferOutput<Content[Key]["schema"]>
>;
function load<const Key extends ContentCollection<Content> & string>(
key: Key,
query?: Record<string, string>,
): AsyncGenerator<
| PipelineStageError
| ({
documentId: string;
publishedAt: Date | null;
updatedAt: Date;
} & StandardTypedV1.InferOutput<Content[Key]["schema"]>),
void,
unknown
>;
function load<const Key extends Content & string>(
key: Key,
query?: Record<string, string>,
):
| Promise<
{
documentId: string;
publishedAt: Date | null;
updatedAt: Date;
} & StandardTypedV1.InferOutput<Content[Key]["schema"]>
>
| AsyncGenerator<
| PipelineStageError
| ({
documentId: string;
publishedAt: Date | null;
updatedAt: Date;
} & StandardTypedV1.InferOutput<Content[Key]["schema"]>),
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 <Loaders extends string, Out extends { ".strapi": [Node[], {}] }, Helpers extends {}>(
pipeline: Pipeline<Loaders, Out, Helpers>,
) =>
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);
}
|