summaryrefslogtreecommitdiffstats
path: root/src/deps/strapi/loader.ts
blob: c3b8e85537ec659dc353edb8e1ed2342363c3642 (plain) (blame)
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
111
112
113
114
115
116
import type { StandardSchemaV1 } from "corvos/deps/standard_schema.ts";

import { type } from "arktype";
import { PipelineStageError } from "corvos";
import { Severity } from "corvos/core/error.ts";

import { strapiCollectionResponseSchema, strapiItemResponseSchema } from "./schema.ts";

async function* loadPaginated(url: URL | string, init?: RequestInit) {
	let pageSize: number | undefined;
	let pages = 1;

	for (let i = 0; i < pages; ++i) {
		const pageUrl = new URL(url);

		if (pageSize) {
			pageUrl.searchParams.set("pagination[page]", `${i + 1}`);
			pageUrl.searchParams.set("pagination[pageSize]", `${pageSize}`);
		}

		const res = await fetch(pageUrl, init);
		const json = await res.json();

		const dec = strapiCollectionResponseSchema(json);

		if (dec instanceof type.errors) {
			throw new Error(dec.summary);
		}

		pages = dec.meta.pagination.pageCount;
		pageSize = dec.meta.pagination.pageSize;

		for (const item of dec.data) {
			yield item;
		}
	}
}

export class StrapiLoader {
	init: RequestInit;

	constructor(
		readonly host: string | URL,
		token: string,
	) {
		this.init = {
			headers: {
				Authorization: `Bearer ${token}`,
			},
			keepalive: true,
		};
	}

	async loadSingle<T>(
		collection: string,
		schema: StandardSchemaV1<unknown, T>,
		query: Record<string, string> = {},
	) {
		const url = new URL(`/api/${collection}`, this.host);
		Object.entries(query).forEach(([key, value]) => {
			url.searchParams.append(key, value);
		});

		const res = await fetch(url, this.init);
		const json = await res.json();

		const dec = strapiItemResponseSchema(json);

		if (dec instanceof type.errors) {
			throw new Error(`Schema mismatch in ${url}:\n${dec.summary}`);
		}

		const dec2 = await schema["~standard"].validate(dec.data);

		if (dec2.issues) {
			throw new Error(`Schema mismatch in ${url}:\n${dec2.issues.join("\n")}`);
		}

		return {
			documentId: dec.data.documentId,
			publishedAt: dec.data.publishedAt,
			updatedAt: dec.data.updatedAt,
			...dec2.value,
		};
	}

	async *loadCollection<T>(
		collection: string,
		schema: StandardSchemaV1<unknown, T>,
		query: Record<string, string> = {},
	) {
		const url = new URL(`/api/${collection}`, this.host);
		Object.entries(query).forEach(([key, value]) => {
			url.searchParams.append(key, value);
		});

		for await (const item of loadPaginated(url, this.init)) {
			const dec = await schema["~standard"].validate(item);

			if (dec.issues) {
				yield new PipelineStageError(
					Severity.ERROR,
					`Schema mismatch in ${url}`,
					dec.issues.join("\n"),
				);
			} else {
				yield {
					documentId: item.documentId,
					publishedAt: item.publishedAt,
					updatedAt: item.updatedAt,
					...dec.value,
				};
			}
		}
	}
}