@panneau/protocol 5.0.0-alpha.3 → 5.0.0-alpha.32

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 (147) 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 +801 -11
  13. package/dist/conformance/index.js.map +1 -1
  14. package/dist/constants.d.ts +108 -2
  15. package/dist/constants.d.ts.map +1 -1
  16. package/dist/constants.js +181 -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 +19 -8
  28. package/dist/libraries.d.ts +33 -0
  29. package/dist/libraries.d.ts.map +1 -0
  30. package/dist/libraries.js +59 -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 +168 -7
  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 +241 -144
  47. package/dist/mock/example.js.map +1 -1
  48. package/dist/mock/index.d.ts +79 -7
  49. package/dist/mock/index.d.ts.map +1 -1
  50. package/dist/mock/index.js +603 -38
  51. package/dist/mock/index.js.map +1 -1
  52. package/dist/openapi.d.ts.map +1 -1
  53. package/dist/openapi.js +392 -144
  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/resourceTypes.d.ts +39 -0
  61. package/dist/resourceTypes.d.ts.map +1 -0
  62. package/dist/resourceTypes.js +34 -0
  63. package/dist/resourceTypes.js.map +1 -0
  64. package/dist/rules.d.ts +18 -5
  65. package/dist/rules.d.ts.map +1 -1
  66. package/dist/rules.js +233 -15
  67. package/dist/rules.js.map +1 -1
  68. package/dist/runtime/index.d.ts +20 -10
  69. package/dist/runtime/index.d.ts.map +1 -1
  70. package/dist/runtime/index.js +13 -3
  71. package/dist/schemas/action.d.ts +118 -10
  72. package/dist/schemas/action.d.ts.map +1 -1
  73. package/dist/schemas/action.js +20 -3
  74. package/dist/schemas/action.js.map +1 -1
  75. package/dist/schemas/api.d.ts +334 -42
  76. package/dist/schemas/api.d.ts.map +1 -1
  77. package/dist/schemas/api.js +194 -34
  78. package/dist/schemas/api.js.map +1 -1
  79. package/dist/schemas/boot.d.ts +25 -1
  80. package/dist/schemas/boot.d.ts.map +1 -1
  81. package/dist/schemas/boot.js +2 -1
  82. package/dist/schemas/boot.js.map +1 -1
  83. package/dist/schemas/common.d.ts +1 -0
  84. package/dist/schemas/common.d.ts.map +1 -1
  85. package/dist/schemas/common.js +2 -1
  86. package/dist/schemas/common.js.map +1 -1
  87. package/dist/schemas/definition.d.ts +341 -28
  88. package/dist/schemas/definition.d.ts.map +1 -1
  89. package/dist/schemas/definition.js +65 -19
  90. package/dist/schemas/definition.js.map +1 -1
  91. package/dist/schemas/field.d.ts +357 -2
  92. package/dist/schemas/field.d.ts.map +1 -1
  93. package/dist/schemas/field.js +160 -20
  94. package/dist/schemas/field.js.map +1 -1
  95. package/dist/schemas/index.d.ts +1429 -80
  96. package/dist/schemas/index.d.ts.map +1 -1
  97. package/dist/schemas/index.js +60 -10
  98. package/dist/schemas/index.js.map +1 -1
  99. package/dist/schemas/jsonRules.d.ts +83 -0
  100. package/dist/schemas/jsonRules.d.ts.map +1 -0
  101. package/dist/schemas/jsonRules.js +46 -0
  102. package/dist/schemas/jsonRules.js.map +1 -0
  103. package/dist/schemas/layout.d.ts +2 -1
  104. package/dist/schemas/layout.d.ts.map +1 -1
  105. package/dist/schemas/layout.js +2 -1
  106. package/dist/schemas/layout.js.map +1 -1
  107. package/dist/schemas/list.d.ts +23 -2
  108. package/dist/schemas/list.d.ts.map +1 -1
  109. package/dist/schemas/list.js +15 -6
  110. package/dist/schemas/list.js.map +1 -1
  111. package/dist/schemas/resource.d.ts +263 -17
  112. package/dist/schemas/resource.d.ts.map +1 -1
  113. package/dist/schemas/resource.js +19 -4
  114. package/dist/schemas/resource.js.map +1 -1
  115. package/dist/schemas/theme.d.ts +4 -0
  116. package/dist/schemas/theme.d.ts.map +1 -1
  117. package/dist/schemas/theme.js +1 -0
  118. package/dist/schemas/theme.js.map +1 -1
  119. package/dist/schemas/widget.d.ts +109 -0
  120. package/dist/schemas/widget.d.ts.map +1 -0
  121. package/dist/schemas/widget.js +74 -0
  122. package/dist/schemas/widget.js.map +1 -0
  123. package/dist/search.d.ts +51 -0
  124. package/dist/search.d.ts.map +1 -0
  125. package/dist/search.js +53 -0
  126. package/dist/search.js.map +1 -0
  127. package/dist/themes.d.ts +1 -1
  128. package/dist/transforms.d.ts +17 -0
  129. package/dist/transforms.d.ts.map +1 -0
  130. package/dist/transforms.js +34 -0
  131. package/dist/transforms.js.map +1 -0
  132. package/dist/validate.d.ts +5 -3
  133. package/dist/validate.d.ts.map +1 -1
  134. package/dist/validate.js +36 -2
  135. package/dist/validate.js.map +1 -1
  136. package/dist/values.d.ts +5 -1
  137. package/dist/values.d.ts.map +1 -1
  138. package/dist/values.js +16 -2
  139. package/dist/values.js.map +1 -1
  140. package/dist/widgets.d.ts +42 -0
  141. package/dist/widgets.d.ts.map +1 -0
  142. package/dist/widgets.js +130 -0
  143. package/dist/widgets.js.map +1 -0
  144. package/package.json +6 -3
  145. package/schema/boot.schema.json +716 -20
  146. package/schema/definition.schema.json +713 -20
  147. package/schema/openapi.json +5659 -2229
@@ -1 +1 @@
1
- {"version":3,"file":"values.js","names":[],"sources":["../src/values.ts"],"sourcesContent":["/** Reads a value at a dot path (`blocks.2.title.fr`). */\nexport function getValue(values: unknown, path: string): unknown {\n if (path === '') {\n return values;\n }\n return path.split('.').reduce<unknown>((current, key) => {\n if (current === null || typeof current !== 'object') {\n return undefined;\n }\n return (current as Record<string, unknown>)[key];\n }, values);\n}\n\n/** Whether a value counts as filled: not null, not an empty or blank string, not an empty array or object. */\nexport function isFilled(value: unknown): boolean {\n if (value === null || value === undefined) {\n return false;\n }\n if (typeof value === 'string') {\n return value.trim() !== '';\n }\n if (Array.isArray(value)) {\n return value.length > 0;\n }\n if (typeof value === 'object') {\n return Object.keys(value).length > 0;\n }\n return true;\n}\n\nexport function isRecord(value: unknown): value is Record<string, unknown> {\n return value !== null && typeof value === 'object' && !Array.isArray(value);\n}\n\nexport function isEqual(a: unknown, b: unknown): boolean {\n if (a === b) {\n return true;\n }\n if (typeof a !== 'object' || typeof b !== 'object' || a === null || b === null) {\n return false;\n }\n return JSON.stringify(a) === JSON.stringify(b);\n}\n"],"mappings":";;AACA,SAAgB,SAAS,QAAiB,MAAuB;CAC7D,IAAI,SAAS,IACT,OAAO;CAEX,OAAO,KAAK,MAAM,GAAG,CAAC,CAAC,QAAiB,SAAS,QAAQ;EACrD,IAAI,YAAY,QAAQ,OAAO,YAAY,UACvC;EAEJ,OAAQ,QAAoC;CAChD,GAAG,MAAM;AACb;;AAGA,SAAgB,SAAS,OAAyB;CAC9C,IAAI,UAAU,QAAQ,UAAU,KAAA,GAC5B,OAAO;CAEX,IAAI,OAAO,UAAU,UACjB,OAAO,MAAM,KAAK,MAAM;CAE5B,IAAI,MAAM,QAAQ,KAAK,GACnB,OAAO,MAAM,SAAS;CAE1B,IAAI,OAAO,UAAU,UACjB,OAAO,OAAO,KAAK,KAAK,CAAC,CAAC,SAAS;CAEvC,OAAO;AACX;AAEA,SAAgB,SAAS,OAAkD;CACvE,OAAO,UAAU,QAAQ,OAAO,UAAU,YAAY,CAAC,MAAM,QAAQ,KAAK;AAC9E;AAEA,SAAgB,QAAQ,GAAY,GAAqB;CACrD,IAAI,MAAM,GACN,OAAO;CAEX,IAAI,OAAO,MAAM,YAAY,OAAO,MAAM,YAAY,MAAM,QAAQ,MAAM,MACtE,OAAO;CAEX,OAAO,KAAK,UAAU,CAAC,MAAM,KAAK,UAAU,CAAC;AACjD"}
1
+ {"version":3,"file":"values.js","names":[],"sources":["../src/values.ts"],"sourcesContent":["/** Reads a value at a dot path (`blocks.2.title.fr`). */\nexport function getValue(values: unknown, path: string): unknown {\n if (path === '') {\n return values;\n }\n return path.split('.').reduce<unknown>((current, key) => {\n if (current === null || typeof current !== 'object') {\n return undefined;\n }\n return (current as Record<string, unknown>)[key];\n }, values);\n}\n\n/**\n * Whether a value counts as filled: not null, not an empty or blank string, not an empty array or\n * plain object. A file (`Blob`, `File`, even empty) is filled. Other collections count by their\n * `length` or `size` (an empty `FileList`, `Map` or `Set` isn't filled); other objects are filled.\n */\nexport function isFilled(value: unknown): boolean {\n if (value === null || value === undefined) {\n return false;\n }\n if (typeof value === 'string') {\n return value.trim() !== '';\n }\n if (Array.isArray(value)) {\n return value.length > 0;\n }\n if (typeof value === 'object') {\n if (typeof Blob !== 'undefined' && value instanceof Blob) {\n return true;\n }\n // A chosen file has no own keys: only plain objects are empty without them.\n if (isPlainObject(value)) {\n return Object.keys(value).length > 0;\n }\n const { length, size } = value as { length?: unknown; size?: unknown };\n if (typeof length === 'number') {\n return length > 0;\n }\n return typeof size === 'number' ? size > 0 : true;\n }\n return true;\n}\n\nfunction isPlainObject(value: object): boolean {\n const prototype: unknown = Object.getPrototypeOf(value);\n return prototype === Object.prototype || prototype === null;\n}\n\nexport function isRecord(value: unknown): value is Record<string, unknown> {\n return value !== null && typeof value === 'object' && !Array.isArray(value);\n}\n\nexport function isEqual(a: unknown, b: unknown): boolean {\n if (a === b) {\n return true;\n }\n if (typeof a !== 'object' || typeof b !== 'object' || a === null || b === null) {\n return false;\n }\n return JSON.stringify(a) === JSON.stringify(b);\n}\n"],"mappings":";;AACA,SAAgB,SAAS,QAAiB,MAAuB;CAC7D,IAAI,SAAS,IACT,OAAO;CAEX,OAAO,KAAK,MAAM,GAAG,CAAC,CAAC,QAAiB,SAAS,QAAQ;EACrD,IAAI,YAAY,QAAQ,OAAO,YAAY,UACvC;EAEJ,OAAQ,QAAoC;CAChD,GAAG,MAAM;AACb;;;;;;AAOA,SAAgB,SAAS,OAAyB;CAC9C,IAAI,UAAU,QAAQ,UAAU,KAAA,GAC5B,OAAO;CAEX,IAAI,OAAO,UAAU,UACjB,OAAO,MAAM,KAAK,MAAM;CAE5B,IAAI,MAAM,QAAQ,KAAK,GACnB,OAAO,MAAM,SAAS;CAE1B,IAAI,OAAO,UAAU,UAAU;EAC3B,IAAI,OAAO,SAAS,eAAe,iBAAiB,MAChD,OAAO;EAGX,IAAI,cAAc,KAAK,GACnB,OAAO,OAAO,KAAK,KAAK,CAAC,CAAC,SAAS;EAEvC,MAAM,EAAE,QAAQ,SAAS;EACzB,IAAI,OAAO,WAAW,UAClB,OAAO,SAAS;EAEpB,OAAO,OAAO,SAAS,WAAW,OAAO,IAAI;CACjD;CACA,OAAO;AACX;AAEA,SAAS,cAAc,OAAwB;CAC3C,MAAM,YAAqB,OAAO,eAAe,KAAK;CACtD,OAAO,cAAc,OAAO,aAAa,cAAc;AAC3D;AAEA,SAAgB,SAAS,OAAkD;CACvE,OAAO,UAAU,QAAQ,OAAO,UAAU,YAAY,CAAC,MAAM,QAAQ,KAAK;AAC9E;AAEA,SAAgB,QAAQ,GAAY,GAAqB;CACrD,IAAI,MAAM,GACN,OAAO;CAEX,IAAI,OAAO,MAAM,YAAY,OAAO,MAAM,YAAY,MAAM,QAAQ,MAAM,MACtE,OAAO;CAEX,OAAO,KAAK,UAAU,CAAC,MAAM,KAAK,UAAU,CAAC;AACjD"}
@@ -0,0 +1,42 @@
1
+ import { type ListQuery } from './query.js';
2
+ /** The widget types that every client draws (decision 0064). Other types are widgets of plugins. */
3
+ export declare const BUILT_IN_WIDGET_TYPES: readonly ["stat", "series", "list"];
4
+ export type BuiltInWidgetType = (typeof BUILT_IN_WIDGET_TYPES)[number];
5
+ /** The periods of the points of a `series` widget. */
6
+ export declare const WIDGET_SERIES_PERIODS: readonly ["day", "week", "month"];
7
+ export type WidgetSeriesPeriod = (typeof WIDGET_SERIES_PERIODS)[number];
8
+ /** The most items a `list` widget shows: the backend sends the most useful first. */
9
+ export declare const WIDGET_LIST_MAX_ITEMS = 5;
10
+ /** The built-in types that can read the list of a resource (`settings.resource`). */
11
+ export declare const RESOURCE_WIDGET_TYPES: readonly ["stat", "list"];
12
+ /** Query parameters that the client sets on the list of a widget on a resource, never `query`. */
13
+ export declare const WIDGET_RESERVED_QUERY_PARAMS: readonly ["page", "perPage", "cursor"];
14
+ export declare function isBuiltInWidgetType(type: string): type is BuiltInWidgetType;
15
+ /** A widget as the helpers read it: the `Widget` of the definition, or one a backend writes. */
16
+ interface WidgetLike {
17
+ type: string;
18
+ settings?: Record<string, unknown> | undefined;
19
+ }
20
+ /** A built-in widget that reads the list of a resource, with no data of its own (decision 0064). */
21
+ export interface WidgetResource {
22
+ /** The id of the resource. */
23
+ resource: string;
24
+ /** The `query` of the widget, as query parameters, without `page`, `perPage` and `cursor`. */
25
+ params: URLSearchParams;
26
+ /** The same query, parsed: the query of the list request, and of the link to the list. */
27
+ query: ListQuery;
28
+ }
29
+ /**
30
+ * The resource whose list feeds a built-in `stat` or `list` widget, and its query: `null` for a
31
+ * widget with data of its own at `GET /dashboard/widgets/{widget}`, and for widgets of plugins.
32
+ */
33
+ export declare function getWidgetResource(widget: WidgetLike): WidgetResource | null;
34
+ /**
35
+ * Checks the data of a widget against the shape of its type (decision 0064), without the schemas:
36
+ * the paths of the wrong keys (`value`, `points.2.date`), empty when it is right. The data of a
37
+ * widget of a plugin only has to be an object. The dates of a series must go up, one point per
38
+ * period.
39
+ */
40
+ export declare function checkWidgetData(type: string, data: unknown): string[];
41
+ export {};
42
+ //# sourceMappingURL=widgets.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"widgets.d.ts","sourceRoot":"","sources":["../src/widgets.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,SAAS,EAAoB,MAAM,SAAS,CAAC;AAG3D,oGAAoG;AACpG,eAAO,MAAM,qBAAqB,qCAAsC,CAAC;AAEzE,MAAM,MAAM,iBAAiB,GAAG,CAAC,OAAO,qBAAqB,CAAC,CAAC,MAAM,CAAC,CAAC;AAEvE,sDAAsD;AACtD,eAAO,MAAM,qBAAqB,mCAAoC,CAAC;AAEvE,MAAM,MAAM,kBAAkB,GAAG,CAAC,OAAO,qBAAqB,CAAC,CAAC,MAAM,CAAC,CAAC;AAExE,qFAAqF;AACrF,eAAO,MAAM,qBAAqB,IAAI,CAAC;AAEvC,qFAAqF;AACrF,eAAO,MAAM,qBAAqB,2BAA4B,CAAC;AAE/D,kGAAkG;AAClG,eAAO,MAAM,4BAA4B,wCAAyC,CAAC;AAEnF,wBAAgB,mBAAmB,CAAC,IAAI,EAAE,MAAM,GAAG,IAAI,IAAI,iBAAiB,CAE3E;AAED,gGAAgG;AAChG,UAAU,UAAU;IAChB,IAAI,EAAE,MAAM,CAAC;IACb,QAAQ,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAAG,SAAS,CAAC;CAClD;AAED,oGAAoG;AACpG,MAAM,WAAW,cAAc;IAC3B,8BAA8B;IAC9B,QAAQ,EAAE,MAAM,CAAC;IACjB,8FAA8F;IAC9F,MAAM,EAAE,eAAe,CAAC;IACxB,0FAA0F;IAC1F,KAAK,EAAE,SAAS,CAAC;CACpB;AAED;;;GAGG;AACH,wBAAgB,iBAAiB,CAAC,MAAM,EAAE,UAAU,GAAG,cAAc,GAAG,IAAI,CA4B3E;AAwGD;;;;;GAKG;AACH,wBAAgB,eAAe,CAAC,IAAI,EAAE,MAAM,EAAE,IAAI,EAAE,OAAO,GAAG,MAAM,EAAE,CAcrE"}
@@ -0,0 +1,130 @@
1
+ import { isRecord } from "./values.js";
2
+ import { fromSearchParams } from "./query.js";
3
+ //#region src/widgets.ts
4
+ /** The widget types that every client draws (decision 0064). Other types are widgets of plugins. */
5
+ var BUILT_IN_WIDGET_TYPES = [
6
+ "stat",
7
+ "series",
8
+ "list"
9
+ ];
10
+ /** The periods of the points of a `series` widget. */
11
+ var WIDGET_SERIES_PERIODS = [
12
+ "day",
13
+ "week",
14
+ "month"
15
+ ];
16
+ /** The most items a `list` widget shows: the backend sends the most useful first. */
17
+ var WIDGET_LIST_MAX_ITEMS = 5;
18
+ /** The built-in types that can read the list of a resource (`settings.resource`). */
19
+ var RESOURCE_WIDGET_TYPES = ["stat", "list"];
20
+ /** Query parameters that the client sets on the list of a widget on a resource, never `query`. */
21
+ var WIDGET_RESERVED_QUERY_PARAMS = [
22
+ "page",
23
+ "perPage",
24
+ "cursor"
25
+ ];
26
+ function isBuiltInWidgetType(type) {
27
+ return BUILT_IN_WIDGET_TYPES.includes(type);
28
+ }
29
+ /**
30
+ * The resource whose list feeds a built-in `stat` or `list` widget, and its query: `null` for a
31
+ * widget with data of its own at `GET /dashboard/widgets/{widget}`, and for widgets of plugins.
32
+ */
33
+ function getWidgetResource(widget) {
34
+ const resource = widget.settings?.resource;
35
+ if (!RESOURCE_WIDGET_TYPES.includes(widget.type) || typeof resource !== "string" || resource === "") return null;
36
+ const params = new URLSearchParams();
37
+ const query = widget.settings?.query;
38
+ if (isRecord(query)) Object.entries(query).forEach(([key, value]) => {
39
+ if (WIDGET_RESERVED_QUERY_PARAMS.includes(key)) return;
40
+ (Array.isArray(value) ? value : [value]).forEach((entry) => {
41
+ if (typeof entry === "string" || typeof entry === "number" || typeof entry === "boolean") params.append(key, String(entry));
42
+ });
43
+ });
44
+ return {
45
+ resource,
46
+ params,
47
+ query: fromSearchParams(params)
48
+ };
49
+ }
50
+ var DATE = /^\d{4}-\d{2}-\d{2}$/;
51
+ var TONE = /^[a-z0-9-]+$/;
52
+ var isNumber = (value) => typeof value === "number" && Number.isFinite(value);
53
+ /** Whether a key is missing, `null`, or passes the check. */
54
+ function optional(data, key, check) {
55
+ return data[key] === void 0 || data[key] === null || check(data[key]);
56
+ }
57
+ var isString = (value) => typeof value === "string";
58
+ var isTone = (value) => isString(value) && TONE.test(value);
59
+ function checkStat(data) {
60
+ const wrong = [];
61
+ if (!isNumber(data.value)) wrong.push("value");
62
+ if (!optional(data, "help", isString)) wrong.push("help");
63
+ if (!optional(data, "tone", isTone)) wrong.push("tone");
64
+ if (!optional(data, "url", isString)) wrong.push("url");
65
+ return wrong;
66
+ }
67
+ function checkSeries(data) {
68
+ const wrong = [];
69
+ if (!WIDGET_SERIES_PERIODS.includes(data.period)) wrong.push("period");
70
+ if (!Array.isArray(data.points)) wrong.push("points");
71
+ else {
72
+ let previous = "";
73
+ data.points.forEach((point, index) => {
74
+ if (!isRecord(point)) {
75
+ wrong.push(`points.${index}`);
76
+ return;
77
+ }
78
+ if (!isString(point.date) || !DATE.test(point.date) || point.date <= previous) wrong.push(`points.${index}.date`);
79
+ else previous = point.date;
80
+ if (!isNumber(point.value)) wrong.push(`points.${index}.value`);
81
+ });
82
+ }
83
+ if (!optional(data, "total", isNumber)) wrong.push("total");
84
+ return wrong;
85
+ }
86
+ function checkList(data) {
87
+ const wrong = [];
88
+ if (!Array.isArray(data.items) || data.items.length > 5) wrong.push("items");
89
+ else data.items.forEach((item, index) => {
90
+ if (!isRecord(item)) {
91
+ wrong.push(`items.${index}`);
92
+ return;
93
+ }
94
+ if (!isString(item.title)) wrong.push(`items.${index}.title`);
95
+ [
96
+ "description",
97
+ "meta",
98
+ "url"
99
+ ].forEach((key) => {
100
+ if (!optional(item, key, isString)) wrong.push(`items.${index}.${key}`);
101
+ });
102
+ const badge = item.badge;
103
+ if (badge !== void 0 && badge !== null) {
104
+ if (!isRecord(badge) || !isString(badge.label)) wrong.push(`items.${index}.badge`);
105
+ else if (!optional(badge, "tone", isTone)) wrong.push(`items.${index}.badge.tone`);
106
+ }
107
+ });
108
+ if (!optional(data, "empty", isString)) wrong.push("empty");
109
+ if (!optional(data, "url", isString)) wrong.push("url");
110
+ return wrong;
111
+ }
112
+ /**
113
+ * Checks the data of a widget against the shape of its type (decision 0064), without the schemas:
114
+ * the paths of the wrong keys (`value`, `points.2.date`), empty when it is right. The data of a
115
+ * widget of a plugin only has to be an object. The dates of a series must go up, one point per
116
+ * period.
117
+ */
118
+ function checkWidgetData(type, data) {
119
+ if (!isRecord(data)) return [""];
120
+ switch (type) {
121
+ case "stat": return checkStat(data);
122
+ case "series": return checkSeries(data);
123
+ case "list": return checkList(data);
124
+ default: return [];
125
+ }
126
+ }
127
+ //#endregion
128
+ export { BUILT_IN_WIDGET_TYPES, RESOURCE_WIDGET_TYPES, WIDGET_LIST_MAX_ITEMS, WIDGET_RESERVED_QUERY_PARAMS, WIDGET_SERIES_PERIODS, checkWidgetData, getWidgetResource, isBuiltInWidgetType };
129
+
130
+ //# sourceMappingURL=widgets.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"widgets.js","names":[],"sources":["../src/widgets.ts"],"sourcesContent":["import { type ListQuery, fromSearchParams } from './query';\nimport { isRecord } from './values';\n\n/** The widget types that every client draws (decision 0064). Other types are widgets of plugins. */\nexport const BUILT_IN_WIDGET_TYPES = ['stat', 'series', 'list'] as const;\n\nexport type BuiltInWidgetType = (typeof BUILT_IN_WIDGET_TYPES)[number];\n\n/** The periods of the points of a `series` widget. */\nexport const WIDGET_SERIES_PERIODS = ['day', 'week', 'month'] as const;\n\nexport type WidgetSeriesPeriod = (typeof WIDGET_SERIES_PERIODS)[number];\n\n/** The most items a `list` widget shows: the backend sends the most useful first. */\nexport const WIDGET_LIST_MAX_ITEMS = 5;\n\n/** The built-in types that can read the list of a resource (`settings.resource`). */\nexport const RESOURCE_WIDGET_TYPES = ['stat', 'list'] as const;\n\n/** Query parameters that the client sets on the list of a widget on a resource, never `query`. */\nexport const WIDGET_RESERVED_QUERY_PARAMS = ['page', 'perPage', 'cursor'] as const;\n\nexport function isBuiltInWidgetType(type: string): type is BuiltInWidgetType {\n return (BUILT_IN_WIDGET_TYPES as readonly string[]).includes(type);\n}\n\n/** A widget as the helpers read it: the `Widget` of the definition, or one a backend writes. */\ninterface WidgetLike {\n type: string;\n settings?: Record<string, unknown> | undefined;\n}\n\n/** A built-in widget that reads the list of a resource, with no data of its own (decision 0064). */\nexport interface WidgetResource {\n /** The id of the resource. */\n resource: string;\n /** The `query` of the widget, as query parameters, without `page`, `perPage` and `cursor`. */\n params: URLSearchParams;\n /** The same query, parsed: the query of the list request, and of the link to the list. */\n query: ListQuery;\n}\n\n/**\n * The resource whose list feeds a built-in `stat` or `list` widget, and its query: `null` for a\n * widget with data of its own at `GET /dashboard/widgets/{widget}`, and for widgets of plugins.\n */\nexport function getWidgetResource(widget: WidgetLike): WidgetResource | null {\n const resource = widget.settings?.resource;\n if (\n !(RESOURCE_WIDGET_TYPES as readonly string[]).includes(widget.type) ||\n typeof resource !== 'string' ||\n resource === ''\n ) {\n return null;\n }\n const params = new URLSearchParams();\n const query = widget.settings?.query;\n if (isRecord(query)) {\n Object.entries(query).forEach(([key, value]) => {\n if ((WIDGET_RESERVED_QUERY_PARAMS as readonly string[]).includes(key)) {\n return;\n }\n (Array.isArray(value) ? value : [value]).forEach((entry) => {\n if (\n typeof entry === 'string' ||\n typeof entry === 'number' ||\n typeof entry === 'boolean'\n ) {\n params.append(key, String(entry));\n }\n });\n });\n }\n return { resource, params, query: fromSearchParams(params) };\n}\n\nconst DATE = /^\\d{4}-\\d{2}-\\d{2}$/;\nconst TONE = /^[a-z0-9-]+$/;\n\nconst isNumber = (value: unknown): value is number =>\n typeof value === 'number' && Number.isFinite(value);\n\n/** Whether a key is missing, `null`, or passes the check. */\nfunction optional(\n data: Record<string, unknown>,\n key: string,\n check: (value: unknown) => boolean,\n): boolean {\n return data[key] === undefined || data[key] === null || check(data[key]);\n}\n\nconst isString = (value: unknown): value is string => typeof value === 'string';\nconst isTone = (value: unknown): boolean => isString(value) && TONE.test(value);\n\nfunction checkStat(data: Record<string, unknown>): string[] {\n const wrong: string[] = [];\n if (!isNumber(data.value)) {\n wrong.push('value');\n }\n if (!optional(data, 'help', isString)) {\n wrong.push('help');\n }\n if (!optional(data, 'tone', isTone)) {\n wrong.push('tone');\n }\n if (!optional(data, 'url', isString)) {\n wrong.push('url');\n }\n return wrong;\n}\n\nfunction checkSeries(data: Record<string, unknown>): string[] {\n const wrong: string[] = [];\n if (!(WIDGET_SERIES_PERIODS as readonly unknown[]).includes(data.period)) {\n wrong.push('period');\n }\n if (!Array.isArray(data.points)) {\n wrong.push('points');\n } else {\n let previous = '';\n data.points.forEach((point: unknown, index) => {\n if (!isRecord(point)) {\n wrong.push(`points.${index}`);\n return;\n }\n if (!isString(point.date) || !DATE.test(point.date) || point.date <= previous) {\n wrong.push(`points.${index}.date`);\n } else {\n previous = point.date;\n }\n if (!isNumber(point.value)) {\n wrong.push(`points.${index}.value`);\n }\n });\n }\n if (!optional(data, 'total', isNumber)) {\n wrong.push('total');\n }\n return wrong;\n}\n\nfunction checkList(data: Record<string, unknown>): string[] {\n const wrong: string[] = [];\n if (!Array.isArray(data.items) || data.items.length > WIDGET_LIST_MAX_ITEMS) {\n wrong.push('items');\n } else {\n data.items.forEach((item: unknown, index) => {\n if (!isRecord(item)) {\n wrong.push(`items.${index}`);\n return;\n }\n if (!isString(item.title)) {\n wrong.push(`items.${index}.title`);\n }\n ['description', 'meta', 'url'].forEach((key) => {\n if (!optional(item, key, isString)) {\n wrong.push(`items.${index}.${key}`);\n }\n });\n const badge = item.badge;\n if (badge !== undefined && badge !== null) {\n if (!isRecord(badge) || !isString(badge.label)) {\n wrong.push(`items.${index}.badge`);\n } else if (!optional(badge, 'tone', isTone)) {\n wrong.push(`items.${index}.badge.tone`);\n }\n }\n });\n }\n if (!optional(data, 'empty', isString)) {\n wrong.push('empty');\n }\n if (!optional(data, 'url', isString)) {\n wrong.push('url');\n }\n return wrong;\n}\n\n/**\n * Checks the data of a widget against the shape of its type (decision 0064), without the schemas:\n * the paths of the wrong keys (`value`, `points.2.date`), empty when it is right. The data of a\n * widget of a plugin only has to be an object. The dates of a series must go up, one point per\n * period.\n */\nexport function checkWidgetData(type: string, data: unknown): string[] {\n if (!isRecord(data)) {\n return [''];\n }\n switch (type) {\n case 'stat':\n return checkStat(data);\n case 'series':\n return checkSeries(data);\n case 'list':\n return checkList(data);\n default:\n return [];\n }\n}\n"],"mappings":";;;;AAIA,IAAa,wBAAwB;CAAC;CAAQ;CAAU;AAAM;;AAK9D,IAAa,wBAAwB;CAAC;CAAO;CAAQ;AAAO;;AAK5D,IAAa,wBAAwB;;AAGrC,IAAa,wBAAwB,CAAC,QAAQ,MAAM;;AAGpD,IAAa,+BAA+B;CAAC;CAAQ;CAAW;AAAQ;AAExE,SAAgB,oBAAoB,MAAyC;CACzE,OAAQ,sBAA4C,SAAS,IAAI;AACrE;;;;;AAsBA,SAAgB,kBAAkB,QAA2C;CACzE,MAAM,WAAW,OAAO,UAAU;CAClC,IACI,CAAE,sBAA4C,SAAS,OAAO,IAAI,KAClE,OAAO,aAAa,YACpB,aAAa,IAEb,OAAO;CAEX,MAAM,SAAS,IAAI,gBAAgB;CACnC,MAAM,QAAQ,OAAO,UAAU;CAC/B,IAAI,SAAS,KAAK,GACd,OAAO,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,OAAO;CAEtB,IAAI,CAAC,SAAS,MAAM,OAAO,QAAQ,GAC/B,MAAM,KAAK,KAAK;CAEpB,OAAO;AACX;;;;;;;AAQA,SAAgB,gBAAgB,MAAc,MAAyB;CACnE,IAAI,CAAC,SAAS,IAAI,GACd,OAAO,CAAC,EAAE;CAEd,QAAQ,MAAR;EACI,KAAK,QACD,OAAO,UAAU,IAAI;EACzB,KAAK,UACD,OAAO,YAAY,IAAI;EAC3B,KAAK,QACD,OAAO,UAAU,IAAI;EACzB,SACI,OAAO,CAAC;CAChB;AACJ"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@panneau/protocol",
3
- "version": "5.0.0-alpha.3",
3
+ "version": "5.0.0-alpha.32",
4
4
  "description": "The Panneau protocol: definition schema, HTTP API spec and conformance kit",
5
5
  "type": "module",
6
6
  "license": "MIT",
@@ -42,14 +42,17 @@
42
42
  "schema"
43
43
  ],
44
44
  "scripts": {
45
- "build": "vite build && tsc -p tsconfig.build.json && node scripts/generate-schemas.js",
45
+ "build": "vite build && node ../../scripts/declarations.js && node scripts/generate-schemas.js",
46
46
  "typecheck": "tsc --noEmit",
47
- "api": "tsc -p tsconfig.build.json && node ../../scripts/api-report.js"
47
+ "api": "node ../../scripts/declarations.js && node ../../scripts/api-report.js"
48
48
  },
49
49
  "publishConfig": {
50
50
  "access": "public"
51
51
  },
52
52
  "dependencies": {
53
53
  "zod": "^4.6.7"
54
+ },
55
+ "devDependencies": {
56
+ "ajv": "^8.18.0"
54
57
  }
55
58
  }