@quad-education/sdk 0.7.0 → 0.9.0
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/README.md +5 -2
- package/dist/contract-tests/apps.d.ts +617 -0
- package/dist/contract-tests/apps.js +215 -1
- package/dist/contract-tests/harness.d.ts +1 -1
- package/dist/contract-tests/harness.js +7 -0
- package/dist/contract-tests/lists-and-files.d.ts +19 -0
- package/dist/contract-tests/lists-and-files.js +279 -0
- package/dist/contract-tests/live.d.ts +3 -0
- package/dist/contract-tests/live.js +156 -0
- package/dist/contract-tests/notifications.d.ts +3 -0
- package/dist/contract-tests/notifications.js +218 -0
- package/dist/contract-tests/search-and-totals.d.ts +13 -0
- package/dist/contract-tests/search-and-totals.js +226 -0
- package/dist/contract-tests/suite.d.ts +7 -0
- package/dist/contract-tests/suite.js +180 -2
- package/dist/contract-tests/writes.d.ts +3 -0
- package/dist/contract-tests/writes.js +265 -0
- package/dist/src/base64.d.ts +3 -0
- package/dist/src/base64.js +52 -0
- package/dist/src/client/connect.d.ts +1 -0
- package/dist/src/client/connect.js +1 -0
- package/dist/src/client/create-client.js +39 -5
- package/dist/src/client/save-file.d.ts +16 -0
- package/dist/src/client/save-file.js +28 -0
- package/dist/src/client/watch.d.ts +20 -0
- package/dist/src/client/watch.js +175 -0
- package/dist/src/contract/errors.d.ts +1 -1
- package/dist/src/contract/types.d.ts +220 -10
- package/dist/src/contract/types.js +4 -1
- package/dist/src/engine/engine.d.ts +91 -2
- package/dist/src/engine/engine.js +604 -67
- package/dist/src/engine/files.d.ts +25 -0
- package/dist/src/engine/files.js +88 -0
- package/dist/src/engine/index.d.ts +1 -1
- package/dist/src/engine/memory-store.d.ts +33 -3
- package/dist/src/engine/memory-store.js +116 -8
- package/dist/src/engine/notify-content.d.ts +9 -0
- package/dist/src/engine/notify-content.js +88 -0
- package/dist/src/engine/query.d.ts +14 -2
- package/dist/src/engine/query.js +45 -13
- package/dist/src/engine/search.d.ts +8 -0
- package/dist/src/engine/search.js +32 -0
- package/dist/src/engine/state.d.ts +5 -0
- package/dist/src/engine/storage.d.ts +46 -1
- package/dist/src/engine/storage.js +46 -3
- package/dist/src/engine/store.d.ts +39 -2
- package/dist/src/engine/summary.d.ts +25 -0
- package/dist/src/engine/summary.js +107 -0
- package/dist/src/engine/validate-input.d.ts +7 -1
- package/dist/src/engine/validate-input.js +28 -1
- package/dist/src/engine/working-copy.d.ts +30 -0
- package/dist/src/engine/working-copy.js +91 -0
- package/dist/src/platform-types.d.ts +11 -1
- package/dist/src/protocol.d.ts +1 -1
- package/dist/src/protocol.js +9 -0
- package/dist/src/testing/index.d.ts +9 -0
- package/dist/src/testing/index.js +16 -1
- package/package.json +4 -4
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
/** A file an app has made, to hand to the person (D156). */
|
|
2
|
+
export type SavedFile = {
|
|
3
|
+
/** The name it is saved under, e.g. `notes.csv`. Any folders in it are dropped. */
|
|
4
|
+
readonly name: string;
|
|
5
|
+
/** The media type, e.g. `text/csv` or `application/pdf`. */
|
|
6
|
+
readonly type: string;
|
|
7
|
+
/** The contents: text, or bytes such as a PDF library returns. */
|
|
8
|
+
readonly data: string | Uint8Array | ArrayBuffer | Blob;
|
|
9
|
+
};
|
|
10
|
+
/**
|
|
11
|
+
* Gives the person a file the app has made: a spreadsheet, a PDF, a list. The browser saves it as a download;
|
|
12
|
+
* nothing is sent anywhere. To keep a file with the app's records instead, use `sdk.files.upload`.
|
|
13
|
+
*
|
|
14
|
+
* saveFile({ name: 'class-list.csv', type: 'text/csv', data: csv })
|
|
15
|
+
*/
|
|
16
|
+
export declare function saveFile(file: SavedFile): void;
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Gives the person a file the app has made: a spreadsheet, a PDF, a list. The browser saves it as a download;
|
|
3
|
+
* nothing is sent anywhere. To keep a file with the app's records instead, use `sdk.files.upload`.
|
|
4
|
+
*
|
|
5
|
+
* saveFile({ name: 'class-list.csv', type: 'text/csv', data: csv })
|
|
6
|
+
*/
|
|
7
|
+
export function saveFile(file) {
|
|
8
|
+
const name = file.name.split(/[\\/]/).pop()?.trim();
|
|
9
|
+
if (!name)
|
|
10
|
+
throw new Error('saveFile: give the file a name');
|
|
11
|
+
if (typeof file.type !== 'string' || file.type === '')
|
|
12
|
+
throw new Error('saveFile: give the file a type, such as text/csv');
|
|
13
|
+
// A text file opens correctly in a spreadsheet only when it says it is UTF-8.
|
|
14
|
+
const type = /^text\//.test(file.type) && !/charset=/i.test(file.type)
|
|
15
|
+
? `${file.type};charset=utf-8`
|
|
16
|
+
: file.type;
|
|
17
|
+
const blob = file.data instanceof Blob ? file.data : new Blob([file.data], { type });
|
|
18
|
+
const url = URL.createObjectURL(blob);
|
|
19
|
+
const link = document.createElement('a');
|
|
20
|
+
link.href = url;
|
|
21
|
+
link.download = name;
|
|
22
|
+
link.style.display = 'none';
|
|
23
|
+
document.body.append(link);
|
|
24
|
+
link.click();
|
|
25
|
+
link.remove();
|
|
26
|
+
// The download has started by now; the address is no longer needed.
|
|
27
|
+
setTimeout(() => URL.revokeObjectURL(url), 0);
|
|
28
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import type { DbWatch } from '../contract/types.ts';
|
|
2
|
+
/** How often a watch asks when the app doesn't say (D157). */
|
|
3
|
+
export declare const WATCH_INTERVAL_MS = 5000;
|
|
4
|
+
/** The fastest a watch may ask. */
|
|
5
|
+
export declare const MIN_WATCH_INTERVAL_MS = 2000;
|
|
6
|
+
type Options = {
|
|
7
|
+
readonly intervalMs?: number;
|
|
8
|
+
readonly onError?: (error: Error) => void;
|
|
9
|
+
};
|
|
10
|
+
/**
|
|
11
|
+
* The watches of one SDK client (D157). They share one timer and one request: each round asks Quad for the
|
|
12
|
+
* stamps of every watched entity, and runs the callbacks whose entities changed. The timer stops while the page
|
|
13
|
+
* is hidden, and slows after errors until a round works again.
|
|
14
|
+
*/
|
|
15
|
+
export declare function createWatcher(changes: (entities: readonly string[]) => Promise<Record<string, string>>): {
|
|
16
|
+
watch(entities: unknown, callback: unknown, options?: Options): DbWatch;
|
|
17
|
+
/** The app wrote through this client: ask straight away, so the person sees their own change at once. */
|
|
18
|
+
wrote(): void;
|
|
19
|
+
};
|
|
20
|
+
export {};
|
|
@@ -0,0 +1,175 @@
|
|
|
1
|
+
import { QuadError } from "../contract/errors.js";
|
|
2
|
+
/** How often a watch asks when the app doesn't say (D157). */
|
|
3
|
+
export const WATCH_INTERVAL_MS = 5000;
|
|
4
|
+
/** The fastest a watch may ask. */
|
|
5
|
+
export const MIN_WATCH_INTERVAL_MS = 2000;
|
|
6
|
+
/** The slowest it asks after errors. */
|
|
7
|
+
const MAX_BACKOFF_MS = 60_000;
|
|
8
|
+
/** Timers as every runtime has them. The SDK's core is built with neither the browser's types nor Node's. */
|
|
9
|
+
const timers = globalThis;
|
|
10
|
+
const asError = (e) => (e instanceof Error ? e : new Error(String(e)));
|
|
11
|
+
/** An error nobody asked to be told about is the app's to see, not ours to swallow. */
|
|
12
|
+
const surface = (e) => void timers.setTimeout(() => {
|
|
13
|
+
throw e;
|
|
14
|
+
}, 0);
|
|
15
|
+
/** Quad's answer is about the watch itself, so asking again will not help. */
|
|
16
|
+
const final = (e) => e instanceof QuadError && (e.code === 'VALIDATION' || e.code === 'NOT_PERMITTED');
|
|
17
|
+
/**
|
|
18
|
+
* The watches of one SDK client (D157). They share one timer and one request: each round asks Quad for the
|
|
19
|
+
* stamps of every watched entity, and runs the callbacks whose entities changed. The timer stops while the page
|
|
20
|
+
* is hidden, and slows after errors until a round works again.
|
|
21
|
+
*/
|
|
22
|
+
export function createWatcher(changes) {
|
|
23
|
+
const watches = new Set();
|
|
24
|
+
let timer;
|
|
25
|
+
let soon;
|
|
26
|
+
let rounds = Promise.resolve();
|
|
27
|
+
let failures = 0;
|
|
28
|
+
let listening = false;
|
|
29
|
+
const page = () => globalThis.document;
|
|
30
|
+
const hidden = () => page()?.visibilityState === 'hidden';
|
|
31
|
+
const stop = () => {
|
|
32
|
+
if (timer !== undefined)
|
|
33
|
+
timers.clearTimeout(timer);
|
|
34
|
+
timer = undefined;
|
|
35
|
+
};
|
|
36
|
+
const schedule = () => {
|
|
37
|
+
stop();
|
|
38
|
+
if (watches.size === 0 || hidden())
|
|
39
|
+
return;
|
|
40
|
+
const every = Math.min(...[...watches].map((w) => w.intervalMs));
|
|
41
|
+
timer = timers.setTimeout(() => {
|
|
42
|
+
timer = undefined;
|
|
43
|
+
void round().catch(() => undefined);
|
|
44
|
+
}, Math.min(every * 2 ** failures, Math.max(every, MAX_BACKOFF_MS)));
|
|
45
|
+
timer.unref?.();
|
|
46
|
+
};
|
|
47
|
+
const onVisibility = () => {
|
|
48
|
+
if (hidden())
|
|
49
|
+
stop();
|
|
50
|
+
else
|
|
51
|
+
void round().catch(() => undefined);
|
|
52
|
+
};
|
|
53
|
+
const listen = () => {
|
|
54
|
+
const doc = page();
|
|
55
|
+
if (watches.size > 0 && !listening && doc) {
|
|
56
|
+
doc.addEventListener('visibilitychange', onVisibility);
|
|
57
|
+
listening = true;
|
|
58
|
+
}
|
|
59
|
+
else if (watches.size === 0 && listening) {
|
|
60
|
+
doc?.removeEventListener('visibilitychange', onVisibility);
|
|
61
|
+
listening = false;
|
|
62
|
+
}
|
|
63
|
+
};
|
|
64
|
+
const report = (w, e) => (w.onError ? w.onError(e) : surface(e));
|
|
65
|
+
const apply = async (group, stamps) => {
|
|
66
|
+
for (const w of group) {
|
|
67
|
+
if (!watches.has(w))
|
|
68
|
+
continue;
|
|
69
|
+
const next = Object.fromEntries(w.entities.map((e) => [e, stamps[e] ?? '']));
|
|
70
|
+
const before = w.stamps;
|
|
71
|
+
if (before && w.entities.every((e) => before[e] === next[e]))
|
|
72
|
+
continue;
|
|
73
|
+
w.stamps = next;
|
|
74
|
+
try {
|
|
75
|
+
await w.callback();
|
|
76
|
+
}
|
|
77
|
+
catch (e) {
|
|
78
|
+
report(w, asError(e));
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
};
|
|
82
|
+
const ask = async () => {
|
|
83
|
+
const group = [...watches];
|
|
84
|
+
if (group.length === 0)
|
|
85
|
+
return;
|
|
86
|
+
try {
|
|
87
|
+
await apply(group, await changes([...new Set(group.flatMap((w) => w.entities))]));
|
|
88
|
+
}
|
|
89
|
+
catch (e) {
|
|
90
|
+
if (!final(e))
|
|
91
|
+
throw e;
|
|
92
|
+
// One watch names something Quad refuses: ask for each alone, so the others carry on.
|
|
93
|
+
for (const w of group) {
|
|
94
|
+
try {
|
|
95
|
+
await apply([w], await changes(w.entities));
|
|
96
|
+
}
|
|
97
|
+
catch (own) {
|
|
98
|
+
if (!final(own))
|
|
99
|
+
throw own;
|
|
100
|
+
w.refused = asError(own);
|
|
101
|
+
watches.delete(w);
|
|
102
|
+
report(w, w.refused);
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
listen();
|
|
106
|
+
}
|
|
107
|
+
};
|
|
108
|
+
/** One round, after any already under way. Rejects when Quad could not be asked. */
|
|
109
|
+
function round() {
|
|
110
|
+
const run = rounds.then(async () => {
|
|
111
|
+
try {
|
|
112
|
+
await ask();
|
|
113
|
+
failures = 0;
|
|
114
|
+
}
|
|
115
|
+
catch (e) {
|
|
116
|
+
failures += 1;
|
|
117
|
+
for (const w of watches)
|
|
118
|
+
w.onError?.(asError(e));
|
|
119
|
+
throw e;
|
|
120
|
+
}
|
|
121
|
+
finally {
|
|
122
|
+
schedule();
|
|
123
|
+
}
|
|
124
|
+
});
|
|
125
|
+
rounds = run.catch(() => undefined);
|
|
126
|
+
return run;
|
|
127
|
+
}
|
|
128
|
+
return {
|
|
129
|
+
watch(entities, callback, options = {}) {
|
|
130
|
+
if (!Array.isArray(entities) ||
|
|
131
|
+
entities.length === 0 ||
|
|
132
|
+
!entities.every((e) => typeof e === 'string' && e.length > 0))
|
|
133
|
+
throw new QuadError('VALIDATION', "db.watch takes a list of the app's entities to watch");
|
|
134
|
+
if (typeof callback !== 'function')
|
|
135
|
+
throw new QuadError('VALIDATION', 'db.watch takes a function to call when they change');
|
|
136
|
+
const asked = options.intervalMs;
|
|
137
|
+
if (asked !== undefined && (typeof asked !== 'number' || !Number.isFinite(asked)))
|
|
138
|
+
throw new QuadError('VALIDATION', 'intervalMs must be a number of milliseconds');
|
|
139
|
+
const w = {
|
|
140
|
+
entities: [...new Set(entities)],
|
|
141
|
+
callback: callback,
|
|
142
|
+
intervalMs: Math.max(MIN_WATCH_INTERVAL_MS, asked ?? WATCH_INTERVAL_MS),
|
|
143
|
+
onError: options.onError,
|
|
144
|
+
stamps: undefined,
|
|
145
|
+
refused: undefined,
|
|
146
|
+
};
|
|
147
|
+
watches.add(w);
|
|
148
|
+
listen();
|
|
149
|
+
void round().catch(() => undefined);
|
|
150
|
+
return Object.freeze({
|
|
151
|
+
check: async () => {
|
|
152
|
+
await round();
|
|
153
|
+
if (w.refused)
|
|
154
|
+
throw w.refused;
|
|
155
|
+
},
|
|
156
|
+
stop: () => {
|
|
157
|
+
watches.delete(w);
|
|
158
|
+
listen();
|
|
159
|
+
if (watches.size === 0)
|
|
160
|
+
stop();
|
|
161
|
+
},
|
|
162
|
+
});
|
|
163
|
+
},
|
|
164
|
+
/** The app wrote through this client: ask straight away, so the person sees their own change at once. */
|
|
165
|
+
wrote() {
|
|
166
|
+
if (watches.size === 0 || soon !== undefined)
|
|
167
|
+
return;
|
|
168
|
+
soon = timers.setTimeout(() => {
|
|
169
|
+
soon = undefined;
|
|
170
|
+
void round().catch(() => undefined);
|
|
171
|
+
}, 0);
|
|
172
|
+
soon.unref?.();
|
|
173
|
+
},
|
|
174
|
+
};
|
|
175
|
+
}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/** Error codes the SDK contract can return. Both implementations use the same codes. */
|
|
2
|
-
export type QuadErrorCode = 'SCOPE_NOT_GRANTED' | 'UNDECLARED_TYPE' | 'NOT_FOUND' | 'VALIDATION' | 'FORBIDDEN' | 'NOT_PERMITTED' | 'NOT_ASSIGNEE' | 'TRANSPORT' | 'INTERNAL';
|
|
2
|
+
export type QuadErrorCode = 'SCOPE_NOT_GRANTED' | 'UNDECLARED_TYPE' | 'NOT_FOUND' | 'VALIDATION' | 'FORBIDDEN' | 'NOT_PERMITTED' | 'CONFLICT' | 'NOT_ASSIGNEE' | 'TRANSPORT' | 'INTERNAL';
|
|
3
3
|
export declare class QuadError extends Error {
|
|
4
4
|
readonly code: QuadErrorCode;
|
|
5
5
|
readonly details: unknown;
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import type { CanonicalRecord, ContactPurpose, Entities, EntityName, RecordWithTiers, Role, TiersFor } from '@quad-education/canonical';
|
|
2
2
|
import type { AppEntityDef, AppInput, AppSchema, FieldValue, SystemFields } from '@quad-education/schema';
|
|
3
3
|
import type { Recipient } from './recipients.ts';
|
|
4
|
-
export declare const SDK_VERSION = "0.
|
|
4
|
+
export declare const SDK_VERSION = "0.9.0";
|
|
5
5
|
/**
|
|
6
6
|
* Describes the current app to the type system. `quad gen` augments it from
|
|
7
7
|
* app.manifest.json and schema/ (D23); without that, nothing is readable.
|
|
@@ -61,6 +61,11 @@ export type ListOptions<R> = {
|
|
|
61
61
|
/** 1–200, default 50. */
|
|
62
62
|
readonly limit?: number;
|
|
63
63
|
readonly cursor?: string;
|
|
64
|
+
/**
|
|
65
|
+
* Words to look for (D153): every word must appear, ignoring case and accents. School data is searched in the
|
|
66
|
+
* text the app is granted; an app's own records in the fields its entity names in `search`.
|
|
67
|
+
*/
|
|
68
|
+
readonly search?: string;
|
|
64
69
|
};
|
|
65
70
|
export type Page<R> = {
|
|
66
71
|
readonly items: readonly R[];
|
|
@@ -81,6 +86,12 @@ export type Me = {
|
|
|
81
86
|
readonly kind: 'user';
|
|
82
87
|
readonly role: Role;
|
|
83
88
|
readonly appAdmin: boolean;
|
|
89
|
+
/**
|
|
90
|
+
* The app's own roles this person holds (D143): names from the manifest's `roles`, as the manifest spells them,
|
|
91
|
+
* assigned by the school. Empty for most people. A role named like the person's school role (an app role
|
|
92
|
+
* `Staff` for a staff member) is held by everyone with that school role.
|
|
93
|
+
*/
|
|
94
|
+
readonly appRoles: readonly string[];
|
|
84
95
|
readonly person: PersonSummary;
|
|
85
96
|
} | {
|
|
86
97
|
readonly kind: 'system';
|
|
@@ -160,16 +171,30 @@ export type RecordFor<A extends AppConfig, E extends AppEntityDef> = Simplify<Sy
|
|
|
160
171
|
type RefFields<E extends AppEntityDef> = {
|
|
161
172
|
[K in keyof E['fields']]: E['fields'][K] extends {
|
|
162
173
|
kind: 'ref';
|
|
174
|
+
} | {
|
|
175
|
+
kind: 'list';
|
|
176
|
+
of: {
|
|
177
|
+
kind: 'ref';
|
|
178
|
+
};
|
|
163
179
|
} ? K : never;
|
|
164
180
|
}[keyof E['fields']] & string;
|
|
165
181
|
type RefTarget<E extends AppEntityDef, K extends keyof E['fields']> = E['fields'][K] extends {
|
|
166
182
|
to: infer T;
|
|
183
|
+
} ? T : E['fields'][K] extends {
|
|
184
|
+
of: {
|
|
185
|
+
to: infer T;
|
|
186
|
+
};
|
|
167
187
|
} ? T : never;
|
|
168
188
|
type Resolved<A extends AppConfig, T> = T extends `school.${infer N extends EntityName}` ? SchoolRecord<A, N> : T extends keyof A['schema'] ? RecordFor<A, A['schema'][T]> : never;
|
|
169
189
|
export type WithIncluded<A extends AppConfig, E extends AppEntityDef, I extends string> = RecordFor<A, E> & {
|
|
170
|
-
/**
|
|
190
|
+
/**
|
|
191
|
+
* Included refs; null when the target doesn't exist or isn't visible to the current user. A list of refs
|
|
192
|
+
* gives a list, in the same order (D147).
|
|
193
|
+
*/
|
|
171
194
|
readonly $included: {
|
|
172
|
-
readonly [K in I]: K extends keyof E['fields'] ?
|
|
195
|
+
readonly [K in I]: K extends keyof E['fields'] ? E['fields'][K] extends {
|
|
196
|
+
kind: 'list';
|
|
197
|
+
} ? readonly (Resolved<A, RefTarget<E, K>> | null)[] : Resolved<A, RefTarget<E, K>> | null : never;
|
|
173
198
|
};
|
|
174
199
|
};
|
|
175
200
|
/**
|
|
@@ -183,9 +208,13 @@ type EqualityCondition<V> = V | {
|
|
|
183
208
|
} | {
|
|
184
209
|
readonly ne: V;
|
|
185
210
|
};
|
|
186
|
-
/**
|
|
211
|
+
/** What a list field is asked (D147): whether it has a value. */
|
|
212
|
+
export type HasCondition<V> = {
|
|
213
|
+
readonly has: V;
|
|
214
|
+
};
|
|
215
|
+
/** Like `Where`, but range conditions only on indexed fields (D37), and a list only with `has` (D147). */
|
|
187
216
|
export type DbWhere<R, IX extends string> = {
|
|
188
|
-
readonly [K in keyof R]?: R[K] extends Scalar | undefined ? K extends IX ? Condition<Exclude<R[K], undefined>> : EqualityCondition<Exclude<R[K], undefined>> : never;
|
|
217
|
+
readonly [K in keyof R]?: R[K] extends Scalar | undefined ? K extends IX ? Condition<Exclude<R[K], undefined>> : EqualityCondition<Exclude<R[K], undefined>> : Exclude<R[K], undefined> extends readonly (infer V)[] ? HasCondition<V> : never;
|
|
189
218
|
};
|
|
190
219
|
export type DbOrderBy<R, IX extends string> = {
|
|
191
220
|
readonly field: keyof R & IX & string;
|
|
@@ -196,24 +225,154 @@ export type DbListOptions<A extends AppConfig, E extends AppEntityDef, I extends
|
|
|
196
225
|
readonly orderBy?: DbOrderBy<RecordFor<A, E>, IndexedField<E>> | readonly DbOrderBy<RecordFor<A, E>, IndexedField<E>>[];
|
|
197
226
|
readonly include?: readonly I[];
|
|
198
227
|
};
|
|
199
|
-
/**
|
|
228
|
+
/**
|
|
229
|
+
* A write that happens only when the record still holds these values (D150): equality, on single-valued fields
|
|
230
|
+
* you can see; `null` means the field is empty. Otherwise CONFLICT.
|
|
231
|
+
*/
|
|
232
|
+
export type WriteCondition<R> = {
|
|
233
|
+
readonly [K in keyof R]?: Exclude<R[K], undefined> extends Scalar ? Exclude<R[K], undefined> | null : never;
|
|
234
|
+
};
|
|
235
|
+
/** One write in a `db.batch` (D150). */
|
|
236
|
+
export type DbBatchOp<A extends AppConfig> = {
|
|
237
|
+
[N in keyof A['schema'] & string]: {
|
|
238
|
+
readonly create: N;
|
|
239
|
+
readonly input: AppInput<A['schema'][N]>;
|
|
240
|
+
readonly idempotencyKey?: string;
|
|
241
|
+
} | {
|
|
242
|
+
readonly update: N;
|
|
243
|
+
readonly id: string;
|
|
244
|
+
readonly patch: Partial<AppInput<A['schema'][N]>>;
|
|
245
|
+
readonly if?: WriteCondition<RecordFor<A, A['schema'][N]>>;
|
|
246
|
+
} | {
|
|
247
|
+
readonly delete: N;
|
|
248
|
+
readonly id: string;
|
|
249
|
+
readonly if?: WriteCondition<RecordFor<A, A['schema'][N]>>;
|
|
250
|
+
};
|
|
251
|
+
}[keyof A['schema'] & string];
|
|
252
|
+
/** What each operation of a batch returns: the record it wrote, or null for a delete. */
|
|
253
|
+
export type DbBatchResults<A extends AppConfig, O extends readonly unknown[]> = {
|
|
254
|
+
readonly [I in keyof O]: O[I] extends {
|
|
255
|
+
create: infer N extends keyof A['schema'];
|
|
256
|
+
} ? RecordFor<A, A['schema'][N]> : O[I] extends {
|
|
257
|
+
update: infer N extends keyof A['schema'];
|
|
258
|
+
} ? RecordFor<A, A['schema'][N]> : null;
|
|
259
|
+
};
|
|
260
|
+
type NumberFields<E extends AppEntityDef> = {
|
|
261
|
+
[K in keyof E['fields']]: E['fields'][K] extends {
|
|
262
|
+
kind: 'number';
|
|
263
|
+
} ? K : never;
|
|
264
|
+
}[keyof E['fields']] & string;
|
|
265
|
+
type GroupFields<E extends AppEntityDef> = 'createdBy' | ({
|
|
266
|
+
[K in keyof E['fields']]: E['fields'][K] extends {
|
|
267
|
+
kind: 'list' | 'file';
|
|
268
|
+
} ? never : K;
|
|
269
|
+
}[keyof E['fields']] & string);
|
|
270
|
+
/** A count, and the totals asked for: each by field name. `min`, `max` and `avg` are null when no record has the field. */
|
|
271
|
+
export type DbTotals = {
|
|
272
|
+
readonly count: number;
|
|
273
|
+
readonly sum?: Readonly<Record<string, number>>;
|
|
274
|
+
readonly min?: Readonly<Record<string, number | null>>;
|
|
275
|
+
readonly max?: Readonly<Record<string, number | null>>;
|
|
276
|
+
readonly avg?: Readonly<Record<string, number | null>>;
|
|
277
|
+
};
|
|
278
|
+
/** What `summarise` returns (D154): the totals of every matching record, and of each group when `groupBy` is given. */
|
|
279
|
+
export type DbSummary = DbTotals & {
|
|
280
|
+
/** In order of value; `value` is null for the records that leave the field empty. */
|
|
281
|
+
readonly groups?: readonly (DbTotals & {
|
|
282
|
+
readonly value: string | number | boolean | null;
|
|
283
|
+
})[];
|
|
284
|
+
};
|
|
285
|
+
/** Operations on one app entity. */
|
|
200
286
|
export type DbOps<A extends AppConfig, E extends AppEntityDef> = {
|
|
201
287
|
/** NOT_PERMITTED if the access rules deny it, or the subject is a student you can't see (D25). */
|
|
288
|
+
/** CONFLICT if another record already holds the values of one of the entity's `unique` rules (D149). */
|
|
202
289
|
create(input: AppInput<E>, options?: {
|
|
203
290
|
idempotencyKey?: string;
|
|
204
291
|
}): Promise<RecordFor<A, E>>;
|
|
205
|
-
|
|
292
|
+
/** With `if`, only when the record still holds those values; otherwise CONFLICT (D150). */
|
|
293
|
+
update(id: string, patch: Partial<AppInput<E>>, options?: {
|
|
294
|
+
readonly if?: WriteCondition<RecordFor<A, E>>;
|
|
295
|
+
}): Promise<RecordFor<A, E>>;
|
|
206
296
|
/** NOT_FOUND if the record doesn't exist or you may not read it (D25). */
|
|
207
297
|
get(id: string): Promise<RecordFor<A, E>>;
|
|
208
298
|
/** Only records you may read; filters and pages apply after access checks (D25). */
|
|
209
299
|
list<const I extends RefFields<E> = never>(options?: DbListOptions<A, E, I>): Promise<Page<[I] extends [never] ? RecordFor<A, E> : WithIncluded<A, E, I>>>;
|
|
210
|
-
delete(id: string
|
|
300
|
+
delete(id: string, options?: {
|
|
301
|
+
readonly if?: WriteCondition<RecordFor<A, E>>;
|
|
302
|
+
}): Promise<void>;
|
|
303
|
+
/** How many records match, of those you may read (D154). */
|
|
304
|
+
count(options?: {
|
|
305
|
+
readonly where?: DbWhere<RecordFor<A, E>, IndexedField<E>>;
|
|
306
|
+
readonly search?: string;
|
|
307
|
+
}): Promise<number>;
|
|
308
|
+
/**
|
|
309
|
+
* The count of the records you may read that match, with the sum, least, greatest and average of number
|
|
310
|
+
* fields, and the same for each value of one field with `groupBy` (D154). VALIDATION past 20,000 records or
|
|
311
|
+
* 200 groups: narrow it with `where`.
|
|
312
|
+
*/
|
|
313
|
+
summarise(options?: {
|
|
314
|
+
readonly where?: DbWhere<RecordFor<A, E>, IndexedField<E>>;
|
|
315
|
+
readonly search?: string;
|
|
316
|
+
readonly groupBy?: GroupFields<E>;
|
|
317
|
+
readonly sum?: readonly NumberFields<E>[];
|
|
318
|
+
readonly min?: readonly NumberFields<E>[];
|
|
319
|
+
readonly max?: readonly NumberFields<E>[];
|
|
320
|
+
readonly avg?: readonly NumberFields<E>[];
|
|
321
|
+
}): Promise<DbSummary>;
|
|
211
322
|
};
|
|
212
323
|
/** The app's own data, stored by the platform (IndexedDB in V1). */
|
|
213
324
|
export type DbApi<A extends AppConfig> = {
|
|
214
325
|
entity<N extends keyof A['schema'] & string>(name: N): DbOps<A, A['schema'][N]>;
|
|
326
|
+
/**
|
|
327
|
+
* Up to 25 creates, updates and deletes, across the app's entities: every one is written, or none is (D150).
|
|
328
|
+
* Each is checked as it is on its own; the first to fail stops the batch with its error, and `details.index`
|
|
329
|
+
* says which it was. Results come back in order. An operation can't refer to a record created earlier in the
|
|
330
|
+
* same batch.
|
|
331
|
+
*/
|
|
332
|
+
batch<const O extends readonly DbBatchOp<A>[]>(ops: O, options?: {
|
|
333
|
+
readonly idempotencyKey?: string;
|
|
334
|
+
}): Promise<DbBatchResults<A, O>>;
|
|
335
|
+
/**
|
|
336
|
+
* Keep a screen in step with the app's records (D157). The callback runs once at the start, and again whenever
|
|
337
|
+
* a record in one of the named entities is created, changed or deleted, by anyone or by a job; read what the
|
|
338
|
+
* screen shows inside it, in the ordinary way. Quad is asked every 5 seconds (`intervalMs`, never less than
|
|
339
|
+
* 2000), not while the page is hidden, and at once after a write made through this SDK. It says that something
|
|
340
|
+
* changed, never what.
|
|
341
|
+
*/
|
|
342
|
+
watch(entities: readonly (keyof A['schema'] & string)[], callback: () => void | Promise<void>, options?: {
|
|
343
|
+
readonly intervalMs?: number;
|
|
344
|
+
/** Told when Quad can't be asked, or the callback fails. The watch carries on. */
|
|
345
|
+
readonly onError?: (error: Error) => void;
|
|
346
|
+
}): DbWatch;
|
|
347
|
+
};
|
|
348
|
+
/** A watch started with `sdk.db.watch` (D157). */
|
|
349
|
+
export type DbWatch = {
|
|
350
|
+
/** Ask now, and wait for the callback if anything changed. */
|
|
351
|
+
check(): Promise<void>;
|
|
352
|
+
/** Stop watching, when the screen goes. */
|
|
353
|
+
stop(): void;
|
|
215
354
|
};
|
|
216
355
|
export type Channel = 'inApp' | 'email';
|
|
356
|
+
/**
|
|
357
|
+
* One part of a formatted notification (D151). All text is plain text: Quad renders the blocks, and an app never
|
|
358
|
+
* writes HTML. A button's link is a path in the app, like `link`.
|
|
359
|
+
*/
|
|
360
|
+
export type NotifyBlock = {
|
|
361
|
+
readonly heading: string;
|
|
362
|
+
} | {
|
|
363
|
+
readonly text: string;
|
|
364
|
+
} | {
|
|
365
|
+
readonly list: readonly string[];
|
|
366
|
+
}
|
|
367
|
+
/** Rows of a label and a value: the facts of an event. */
|
|
368
|
+
| {
|
|
369
|
+
readonly details: readonly (readonly [label: string, value: string])[];
|
|
370
|
+
} | {
|
|
371
|
+
readonly button: {
|
|
372
|
+
readonly label: string;
|
|
373
|
+
readonly link: string;
|
|
374
|
+
};
|
|
375
|
+
};
|
|
217
376
|
export type NotifyInput<A extends AppConfig> = {
|
|
218
377
|
readonly type: A['notifications'];
|
|
219
378
|
readonly to: Recipient | readonly Recipient[];
|
|
@@ -223,11 +382,25 @@ export type NotifyInput<A extends AppConfig> = {
|
|
|
223
382
|
};
|
|
224
383
|
readonly channel?: Channel;
|
|
225
384
|
readonly subject: string;
|
|
226
|
-
readonly body: string;
|
|
227
385
|
/** An app-relative link, e.g. `/greetings/abc`. */
|
|
228
386
|
readonly link?: string;
|
|
387
|
+
/**
|
|
388
|
+
* Files to send with the message (D152): up to five ids, 10 MB in all, of files you can read. Each recipient
|
|
389
|
+
* gets a copy made now, whether or not they could read the record it came from. NOT_PERMITTED when a file's
|
|
390
|
+
* field is hidden from a recipient's role (`visibleTo`) or is at a staff-only level and a recipient is not staff.
|
|
391
|
+
*/
|
|
392
|
+
readonly attachments?: readonly string[];
|
|
229
393
|
readonly idempotencyKey?: string;
|
|
230
|
-
}
|
|
394
|
+
} & ({
|
|
395
|
+
/** The message as plain text. */
|
|
396
|
+
readonly body: string;
|
|
397
|
+
/** The message in blocks, up to 30, with at most one button (D151). */
|
|
398
|
+
readonly content?: readonly NotifyBlock[];
|
|
399
|
+
} | {
|
|
400
|
+
/** Left out, the plain text is made from `content`. */
|
|
401
|
+
readonly body?: string;
|
|
402
|
+
readonly content: readonly NotifyBlock[];
|
|
403
|
+
});
|
|
231
404
|
export type ResolvedRecipient = {
|
|
232
405
|
readonly personId: string;
|
|
233
406
|
readonly role: Role;
|
|
@@ -405,6 +578,42 @@ export type AuditApi = {
|
|
|
405
578
|
readonly cursor?: string;
|
|
406
579
|
}): Promise<Page<AuditEntry>>;
|
|
407
580
|
};
|
|
581
|
+
/** The most one file can hold: 10 MB. The same number as the schema package's, kept here so an app's bundle needs only this one. */
|
|
582
|
+
export declare const MAX_FILE_BYTES: number;
|
|
583
|
+
/** A file an app keeps. Its id is what a `file` field holds. */
|
|
584
|
+
export type FileInfo = {
|
|
585
|
+
readonly id: string;
|
|
586
|
+
readonly name: string;
|
|
587
|
+
/** The media type, e.g. `image/png`. */
|
|
588
|
+
readonly type: string;
|
|
589
|
+
/** The size in bytes. */
|
|
590
|
+
readonly size: number;
|
|
591
|
+
readonly createdAt: string;
|
|
592
|
+
readonly createdBy: string;
|
|
593
|
+
};
|
|
594
|
+
/** Anything with a type and contents: a browser's `File` or `Blob` fits. */
|
|
595
|
+
export type FileSource = {
|
|
596
|
+
readonly type: string;
|
|
597
|
+
readonly name?: string;
|
|
598
|
+
arrayBuffer(): Promise<ArrayBuffer>;
|
|
599
|
+
};
|
|
600
|
+
export type FilesApi = {
|
|
601
|
+
/**
|
|
602
|
+
* Keep a file. It is yours alone until you write its id into a `file` field of a record; from then on whoever
|
|
603
|
+
* can read that field can read the file, and it is removed with the field or the record. A file not attached
|
|
604
|
+
* within 24 hours is removed. VALIDATION when the file is too large or not a kind Quad keeps.
|
|
605
|
+
*/
|
|
606
|
+
upload(file: FileSource, options?: {
|
|
607
|
+
readonly name?: string;
|
|
608
|
+
}): Promise<FileInfo>;
|
|
609
|
+
/** NOT_FOUND if the file doesn't exist or you may not read the field it is attached to. */
|
|
610
|
+
info(id: string): Promise<FileInfo>;
|
|
611
|
+
/** The file's contents, to show or to save. NOT_FOUND as for `info`. */
|
|
612
|
+
read(id: string): Promise<{
|
|
613
|
+
readonly info: FileInfo;
|
|
614
|
+
readonly bytes: Uint8Array;
|
|
615
|
+
}>;
|
|
616
|
+
};
|
|
408
617
|
export type ClockApi = {
|
|
409
618
|
/** The current instant (ISO 8601, UTC). Follows the demo clock, not the wall clock (D14). */
|
|
410
619
|
now(): string;
|
|
@@ -422,6 +631,7 @@ export type QuadSdk<A extends AppConfig = CurrentApp> = {
|
|
|
422
631
|
readonly schedule: ScheduleApi<A>;
|
|
423
632
|
readonly cron: CronApi;
|
|
424
633
|
readonly audit: AuditApi;
|
|
634
|
+
readonly files: FilesApi;
|
|
425
635
|
readonly clock: ClockApi;
|
|
426
636
|
};
|
|
427
637
|
export type { ContactPurpose, Recipient, Role };
|
|
@@ -1 +1,4 @@
|
|
|
1
|
-
export const SDK_VERSION = '0.
|
|
1
|
+
export const SDK_VERSION = '0.9.0';
|
|
2
|
+
// ---- files (D148) ----
|
|
3
|
+
/** The most one file can hold: 10 MB. The same number as the schema package's, kept here so an app's bundle needs only this one. */
|
|
4
|
+
export const MAX_FILE_BYTES = 10 * 1024 * 1024;
|