@composable-os/sdk 0.0.0-stage → 0.1.0-beta.10
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.
- package/LICENSE +109 -0
- package/dist/allowance.d.ts +198 -0
- package/dist/allowance.js +374 -0
- package/dist/automation-check.d.ts +38 -0
- package/dist/automation-check.js +454 -0
- package/dist/automation.d.ts +767 -0
- package/dist/automation.js +303 -0
- package/dist/behaviour.d.ts +3 -0
- package/dist/behaviour.js +153 -0
- package/dist/capacity.d.ts +77 -0
- package/dist/capacity.js +127 -0
- package/dist/contract.d.ts +819 -0
- package/dist/contract.js +42 -0
- package/dist/document.d.ts +211 -0
- package/dist/document.js +726 -0
- package/dist/field-types.d.ts +11 -0
- package/dist/field-types.js +98 -0
- package/dist/formula/ast.d.ts +67 -0
- package/dist/formula/ast.js +22 -0
- package/dist/formula/compile.d.ts +37 -0
- package/dist/formula/compile.js +379 -0
- package/dist/formula/index.d.ts +3 -0
- package/dist/formula/index.js +4 -0
- package/dist/formula/parser.d.ts +13 -0
- package/dist/formula/parser.js +365 -0
- package/dist/index.d.ts +20 -0
- package/dist/index.js +24 -0
- package/dist/manifest.d.ts +1596 -0
- package/dist/manifest.js +1420 -0
- package/dist/media.d.ts +41 -0
- package/dist/media.js +90 -0
- package/dist/permissions.d.ts +17 -0
- package/dist/permissions.js +44 -0
- package/dist/recurring.d.ts +114 -0
- package/dist/recurring.js +173 -0
- package/dist/renamed.d.ts +16 -0
- package/dist/renamed.js +59 -0
- package/dist/rich-text-fixture.d.ts +121 -0
- package/dist/rich-text-fixture.js +67 -0
- package/dist/rich-text.d.ts +48 -0
- package/dist/rich-text.js +184 -0
- package/dist/runtime.d.ts +95 -0
- package/dist/runtime.js +312 -0
- package/dist/sellable.d.ts +253 -0
- package/dist/sellable.js +593 -0
- package/dist/slug.d.ts +11 -0
- package/dist/slug.js +23 -0
- package/dist/states.d.ts +238 -0
- package/dist/states.js +315 -0
- package/dist/testing.d.ts +151 -0
- package/dist/testing.js +679 -0
- package/dist/unique.d.ts +20 -0
- package/dist/unique.js +58 -0
- package/package.json +34 -4
- package/src/allowance.ts +522 -0
- package/src/automation-check.ts +534 -0
- package/src/automation.ts +743 -0
- package/src/behaviour.ts +215 -0
- package/src/capacity.ts +201 -0
- package/src/contract.ts +884 -0
- package/src/document.ts +935 -0
- package/src/field-types.ts +111 -0
- package/src/formula/ast.ts +72 -0
- package/src/formula/compile.ts +454 -0
- package/src/formula/index.ts +4 -0
- package/src/formula/parser.ts +417 -0
- package/src/index.ts +36 -0
- package/src/manifest.ts +1709 -0
- package/src/media.ts +108 -0
- package/src/permissions.ts +60 -0
- package/src/recurring.ts +321 -0
- package/src/renamed.ts +63 -0
- package/src/rich-text-fixture.ts +68 -0
- package/src/rich-text.ts +221 -0
- package/src/runtime.ts +410 -0
- package/src/sellable.ts +888 -0
- package/src/slug.ts +25 -0
- package/src/states.ts +484 -0
- package/src/testing.ts +847 -0
- package/src/unique.ts +81 -0
- package/README.md +0 -3
package/dist/media.d.ts
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
/** The types the media library takes. */
|
|
2
|
+
export declare const MEDIA_TYPES: readonly ["image/jpeg", "image/png", "image/gif", "image/webp", "image/avif"];
|
|
3
|
+
export type MediaType = (typeof MEDIA_TYPES)[number];
|
|
4
|
+
/** The most an upload may be, in bytes. */
|
|
5
|
+
export declare const MEDIA_MAX_BYTES: number;
|
|
6
|
+
/** The type the bytes are, or null when they are not a picture the library takes. */
|
|
7
|
+
export declare function sniffMediaType(bytes: Uint8Array): MediaType | null;
|
|
8
|
+
/** Why `bytes` cannot be uploaded, or null when they can. */
|
|
9
|
+
export declare function mediaProblem(bytes: Uint8Array): string | null;
|
|
10
|
+
/** The kinds of item the media library holds. */
|
|
11
|
+
export declare const MEDIA_KINDS: readonly ["image", "video", "file"];
|
|
12
|
+
export type MediaKind = (typeof MEDIA_KINDS)[number];
|
|
13
|
+
/** The types a `file` item may be. */
|
|
14
|
+
export declare const MEDIA_FILE_TYPES: readonly ["application/pdf"];
|
|
15
|
+
export type MediaFileType = (typeof MEDIA_FILE_TYPES)[number];
|
|
16
|
+
/** The `type` the library records for a video (its bytes are Stream's, never the host's). */
|
|
17
|
+
export declare const MEDIA_VIDEO_TYPE: "video/stream";
|
|
18
|
+
/** The most a downloadable file may be, in bytes. */
|
|
19
|
+
export declare const MEDIA_FILE_MAX_BYTES: number;
|
|
20
|
+
/** The most a video may be, in bytes: under Stream's 30 GB. */
|
|
21
|
+
export declare const MEDIA_VIDEO_MAX_BYTES: number;
|
|
22
|
+
/** The longest a video may be, in seconds (Stream refuses an upload longer than it was told). */
|
|
23
|
+
export declare const MEDIA_VIDEO_MAX_SECONDS: number;
|
|
24
|
+
/** The most a caption file may be (Stream's limit): WebVTT only. */
|
|
25
|
+
export declare const MEDIA_CAPTION_MAX_BYTES: number;
|
|
26
|
+
/** The type a downloadable file's bytes are, or null when the library does not take it. */
|
|
27
|
+
export declare function sniffFileType(bytes: Uint8Array): MediaFileType | null;
|
|
28
|
+
/** The kind and type an uploaded file is, from its bytes: a picture or a download. */
|
|
29
|
+
export declare function sniffUpload(bytes: Uint8Array): {
|
|
30
|
+
kind: "image";
|
|
31
|
+
type: MediaType;
|
|
32
|
+
} | {
|
|
33
|
+
kind: "file";
|
|
34
|
+
type: MediaFileType;
|
|
35
|
+
} | null;
|
|
36
|
+
/** The most an upload of this kind may be. */
|
|
37
|
+
export declare function maxBytesOf(kind: "image" | "file"): number;
|
|
38
|
+
/** Whether `bytes` are a WebVTT file (its header, after an optional byte order mark). */
|
|
39
|
+
export declare function isWebVtt(bytes: Uint8Array): boolean;
|
|
40
|
+
/** A caption's language: a BCP 47 tag as Stream takes it (`en`, `pt-BR`). */
|
|
41
|
+
export declare const CAPTION_LANGUAGE_RE: RegExp;
|
package/dist/media.js
ADDED
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
// What a picture of the media library may be (stage 28, ADR 0444), as data and pure functions so
|
|
2
|
+
// the host and `cos content import` check one rule: the type is the file's first bytes, never its
|
|
3
|
+
// name or a client's word; only raster images the host recognises (JPEG, PNG, GIF, WebP, AVIF);
|
|
4
|
+
// no SVG, which can carry script; at most 10 MB.
|
|
5
|
+
/** The types the media library takes. */
|
|
6
|
+
export const MEDIA_TYPES = [
|
|
7
|
+
"image/jpeg",
|
|
8
|
+
"image/png",
|
|
9
|
+
"image/gif",
|
|
10
|
+
"image/webp",
|
|
11
|
+
"image/avif",
|
|
12
|
+
];
|
|
13
|
+
/** The most an upload may be, in bytes. */
|
|
14
|
+
export const MEDIA_MAX_BYTES = 10 * 1024 * 1024;
|
|
15
|
+
const starts = (bytes, at, ...values) => values.every((v, i) => bytes[at + i] === v);
|
|
16
|
+
const ascii = (bytes, at, text) => [...text].every((c, i) => bytes[at + i] === c.charCodeAt(0));
|
|
17
|
+
/** The type the bytes are, or null when they are not a picture the library takes. */
|
|
18
|
+
export function sniffMediaType(bytes) {
|
|
19
|
+
if (starts(bytes, 0, 0xff, 0xd8, 0xff))
|
|
20
|
+
return "image/jpeg";
|
|
21
|
+
if (starts(bytes, 0, 0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a))
|
|
22
|
+
return "image/png";
|
|
23
|
+
if (ascii(bytes, 0, "GIF87a") || ascii(bytes, 0, "GIF89a"))
|
|
24
|
+
return "image/gif";
|
|
25
|
+
if (ascii(bytes, 0, "RIFF") && ascii(bytes, 8, "WEBP"))
|
|
26
|
+
return "image/webp";
|
|
27
|
+
if (ascii(bytes, 4, "ftyp") && (ascii(bytes, 8, "avif") || ascii(bytes, 8, "avis"))) {
|
|
28
|
+
return "image/avif";
|
|
29
|
+
}
|
|
30
|
+
return null;
|
|
31
|
+
}
|
|
32
|
+
/** Why `bytes` cannot be uploaded, or null when they can. */
|
|
33
|
+
export function mediaProblem(bytes) {
|
|
34
|
+
if (bytes.byteLength === 0)
|
|
35
|
+
return "The file is empty";
|
|
36
|
+
if (bytes.byteLength > MEDIA_MAX_BYTES) {
|
|
37
|
+
return `The file is ${bytes.byteLength} bytes; a picture is at most ${MEDIA_MAX_BYTES}`;
|
|
38
|
+
}
|
|
39
|
+
if (!sniffMediaType(bytes)) {
|
|
40
|
+
return "This is not a picture the library takes: JPEG, PNG, GIF, WebP or AVIF";
|
|
41
|
+
}
|
|
42
|
+
return null;
|
|
43
|
+
}
|
|
44
|
+
// --- Files and videos (stage 42, ADR 0475) ------------------------------------------------------
|
|
45
|
+
//
|
|
46
|
+
// A `file` item is a download (a PDF only, at first), kept in R2 like a picture and served by the
|
|
47
|
+
// same rule. Its type is read from its bytes too. A `video` item is held in Cloudflare Stream on
|
|
48
|
+
// the deployment's own account: the file goes from the staff browser straight to Stream, so the
|
|
49
|
+
// host only checks the size a person says it is, and Stream refuses what is not a video.
|
|
50
|
+
/** The kinds of item the media library holds. */
|
|
51
|
+
export const MEDIA_KINDS = ["image", "video", "file"];
|
|
52
|
+
/** The types a `file` item may be. */
|
|
53
|
+
export const MEDIA_FILE_TYPES = ["application/pdf"];
|
|
54
|
+
/** The `type` the library records for a video (its bytes are Stream's, never the host's). */
|
|
55
|
+
export const MEDIA_VIDEO_TYPE = "video/stream";
|
|
56
|
+
/** The most a downloadable file may be, in bytes. */
|
|
57
|
+
export const MEDIA_FILE_MAX_BYTES = 50 * 1024 * 1024;
|
|
58
|
+
/** The most a video may be, in bytes: under Stream's 30 GB. */
|
|
59
|
+
export const MEDIA_VIDEO_MAX_BYTES = 30 * 1000 * 1000 * 1000 - 1;
|
|
60
|
+
/** The longest a video may be, in seconds (Stream refuses an upload longer than it was told). */
|
|
61
|
+
export const MEDIA_VIDEO_MAX_SECONDS = 6 * 60 * 60;
|
|
62
|
+
/** The most a caption file may be (Stream's limit): WebVTT only. */
|
|
63
|
+
export const MEDIA_CAPTION_MAX_BYTES = 10 * 1024 * 1024;
|
|
64
|
+
/** The type a downloadable file's bytes are, or null when the library does not take it. */
|
|
65
|
+
export function sniffFileType(bytes) {
|
|
66
|
+
return ascii(bytes, 0, "%PDF-") ? "application/pdf" : null;
|
|
67
|
+
}
|
|
68
|
+
/** The kind and type an uploaded file is, from its bytes: a picture or a download. */
|
|
69
|
+
export function sniffUpload(bytes) {
|
|
70
|
+
const image = sniffMediaType(bytes);
|
|
71
|
+
if (image)
|
|
72
|
+
return { kind: "image", type: image };
|
|
73
|
+
const file = sniffFileType(bytes);
|
|
74
|
+
return file ? { kind: "file", type: file } : null;
|
|
75
|
+
}
|
|
76
|
+
/** The most an upload of this kind may be. */
|
|
77
|
+
export function maxBytesOf(kind) {
|
|
78
|
+
return kind === "file" ? MEDIA_FILE_MAX_BYTES : MEDIA_MAX_BYTES;
|
|
79
|
+
}
|
|
80
|
+
/** Whether `bytes` are a WebVTT file (its header, after an optional byte order mark). */
|
|
81
|
+
export function isWebVtt(bytes) {
|
|
82
|
+
const at = bytes[0] === 0xef && bytes[1] === 0xbb && bytes[2] === 0xbf ? 3 : 0;
|
|
83
|
+
if (!ascii(bytes, at, "WEBVTT"))
|
|
84
|
+
return false;
|
|
85
|
+
const next = bytes[at + 6];
|
|
86
|
+
// "WEBVTT" alone on its line, or followed by a space or a tab and a header.
|
|
87
|
+
return next === undefined || next === 0x0a || next === 0x0d || next === 0x20 || next === 0x09;
|
|
88
|
+
}
|
|
89
|
+
/** A caption's language: a BCP 47 tag as Stream takes it (`en`, `pt-BR`). */
|
|
90
|
+
export const CAPTION_LANGUAGE_RE = /^[a-z]{2,3}(?:-[A-Za-z0-9]{2,8}){0,2}$/;
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import type { PermissionsManifest } from "./manifest.ts";
|
|
2
|
+
export interface GrantRequest {
|
|
3
|
+
/** Operation name, e.g. `organisation.get`, `record.create`. */
|
|
4
|
+
operation: string;
|
|
5
|
+
/** The operation's risk class. */
|
|
6
|
+
risk: string;
|
|
7
|
+
/** The object the call reads or writes, when the operation is about one (else null). */
|
|
8
|
+
object: string | null;
|
|
9
|
+
}
|
|
10
|
+
export type GrantDecision = {
|
|
11
|
+
ok: true;
|
|
12
|
+
} | {
|
|
13
|
+
ok: false;
|
|
14
|
+
reason: string;
|
|
15
|
+
};
|
|
16
|
+
/** Decides whether extension `id` with `permissions` may make `request`. */
|
|
17
|
+
export declare function checkGrant(id: string, permissions: PermissionsManifest, request: GrantRequest): GrantDecision;
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import { FORBIDDEN_OPERATION_PREFIXES } from "./manifest.js";
|
|
2
|
+
/** Decides whether extension `id` with `permissions` may make `request`. */
|
|
3
|
+
export function checkGrant(id, permissions, request) {
|
|
4
|
+
const { operation, risk, object } = request;
|
|
5
|
+
if (FORBIDDEN_OPERATION_PREFIXES.some((prefix) => operation.startsWith(prefix))) {
|
|
6
|
+
return { ok: false, reason: `Extensions cannot call ${operation}` };
|
|
7
|
+
}
|
|
8
|
+
if (operation.startsWith(`x.${id}.`)) {
|
|
9
|
+
return { ok: false, reason: "An extension cannot call its own tools through codemode:ops" };
|
|
10
|
+
}
|
|
11
|
+
if (permissions.operations.includes(operation))
|
|
12
|
+
return { ok: true };
|
|
13
|
+
if (operation.startsWith("x.")) {
|
|
14
|
+
return { ok: false, reason: `${operation} is not in permissions.operations` };
|
|
15
|
+
}
|
|
16
|
+
if (!object) {
|
|
17
|
+
return {
|
|
18
|
+
ok: false,
|
|
19
|
+
reason: `${operation} is not in permissions.operations of extension ${id}`,
|
|
20
|
+
};
|
|
21
|
+
}
|
|
22
|
+
if (object.startsWith(`${id}.`))
|
|
23
|
+
return { ok: true };
|
|
24
|
+
if (risk === "read") {
|
|
25
|
+
if (permissions.read.includes(object) || permissions.write.includes(object))
|
|
26
|
+
return { ok: true };
|
|
27
|
+
return {
|
|
28
|
+
ok: false,
|
|
29
|
+
reason: `Extension ${id} may not read ${object} (add it to permissions.read)`,
|
|
30
|
+
};
|
|
31
|
+
}
|
|
32
|
+
if (risk === "write" || risk === "bulk-write") {
|
|
33
|
+
if (permissions.write.includes(object))
|
|
34
|
+
return { ok: true };
|
|
35
|
+
return {
|
|
36
|
+
ok: false,
|
|
37
|
+
reason: `Extension ${id} may not write ${object} (add it to permissions.write)`,
|
|
38
|
+
};
|
|
39
|
+
}
|
|
40
|
+
return {
|
|
41
|
+
ok: false,
|
|
42
|
+
reason: `${operation} is ${risk}; list it in permissions.operations to allow it`,
|
|
43
|
+
};
|
|
44
|
+
}
|
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
import type { ExtensionManifest, ObjectManifest } from "./manifest.ts";
|
|
3
|
+
/** The weekday values a rule's `weekdays` field must use (ISO order, Monday first). */
|
|
4
|
+
export declare const WEEKDAYS: readonly ["mon", "tue", "wed", "thu", "fri", "sat", "sun"];
|
|
5
|
+
export type Weekday = (typeof WEEKDAYS)[number];
|
|
6
|
+
/** How far ahead the host makes records when a series does not say (days). */
|
|
7
|
+
export declare const RECURRING_HORIZON_DAYS = 56;
|
|
8
|
+
/** The most days ahead a series may ask for. */
|
|
9
|
+
export declare const RECURRING_HORIZON_MAX = 366;
|
|
10
|
+
/**
|
|
11
|
+
* A weekly schedule that generates dated records (ADR 0500). The rule is kept in local
|
|
12
|
+
* wall-clock time in the series' zone: 06:00 is 06:00 on both sides of a clock change. A local
|
|
13
|
+
* time that does not exist on a change day (02:30 when clocks go forward) moves forward by the
|
|
14
|
+
* gap; one that happens twice is the first of the two.
|
|
15
|
+
*
|
|
16
|
+
* - `generates` is another of this extension's objects, and `link` its relation field (one
|
|
17
|
+
* record, read-only) back to this object: the host sets it.
|
|
18
|
+
* - `rule` names this object's fields: `weekdays` (a multiSelect whose option values are
|
|
19
|
+
* `mon`…`sun`), `time` (a text field, `HH:MM`), `length` (a number field, minutes), `from` and
|
|
20
|
+
* `until` (date fields; `until` empty for ongoing) and `zone` (a timezone field; empty is the
|
|
21
|
+
* organisation's zone).
|
|
22
|
+
* - `into` names the generated object's fields the host fills: `starts` (datetime), `ends`
|
|
23
|
+
* (datetime, starts plus the length), `zone` (timezone) and `changed` (a read-only boolean the
|
|
24
|
+
* host sets when someone other than the host edits the record).
|
|
25
|
+
* - `copy` maps generated fields to this object's fields copied onto each record (`{ coach:
|
|
26
|
+
* "coach" }`).
|
|
27
|
+
* - `cancel` is the generated object's state the host moves a record to when a closure covers it
|
|
28
|
+
* or the series ends while places are claimed on it. The generated object declares `states`
|
|
29
|
+
* with a `host` move into it.
|
|
30
|
+
* - `ended` (optional) is this object's select field and the values that mean the series has
|
|
31
|
+
* ended: nothing more is made, and its future records go.
|
|
32
|
+
* - `closures` (optional) is another of this extension's objects whose records are date ranges
|
|
33
|
+
* (`from`, `until`, both date fields, inclusive) in which nothing is made and records already
|
|
34
|
+
* made are cancelled.
|
|
35
|
+
* - `horizonDays` (default 56): how far ahead records are made.
|
|
36
|
+
* - `horizonAtLeast` (optional): another of this extension's objects and a number field of it in
|
|
37
|
+
* days (a class type's booking window): the horizon is never less than the largest value any
|
|
38
|
+
* live record of it holds, so a class can be booked as soon as its window opens.
|
|
39
|
+
*
|
|
40
|
+
* Made records are never made twice (the host keys each on the series and its local date). A
|
|
41
|
+
* record edited by staff (its time, copied fields or state) is never overwritten or made again;
|
|
42
|
+
* a change of the series applies to the future records nobody edited.
|
|
43
|
+
*/
|
|
44
|
+
export interface RecurringScheduleSpec {
|
|
45
|
+
generates: string;
|
|
46
|
+
link: string;
|
|
47
|
+
rule: {
|
|
48
|
+
weekdays: string;
|
|
49
|
+
time: string;
|
|
50
|
+
length?: string;
|
|
51
|
+
from: string;
|
|
52
|
+
until?: string;
|
|
53
|
+
zone?: string;
|
|
54
|
+
};
|
|
55
|
+
into: {
|
|
56
|
+
starts: string;
|
|
57
|
+
ends?: string;
|
|
58
|
+
zone?: string;
|
|
59
|
+
changed?: string;
|
|
60
|
+
};
|
|
61
|
+
copy?: Readonly<Record<string, string>>;
|
|
62
|
+
cancel: string;
|
|
63
|
+
ended?: {
|
|
64
|
+
field: string;
|
|
65
|
+
in: readonly string[];
|
|
66
|
+
};
|
|
67
|
+
closures?: {
|
|
68
|
+
object: string;
|
|
69
|
+
from: string;
|
|
70
|
+
until: string;
|
|
71
|
+
};
|
|
72
|
+
horizonDays?: number;
|
|
73
|
+
horizonAtLeast?: {
|
|
74
|
+
object: string;
|
|
75
|
+
field: string;
|
|
76
|
+
};
|
|
77
|
+
}
|
|
78
|
+
export declare const recurringManifestSchema: z.ZodObject<{
|
|
79
|
+
generates: z.ZodString;
|
|
80
|
+
link: z.ZodString;
|
|
81
|
+
rule: z.ZodObject<{
|
|
82
|
+
weekdays: z.ZodString;
|
|
83
|
+
time: z.ZodString;
|
|
84
|
+
length: z.ZodOptional<z.ZodString>;
|
|
85
|
+
from: z.ZodString;
|
|
86
|
+
until: z.ZodOptional<z.ZodString>;
|
|
87
|
+
zone: z.ZodOptional<z.ZodString>;
|
|
88
|
+
}, z.core.$strict>;
|
|
89
|
+
into: z.ZodObject<{
|
|
90
|
+
starts: z.ZodString;
|
|
91
|
+
ends: z.ZodOptional<z.ZodString>;
|
|
92
|
+
zone: z.ZodOptional<z.ZodString>;
|
|
93
|
+
changed: z.ZodOptional<z.ZodString>;
|
|
94
|
+
}, z.core.$strict>;
|
|
95
|
+
copy: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodString>>;
|
|
96
|
+
cancel: z.ZodString;
|
|
97
|
+
ended: z.ZodOptional<z.ZodObject<{
|
|
98
|
+
field: z.ZodString;
|
|
99
|
+
in: z.ZodArray<z.ZodString>;
|
|
100
|
+
}, z.core.$strict>>;
|
|
101
|
+
closures: z.ZodOptional<z.ZodObject<{
|
|
102
|
+
object: z.ZodString;
|
|
103
|
+
from: z.ZodString;
|
|
104
|
+
until: z.ZodString;
|
|
105
|
+
}, z.core.$strict>>;
|
|
106
|
+
horizonDays: z.ZodOptional<z.ZodNumber>;
|
|
107
|
+
horizonAtLeast: z.ZodOptional<z.ZodObject<{
|
|
108
|
+
object: z.ZodString;
|
|
109
|
+
field: z.ZodString;
|
|
110
|
+
}, z.core.$strict>>;
|
|
111
|
+
}, z.core.$strict>;
|
|
112
|
+
export type RecurringScheduleManifest = z.infer<typeof recurringManifestSchema>;
|
|
113
|
+
/** Reports problems with `objects.<name>.recurring` through `contract`. */
|
|
114
|
+
export declare function checkObjectRecurring(m: ExtensionManifest, name: string, object: ObjectManifest, contract: (path: string, message: string) => void): void;
|
|
@@ -0,0 +1,173 @@
|
|
|
1
|
+
// The `recurring` behaviour (stage 31, ADR 0500): a record (a series: one line of a timetable)
|
|
2
|
+
// declares a weekly rule in local wall-clock time, and the host makes the dated records it
|
|
3
|
+
// generates (its classes) ahead of time. Its declaration, its manifest schema and its contract
|
|
4
|
+
// checks live here; the host's side is packages/core/src/recurring/.
|
|
5
|
+
import { z } from "zod";
|
|
6
|
+
/** The weekday values a rule's `weekdays` field must use (ISO order, Monday first). */
|
|
7
|
+
export const WEEKDAYS = ["mon", "tue", "wed", "thu", "fri", "sat", "sun"];
|
|
8
|
+
/** How far ahead the host makes records when a series does not say (days). */
|
|
9
|
+
export const RECURRING_HORIZON_DAYS = 56;
|
|
10
|
+
/** The most days ahead a series may ask for. */
|
|
11
|
+
export const RECURRING_HORIZON_MAX = 366;
|
|
12
|
+
const fieldName = z.string().max(40);
|
|
13
|
+
export const recurringManifestSchema = z
|
|
14
|
+
.object({
|
|
15
|
+
generates: fieldName,
|
|
16
|
+
link: fieldName,
|
|
17
|
+
rule: z
|
|
18
|
+
.object({
|
|
19
|
+
weekdays: fieldName,
|
|
20
|
+
time: fieldName,
|
|
21
|
+
length: fieldName.optional(),
|
|
22
|
+
from: fieldName,
|
|
23
|
+
until: fieldName.optional(),
|
|
24
|
+
zone: fieldName.optional(),
|
|
25
|
+
})
|
|
26
|
+
.strict(),
|
|
27
|
+
into: z
|
|
28
|
+
.object({
|
|
29
|
+
starts: fieldName,
|
|
30
|
+
ends: fieldName.optional(),
|
|
31
|
+
zone: fieldName.optional(),
|
|
32
|
+
changed: fieldName.optional(),
|
|
33
|
+
})
|
|
34
|
+
.strict(),
|
|
35
|
+
copy: z
|
|
36
|
+
.record(fieldName, fieldName)
|
|
37
|
+
.refine((copy) => Object.keys(copy).length <= 20, "At most 20 copied fields")
|
|
38
|
+
.optional(),
|
|
39
|
+
cancel: z.string().min(1).max(100),
|
|
40
|
+
ended: z
|
|
41
|
+
.object({ field: fieldName, in: z.array(z.string().min(1).max(100)).min(1).max(20) })
|
|
42
|
+
.strict()
|
|
43
|
+
.optional(),
|
|
44
|
+
closures: z
|
|
45
|
+
.object({ object: fieldName, from: fieldName, until: fieldName })
|
|
46
|
+
.strict()
|
|
47
|
+
.optional(),
|
|
48
|
+
horizonDays: z.number().int().min(1).max(RECURRING_HORIZON_MAX).optional(),
|
|
49
|
+
horizonAtLeast: z.object({ object: fieldName, field: fieldName }).strict().optional(),
|
|
50
|
+
})
|
|
51
|
+
.strict();
|
|
52
|
+
/** Reports problems with `objects.<name>.recurring` through `contract`. */
|
|
53
|
+
export function checkObjectRecurring(m, name, object, contract) {
|
|
54
|
+
const recurring = object.recurring;
|
|
55
|
+
if (!recurring)
|
|
56
|
+
return;
|
|
57
|
+
const path = `objects.${name}.recurring`;
|
|
58
|
+
const own = (o, local) => Object.hasOwn(o.fields, local) ? o.fields[local] : undefined;
|
|
59
|
+
const expect = (o, oName, at, local, types, what) => {
|
|
60
|
+
if (local === undefined)
|
|
61
|
+
return undefined;
|
|
62
|
+
const field = own(o, local);
|
|
63
|
+
if (!field || !types.includes(field.type) || field.many) {
|
|
64
|
+
contract(at, `"${local}" must be ${what} of ${oName}`);
|
|
65
|
+
return undefined;
|
|
66
|
+
}
|
|
67
|
+
return field;
|
|
68
|
+
};
|
|
69
|
+
const rule = recurring.rule;
|
|
70
|
+
const weekdays = expect(object, name, `${path}.rule.weekdays`, rule.weekdays, ["multiSelect"], "a multiSelect field");
|
|
71
|
+
if (weekdays) {
|
|
72
|
+
const values = (weekdays.options ?? []).map((o) => o.value);
|
|
73
|
+
const wrong = values.filter((v) => !WEEKDAYS.includes(v));
|
|
74
|
+
if (wrong.length || !values.length) {
|
|
75
|
+
contract(`${path}.rule.weekdays`, `"${rule.weekdays}"'s options must be weekdays (${WEEKDAYS.join(", ")})`);
|
|
76
|
+
}
|
|
77
|
+
if (weekdays.editableOptions) {
|
|
78
|
+
contract(`${path}.rule.weekdays`, "The weekdays field cannot have editableOptions");
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
expect(object, name, `${path}.rule.time`, rule.time, ["text"], "a text field (HH:MM)");
|
|
82
|
+
expect(object, name, `${path}.rule.length`, rule.length, ["number"], "a number field (minutes)");
|
|
83
|
+
expect(object, name, `${path}.rule.from`, rule.from, ["date"], "a date field");
|
|
84
|
+
expect(object, name, `${path}.rule.until`, rule.until, ["date"], "a date field");
|
|
85
|
+
expect(object, name, `${path}.rule.zone`, rule.zone, ["timezone"], "a timezone field");
|
|
86
|
+
if (recurring.ended) {
|
|
87
|
+
const field = expect(object, name, `${path}.ended.field`, recurring.ended.field, ["select"], "a select field");
|
|
88
|
+
const values = new Set((field?.options ?? []).map((o) => o.value));
|
|
89
|
+
for (const value of recurring.ended.in) {
|
|
90
|
+
if (field && !values.has(value)) {
|
|
91
|
+
contract(`${path}.ended.in`, `"${value}" is not an option of ${recurring.ended.field}`);
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
const generatedName = recurring.generates;
|
|
96
|
+
const generated = Object.hasOwn(m.objects, generatedName) ? m.objects[generatedName] : undefined;
|
|
97
|
+
if (!generated || generatedName === name) {
|
|
98
|
+
contract(`${path}.generates`, `"${generatedName}" must be another of this extension's objects`);
|
|
99
|
+
return;
|
|
100
|
+
}
|
|
101
|
+
const link = own(generated, recurring.link);
|
|
102
|
+
if (link?.type !== "relation" || link.many || link.to !== `${m.id}.${name}`) {
|
|
103
|
+
contract(`${path}.link`, `"${recurring.link}" must be a relation field of ${generatedName} to ${m.id}.${name}`);
|
|
104
|
+
}
|
|
105
|
+
else if (!link.readOnly) {
|
|
106
|
+
contract(`${path}.link`, `"${recurring.link}" must be read-only: the host sets it`);
|
|
107
|
+
}
|
|
108
|
+
expect(generated, generatedName, `${path}.into.starts`, recurring.into.starts, ["datetime"], "a datetime field");
|
|
109
|
+
expect(generated, generatedName, `${path}.into.ends`, recurring.into.ends, ["datetime"], "a datetime field");
|
|
110
|
+
expect(generated, generatedName, `${path}.into.zone`, recurring.into.zone, ["timezone"], "a timezone field");
|
|
111
|
+
const changed = expect(generated, generatedName, `${path}.into.changed`, recurring.into.changed, ["boolean"], "a boolean field");
|
|
112
|
+
if (changed && !changed.readOnly) {
|
|
113
|
+
contract(`${path}.into.changed`, `"${recurring.into.changed}" must be read-only: the host sets it`);
|
|
114
|
+
}
|
|
115
|
+
const into = new Set(Object.values(recurring.into));
|
|
116
|
+
// The state is the host's to move and staff's to change; a series never copies one in.
|
|
117
|
+
if (generated.states)
|
|
118
|
+
into.add(generated.states.field);
|
|
119
|
+
for (const [target, source] of Object.entries(recurring.copy ?? {})) {
|
|
120
|
+
const to = own(generated, target);
|
|
121
|
+
const from = own(object, source);
|
|
122
|
+
if (!to || to.type === "formula" || into.has(target) || target === recurring.link) {
|
|
123
|
+
contract(`${path}.copy.${target}`, `"${target}" must be a field of ${generatedName} the host does not already fill`);
|
|
124
|
+
}
|
|
125
|
+
else if (!from ||
|
|
126
|
+
from.type !== to.type ||
|
|
127
|
+
Boolean(from.many) !== Boolean(to.many) ||
|
|
128
|
+
from.to !== to.to) {
|
|
129
|
+
contract(`${path}.copy.${target}`, `"${source}" must be a field of ${name} of the same type as ${generatedName}.${target}`);
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
const states = generated.states;
|
|
133
|
+
if (!states) {
|
|
134
|
+
contract(`${path}.cancel`, `${generatedName} must declare states, with a host move into "${recurring.cancel}"`);
|
|
135
|
+
}
|
|
136
|
+
else {
|
|
137
|
+
const field = own(generated, states.field);
|
|
138
|
+
if (!(field?.options ?? []).some((o) => o.value === recurring.cancel)) {
|
|
139
|
+
contract(`${path}.cancel`, `"${recurring.cancel}" is not a state of ${generatedName}`);
|
|
140
|
+
}
|
|
141
|
+
else if (!states.moves.some((move) => move.to === recurring.cancel && move.by.includes("host"))) {
|
|
142
|
+
contract(`${path}.cancel`, `${generatedName} needs a move into "${recurring.cancel}" that the host may make`);
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
if (recurring.horizonAtLeast) {
|
|
146
|
+
const h = recurring.horizonAtLeast;
|
|
147
|
+
// One of this extension's objects, or another extension's object with a number field this
|
|
148
|
+
// extension adds to it (a class type of Programs' carrying Bookings' window, ADR 0505).
|
|
149
|
+
const added = h.object.includes(".") ? m.fields?.[h.object] : undefined;
|
|
150
|
+
const target = added
|
|
151
|
+
? { fields: added }
|
|
152
|
+
: Object.hasOwn(m.objects, h.object)
|
|
153
|
+
? m.objects[h.object]
|
|
154
|
+
: undefined;
|
|
155
|
+
if (!target) {
|
|
156
|
+
contract(`${path}.horizonAtLeast.object`, `"${h.object}" must be one of this extension's objects, or an object this extension adds fields to`);
|
|
157
|
+
}
|
|
158
|
+
else {
|
|
159
|
+
expect(target, h.object, `${path}.horizonAtLeast.field`, h.field, ["number"], "a number field (days)");
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
if (recurring.closures) {
|
|
163
|
+
const c = recurring.closures;
|
|
164
|
+
const closure = Object.hasOwn(m.objects, c.object) ? m.objects[c.object] : undefined;
|
|
165
|
+
if (!closure || c.object === name || c.object === generatedName) {
|
|
166
|
+
contract(`${path}.closures.object`, `"${c.object}" must be another of this extension's objects`);
|
|
167
|
+
}
|
|
168
|
+
else {
|
|
169
|
+
expect(closure, c.object, `${path}.closures.from`, c.from, ["date"], "a date field");
|
|
170
|
+
expect(closure, c.object, `${path}.closures.until`, c.until, ["date"], "a date field");
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
/** The host module extension code calls operations through, and what it was called. */
|
|
2
|
+
export declare const OPS_MODULE = "ops";
|
|
3
|
+
export declare const OLD_OPS_MODULE = "crm";
|
|
4
|
+
/** The module specifier Code Mode scripts import, and what it was. */
|
|
5
|
+
export declare const OPS_SPECIFIER = "codemode:ops";
|
|
6
|
+
export declare const OLD_OPS_SPECIFIER = "codemode:crm";
|
|
7
|
+
/** The prefix of every read view, and what it was. */
|
|
8
|
+
export declare const VIEW_PREFIX = "rec_";
|
|
9
|
+
export declare const OLD_VIEW_PREFIX = "crm_";
|
|
10
|
+
/**
|
|
11
|
+
* What a view named under the old contract is called now: `rec_person` for the old people view,
|
|
12
|
+
* `rec_x_deals_deal` for an extension object's. Null when `name` is not an old view name.
|
|
13
|
+
*/
|
|
14
|
+
export declare function renamedView(name: string): string | null;
|
|
15
|
+
/** Why a use of an old contract name is refused, naming the new form. Null for any other name. */
|
|
16
|
+
export declare function renamedMessage(name: string): string | null;
|
package/dist/renamed.js
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
// The names the extension contract had before stage 20 (ADR 0400), when it still said `crm`.
|
|
2
|
+
//
|
|
3
|
+
// None of them is accepted: no release was installed under them, so there is nothing to keep
|
|
4
|
+
// working. They are listed so that code written against the old contract is refused with a
|
|
5
|
+
// message that names the new form, by the extension build, Code Mode, the manifest check and
|
|
6
|
+
// query.sql. This file is the only place the old names are written.
|
|
7
|
+
const OLD_PREFIX = "crm";
|
|
8
|
+
/** The host module extension code calls operations through, and what it was called. */
|
|
9
|
+
export const OPS_MODULE = "ops";
|
|
10
|
+
export const OLD_OPS_MODULE = OLD_PREFIX;
|
|
11
|
+
/** The module specifier Code Mode scripts import, and what it was. */
|
|
12
|
+
export const OPS_SPECIFIER = `codemode:${OPS_MODULE}`;
|
|
13
|
+
export const OLD_OPS_SPECIFIER = `codemode:${OLD_OPS_MODULE}`;
|
|
14
|
+
/** The prefix of every read view, and what it was. */
|
|
15
|
+
export const VIEW_PREFIX = "rec_";
|
|
16
|
+
export const OLD_VIEW_PREFIX = `${OLD_PREFIX}_`;
|
|
17
|
+
/** The core views, which were named in the plural: old suffix → new suffix. */
|
|
18
|
+
const RENAMED_CORE_VIEWS = {
|
|
19
|
+
people: "person",
|
|
20
|
+
organisations: "organisation",
|
|
21
|
+
affiliations: "affiliation",
|
|
22
|
+
activities: "activity",
|
|
23
|
+
activity_links: "activity_link",
|
|
24
|
+
tasks: "task",
|
|
25
|
+
task_links: "task_link",
|
|
26
|
+
record_tags: "record_tag",
|
|
27
|
+
users: "user",
|
|
28
|
+
stage_history: "stage_history",
|
|
29
|
+
};
|
|
30
|
+
/**
|
|
31
|
+
* What a view named under the old contract is called now: `rec_person` for the old people view,
|
|
32
|
+
* `rec_x_deals_deal` for an extension object's. Null when `name` is not an old view name.
|
|
33
|
+
*/
|
|
34
|
+
export function renamedView(name) {
|
|
35
|
+
const lower = name.toLowerCase();
|
|
36
|
+
if (!lower.startsWith(OLD_VIEW_PREFIX))
|
|
37
|
+
return null;
|
|
38
|
+
const suffix = lower.slice(OLD_VIEW_PREFIX.length);
|
|
39
|
+
if (suffix.startsWith("x_"))
|
|
40
|
+
return `${VIEW_PREFIX}${suffix}`;
|
|
41
|
+
const core = RENAMED_CORE_VIEWS[suffix];
|
|
42
|
+
return core ? `${VIEW_PREFIX}${core}` : null;
|
|
43
|
+
}
|
|
44
|
+
/** Why a use of an old contract name is refused, naming the new form. Null for any other name. */
|
|
45
|
+
export function renamedMessage(name) {
|
|
46
|
+
if (name === OLD_OPS_SPECIFIER) {
|
|
47
|
+
return `"${OLD_OPS_SPECIFIER}" was renamed: import { ${OPS_MODULE} } from "${OPS_SPECIFIER}" and call ${OPS_MODULE}.call(…)`;
|
|
48
|
+
}
|
|
49
|
+
if (name === OLD_OPS_MODULE) {
|
|
50
|
+
return `The "${OLD_OPS_MODULE}" module was renamed "${OPS_MODULE}": declare "${OPS_MODULE}" in permissions.modules and call ctx.${OPS_MODULE}`;
|
|
51
|
+
}
|
|
52
|
+
const view = renamedView(name);
|
|
53
|
+
if (view)
|
|
54
|
+
return `${name} was renamed ${view}; query the ${VIEW_PREFIX}* views`;
|
|
55
|
+
if (name.toLowerCase().startsWith(OLD_VIEW_PREFIX)) {
|
|
56
|
+
return `${name} is not a view: the ${OLD_VIEW_PREFIX}* views were renamed ${VIEW_PREFIX}*`;
|
|
57
|
+
}
|
|
58
|
+
return null;
|
|
59
|
+
}
|