@panneau/protocol 5.0.0-alpha.2 → 5.0.0-alpha.20

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (136) hide show
  1. package/dist/addresses.d.ts +23 -0
  2. package/dist/addresses.d.ts.map +1 -0
  3. package/dist/addresses.js +52 -0
  4. package/dist/addresses.js.map +1 -0
  5. package/dist/compound.d.ts +20 -0
  6. package/dist/compound.d.ts.map +1 -0
  7. package/dist/compound.js +27 -0
  8. package/dist/compound.js.map +1 -0
  9. package/dist/conditions.d.ts +1 -1
  10. package/dist/conformance/index.d.ts +6 -0
  11. package/dist/conformance/index.d.ts.map +1 -1
  12. package/dist/conformance/index.js +787 -11
  13. package/dist/conformance/index.js.map +1 -1
  14. package/dist/constants.d.ts +101 -2
  15. package/dist/constants.d.ts.map +1 -1
  16. package/dist/constants.js +174 -3
  17. package/dist/constants.js.map +1 -1
  18. package/dist/context.d.ts +15 -0
  19. package/dist/context.d.ts.map +1 -0
  20. package/dist/context.js +34 -0
  21. package/dist/context.js.map +1 -0
  22. package/dist/currencies.d.ts +22 -0
  23. package/dist/currencies.d.ts.map +1 -0
  24. package/dist/currencies.js +231 -0
  25. package/dist/currencies.js.map +1 -0
  26. package/dist/index.d.ts +4 -4
  27. package/dist/index.js +17 -7
  28. package/dist/libraries.d.ts +26 -0
  29. package/dist/libraries.d.ts.map +1 -0
  30. package/dist/libraries.js +22 -0
  31. package/dist/libraries.js.map +1 -0
  32. package/dist/links.d.ts +38 -0
  33. package/dist/links.d.ts.map +1 -0
  34. package/dist/links.js +65 -0
  35. package/dist/links.js.map +1 -0
  36. package/dist/lint.d.ts +4 -3
  37. package/dist/lint.d.ts.map +1 -1
  38. package/dist/lint.js +114 -2
  39. package/dist/lint.js.map +1 -1
  40. package/dist/mock/auth.d.ts +71 -0
  41. package/dist/mock/auth.d.ts.map +1 -0
  42. package/dist/mock/auth.js +346 -0
  43. package/dist/mock/auth.js.map +1 -0
  44. package/dist/mock/example.d.ts +3 -2
  45. package/dist/mock/example.d.ts.map +1 -1
  46. package/dist/mock/example.js +223 -142
  47. package/dist/mock/example.js.map +1 -1
  48. package/dist/mock/index.d.ts +77 -7
  49. package/dist/mock/index.d.ts.map +1 -1
  50. package/dist/mock/index.js +577 -34
  51. package/dist/mock/index.js.map +1 -1
  52. package/dist/openapi.d.ts.map +1 -1
  53. package/dist/openapi.js +394 -145
  54. package/dist/openapi.js.map +1 -1
  55. package/dist/problem.d.ts +1 -1
  56. package/dist/query.d.ts +18 -0
  57. package/dist/query.d.ts.map +1 -1
  58. package/dist/query.js +34 -8
  59. package/dist/query.js.map +1 -1
  60. package/dist/rules.d.ts +16 -4
  61. package/dist/rules.d.ts.map +1 -1
  62. package/dist/rules.js +230 -13
  63. package/dist/rules.js.map +1 -1
  64. package/dist/runtime/index.d.ts +19 -10
  65. package/dist/runtime/index.d.ts.map +1 -1
  66. package/dist/runtime/index.js +12 -3
  67. package/dist/schemas/action.d.ts +117 -10
  68. package/dist/schemas/action.d.ts.map +1 -1
  69. package/dist/schemas/action.js +21 -3
  70. package/dist/schemas/action.js.map +1 -1
  71. package/dist/schemas/api.d.ts +326 -42
  72. package/dist/schemas/api.d.ts.map +1 -1
  73. package/dist/schemas/api.js +192 -34
  74. package/dist/schemas/api.js.map +1 -1
  75. package/dist/schemas/boot.d.ts +25 -1
  76. package/dist/schemas/boot.d.ts.map +1 -1
  77. package/dist/schemas/boot.js +2 -1
  78. package/dist/schemas/boot.js.map +1 -1
  79. package/dist/schemas/common.d.ts +1 -0
  80. package/dist/schemas/common.d.ts.map +1 -1
  81. package/dist/schemas/common.js +2 -1
  82. package/dist/schemas/common.js.map +1 -1
  83. package/dist/schemas/definition.d.ts +332 -28
  84. package/dist/schemas/definition.d.ts.map +1 -1
  85. package/dist/schemas/definition.js +65 -19
  86. package/dist/schemas/definition.js.map +1 -1
  87. package/dist/schemas/field.d.ts +356 -2
  88. package/dist/schemas/field.d.ts.map +1 -1
  89. package/dist/schemas/field.js +159 -20
  90. package/dist/schemas/field.js.map +1 -1
  91. package/dist/schemas/index.d.ts +1384 -72
  92. package/dist/schemas/index.d.ts.map +1 -1
  93. package/dist/schemas/index.js +58 -9
  94. package/dist/schemas/index.js.map +1 -1
  95. package/dist/schemas/jsonRules.d.ts +83 -0
  96. package/dist/schemas/jsonRules.d.ts.map +1 -0
  97. package/dist/schemas/jsonRules.js +46 -0
  98. package/dist/schemas/jsonRules.js.map +1 -0
  99. package/dist/schemas/layout.d.ts +1 -1
  100. package/dist/schemas/list.d.ts +23 -2
  101. package/dist/schemas/list.d.ts.map +1 -1
  102. package/dist/schemas/list.js +14 -5
  103. package/dist/schemas/list.js.map +1 -1
  104. package/dist/schemas/resource.d.ts +247 -15
  105. package/dist/schemas/resource.d.ts.map +1 -1
  106. package/dist/schemas/resource.js +6 -1
  107. package/dist/schemas/resource.js.map +1 -1
  108. package/dist/schemas/widget.d.ts +109 -0
  109. package/dist/schemas/widget.d.ts.map +1 -0
  110. package/dist/schemas/widget.js +74 -0
  111. package/dist/schemas/widget.js.map +1 -0
  112. package/dist/search.d.ts +51 -0
  113. package/dist/search.d.ts.map +1 -0
  114. package/dist/search.js +53 -0
  115. package/dist/search.js.map +1 -0
  116. package/dist/themes.d.ts +1 -1
  117. package/dist/transforms.d.ts +17 -0
  118. package/dist/transforms.d.ts.map +1 -0
  119. package/dist/transforms.js +34 -0
  120. package/dist/transforms.js.map +1 -0
  121. package/dist/validate.d.ts +5 -3
  122. package/dist/validate.d.ts.map +1 -1
  123. package/dist/validate.js +36 -2
  124. package/dist/validate.js.map +1 -1
  125. package/dist/values.d.ts +5 -1
  126. package/dist/values.d.ts.map +1 -1
  127. package/dist/values.js +16 -2
  128. package/dist/values.js.map +1 -1
  129. package/dist/widgets.d.ts +42 -0
  130. package/dist/widgets.d.ts.map +1 -0
  131. package/dist/widgets.js +130 -0
  132. package/dist/widgets.js.map +1 -0
  133. package/package.json +6 -3
  134. package/schema/boot.schema.json +666 -18
  135. package/schema/definition.schema.json +663 -18
  136. package/schema/openapi.json +5694 -2328
@@ -0,0 +1,26 @@
1
+ /** What `getMediaLibraries()` reads of a definition. */
2
+ export interface MediaLibrariesDefinition {
3
+ resources: readonly {
4
+ id: string;
5
+ kind?: string | undefined;
6
+ }[];
7
+ }
8
+ /**
9
+ * The ids of the libraries a `media` field allows (decision 0027), in the order of its `resource`:
10
+ * the one it names, the ones it lists, or every resource of kind `media` of the definition when it
11
+ * names none. Ids that aren't libraries are left out: `lintDefinition()` reports them.
12
+ */
13
+ export declare function getMediaLibraries(definition: MediaLibrariesDefinition | null, field: {
14
+ resource?: string | readonly string[] | undefined;
15
+ }): string[];
16
+ /** What `getItemImageField()` reads of a resource. */
17
+ export interface ItemImageResource {
18
+ kind?: string | undefined;
19
+ itemImage?: string | undefined;
20
+ }
21
+ /**
22
+ * The key of an item that holds its image (decision 0061): the resource's `itemImage`, or `media`
23
+ * in a media library, whose items all have one. `undefined` for a resource without an image.
24
+ */
25
+ export declare function getItemImageField(resource: ItemImageResource): string | undefined;
26
+ //# sourceMappingURL=libraries.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"libraries.d.ts","sourceRoot":"","sources":["../src/libraries.ts"],"names":[],"mappings":"AAAA,wDAAwD;AACxD,MAAM,WAAW,wBAAwB;IACrC,SAAS,EAAE,SAAS;QAAE,EAAE,EAAE,MAAM,CAAC;QAAC,IAAI,CAAC,EAAE,MAAM,GAAG,SAAS,CAAA;KAAE,EAAE,CAAC;CACnE;AAED;;;;GAIG;AACH,wBAAgB,iBAAiB,CAC7B,UAAU,EAAE,wBAAwB,GAAG,IAAI,EAC3C,KAAK,EAAE;IAAE,QAAQ,CAAC,EAAE,MAAM,GAAG,SAAS,MAAM,EAAE,GAAG,SAAS,CAAA;CAAE,GAC7D,MAAM,EAAE,CAUV;AAED,sDAAsD;AACtD,MAAM,WAAW,iBAAiB;IAC9B,IAAI,CAAC,EAAE,MAAM,GAAG,SAAS,CAAC;IAC1B,SAAS,CAAC,EAAE,MAAM,GAAG,SAAS,CAAC;CAClC;AAED;;;GAGG;AACH,wBAAgB,iBAAiB,CAAC,QAAQ,EAAE,iBAAiB,GAAG,MAAM,GAAG,SAAS,CAEjF"}
@@ -0,0 +1,22 @@
1
+ //#region src/libraries.ts
2
+ /**
3
+ * The ids of the libraries a `media` field allows (decision 0027), in the order of its `resource`:
4
+ * the one it names, the ones it lists, or every resource of kind `media` of the definition when it
5
+ * names none. Ids that aren't libraries are left out: `lintDefinition()` reports them.
6
+ */
7
+ function getMediaLibraries(definition, field) {
8
+ const libraries = (definition?.resources ?? []).filter((resource) => resource.kind === "media").map((resource) => resource.id);
9
+ if (field.resource === void 0) return libraries;
10
+ return (typeof field.resource === "string" ? [field.resource] : field.resource).filter((id) => libraries.includes(id));
11
+ }
12
+ /**
13
+ * The key of an item that holds its image (decision 0061): the resource's `itemImage`, or `media`
14
+ * in a media library, whose items all have one. `undefined` for a resource without an image.
15
+ */
16
+ function getItemImageField(resource) {
17
+ return resource.itemImage ?? (resource.kind === "media" ? "media" : void 0);
18
+ }
19
+ //#endregion
20
+ export { getItemImageField, getMediaLibraries };
21
+
22
+ //# sourceMappingURL=libraries.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"libraries.js","names":[],"sources":["../src/libraries.ts"],"sourcesContent":["/** What `getMediaLibraries()` reads of a definition. */\nexport interface MediaLibrariesDefinition {\n resources: readonly { id: string; kind?: string | undefined }[];\n}\n\n/**\n * The ids of the libraries a `media` field allows (decision 0027), in the order of its `resource`:\n * the one it names, the ones it lists, or every resource of kind `media` of the definition when it\n * names none. Ids that aren't libraries are left out: `lintDefinition()` reports them.\n */\nexport function getMediaLibraries(\n definition: MediaLibrariesDefinition | null,\n field: { resource?: string | readonly string[] | undefined },\n): string[] {\n const libraries = (definition?.resources ?? [])\n .filter((resource) => resource.kind === 'media')\n .map((resource) => resource.id);\n if (field.resource === undefined) {\n return libraries;\n }\n const named: readonly string[] =\n typeof field.resource === 'string' ? [field.resource] : field.resource;\n return named.filter((id) => libraries.includes(id));\n}\n\n/** What `getItemImageField()` reads of a resource. */\nexport interface ItemImageResource {\n kind?: string | undefined;\n itemImage?: string | undefined;\n}\n\n/**\n * The key of an item that holds its image (decision 0061): the resource's `itemImage`, or `media`\n * in a media library, whose items all have one. `undefined` for a resource without an image.\n */\nexport function getItemImageField(resource: ItemImageResource): string | undefined {\n return resource.itemImage ?? (resource.kind === 'media' ? 'media' : undefined);\n}\n"],"mappings":";;;;;;AAUA,SAAgB,kBACZ,YACA,OACQ;CACR,MAAM,aAAa,YAAY,aAAa,CAAC,EAAA,CACxC,QAAQ,aAAa,SAAS,SAAS,OAAO,CAAC,CAC/C,KAAK,aAAa,SAAS,EAAE;CAClC,IAAI,MAAM,aAAa,KAAA,GACnB,OAAO;CAIX,QADI,OAAO,MAAM,aAAa,WAAW,CAAC,MAAM,QAAQ,IAAI,MAAM,SAAA,CACrD,QAAQ,OAAO,UAAU,SAAS,EAAE,CAAC;AACtD;;;;;AAYA,SAAgB,kBAAkB,UAAiD;CAC/E,OAAO,SAAS,cAAc,SAAS,SAAS,UAAU,UAAU,KAAA;AACxE"}
@@ -0,0 +1,38 @@
1
+ import { type BuiltInLinkType } from './constants.js';
2
+ import type { Field } from './schemas/field.js';
3
+ /** A type of a `link` field: a built-in one, or one that a site adds in `settings.types`. */
4
+ export interface LinkType {
5
+ /** `url`, `page`, `email`, `tel`, `file`, or the id of a site's type (`anchor`). */
6
+ id: string;
7
+ /** The name of a site's type. Built-in types are named by the client. */
8
+ label?: string;
9
+ /** A site's type: the pattern its `url` must match. */
10
+ pattern?: string;
11
+ /** A site's type: the placeholder of its input. */
12
+ placeholder?: string;
13
+ }
14
+ /** Which links open in a new tab: as the user chooses, by their type, or none. */
15
+ export type LinkNewTab = 'choose' | 'external' | 'never';
16
+ /** Whether a type of link is built in. */
17
+ export declare function isBuiltInLinkType(type: string): type is BuiltInLinkType;
18
+ /**
19
+ * The types a `link` field offers, in order: those of `settings.types`, or `url`, `email` and
20
+ * `tel`, after `page` when the field has a `resource`. A `page` type without a `resource` is left
21
+ * out, as are entries that aren't a built-in name nor a type with an `id`.
22
+ */
23
+ export declare function getLinkTypes(field: Pick<Field, 'resource' | 'settings'>): LinkType[];
24
+ /** The `newTab` setting of a `link` field: `choose` without one. */
25
+ export declare function getLinkNewTab(field: Pick<Field, 'settings'>): LinkNewTab;
26
+ /** Whether a URL points to another site: `https://…`, `http://…` or `//…`. */
27
+ export declare function isExternalUrl(url: string): boolean;
28
+ /**
29
+ * Whether a link opens in a new tab: the user's choice with `choose`, never with `never`, and with
30
+ * `external`, `true` for `url` and `file` links, `false` for `page`, `email` and `tel` links, and
31
+ * for a site's types, whether their URL points to another site.
32
+ */
33
+ export declare function resolveLinkNewTab(newTab: LinkNewTab, link: {
34
+ type?: unknown;
35
+ url?: unknown;
36
+ newTab?: unknown;
37
+ }): boolean;
38
+ //# sourceMappingURL=links.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"links.d.ts","sourceRoot":"","sources":["../src/links.ts"],"names":[],"mappings":"AACA,OAAO,EAAuB,KAAK,eAAe,EAAE,MAAM,aAAa,CAAC;AACxE,OAAO,KAAK,EAAE,KAAK,EAAE,MAAM,iBAAiB,CAAC;AAG7C,6FAA6F;AAC7F,MAAM,WAAW,QAAQ;IACrB,oFAAoF;IACpF,EAAE,EAAE,MAAM,CAAC;IACX,yEAAyE;IACzE,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,uDAAuD;IACvD,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,mDAAmD;IACnD,WAAW,CAAC,EAAE,MAAM,CAAC;CACxB;AAED,kFAAkF;AAClF,MAAM,MAAM,UAAU,GAAG,QAAQ,GAAG,UAAU,GAAG,OAAO,CAAC;AAEzD,0CAA0C;AAC1C,wBAAgB,iBAAiB,CAAC,IAAI,EAAE,MAAM,GAAG,IAAI,IAAI,eAAe,CAEvE;AAED;;;;GAIG;AACH,wBAAgB,YAAY,CAAC,KAAK,EAAE,IAAI,CAAC,KAAK,EAAE,UAAU,GAAG,UAAU,CAAC,GAAG,QAAQ,EAAE,CA8BpF;AAED,oEAAoE;AACpE,wBAAgB,aAAa,CAAC,KAAK,EAAE,IAAI,CAAC,KAAK,EAAE,UAAU,CAAC,GAAG,UAAU,CAGxE;AAED,8EAA8E;AAC9E,wBAAgB,aAAa,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAElD;AAED;;;;GAIG;AACH,wBAAgB,iBAAiB,CAC7B,MAAM,EAAE,UAAU,EAClB,IAAI,EAAE;IAAE,IAAI,CAAC,EAAE,OAAO,CAAC;IAAC,GAAG,CAAC,EAAE,OAAO,CAAC;IAAC,MAAM,CAAC,EAAE,OAAO,CAAA;CAAE,GAC1D,OAAO,CAkBT"}
package/dist/links.js ADDED
@@ -0,0 +1,65 @@
1
+ import { BUILT_IN_LINK_TYPES } from "./constants.js";
2
+ import { isRecord } from "./values.js";
3
+ //#region src/links.ts
4
+ /** Whether a type of link is built in. */
5
+ function isBuiltInLinkType(type) {
6
+ return BUILT_IN_LINK_TYPES.includes(type);
7
+ }
8
+ /**
9
+ * The types a `link` field offers, in order: those of `settings.types`, or `url`, `email` and
10
+ * `tel`, after `page` when the field has a `resource`. A `page` type without a `resource` is left
11
+ * out, as are entries that aren't a built-in name nor a type with an `id`.
12
+ */
13
+ function getLinkTypes(field) {
14
+ const hasPages = field.resource !== void 0;
15
+ const setting = field.settings?.types;
16
+ const entries = Array.isArray(setting) ? setting : [
17
+ ...hasPages ? ["page"] : [],
18
+ "url",
19
+ "email",
20
+ "tel"
21
+ ];
22
+ const types = [];
23
+ entries.forEach((entry) => {
24
+ let type = null;
25
+ if (typeof entry === "string" && isBuiltInLinkType(entry)) type = { id: entry };
26
+ else if (isRecord(entry) && typeof entry.id === "string" && entry.id !== "") type = {
27
+ id: entry.id,
28
+ ...typeof entry.label === "string" ? { label: entry.label } : {},
29
+ ...typeof entry.pattern === "string" ? { pattern: entry.pattern } : {},
30
+ ...typeof entry.placeholder === "string" ? { placeholder: entry.placeholder } : {}
31
+ };
32
+ if (type !== null && (type.id !== "page" || hasPages) && !types.some((other) => other.id === type.id)) types.push(type);
33
+ });
34
+ return types;
35
+ }
36
+ /** The `newTab` setting of a `link` field: `choose` without one. */
37
+ function getLinkNewTab(field) {
38
+ const setting = field.settings?.newTab;
39
+ return setting === "external" || setting === "never" ? setting : "choose";
40
+ }
41
+ /** Whether a URL points to another site: `https://…`, `http://…` or `//…`. */
42
+ function isExternalUrl(url) {
43
+ return /^(https?:)?\/\//i.test(url.trim());
44
+ }
45
+ /**
46
+ * Whether a link opens in a new tab: the user's choice with `choose`, never with `never`, and with
47
+ * `external`, `true` for `url` and `file` links, `false` for `page`, `email` and `tel` links, and
48
+ * for a site's types, whether their URL points to another site.
49
+ */
50
+ function resolveLinkNewTab(newTab, link) {
51
+ if (newTab === "never") return false;
52
+ if (newTab === "choose") return link.newTab === true;
53
+ switch (link.type) {
54
+ case "url":
55
+ case "file": return true;
56
+ case "page":
57
+ case "email":
58
+ case "tel": return false;
59
+ default: return typeof link.url === "string" && isExternalUrl(link.url);
60
+ }
61
+ }
62
+ //#endregion
63
+ export { getLinkNewTab, getLinkTypes, isBuiltInLinkType, isExternalUrl, resolveLinkNewTab };
64
+
65
+ //# sourceMappingURL=links.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"links.js","names":[],"sources":["../src/links.ts"],"sourcesContent":["// The types of `link` values and their new tabs (decision 0055). This module doesn't import zod.\nimport { BUILT_IN_LINK_TYPES, type BuiltInLinkType } from './constants';\nimport type { Field } from './schemas/field';\nimport { isRecord } from './values';\n\n/** A type of a `link` field: a built-in one, or one that a site adds in `settings.types`. */\nexport interface LinkType {\n /** `url`, `page`, `email`, `tel`, `file`, or the id of a site's type (`anchor`). */\n id: string;\n /** The name of a site's type. Built-in types are named by the client. */\n label?: string;\n /** A site's type: the pattern its `url` must match. */\n pattern?: string;\n /** A site's type: the placeholder of its input. */\n placeholder?: string;\n}\n\n/** Which links open in a new tab: as the user chooses, by their type, or none. */\nexport type LinkNewTab = 'choose' | 'external' | 'never';\n\n/** Whether a type of link is built in. */\nexport function isBuiltInLinkType(type: string): type is BuiltInLinkType {\n return (BUILT_IN_LINK_TYPES as readonly string[]).includes(type);\n}\n\n/**\n * The types a `link` field offers, in order: those of `settings.types`, or `url`, `email` and\n * `tel`, after `page` when the field has a `resource`. A `page` type without a `resource` is left\n * out, as are entries that aren't a built-in name nor a type with an `id`.\n */\nexport function getLinkTypes(field: Pick<Field, 'resource' | 'settings'>): LinkType[] {\n const hasPages = field.resource !== undefined;\n const setting = field.settings?.types;\n const entries: unknown[] = Array.isArray(setting)\n ? setting\n : [...(hasPages ? ['page'] : []), 'url', 'email', 'tel'];\n const types: LinkType[] = [];\n entries.forEach((entry) => {\n let type: LinkType | null = null;\n if (typeof entry === 'string' && isBuiltInLinkType(entry)) {\n type = { id: entry };\n } else if (isRecord(entry) && typeof entry.id === 'string' && entry.id !== '') {\n type = {\n id: entry.id,\n ...(typeof entry.label === 'string' ? { label: entry.label } : {}),\n ...(typeof entry.pattern === 'string' ? { pattern: entry.pattern } : {}),\n ...(typeof entry.placeholder === 'string'\n ? { placeholder: entry.placeholder }\n : {}),\n };\n }\n if (\n type !== null &&\n (type.id !== 'page' || hasPages) &&\n !types.some((other) => other.id === type.id)\n ) {\n types.push(type);\n }\n });\n return types;\n}\n\n/** The `newTab` setting of a `link` field: `choose` without one. */\nexport function getLinkNewTab(field: Pick<Field, 'settings'>): LinkNewTab {\n const setting = field.settings?.newTab;\n return setting === 'external' || setting === 'never' ? setting : 'choose';\n}\n\n/** Whether a URL points to another site: `https://…`, `http://…` or `//…`. */\nexport function isExternalUrl(url: string): boolean {\n return /^(https?:)?\\/\\//i.test(url.trim());\n}\n\n/**\n * Whether a link opens in a new tab: the user's choice with `choose`, never with `never`, and with\n * `external`, `true` for `url` and `file` links, `false` for `page`, `email` and `tel` links, and\n * for a site's types, whether their URL points to another site.\n */\nexport function resolveLinkNewTab(\n newTab: LinkNewTab,\n link: { type?: unknown; url?: unknown; newTab?: unknown },\n): boolean {\n if (newTab === 'never') {\n return false;\n }\n if (newTab === 'choose') {\n return link.newTab === true;\n }\n switch (link.type) {\n case 'url':\n case 'file':\n return true;\n case 'page':\n case 'email':\n case 'tel':\n return false;\n default:\n return typeof link.url === 'string' && isExternalUrl(link.url);\n }\n}\n"],"mappings":";;;;AAqBA,SAAgB,kBAAkB,MAAuC;CACrE,OAAQ,oBAA0C,SAAS,IAAI;AACnE;;;;;;AAOA,SAAgB,aAAa,OAAyD;CAClF,MAAM,WAAW,MAAM,aAAa,KAAA;CACpC,MAAM,UAAU,MAAM,UAAU;CAChC,MAAM,UAAqB,MAAM,QAAQ,OAAO,IAC1C,UACA;EAAC,GAAI,WAAW,CAAC,MAAM,IAAI,CAAC;EAAI;EAAO;EAAS;CAAK;CAC3D,MAAM,QAAoB,CAAC;CAC3B,QAAQ,SAAS,UAAU;EACvB,IAAI,OAAwB;EAC5B,IAAI,OAAO,UAAU,YAAY,kBAAkB,KAAK,GACpD,OAAO,EAAE,IAAI,MAAM;OAChB,IAAI,SAAS,KAAK,KAAK,OAAO,MAAM,OAAO,YAAY,MAAM,OAAO,IACvE,OAAO;GACH,IAAI,MAAM;GACV,GAAI,OAAO,MAAM,UAAU,WAAW,EAAE,OAAO,MAAM,MAAM,IAAI,CAAC;GAChE,GAAI,OAAO,MAAM,YAAY,WAAW,EAAE,SAAS,MAAM,QAAQ,IAAI,CAAC;GACtE,GAAI,OAAO,MAAM,gBAAgB,WAC3B,EAAE,aAAa,MAAM,YAAY,IACjC,CAAC;EACX;EAEJ,IACI,SAAS,SACR,KAAK,OAAO,UAAU,aACvB,CAAC,MAAM,MAAM,UAAU,MAAM,OAAO,KAAK,EAAE,GAE3C,MAAM,KAAK,IAAI;CAEvB,CAAC;CACD,OAAO;AACX;;AAGA,SAAgB,cAAc,OAA4C;CACtE,MAAM,UAAU,MAAM,UAAU;CAChC,OAAO,YAAY,cAAc,YAAY,UAAU,UAAU;AACrE;;AAGA,SAAgB,cAAc,KAAsB;CAChD,OAAO,mBAAmB,KAAK,IAAI,KAAK,CAAC;AAC7C;;;;;;AAOA,SAAgB,kBACZ,QACA,MACO;CACP,IAAI,WAAW,SACX,OAAO;CAEX,IAAI,WAAW,UACX,OAAO,KAAK,WAAW;CAE3B,QAAQ,KAAK,MAAb;EACI,KAAK;EACL,KAAK,QACD,OAAO;EACX,KAAK;EACL,KAAK;EACL,KAAK,OACD,OAAO;EACX,SACI,OAAO,OAAO,KAAK,QAAQ,YAAY,cAAc,KAAK,GAAG;CACrE;AACJ"}
package/dist/lint.d.ts CHANGED
@@ -1,8 +1,9 @@
1
- import type { DefinitionInput } from './schemas';
2
- import type { ValidationIssue } from './validate';
1
+ import type { DefinitionInput } from './schemas/index.js';
2
+ import type { ValidationIssue } from './validate.js';
3
3
  /**
4
4
  * Checks what the schema can't: references between parts of a definition (layout nodes and
5
- * columns that name existing fields, relations that name a resource, unique names).
5
+ * columns that name existing fields, relations, `media` and `image` fields that name a resource,
6
+ * unique names), and the `file` field of `media` resources.
6
7
  */
7
8
  export declare function lintDefinition(definition: DefinitionInput): ValidationIssue[];
8
9
  //# sourceMappingURL=lint.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"lint.d.ts","sourceRoot":"","sources":["../src/lint.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,eAAe,EAAoC,MAAM,WAAW,CAAC;AACnF,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,YAAY,CAAC;AA2ElD;;;GAGG;AACH,wBAAgB,cAAc,CAAC,UAAU,EAAE,eAAe,GAAG,eAAe,EAAE,CAmB7E"}
1
+ {"version":3,"file":"lint.d.ts","sourceRoot":"","sources":["../src/lint.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,eAAe,EAAoC,MAAM,WAAW,CAAC;AACnF,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,YAAY,CAAC;AA8HlD;;;;GAIG;AACH,wBAAgB,cAAc,CAAC,UAAU,EAAE,eAAe,GAAG,eAAe,EAAE,CAiD7E"}
package/dist/lint.js CHANGED
@@ -1,3 +1,4 @@
1
+ import { RESOURCE_WIDGET_TYPES, WIDGET_RESERVED_QUERY_PARAMS, isBuiltInWidgetType } from "./widgets.js";
1
2
  //#region src/lint.ts
2
3
  function fieldNames(fields) {
3
4
  return new Set(fields.map((field) => field.name));
@@ -39,34 +40,145 @@ function checkResource(resource, path, issues) {
39
40
  message: `No field named \`${column.field}\`.`
40
41
  });
41
42
  });
43
+ const calendar = resource.list?.calendar;
44
+ if (calendar !== void 0) {
45
+ const field = resource.fields.find((candidate) => candidate.name === calendar.field);
46
+ const filter = calendar.filter ?? calendar.field;
47
+ if (field === void 0) issues.push({
48
+ path: `${path}.list.calendar.field`,
49
+ message: `No field named \`${calendar.field}\`.`
50
+ });
51
+ else if (![
52
+ "date",
53
+ "datetime",
54
+ "daterange"
55
+ ].includes(field.type)) issues.push({
56
+ path: `${path}.list.calendar.field`,
57
+ message: "The field of a calendar must be a `date`, a `datetime` or a `daterange`."
58
+ });
59
+ if (!(resource.list?.filters ?? []).some((candidate) => candidate.id === filter && candidate.type === "date-range")) issues.push({
60
+ path: `${path}.list.calendar`,
61
+ message: `The calendar needs a \`date-range\` filter \`${filter}\` in the list.`
62
+ });
63
+ }
42
64
  (resource.actions ?? []).forEach((action, index) => {
43
65
  if (action.layout !== void 0) checkLayout(action.layout, fieldNames(action.fields ?? []), `${path}.actions.${index}.layout`, issues);
44
66
  });
67
+ if (resource.kind === "media") {
68
+ const file = resource.fields.findIndex((field) => field.name === "file");
69
+ const field = resource.fields[file];
70
+ if (field === void 0) issues.push({
71
+ path: `${path}.fields`,
72
+ message: "A resource of kind `media` needs a `file` field."
73
+ });
74
+ else if (field.type !== "file" || field.multiple === true) issues.push({
75
+ path: `${path}.fields.${file}`,
76
+ message: "The `file` field of a `media` resource must be a single `file`."
77
+ });
78
+ }
45
79
  resource.fields.forEach((field, index) => {
46
80
  if (field.type === "relation" && field.resource === void 0) issues.push({
47
81
  path: `${path}.fields.${index}.resource`,
48
82
  message: "A `relation` field needs a `resource`."
49
83
  });
84
+ else if (field.type === "relation" && Array.isArray(field.resource)) issues.push({
85
+ path: `${path}.fields.${index}.resource`,
86
+ message: "A `relation` field names one resource."
87
+ });
50
88
  });
51
89
  }
52
90
  /**
53
91
  * Checks what the schema can't: references between parts of a definition (layout nodes and
54
- * columns that name existing fields, relations that name a resource, unique names).
92
+ * columns that name existing fields, relations, `media` and `image` fields that name a resource,
93
+ * unique names), and the `file` field of `media` resources.
55
94
  */
56
95
  function lintDefinition(definition) {
57
96
  const issues = [];
58
97
  const resourceIds = new Set(definition.resources.map((resource) => resource.id));
98
+ const libraries = definition.resources.filter((resource) => resource.kind === "media").map((resource) => resource.id);
59
99
  definition.resources.forEach((resource, index) => {
60
100
  checkResource(resource, `resources.${index}`, issues);
61
101
  resource.fields.forEach((field, fieldIndex) => {
62
- if (field.type === "relation" && field.resource !== void 0 && !resourceIds.has(field.resource)) issues.push({
102
+ if (field.type === "relation" && typeof field.resource === "string" && !resourceIds.has(field.resource)) issues.push({
63
103
  path: `resources.${index}.fields.${fieldIndex}.resource`,
64
104
  message: `No resource \`${field.resource}\`.`
65
105
  });
106
+ if (field.type === "media" || field.type === "image") {
107
+ const fieldPath = `resources.${index}.fields.${fieldIndex}.resource`;
108
+ if (field.resource === void 0) {
109
+ if (libraries.length === 0) issues.push({
110
+ path: fieldPath,
111
+ message: `${field.type === "image" ? "An" : "A"} \`${field.type}\` field needs a resource of kind \`media\`.`
112
+ });
113
+ } else (typeof field.resource === "string" ? [field.resource] : field.resource).forEach((id, idIndex) => {
114
+ if (!libraries.includes(id)) issues.push({
115
+ path: typeof field.resource === "string" ? fieldPath : `${fieldPath}.${String(idIndex)}`,
116
+ message: `No resource of kind \`media\` named \`${id}\`.`
117
+ });
118
+ });
119
+ }
66
120
  });
67
121
  });
122
+ checkWidgets(definition, issues);
68
123
  return issues;
69
124
  }
125
+ var FILTER_PARAM = /^filter\[([^\]]+)\]/;
126
+ /**
127
+ * The widgets of the dashboard (decision 0064): unique ids, and the built-in `stat` and `list` on
128
+ * a resource that exists, with filters of its list. A `stat` counts the items of the list, so the
129
+ * list can't be paged with cursors, which give no total.
130
+ */
131
+ function checkWidgets(definition, issues) {
132
+ const ids = /* @__PURE__ */ new Set();
133
+ (definition.dashboard?.widgets ?? []).forEach((widget, index) => {
134
+ const path = `dashboard.widgets.${index}`;
135
+ if (ids.has(widget.id)) issues.push({
136
+ path: `${path}.id`,
137
+ message: `Duplicate widget id \`${widget.id}\`.`
138
+ });
139
+ ids.add(widget.id);
140
+ if (!isBuiltInWidgetType(widget.type)) return;
141
+ const settings = widget.settings ?? {};
142
+ if (!(typeof settings.resource === "string")) {
143
+ if (settings.query !== void 0) issues.push({
144
+ path: `${path}.settings.query`,
145
+ message: "`query` goes with a `resource`."
146
+ });
147
+ return;
148
+ }
149
+ if (!RESOURCE_WIDGET_TYPES.includes(widget.type)) {
150
+ issues.push({
151
+ path: `${path}.settings.resource`,
152
+ message: `A \`${widget.type}\` widget has data of its own: only \`stat\` and \`list\` read a resource.`
153
+ });
154
+ return;
155
+ }
156
+ const resource = definition.resources.find((candidate) => candidate.id === settings.resource);
157
+ if (resource === void 0) {
158
+ issues.push({
159
+ path: `${path}.settings.resource`,
160
+ message: `No resource \`${String(settings.resource)}\`.`
161
+ });
162
+ return;
163
+ }
164
+ if (widget.type === "stat" && resource.list?.pagination === "cursor") issues.push({
165
+ path: `${path}.settings.resource`,
166
+ message: `The list of \`${resource.id}\` is paged with cursors and has no total: it can't feed a \`stat\`.`
167
+ });
168
+ const filters = new Set((resource.list?.filters ?? []).map((filter) => filter.id));
169
+ Object.keys(settings.query ?? {}).forEach((key) => {
170
+ const filter = FILTER_PARAM.exec(key)?.[1];
171
+ if (WIDGET_RESERVED_QUERY_PARAMS.includes(key)) issues.push({
172
+ path: `${path}.settings.query.${key}`,
173
+ message: `The client sets \`${key}\`.`
174
+ });
175
+ else if (filter !== void 0 && !filters.has(filter)) issues.push({
176
+ path: `${path}.settings.query.${key}`,
177
+ message: `The list of \`${resource.id}\` has no filter \`${filter}\`.`
178
+ });
179
+ });
180
+ });
181
+ }
70
182
  //#endregion
71
183
  export { lintDefinition };
72
184
 
package/dist/lint.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"lint.js","names":[],"sources":["../src/lint.ts"],"sourcesContent":["import type { DefinitionInput, Field, LayoutNode, ResourceInput } from './schemas';\nimport type { ValidationIssue } from './validate';\n\nfunction fieldNames(fields: Field[]): Set<string> {\n return new Set(fields.map((field) => field.name));\n}\n\nfunction checkLayout(\n nodes: LayoutNode[],\n names: Set<string>,\n path: string,\n issues: ValidationIssue[],\n) {\n nodes.forEach((node, index) => {\n const nodePath = `${path}.${index}`;\n if (node.type === 'field') {\n if (node.name === undefined) {\n issues.push({ path: nodePath, message: 'A `field` node needs a `name`.' });\n } else if (!names.has(node.name)) {\n issues.push({\n path: `${nodePath}.name`,\n message: `No field named \\`${node.name}\\`.`,\n });\n }\n }\n if (node.type === 'component' && node.component === undefined) {\n issues.push({ path: nodePath, message: 'A `component` node needs a `component`.' });\n }\n if (node.type === 'tabs' && node.children?.some((child) => child.type !== 'tab')) {\n issues.push({\n path: `${nodePath}.children`,\n message: 'The children of `tabs` must be `tab` nodes.',\n });\n }\n if (node.children !== undefined) {\n checkLayout(node.children, names, `${nodePath}.children`, issues);\n }\n });\n}\n\nfunction checkResource(resource: ResourceInput, path: string, issues: ValidationIssue[]) {\n const names = fieldNames(resource.fields);\n if (names.size !== resource.fields.length) {\n issues.push({ path: `${path}.fields`, message: 'Field names must be unique.' });\n }\n if (resource.form?.layout !== undefined) {\n checkLayout(resource.form.layout, names, `${path}.form.layout`, issues);\n }\n (resource.list?.columns ?? []).forEach((column, index) => {\n if (column.field !== undefined && !names.has(column.field)) {\n issues.push({\n path: `${path}.list.columns.${index}.field`,\n message: `No field named \\`${column.field}\\`.`,\n });\n }\n });\n (resource.actions ?? []).forEach((action, index) => {\n if (action.layout !== undefined) {\n checkLayout(\n action.layout,\n fieldNames(action.fields ?? []),\n `${path}.actions.${index}.layout`,\n issues,\n );\n }\n });\n resource.fields.forEach((field, index) => {\n if (field.type === 'relation' && field.resource === undefined) {\n issues.push({\n path: `${path}.fields.${index}.resource`,\n message: 'A `relation` field needs a `resource`.',\n });\n }\n });\n}\n\n/**\n * Checks what the schema can't: references between parts of a definition (layout nodes and\n * columns that name existing fields, relations that name a resource, unique names).\n */\nexport function lintDefinition(definition: DefinitionInput): ValidationIssue[] {\n const issues: ValidationIssue[] = [];\n const resourceIds = new Set(definition.resources.map((resource) => resource.id));\n definition.resources.forEach((resource, index) => {\n checkResource(resource, `resources.${index}`, issues);\n resource.fields.forEach((field, fieldIndex) => {\n if (\n field.type === 'relation' &&\n field.resource !== undefined &&\n !resourceIds.has(field.resource)\n ) {\n issues.push({\n path: `resources.${index}.fields.${fieldIndex}.resource`,\n message: `No resource \\`${field.resource}\\`.`,\n });\n }\n });\n });\n return issues;\n}\n"],"mappings":";AAGA,SAAS,WAAW,QAA8B;CAC9C,OAAO,IAAI,IAAI,OAAO,KAAK,UAAU,MAAM,IAAI,CAAC;AACpD;AAEA,SAAS,YACL,OACA,OACA,MACA,QACF;CACE,MAAM,SAAS,MAAM,UAAU;EAC3B,MAAM,WAAW,GAAG,KAAK,GAAG;EAC5B,IAAI,KAAK,SAAS,SAAS;GACvB,IAAI,KAAK,SAAS,KAAA,GACd,OAAO,KAAK;IAAE,MAAM;IAAU,SAAS;GAAiC,CAAC;QACtE,IAAI,CAAC,MAAM,IAAI,KAAK,IAAI,GAC3B,OAAO,KAAK;IACR,MAAM,GAAG,SAAS;IAClB,SAAS,oBAAoB,KAAK,KAAK;GAC3C,CAAC;EAET;EACA,IAAI,KAAK,SAAS,eAAe,KAAK,cAAc,KAAA,GAChD,OAAO,KAAK;GAAE,MAAM;GAAU,S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AAI,IAAI,WAAW,UAAU,KAAK,aAAa,SAAS,EAAE,CAAC;CAC/E,WAAW,UAAU,SAAS,UAAU,UAAU;EAC9C,cAAc,UAAU,aAAa,SAAS,MAAM;EACpD,SAAS,OAAO,SAAS,OAAO,eAAe;GAC3C,IACI,MAAM,SAAS,cACf,MAAM,aAAa,KAAA,KACnB,CAAC,YAAY,IAAI,MAAM,QAAQ,GAE/B,OAAO,KAAK;IACR,MAAM,aAAa,MAAM,UAAU,WAAW;IAC9C,SAAS,iBAAiB,MAAM,SAAS;GAC7C,CAAC;EAET,CAAC;CACL,CAAC;CACD,OAAO;AACX"}
1
+ {"version":3,"file":"lint.js","names":[],"sources":["../src/lint.ts"],"sourcesContent":["import type { DefinitionInput, Field, LayoutNode, ResourceInput } from './schemas';\nimport type { ValidationIssue } from './validate';\nimport {\n RESOURCE_WIDGET_TYPES,\n WIDGET_RESERVED_QUERY_PARAMS,\n isBuiltInWidgetType,\n} from './widgets';\n\nfunction fieldNames(fields: Field[]): Set<string> {\n return new Set(fields.map((field) => field.name));\n}\n\nfunction checkLayout(\n nodes: LayoutNode[],\n names: Set<string>,\n path: string,\n issues: ValidationIssue[],\n) {\n nodes.forEach((node, index) => {\n const nodePath = `${path}.${index}`;\n if (node.type === 'field') {\n if (node.name === undefined) {\n issues.push({ path: nodePath, message: 'A `field` node needs a `name`.' });\n } else if (!names.has(node.name)) {\n issues.push({\n path: `${nodePath}.name`,\n message: `No field named \\`${node.name}\\`.`,\n });\n }\n }\n if (node.type === 'component' && node.component === undefined) {\n issues.push({ path: nodePath, message: 'A `component` node needs a `component`.' });\n }\n if (node.type === 'tabs' && node.children?.some((child) => child.type !== 'tab')) {\n issues.push({\n path: `${nodePath}.children`,\n message: 'The children of `tabs` must be `tab` nodes.',\n });\n }\n if (node.children !== undefined) {\n checkLayout(node.children, names, `${nodePath}.children`, issues);\n }\n });\n}\n\nfunction checkResource(resource: ResourceInput, path: string, issues: ValidationIssue[]) {\n const names = fieldNames(resource.fields);\n if (names.size !== resource.fields.length) {\n issues.push({ path: `${path}.fields`, message: 'Field names must be unique.' });\n }\n if (resource.form?.layout !== undefined) {\n checkLayout(resource.form.layout, names, `${path}.form.layout`, issues);\n }\n (resource.list?.columns ?? []).forEach((column, index) => {\n if (column.field !== undefined && !names.has(column.field)) {\n issues.push({\n path: `${path}.list.columns.${index}.field`,\n message: `No field named \\`${column.field}\\`.`,\n });\n }\n });\n const calendar = resource.list?.calendar;\n if (calendar !== undefined) {\n const field = resource.fields.find((candidate) => candidate.name === calendar.field);\n const filter = calendar.filter ?? calendar.field;\n if (field === undefined) {\n issues.push({\n path: `${path}.list.calendar.field`,\n message: `No field named \\`${calendar.field}\\`.`,\n });\n } else if (!['date', 'datetime', 'daterange'].includes(field.type)) {\n issues.push({\n path: `${path}.list.calendar.field`,\n message: 'The field of a calendar must be a `date`, a `datetime` or a `daterange`.',\n });\n }\n if (\n !(resource.list?.filters ?? []).some(\n (candidate) => candidate.id === filter && candidate.type === 'date-range',\n )\n ) {\n issues.push({\n path: `${path}.list.calendar`,\n message: `The calendar needs a \\`date-range\\` filter \\`${filter}\\` in the list.`,\n });\n }\n }\n (resource.actions ?? []).forEach((action, index) => {\n if (action.layout !== undefined) {\n checkLayout(\n action.layout,\n fieldNames(action.fields ?? []),\n `${path}.actions.${index}.layout`,\n issues,\n );\n }\n });\n if (resource.kind === 'media') {\n const file = resource.fields.findIndex((field) => field.name === 'file');\n const field = resource.fields[file];\n if (field === undefined) {\n issues.push({\n path: `${path}.fields`,\n message: 'A resource of kind `media` needs a `file` field.',\n });\n } else if (field.type !== 'file' || field.multiple === true) {\n issues.push({\n path: `${path}.fields.${file}`,\n message: 'The `file` field of a `media` resource must be a single `file`.',\n });\n }\n }\n resource.fields.forEach((field, index) => {\n if (field.type === 'relation' && field.resource === undefined) {\n issues.push({\n path: `${path}.fields.${index}.resource`,\n message: 'A `relation` field needs a `resource`.',\n });\n } else if (field.type === 'relation' && Array.isArray(field.resource)) {\n issues.push({\n path: `${path}.fields.${index}.resource`,\n message: 'A `relation` field names one resource.',\n });\n }\n });\n}\n\n/**\n * Checks what the schema can't: references between parts of a definition (layout nodes and\n * columns that name existing fields, relations, `media` and `image` fields that name a resource,\n * unique names), and the `file` field of `media` resources.\n */\nexport function lintDefinition(definition: DefinitionInput): ValidationIssue[] {\n const issues: ValidationIssue[] = [];\n const resourceIds = new Set(definition.resources.map((resource) => resource.id));\n const libraries = definition.resources\n .filter((resource) => resource.kind === 'media')\n .map((resource) => resource.id);\n definition.resources.forEach((resource, index) => {\n checkResource(resource, `resources.${index}`, issues);\n resource.fields.forEach((field, fieldIndex) => {\n if (\n field.type === 'relation' &&\n typeof field.resource === 'string' &&\n !resourceIds.has(field.resource)\n ) {\n issues.push({\n path: `resources.${index}.fields.${fieldIndex}.resource`,\n message: `No resource \\`${field.resource}\\`.`,\n });\n }\n if (field.type === 'media' || field.type === 'image') {\n const fieldPath = `resources.${index}.fields.${fieldIndex}.resource`;\n if (field.resource === undefined) {\n // Every library of the definition (decision 0027).\n if (libraries.length === 0) {\n issues.push({\n path: fieldPath,\n message: `${field.type === 'image' ? 'An' : 'A'} \\`${field.type}\\` field needs a resource of kind \\`media\\`.`,\n });\n }\n } else {\n const named =\n typeof field.resource === 'string' ? [field.resource] : field.resource;\n named.forEach((id, idIndex) => {\n if (!libraries.includes(id)) {\n issues.push({\n path:\n typeof field.resource === 'string'\n ? fieldPath\n : `${fieldPath}.${String(idIndex)}`,\n message: `No resource of kind \\`media\\` named \\`${id}\\`.`,\n });\n }\n });\n }\n }\n });\n });\n checkWidgets(definition, issues);\n return issues;\n}\n\nconst FILTER_PARAM = /^filter\\[([^\\]]+)\\]/;\n\n/**\n * The widgets of the dashboard (decision 0064): unique ids, and the built-in `stat` and `list` on\n * a resource that exists, with filters of its list. A `stat` counts the items of the list, so the\n * list can't be paged with cursors, which give no total.\n */\nfunction checkWidgets(definition: DefinitionInput, issues: ValidationIssue[]) {\n const ids = new Set<string>();\n (definition.dashboard?.widgets ?? []).forEach((widget, index) => {\n const path = `dashboard.widgets.${index}`;\n if (ids.has(widget.id)) {\n issues.push({ path: `${path}.id`, message: `Duplicate widget id \\`${widget.id}\\`.` });\n }\n ids.add(widget.id);\n if (!isBuiltInWidgetType(widget.type)) {\n return;\n }\n const settings = widget.settings ?? {};\n const onResource = typeof settings.resource === 'string';\n if (!onResource) {\n if (settings.query !== undefined) {\n issues.push({\n path: `${path}.settings.query`,\n message: '`query` goes with a `resource`.',\n });\n }\n return;\n }\n if (!(RESOURCE_WIDGET_TYPES as readonly string[]).includes(widget.type)) {\n issues.push({\n path: `${path}.settings.resource`,\n message: `A \\`${widget.type}\\` widget has data of its own: only \\`stat\\` and \\`list\\` read a resource.`,\n });\n return;\n }\n const resource = definition.resources.find(\n (candidate) => candidate.id === settings.resource,\n );\n if (resource === undefined) {\n issues.push({\n path: `${path}.settings.resource`,\n message: `No resource \\`${String(settings.resource)}\\`.`,\n });\n return;\n }\n if (widget.type === 'stat' && resource.list?.pagination === 'cursor') {\n issues.push({\n path: `${path}.settings.resource`,\n message: `The list of \\`${resource.id}\\` is paged with cursors and has no total: it can't feed a \\`stat\\`.`,\n });\n }\n const filters = new Set((resource.list?.filters ?? []).map((filter) => filter.id));\n Object.keys(settings.query ?? {}).forEach((key) => {\n const filter = FILTER_PARAM.exec(key)?.[1];\n if ((WIDGET_RESERVED_QUERY_PARAMS as readonly string[]).includes(key)) {\n issues.push({\n path: `${path}.settings.query.${key}`,\n message: `The client sets \\`${key}\\`.`,\n });\n } else if (filter !== undefined && !filters.has(filter)) {\n issues.push({\n path: `${path}.settings.query.${key}`,\n message: `The list of \\`${resource.id}\\` has no filter \\`${filter}\\`.`,\n });\n }\n });\n });\n}\n"],"mappings":";;AAQA,SAAS,WAAW,QAA8B;CAC9C,OAAO,IAAI,IAAI,OAAO,KAAK,UAAU,MAAM,IAAI,CAAC;AACpD;AAEA,SAAS,YACL,OACA,OACA,MACA,QACF;CACE,MAAM,SAAS,MAAM,UAAU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AAa,KAAK,GAAG,CAAC,GAAG;GACxC,IAAK,6BAAmD,SAAS,GAAG,GAChE,OAAO,KAAK;IACR,MAAM,GAAG,KAAK,kBAAkB;IAChC,SAAS,qBAAqB,IAAI;GACtC,CAAC;QACE,IAAI,WAAW,KAAA,KAAa,CAAC,QAAQ,IAAI,MAAM,GAClD,OAAO,KAAK;IACR,MAAM,GAAG,KAAK,kBAAkB;IAChC,SAAS,iBAAiB,SAAS,GAAG,qBAAqB,OAAO;GACtE,CAAC;EAET,CAAC;CACL,CAAC;AACL"}
@@ -0,0 +1,71 @@
1
+ import type { Auth, User } from '../schemas/index.js';
2
+ /** A user of the mock backend. */
3
+ export interface MockUser {
4
+ /** Defaults to the position of the user in `users`, from 1. */
5
+ id?: string;
6
+ /** Defaults to the email. */
7
+ name?: string;
8
+ email: string;
9
+ password: string;
10
+ /** Whether the email address is verified, for `auth.emailVerification`. Defaults to true. */
11
+ emailVerified?: boolean;
12
+ /**
13
+ * Whether the user has two-factor authentication, for `auth.twoFactor`: their password login
14
+ * then asks for `twoFactorCode` or a recovery code. Defaults to false.
15
+ */
16
+ twoFactor?: boolean;
17
+ /** Recovery codes of a user with `twoFactor`. Defaults to none. */
18
+ recoveryCodes?: string[];
19
+ /** Whether the backend requires two-factor authentication for the user. Defaults to false. */
20
+ twoFactorRequired?: boolean;
21
+ /**
22
+ * The passkeys of the user, for `auth.passkeys`: the `id` of their credential, and a name. The
23
+ * mock doesn't check signatures: a credential with one of these ids logs in.
24
+ */
25
+ passkeys?: {
26
+ id: string;
27
+ name: string;
28
+ }[];
29
+ }
30
+ /** An email that the mock backend "sends", with the path of its link under the app's base path. */
31
+ export interface MockMail {
32
+ kind: 'password-reset' | 'email-verification';
33
+ to: string;
34
+ token: string;
35
+ /** The page of the app that the link opens, with its query: `auth/reset-password?token=…`. */
36
+ path: string;
37
+ }
38
+ /**
39
+ * The auth of the mock backend: its users, and who is logged in. With it, the routes of the
40
+ * `panneau` auth mode answer (`/auth/login`, `/auth/logout`, `/auth/session`, and those of
41
+ * `auth.passwordReset` and `auth.emailVerification`), and the other routes answer `401` to
42
+ * guests.
43
+ */
44
+ export interface MockAuthOptions {
45
+ users: MockUser[];
46
+ /** The email of the user logged in at the start. Defaults to the first user; `null`: a guest. */
47
+ loggedIn?: string | null;
48
+ /** Called with each email the backend sends: there is no mailbox. */
49
+ onMail?: (mail: MockMail) => void;
50
+ /** The code of the authenticator app of every user, for `auth.twoFactor`. Defaults to `123456`. */
51
+ twoFactorCode?: string;
52
+ /**
53
+ * The email of the user that each OAuth provider of `auth.providers` logs in, at once, when the
54
+ * app goes to its `redirect` (decision 0052). A provider without one fails with `access`.
55
+ */
56
+ providerUsers?: Record<string, string>;
57
+ }
58
+ type Values = Record<string, unknown>;
59
+ export interface MockAuth {
60
+ /** The current user, as the definition and `/auth/session` give it. */
61
+ user(): User | null;
62
+ /** The answer of a route of `/auth/…`, without its first segment. */
63
+ answer(segments: string[], method: string, readBody: () => Promise<Values>, url?: URL): Promise<Response>;
64
+ /** The answer of a route of `/account/…`, without its first segment, or null for another. */
65
+ account(segments: string[], method: string, readBody: () => Promise<Values>): Promise<Response> | null;
66
+ /** The answer to a request of another route by the current user, when it is refused. */
67
+ refuse(): Response | null;
68
+ }
69
+ export declare function createMockAuth(options: MockAuthOptions, settings: Auth | undefined): MockAuth;
70
+ export {};
71
+ //# sourceMappingURL=auth.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"auth.d.ts","sourceRoot":"","sources":["../../src/mock/auth.ts"],"names":[],"mappings":"AAMA,OAAO,KAAK,EAAE,IAAI,EAAE,IAAI,EAAE,MAAM,YAAY,CAAC;AAI7C,kCAAkC;AAClC,MAAM,WAAW,QAAQ;IACrB,+DAA+D;IAC/D,EAAE,CAAC,EAAE,MAAM,CAAC;IACZ,6BAA6B;IAC7B,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,KAAK,EAAE,MAAM,CAAC;IACd,QAAQ,EAAE,MAAM,CAAC;IACjB,6F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