summaryrefslogtreecommitdiffstats
path: root/src/deps/strapi/schema.ts
blob: 616168032e56e23031edb94610817d8535d3c86f (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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
import { type } from "arktype";

export enum HeadingBlockLevel {
	L1 = 1,
	L2 = 2,
	L3 = 3,
	L4 = 4,
	L5 = 5,
	L6 = 6,
}

export const fileAttributeSchema = type({
	"alternativeText?": "string | null",
	"caption?": "string | null",
	"createdAt": "string",
	"documentId": "string",
	"ext": "string",
	"formats?": "Record<string, unknown> | null",
	"hash": "string",
	"height?": "number | null",
	"mime": "string",
	"name": "string",
	"previewUrl?": "string | null",
	"provider": "string",
	"provider_metadata?": "unknown",
	"size": "number",
	"updatedAt": "string",
	"url": "string",
	"width?": "number | null",
});

export type FileAttribute = typeof fileAttributeSchema.infer;

export const imageAttributeSchema = fileAttributeSchema.narrow(
	(o): o is Omit<typeof o, "width" | "height"> & { width: number; height: number } =>
		!!o.width && !!o.height,
);

export type ImageAttribute = typeof imageAttributeSchema.infer;

const blocksAttributeScope = type
	.scope({
		codeBlockNode: {
			"children": "defaultInlineNode[]",
			"language?": "string",
			"type": "'code'",
		},

		defaultInlineNode: "textInlineNode | linkInlineNode",

		headingBlockNode: {
			children: "defaultInlineNode[]",
			level: "1 | 2 | 3 | 4 | 5 | 6",
			type: "'heading'",
		},

		imageBlockNode: {
			children: type({
				text: "string",
				type: "'text'",
			}).array(),
			image: fileAttributeSchema.omit("documentId"),
			type: "'image'",
		},

		linkInlineNode: {
			children: "textInlineNode[]",
			type: "'link'",
			url: "string",
		},

		listBlockNode: {
			children: "(listItemInlineNode | listBlockNode)[]",
			format: "'ordered' | 'unordered'",
			type: "'list'",
		},

		listItemInlineNode: {
			children: "defaultInlineNode[]",
			type: "'list-item'",
		},

		node: "rootNode | nonTextInlineNode",

		nonTextInlineNode: "linkInlineNode | listItemInlineNode",

		paragraphBlockNode: {
			children: "defaultInlineNode[]",
			type: "'paragraph'",
		},

		quoteBlockNode: {
			children: "defaultInlineNode[]",
			type: "'quote'",
		},

		rootNode:
			"paragraphBlockNode | quoteBlockNode | codeBlockNode | headingBlockNode | listBlockNode | imageBlockNode",

		textInlineNode: {
			"bold?": "boolean",
			"code?": "boolean",
			"italic?": "boolean",
			"strikethrough?": "boolean",
			"text": "string",
			"type": "'text'",
			"underline?": "boolean",
		},
	})
	.export();

export const defaultInlineNodeSchema = blocksAttributeScope.defaultInlineNode;

export type DefaultInlineNode = typeof defaultInlineNodeSchema.infer;

export const nodeSchema = blocksAttributeScope.node;

export type Node = typeof nodeSchema.infer;

export const strapiItemSchema = type({
	"[string]": "unknown",
	"documentId": "string",
	"publishedAt": "string.date.parse | null",
	"updatedAt": "string.date.parse",
});

export type StrapiItem = typeof strapiItemSchema.infer;

export const strapiCollectionResponseSchema = type({
	data: strapiItemSchema.array(),
	meta: {
		pagination: {
			page: "number",
			pageCount: "number",
			pageSize: "number",
			total: "number",
		},
	},
});

export const strapiItemResponseSchema = type({
	data: strapiItemSchema,
});