-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathtypes.ts
135 lines (119 loc) · 3.26 KB
/
types.ts
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
// deno-lint-ignore-file no-explicit-any
import type Collection from "./core/collection.ts";
import type Document from "./core/document.ts";
import type Upload from "./core/upload.ts";
import { FieldKeys } from "./fields/core.ts";
/** Generic data to store */
export type Data = Record<string, unknown>;
export interface EntryMetadata {
label: string;
name: string;
src: string;
}
export interface SiteInfo {
name: string;
description?: string;
url?: string;
body?: string;
}
/** A storage mechanism for data */
export interface Storage extends AsyncIterable<EntryMetadata> {
name(name: string): string;
get(name: string): Entry;
directory(name: string): Storage;
delete(name: string): Promise<void>;
rename(name: string, newName: string): Promise<void>;
}
export interface Entry {
src?: string;
metadata: EntryMetadata;
readData(): Promise<Data>;
writeData(content: Data): Promise<void>;
readFile(): Promise<File>;
writeFile(content: File): Promise<void>;
}
export interface Version {
name: string;
isCurrent: boolean;
isProduction: boolean;
}
export interface Versioning extends AsyncIterable<Version> {
current(): Promise<Version>;
create(id: string): Promise<void>;
change(id: string): Promise<void>;
publish(id: string): Promise<void>;
delete(id: string): Promise<void>;
}
/** A transformer to convert from/to Data */
export interface Transformer<T> {
toData(content: T): Data | Promise<Data>;
fromData(data: Data): T | Promise<T>;
}
/** The schema for a field */
export interface Field {
type: FieldKeys | (string & Record<never, never>);
// ^ Typescript hack to suggest the correct keys but allow any string
// https://x.com/diegohaz/status/1524257274012876801
name: string;
value?: unknown;
fields?: (Field | string)[];
label?: string;
description?: string;
options?: Option[];
/** @deprecated. Use `upload` instead */
uploads?: string;
upload?: string | false;
view?: string;
attributes?: {
required?: boolean;
min?: number;
max?: number;
step?: number;
maxlength?: number;
pattern?: string;
[key: string]: unknown;
};
init?: (field: ResolvedField, content: CMSContent) => void | Promise<void>;
transform?(value: any, field: ResolvedField): any;
[key: string]: unknown;
}
export interface ResolvedField extends Field {
tag: string;
label: string;
fields?: ResolvedField[];
details?: Record<string, any>;
applyChanges(
data: Data,
changes: Data,
field: ResolvedField,
document: Document,
content: CMSContent,
): void | Promise<void>;
}
export interface FieldType {
tag: string;
jsImport: string;
init?: (field: ResolvedField, content: CMSContent) => void;
applyChanges(
data: Data,
changes: Data,
field: ResolvedField,
document: Document,
content: CMSContent,
): void | Promise<void>;
}
export type Labelizer = (
name: string,
prev?: (name: string) => string,
) => string;
type Option = string | { value: string | number; label: string };
export interface CMSContent {
basePath: string;
auth: boolean;
site: SiteInfo;
collections: Record<string, Collection>;
documents: Record<string, Document>;
uploads: Record<string, Upload>;
versioning?: Versioning;
data: Record<string, any>;
}