UNPKG

7.38 kBTypeScriptView Raw
1import { Model } from "./models";
2
3/**
4 * Options for list views.
5 */
6export type ListOpts = {
7 offset?: string | number;
8 limit?: string | number;
9 order?: string;
10 orderBy?: string;
11 models?: string[];
12 search?: {
13 prop?: string;
14 term?: string;
15 scope?: "title" | "global";
16 };
17};
18
19export type ContentFormat = "html" | "plaintext" | "json" | "markdown";
20
21export type MediaListOpts = {
22 offset?: string | number;
23 limit?: string | number;
24 order?: string;
25 orderBy?: string;
26 mimetype?: string;
27 search?: string;
28};
29
30export type PreviewOpts = {
31 publishedOnly?: boolean;
32 ignoreSchedule?: boolean;
33};
34
35/**
36 * Criteria to search for contents.
37 */
38export type Criteria = {
39 [field: string]: {
40 eq?: any;
41 ne?: any;
42 lt?: any;
43 gt?: any;
44 gte?: any;
45 lte?: any;
46 like?: any;
47 path?: string;
48 };
49};
50/**
51 * Joins to resolve referenes in contents.
52 */
53export type Join = {
54 [field: string]: string[];
55};
56
57/**
58 * Metadata extracted from media files.
59 */
60export type Meta = {
61 id: string;
62 size: number;
63 originalname: string;
64 mimetype: string | null;
65 imagetype: string | null;
66 width: number | null;
67 height: number | null;
68};
69
70export type ImageMeta = {
71 focusX: number | null;
72 focusY: number | null;
73 tags: string[] | null;
74 credit: string | null;
75 alt: string | null;
76};
77
78export type Media = Meta &
79 ImageMeta & {
80 created_at: string;
81 };
82
83/**
84 * Settings (like Users & Roles).
85 */
86export type Settings = {
87 id: string;
88 [key: string]: any;
89};
90
91/**
92 * Permissions defined by a Role.
93 */
94export type Permissions = {
95 settings: boolean;
96 preview: boolean;
97 content: {
98 [model: string]: number;
99 };
100};
101
102/**
103 * A (human) user of the system.
104 */
105export type User = {
106 id: string;
107 name: string;
108 email: string;
109 password: string;
110 role: number;
111 picture: string;
112};
113
114/**
115 * The user performing a request.
116 */
117export type Principal = {
118 id: string | null;
119 name: string;
120 permissions: Permissions;
121};
122
123/**
124 * Items displayed in list views.
125 */
126export type Item = {
127 id: string;
128 type: string;
129 model: string;
130 title: string;
131 image?: string | undefined;
132 kind: string;
133 childCount?: string;
134 orderValue?: string;
135};
136
137export type VersionItem = Item & {
138 rev: number;
139 latest: boolean;
140 published_rev: boolean;
141 latest_rev: number;
142 author_name: string;
143 date: string;
144};
145
146export type Schedule = {
147 visibleFrom?: null | Date;
148 visibleUntil?: null | Date;
149};
150
151export type Data = {
152 [field: string]: any;
153};
154
155export type DataRecord = Schedule & {
156 id: string;
157 data: Data;
158};
159
160/**
161 * The REST API uses this format instead of DataRecord
162 */
163export type Record = {
164 _id: string;
165} & Data;
166
167export type Content = DataRecord & {
168 type: string;
169 author: string;
170 date: Date | string;
171};
172
173export type ContentWithRefs = Content & {
174 _refs: Refs;
175};
176
177export type Refs = {
178 media: MediaRefs;
179 content: ContentRefs;
180};
181
182export type MediaRefs = {
183 [id: string]: Meta;
184};
185
186export type ContentRefs = {
187 [type: string]: {
188 [id: string]: Content;
189 };
190};
191
192export type SearchResultItem = {
193 id: string;
194 model: string;
195 title: string;
196 description?: string;
197 image: string | undefined;
198 url: string;
199};
200
201export type Revision = DataRecord & {
202 rev: number;
203};
204
205export type ListChunkWithRefs<ItemType> = {
206 total: number;
207 items: ItemType[];
208 _refs: Refs;
209};
210export type ListChunk<ItemType> = {
211 total: number;
212 items: ItemType[];
213};
214
215export type ReadOnlyDataSource = {
216 contentTypes: string[];
217 list(
218 principal: Principal,
219 model: Model,
220 opts: ListOpts,
221 criteria?: Criteria
222 ): Promise<ListChunk<Item>>;
223 load(
224 principal: Principal,
225 model: Model,
226 id: string,
227 join: Join,
228 format?: string,
229 previewOpts?: PreviewOpts
230 ): Promise<ContentWithRefs | null>;
231 loadInternal(
232 principal: Principal,
233 model: Model,
234 id: string,
235 previewOpts?: PreviewOpts
236 ): Promise<Content | null>;
237 loadItem(
238 principal: Principal,
239 model: Model,
240 id: string
241 ): Promise<Item | null>;
242 find(
243 principal: Principal,
244 model: Model,
245 opts: ListOpts,
246 format: string,
247 join: Join,
248 criteria?: Criteria,
249 previewOpts?: PreviewOpts
250 ): Promise<ListChunkWithRefs<Content>>;
251 findInternal(
252 principal: Principal,
253 model: Model,
254 opts: ListOpts,
255 criteria?: Criteria,
256 previewOpts?: PreviewOpts
257 ): Promise<ListChunk<Content>>;
258};
259
260export type WritableDataSource = ReadOnlyDataSource & {
261 create(
262 principal: Principal,
263 model: Model,
264 data: object,
265 models: Model[]
266 ): Promise<{ id: string; data: object }>;
267 delete(principal: Principal, model: Model, id: string): Promise<void>;
268 update(
269 principal: Principal,
270 model: Model,
271 id: string,
272 data: object,
273 models: Model[]
274 ): Promise<{ id: string; data: object }>;
275};
276
277export type VersionedDataSource = WritableDataSource & {
278 listVersions(
279 principal: Principal,
280 model: Model,
281 id: string
282 ): Promise<Array<VersionItem & { published: boolean }>>;
283 loadRevision(
284 principal: Principal,
285 model: Model,
286 id: string,
287 rev: number
288 ): Promise<Revision>;
289 publishRevision(
290 principal: Principal,
291 model: Model,
292 id: string,
293 rev: string | number,
294 models: Model[]
295 ): Promise<void>;
296 createRevision(
297 principal: Principal,
298 model: Model,
299 id: string,
300 data: object,
301 models: Model[]
302 ): Promise<number>;
303 schedule(
304 principal: Principal,
305 model: Model,
306 id: string,
307 schedule: Schedule
308 ): Promise<void>;
309};
310
311export type DataSource =
312 | ReadOnlyDataSource
313 | WritableDataSource
314 | VersionedDataSource;
315
316export type ExternalDataSource = DataSource & {
317 contentTypes: string[];
318};
319
320export type QuillDelta = {
321 ops: any[];
322};
323
324export type MediaHelper = {
325 original(image: string): string;
326 fromOriginal(image: string): string;
327};
328
329export type CONVERTER_SPREAD_INSTRUCTION = "$$CONVERTER_SPREAD";
330
331export type ConverterInstructions<From, To> = {
332 $$CONVERTER_SPREAD?: (input: From) => Partial<To> | Promise<Partial<To>>;
333} & { [key in keyof To]?: (input: From) => To[key] | Promise<To[key]> };
334
335export interface Converter<ApiDataSet, HubDataSet> {
336 fromHub(input: HubDataSet): Promise<ApiDataSet>;
337 toHub(input: ApiDataSet): Promise<HubDataSet>;
338}
339
340export interface ConverterConstructor {
341 SPREAD: CONVERTER_SPREAD_INSTRUCTION;
342 new <ApiDataSet, HubDataSet>(
343 passThrough: Array<keyof ApiDataSet>,
344 toHub?: ConverterInstructions<ApiDataSet, HubDataSet>,
345 fromHub?: ConverterInstructions<HubDataSet, ApiDataSet>
346 ): Converter<ApiDataSet, HubDataSet>;
347}
348
349export type ExternalDataSourceHelper = {
350 richtextToHtml: (delta: QuillDelta) => string;
351 media: MediaHelper;
352 Converter: ConverterConstructor;
353};
354
355export interface ThumbnailProvider {
356 getThumbUrl(id: string, format: string): Promise<string | null>;
357}
358
359export type BaseUrls = {
360 cms: string;
361 media?: string;
362 preview?: string;
363};
364
365type PostHook = (model: Model, data: Data) => Promise<void>;
366type PreHook = (model: Model, data: Data) => Promise<Data>;
367
368export type PreHooks = {
369 onCreate?: PreHook;
370 onSave?: PreHook;
371};
372export type PostHooks = {
373 onCreate?: PostHook;
374 onSave?: PostHook;
375 onDelete?: PostHook;
376 onPublish?: PostHook;
377 onUnpublish?: PostHook;
378 onSchedule?: PostHook;
379};
380
381export type ContentHooks = {
382 preHooks?: PreHooks;
383 postHooks?: PostHooks;
384};