@quad-education/sdk 0.0.0-stage → 0.1.3
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.md +15 -0
- package/README.md +106 -2
- package/dist/contract-tests/apps.d.ts +373 -0
- package/dist/contract-tests/apps.js +309 -0
- package/dist/contract-tests/index.d.ts +3 -0
- package/dist/contract-tests/index.js +2 -0
- package/dist/contract-tests/suite.d.ts +57 -0
- package/dist/contract-tests/suite.js +1405 -0
- package/dist/src/client/connect.d.ts +10 -0
- package/dist/src/client/connect.js +12 -0
- package/dist/src/client/create-client.d.ts +14 -0
- package/dist/src/client/create-client.js +76 -0
- package/dist/src/client/handshake-client.d.ts +16 -0
- package/dist/src/client/handshake-client.js +37 -0
- package/dist/src/contract/errors.d.ts +8 -0
- package/dist/src/contract/errors.js +13 -0
- package/dist/src/contract/recipients.d.ts +28 -0
- package/dist/src/contract/recipients.js +12 -0
- package/dist/src/contract/types.d.ts +442 -0
- package/dist/src/contract/types.js +1 -0
- package/dist/src/engine/audit.d.ts +27 -0
- package/dist/src/engine/audit.js +105 -0
- package/dist/src/engine/cron.d.ts +17 -0
- package/dist/src/engine/cron.js +143 -0
- package/dist/src/engine/db-indexes.d.ts +11 -0
- package/dist/src/engine/db-indexes.js +82 -0
- package/dist/src/engine/engine.d.ts +156 -0
- package/dist/src/engine/engine.js +953 -0
- package/dist/src/engine/host.d.ts +39 -0
- package/dist/src/engine/host.js +73 -0
- package/dist/src/engine/in-process.d.ts +5 -0
- package/dist/src/engine/in-process.js +18 -0
- package/dist/src/engine/index.d.ts +10 -0
- package/dist/src/engine/index.js +8 -0
- package/dist/src/engine/query.d.ts +16 -0
- package/dist/src/engine/query.js +106 -0
- package/dist/src/engine/runner.d.ts +26 -0
- package/dist/src/engine/runner.js +260 -0
- package/dist/src/engine/state.d.ts +107 -0
- package/dist/src/engine/state.js +16 -0
- package/dist/src/engine/storage.d.ts +46 -0
- package/dist/src/engine/storage.js +86 -0
- package/dist/src/engine/tasks.d.ts +17 -0
- package/dist/src/engine/tasks.js +245 -0
- package/dist/src/engine/validate-input.d.ts +9 -0
- package/dist/src/engine/validate-input.js +76 -0
- package/dist/src/engine/values.d.ts +10 -0
- package/dist/src/engine/values.js +58 -0
- package/dist/src/handshake.d.ts +16 -0
- package/dist/src/handshake.js +9 -0
- package/dist/src/index.d.ts +3 -0
- package/dist/src/index.js +4 -0
- package/dist/src/jobs/define.d.ts +41 -0
- package/dist/src/jobs/define.js +28 -0
- package/dist/src/jobs/host.d.ts +13 -0
- package/dist/src/jobs/host.js +22 -0
- package/dist/src/jobs/index.d.ts +1 -0
- package/dist/src/jobs/index.js +2 -0
- package/dist/src/jobs/worker.d.ts +17 -0
- package/dist/src/jobs/worker.js +25 -0
- package/dist/src/platform-types.d.ts +19 -0
- package/dist/src/platform-types.js +1 -0
- package/dist/src/port-rpc.d.ts +42 -0
- package/dist/src/port-rpc.js +85 -0
- package/dist/src/protocol.d.ts +41 -0
- package/dist/src/protocol.js +45 -0
- package/dist/src/testing/index.d.ts +78 -0
- package/dist/src/testing/index.js +118 -0
- package/package.json +71 -3
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import type { QuadSdk } from '../contract/types.ts';
|
|
2
|
+
import { type HandshakeOptions } from './handshake-client.ts';
|
|
3
|
+
export { DEFAULT_SHELL_ORIGIN } from './handshake-client.ts';
|
|
4
|
+
export type ConnectOptions = HandshakeOptions;
|
|
5
|
+
/**
|
|
6
|
+
* Connect to the platform. In V1 the app runs in the demo shell's iframe; the shell hands
|
|
7
|
+
* over a private MessagePort, and every SDK call goes through it.
|
|
8
|
+
*/
|
|
9
|
+
export declare function connect(options?: ConnectOptions): Promise<QuadSdk>;
|
|
10
|
+
export type { QuadSdk };
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { portTransport } from "../port-rpc.js";
|
|
2
|
+
import { createSdkClient } from "./create-client.js";
|
|
3
|
+
import { handshake } from "./handshake-client.js";
|
|
4
|
+
export { DEFAULT_SHELL_ORIGIN } from "./handshake-client.js";
|
|
5
|
+
/**
|
|
6
|
+
* Connect to the platform. In V1 the app runs in the demo shell's iframe; the shell hands
|
|
7
|
+
* over a private MessagePort, and every SDK call goes through it.
|
|
8
|
+
*/
|
|
9
|
+
export async function connect(options = {}) {
|
|
10
|
+
const { port, session } = await handshake(options);
|
|
11
|
+
return createSdkClient(portTransport(port, session.clock), session);
|
|
12
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import type { QuadSdk } from '../contract/types.ts';
|
|
2
|
+
import type { Method, SessionInfo } from '../protocol.ts';
|
|
3
|
+
/** Per-SDK call context: in the jobs worker, the run a call belongs to (D7). */
|
|
4
|
+
export type CallContext = {
|
|
5
|
+
readonly runId?: string;
|
|
6
|
+
};
|
|
7
|
+
/** Moves RPC calls to an engine. In-process for tests; postMessage in the demo shell. */
|
|
8
|
+
export type Transport = {
|
|
9
|
+
call(method: Method, params: unknown, context?: CallContext): Promise<unknown>;
|
|
10
|
+
/** The engine's current time in ms, kept in step by the transport (D14). */
|
|
11
|
+
nowMs(): number;
|
|
12
|
+
};
|
|
13
|
+
/** Builds the SDK surface over a transport. Both transports share this code, so the contract suite covers it. */
|
|
14
|
+
export declare function createSdkClient(transport: Transport, session: SessionInfo, context?: CallContext): QuadSdk;
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
import { ENTITY_NAMES } from '@quad-education/canonical';
|
|
2
|
+
import { SDK_VERSION } from "../contract/types.js";
|
|
3
|
+
const dateInZone = (instant, timeZone) => new Intl.DateTimeFormat('en-CA', {
|
|
4
|
+
timeZone,
|
|
5
|
+
year: 'numeric',
|
|
6
|
+
month: '2-digit',
|
|
7
|
+
day: '2-digit',
|
|
8
|
+
}).format(instant);
|
|
9
|
+
/** Builds the SDK surface over a transport. Both transports share this code, so the contract suite covers it. */
|
|
10
|
+
export function createSdkClient(transport, session, context) {
|
|
11
|
+
const call = (method, params = {}) => transport.call(method, params, context);
|
|
12
|
+
const nowMs = () => transport.nowMs();
|
|
13
|
+
const school = {
|
|
14
|
+
info: () => call('school.info'),
|
|
15
|
+
calendar: {
|
|
16
|
+
list: (options) => call('school.calendar', options),
|
|
17
|
+
},
|
|
18
|
+
};
|
|
19
|
+
for (const entity of ENTITY_NAMES) {
|
|
20
|
+
if (entity === 'schools')
|
|
21
|
+
continue;
|
|
22
|
+
const collection = {
|
|
23
|
+
get: (id) => call('school.get', { entity, id }),
|
|
24
|
+
list: (options = {}) => call('school.list', { entity, options }),
|
|
25
|
+
};
|
|
26
|
+
const helpers = {
|
|
27
|
+
cohorts: {
|
|
28
|
+
byCode: (code) => call('school.cohortByCode', { code }),
|
|
29
|
+
members: (cohortId, options = {}) => call('school.cohortMembers', { cohortId, options }),
|
|
30
|
+
},
|
|
31
|
+
students: { parents: (studentId) => call('school.studentParents', { studentId }) },
|
|
32
|
+
parents: { children: (parentId) => call('school.parentChildren', { parentId }) },
|
|
33
|
+
};
|
|
34
|
+
school[entity] = Object.freeze({ ...collection, ...helpers[entity] });
|
|
35
|
+
}
|
|
36
|
+
const sdk = {
|
|
37
|
+
version: SDK_VERSION,
|
|
38
|
+
appId: session.appId,
|
|
39
|
+
auth: { me: () => call('auth.me') },
|
|
40
|
+
school: Object.freeze(school),
|
|
41
|
+
db: {
|
|
42
|
+
entity: (name) => Object.freeze({
|
|
43
|
+
create: (input, options = {}) => call('db.create', { entity: name, input, options }),
|
|
44
|
+
update: (id, patch) => call('db.update', { entity: name, id, patch }),
|
|
45
|
+
get: (id) => call('db.get', { entity: name, id }),
|
|
46
|
+
list: (options = {}) => call('db.list', { entity: name, options }),
|
|
47
|
+
delete: async (id) => {
|
|
48
|
+
await call('db.delete', { entity: name, id });
|
|
49
|
+
},
|
|
50
|
+
}),
|
|
51
|
+
},
|
|
52
|
+
notify: { send: (input) => call('notify.send', input) },
|
|
53
|
+
tasks: {
|
|
54
|
+
assign: (input) => call('tasks.assign', input),
|
|
55
|
+
get: (id) => call('tasks.get', { id }),
|
|
56
|
+
list: (options = {}) => call('tasks.list', options),
|
|
57
|
+
complete: (id, options = {}) => call('tasks.complete', { id, options }),
|
|
58
|
+
cancel: (id) => call('tasks.cancel', { id }),
|
|
59
|
+
},
|
|
60
|
+
schedule: {
|
|
61
|
+
at: (input) => call('schedule.at', input),
|
|
62
|
+
cancel: (id) => call('schedule.cancel', { id }),
|
|
63
|
+
list: (options = {}) => call('schedule.list', options),
|
|
64
|
+
},
|
|
65
|
+
cron: { list: () => call('cron.list') },
|
|
66
|
+
audit: {
|
|
67
|
+
record: (input) => call('audit.record', input),
|
|
68
|
+
list: (options = {}) => call('audit.list', options),
|
|
69
|
+
},
|
|
70
|
+
clock: {
|
|
71
|
+
now: () => new Date(nowMs()).toISOString(),
|
|
72
|
+
today: () => dateInZone(new Date(nowMs()), session.timezone),
|
|
73
|
+
},
|
|
74
|
+
};
|
|
75
|
+
return Object.freeze(sdk);
|
|
76
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import type { SessionInfo } from '../protocol.ts';
|
|
2
|
+
export type HandshakeOptions = {
|
|
3
|
+
/** The demo shell's origin. Messages from any other origin are ignored (D15). */
|
|
4
|
+
shellOrigin?: string;
|
|
5
|
+
/** How long to wait for the shell's handshake, in ms. */
|
|
6
|
+
timeoutMs?: number;
|
|
7
|
+
};
|
|
8
|
+
export declare const DEFAULT_SHELL_ORIGIN: string;
|
|
9
|
+
/**
|
|
10
|
+
* The window handshake (D15): announce `quad:ready` to the parent frame on the shell's
|
|
11
|
+
* origin, then accept the matching `quad:hello` and its private port, and nothing else.
|
|
12
|
+
*/
|
|
13
|
+
export declare function handshake(options?: HandshakeOptions): Promise<{
|
|
14
|
+
port: MessagePort;
|
|
15
|
+
session: SessionInfo;
|
|
16
|
+
}>;
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { QuadError } from "../contract/errors.js";
|
|
2
|
+
import { isHello } from "../handshake.js";
|
|
3
|
+
const env = import.meta.env;
|
|
4
|
+
export const DEFAULT_SHELL_ORIGIN = env?.VITE_QUAD_SHELL_ORIGIN ?? 'http://localhost:5173';
|
|
5
|
+
/**
|
|
6
|
+
* The window handshake (D15): announce `quad:ready` to the parent frame on the shell's
|
|
7
|
+
* origin, then accept the matching `quad:hello` and its private port, and nothing else.
|
|
8
|
+
*/
|
|
9
|
+
export function handshake(options = {}) {
|
|
10
|
+
const shellOrigin = options.shellOrigin ?? DEFAULT_SHELL_ORIGIN;
|
|
11
|
+
const timeoutMs = options.timeoutMs ?? 10_000;
|
|
12
|
+
if (window.parent === window) {
|
|
13
|
+
return Promise.reject(new QuadError('TRANSPORT', 'This app runs inside the Quad demo shell. Open it from there.'));
|
|
14
|
+
}
|
|
15
|
+
const nonce = crypto.randomUUID();
|
|
16
|
+
return new Promise((resolve, reject) => {
|
|
17
|
+
const timer = setTimeout(() => {
|
|
18
|
+
window.removeEventListener('message', onMessage);
|
|
19
|
+
reject(new QuadError('TRANSPORT', `no handshake from the demo shell (${shellOrigin}) within ${timeoutMs} ms`));
|
|
20
|
+
}, timeoutMs);
|
|
21
|
+
function onMessage(e) {
|
|
22
|
+
// Only the configured shell, and only our parent frame, may start a session.
|
|
23
|
+
if (e.origin !== shellOrigin || e.source !== window.parent) {
|
|
24
|
+
if (isHello(e.data))
|
|
25
|
+
console.warn(`[quad] ignored a handshake from ${e.origin}`);
|
|
26
|
+
return;
|
|
27
|
+
}
|
|
28
|
+
if (!isHello(e.data) || e.data.nonce !== nonce || !e.ports[0])
|
|
29
|
+
return;
|
|
30
|
+
clearTimeout(timer);
|
|
31
|
+
window.removeEventListener('message', onMessage);
|
|
32
|
+
resolve({ port: e.ports[0], session: e.data.session });
|
|
33
|
+
}
|
|
34
|
+
window.addEventListener('message', onMessage);
|
|
35
|
+
window.parent.postMessage({ type: 'quad:ready', protocol: 1, nonce }, shellOrigin);
|
|
36
|
+
});
|
|
37
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
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';
|
|
3
|
+
export declare class QuadError extends Error {
|
|
4
|
+
readonly code: QuadErrorCode;
|
|
5
|
+
readonly details: unknown;
|
|
6
|
+
constructor(code: QuadErrorCode, message: string, details?: unknown);
|
|
7
|
+
}
|
|
8
|
+
export declare function isQuadError(e: unknown, code?: QuadErrorCode): e is QuadError;
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
export class QuadError extends Error {
|
|
2
|
+
code;
|
|
3
|
+
details;
|
|
4
|
+
constructor(code, message, details) {
|
|
5
|
+
super(message);
|
|
6
|
+
this.name = 'QuadError';
|
|
7
|
+
this.code = code;
|
|
8
|
+
this.details = details;
|
|
9
|
+
}
|
|
10
|
+
}
|
|
11
|
+
export function isQuadError(e, code) {
|
|
12
|
+
return e instanceof QuadError && (code === undefined || e.code === code);
|
|
13
|
+
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import type { ContactPurpose, Role } from '@quad-education/canonical';
|
|
2
|
+
/**
|
|
3
|
+
* Who a notification (or, in Slice B, a task) is for. Parent recipients always need a
|
|
4
|
+
* student they're being contacted about, so the platform can apply D17.
|
|
5
|
+
*/
|
|
6
|
+
export type Recipient = {
|
|
7
|
+
readonly person: string;
|
|
8
|
+
} | {
|
|
9
|
+
readonly role: Role;
|
|
10
|
+
} | {
|
|
11
|
+
readonly relationship: 'parentsOf';
|
|
12
|
+
readonly studentId: string;
|
|
13
|
+
readonly purpose: ContactPurpose;
|
|
14
|
+
} | {
|
|
15
|
+
readonly relationship: 'staffOf';
|
|
16
|
+
readonly cohortId: string;
|
|
17
|
+
} | {
|
|
18
|
+
readonly relationship: 'studentsOf';
|
|
19
|
+
readonly cohortId: string;
|
|
20
|
+
};
|
|
21
|
+
/** The parents who may be contacted about a student (D17). Restricted parents are never included. */
|
|
22
|
+
export declare function parentsOf(studentId: string, options?: {
|
|
23
|
+
purpose?: ContactPurpose;
|
|
24
|
+
}): Recipient;
|
|
25
|
+
/** Staff members of a cohort, including teams and positions such as `deputy-principal` (D18). */
|
|
26
|
+
export declare function staffOf(cohortId: string): Recipient;
|
|
27
|
+
/** Student members of a cohort. */
|
|
28
|
+
export declare function studentsOf(cohortId: string): Recipient;
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
/** The parents who may be contacted about a student (D17). Restricted parents are never included. */
|
|
2
|
+
export function parentsOf(studentId, options = {}) {
|
|
3
|
+
return { relationship: 'parentsOf', studentId, purpose: options.purpose ?? 'correspondence' };
|
|
4
|
+
}
|
|
5
|
+
/** Staff members of a cohort, including teams and positions such as `deputy-principal` (D18). */
|
|
6
|
+
export function staffOf(cohortId) {
|
|
7
|
+
return { relationship: 'staffOf', cohortId };
|
|
8
|
+
}
|
|
9
|
+
/** Student members of a cohort. */
|
|
10
|
+
export function studentsOf(cohortId) {
|
|
11
|
+
return { relationship: 'studentsOf', cohortId };
|
|
12
|
+
}
|