UNPKG

3.61 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3const Client_1 = require("./parts/Client");
4const Editor_1 = require("./Editor");
5const SerializeError_1 = require("./SerializeError");
6const GenericList_1 = require("./GenericList");
7const resolveTypeForDocument = (id) => {
8 const query = '*[_id in [$documentId, $draftId]]._type';
9 const documentId = id.replace(/^drafts\./, '');
10 const draftId = `drafts.${documentId}`;
11 return Client_1.client.fetch(query, { documentId, draftId }).then(types => types[0]);
12};
13const validateFilter = (spec, options) => {
14 const filter = spec.options.filter.trim();
15 if (['*', '{'].includes(filter[0])) {
16 throw new SerializeError_1.SerializeError(`\`filter\` cannot start with \`${filter[0]}\` - looks like you are providing a query, not a filter`, options.path, spec.id, spec.title).withHelpUrl(SerializeError_1.HELP_URL.QUERY_PROVIDED_FOR_FILTER);
17 }
18 return filter;
19};
20const resolveEditorChildForItem = (itemId, options) => {
21 const parentItem = options.parent;
22 return Promise.resolve(parentItem.schemaTypeName || resolveTypeForDocument(itemId)).then(type => new Editor_1.EditorBuilder()
23 .id('editor')
24 .documentId(itemId)
25 .schemaType(type || ''));
26};
27class DocumentListBuilder extends GenericList_1.GenericListBuilder {
28 constructor(spec) {
29 super();
30 this.spec = spec ? spec : {};
31 }
32 filter(filter) {
33 return this.clone({ options: Object.assign({}, (this.spec.options || {}), { filter }) });
34 }
35 getFilter() {
36 return this.spec.options && this.spec.options.filter;
37 }
38 schemaType(type) {
39 return this.clone({ schemaTypeName: typeof type === 'string' ? type : type.name });
40 }
41 getSchemaType() {
42 return this.spec.schemaTypeName;
43 }
44 params(params) {
45 return this.clone({ options: Object.assign({}, (this.spec.options || { filter: '' }), { params }) });
46 }
47 getParams() {
48 return this.spec.options && this.spec.options.params;
49 }
50 defaultOrdering(ordering) {
51 if (!Array.isArray(ordering)) {
52 throw new Error('`defaultOrdering` must be an array of order clauses');
53 }
54 return this.clone({
55 options: Object.assign({}, (this.spec.options || { filter: '' }), { defaultOrdering: ordering })
56 });
57 }
58 getDefaultOrdering() {
59 return this.spec.options && this.spec.options.defaultOrdering;
60 }
61 serialize(options = { path: [] }) {
62 if (typeof this.spec.id !== 'string' || !this.spec.id) {
63 throw new SerializeError_1.SerializeError('`id` is required for document lists', options.path, options.index, this.spec.title).withHelpUrl(SerializeError_1.HELP_URL.ID_REQUIRED);
64 }
65 if (!this.spec.options || !this.spec.options.filter) {
66 throw new SerializeError_1.SerializeError('`filter` is required for document lists', options.path, this.spec.id, this.spec.title).withHelpUrl(SerializeError_1.HELP_URL.FILTER_REQUIRED);
67 }
68 return Object.assign({}, super.serialize(options), { type: 'documentList', schemaTypeName: this.spec.schemaTypeName, child: this.spec.child || resolveEditorChildForItem, options: Object.assign({}, this.spec.options, { filter: validateFilter(this.spec, options) }) });
69 }
70 clone(withSpec) {
71 const builder = new DocumentListBuilder();
72 builder.spec = Object.assign({}, this.spec, (withSpec || {}));
73 return builder;
74 }
75}
76exports.DocumentListBuilder = DocumentListBuilder;
77
78//# sourceMappingURL=DocumentList.js.map