@quad-education/sdk 0.2.0 → 0.4.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.
Files changed (42) hide show
  1. package/README.md +4 -2
  2. package/dist/contract-tests/apps.d.ts +3 -112
  3. package/dist/contract-tests/apps.js +6 -183
  4. package/dist/contract-tests/harness.d.ts +2 -2
  5. package/dist/contract-tests/index.d.ts +2 -3
  6. package/dist/contract-tests/index.js +1 -2
  7. package/dist/contract-tests/suite.d.ts +3 -10
  8. package/dist/contract-tests/suite.js +49 -502
  9. package/dist/src/client/connect.d.ts +3 -13
  10. package/dist/src/client/connect.js +3 -21
  11. package/dist/src/client/create-client.js +0 -3
  12. package/dist/src/client/handshake-client.d.ts +0 -2
  13. package/dist/src/client/handshake-client.js +0 -2
  14. package/dist/src/contract/errors.d.ts +1 -1
  15. package/dist/src/contract/recipients.d.ts +2 -8
  16. package/dist/src/contract/types.d.ts +7 -44
  17. package/dist/src/contract/types.js +1 -1
  18. package/dist/src/engine/engine.d.ts +10 -46
  19. package/dist/src/engine/engine.js +42 -222
  20. package/dist/src/engine/host.d.ts +2 -2
  21. package/dist/src/engine/in-process.d.ts +2 -2
  22. package/dist/src/engine/runner.js +2 -4
  23. package/dist/src/engine/state.d.ts +6 -17
  24. package/dist/src/engine/state.js +3 -31
  25. package/dist/src/engine/tasks.js +3 -10
  26. package/dist/src/index.d.ts +0 -1
  27. package/dist/src/index.js +0 -1
  28. package/dist/src/jobs/define.d.ts +2 -2
  29. package/dist/src/platform-types.d.ts +2 -2
  30. package/dist/src/protocol.d.ts +1 -1
  31. package/dist/src/protocol.js +0 -1
  32. package/package.json +4 -4
  33. package/dist/contract-tests/standalone-apps.d.ts +0 -183
  34. package/dist/contract-tests/standalone-apps.js +0 -178
  35. package/dist/contract-tests/standalone.d.ts +0 -2
  36. package/dist/contract-tests/standalone.js +0 -709
  37. package/dist/src/client/http-transport.d.ts +0 -18
  38. package/dist/src/client/http-transport.js +0 -76
  39. package/dist/src/client/live.d.ts +0 -39
  40. package/dist/src/client/live.js +0 -122
  41. package/dist/src/contract/principals.d.ts +0 -30
  42. package/dist/src/contract/principals.js +0 -12
@@ -1,20 +1,10 @@
1
1
  import type { QuadSdk } from '../contract/types.ts';
2
2
  import { type HandshakeOptions } from './handshake-client.ts';
3
- import { type LiveOptions } from './live.ts';
4
3
  export { DEFAULT_SHELL_ORIGIN } from './handshake-client.ts';
5
- export { connectHttp, createHttpTransport, type HttpOptions, type HttpTransport, } from './http-transport.ts';
6
- export { SignInRequired, connectLive, continueAsGuest, getLiveStatus, signIn, type LiveOptions, type LiveStatus, } from './live.ts';
7
- export type ConnectOptions = HandshakeOptions & {
8
- /** Connect the live way (a shell-less app at its own address), for tests and other runtimes. */
9
- live?: LiveOptions;
10
- };
4
+ export type ConnectOptions = HandshakeOptions;
11
5
  /**
12
- * Connect to the platform. In the demo shell's iframe (the Sandbox and `quad dev`), the shell hands over a
13
- * private MessagePort and every SDK call goes through it. At a live address there is no shell: the app asks its
14
- * own origin who is signed in and talks to the data API with a token. The same built code does both.
15
- *
16
- * Live and nobody signed in: if the app allows guests this throws `SignInRequired` (with `guests: true`) so the
17
- * app can offer both choices; otherwise the page goes to Quad's sign-in and this never settles.
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.
18
8
  */
19
9
  export declare function connect(options?: ConnectOptions): Promise<QuadSdk>;
20
10
  export type { QuadSdk };
@@ -1,30 +1,12 @@
1
1
  import { portTransport } from "../port-rpc.js";
2
2
  import { createSdkClient } from "./create-client.js";
3
- import { handshake, isFramed } from "./handshake-client.js";
4
- import { connectSignedIn, getLiveStatus, signIn } from "./live.js";
3
+ import { handshake } from "./handshake-client.js";
5
4
  export { DEFAULT_SHELL_ORIGIN } from "./handshake-client.js";
6
- export { connectHttp, createHttpTransport, } from "./http-transport.js";
7
- export { SignInRequired, connectLive, continueAsGuest, getLiveStatus, signIn, } from "./live.js";
8
5
  /**
9
- * Connect to the platform. In the demo shell's iframe (the Sandbox and `quad dev`), the shell hands over a
10
- * private MessagePort and every SDK call goes through it. At a live address there is no shell: the app asks its
11
- * own origin who is signed in and talks to the data API with a token. The same built code does both.
12
- *
13
- * Live and nobody signed in: if the app allows guests this throws `SignInRequired` (with `guests: true`) so the
14
- * app can offer both choices; otherwise the page goes to Quad's sign-in and this never settles.
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.
15
8
  */
16
9
  export async function connect(options = {}) {
17
- if (options.live || !isFramed()) {
18
- const live = options.live ?? {};
19
- const status = await getLiveStatus(live);
20
- if (!status.signedIn && !status.guests) {
21
- signIn(live);
22
- return never();
23
- }
24
- return connectSignedIn(status, live);
25
- }
26
10
  const { port, session } = await handshake(options);
27
11
  return createSdkClient(portTransport(port, session.clock), session);
28
12
  }
29
- /** A page that is leaving has nothing to resolve to. */
30
- const never = () => new Promise(() => { });
@@ -12,9 +12,6 @@ export function createSdkClient(transport, session, context) {
12
12
  const nowMs = () => transport.nowMs();
13
13
  const school = {
14
14
  info: () => call('school.info'),
15
- calendar: {
16
- list: (options) => call('school.calendar', options),
17
- },
18
15
  };
19
16
  for (const entity of ENTITY_NAMES) {
20
17
  if (entity === 'schools')
@@ -6,8 +6,6 @@ export type HandshakeOptions = {
6
6
  timeoutMs?: number;
7
7
  };
8
8
  export declare const DEFAULT_SHELL_ORIGIN: string;
9
- /** Whether this page is inside another frame, as an app in the demo shell is. */
10
- export declare const isFramed: () => boolean;
11
9
  /**
12
10
  * The window handshake (D15): announce `quad:ready` to the parent frame on the shell's
13
11
  * origin, then accept the matching `quad:hello` and its private port, and nothing else.
@@ -2,8 +2,6 @@ import { QuadError } from "../contract/errors.js";
2
2
  import { isHello } from "../handshake.js";
3
3
  const env = import.meta.env;
4
4
  export const DEFAULT_SHELL_ORIGIN = env?.VITE_QUAD_SHELL_ORIGIN ?? 'http://localhost:5173';
5
- /** Whether this page is inside another frame, as an app in the demo shell is. */
6
- export const isFramed = () => typeof window !== 'undefined' && window.parent !== window;
7
5
  /**
8
6
  * The window handshake (D15): announce `quad:ready` to the parent frame on the shell's
9
7
  * origin, then accept the matching `quad:hello` and its private port, and nothing else.
@@ -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_AVAILABLE' | 'NOT_PERMITTED' | 'NOT_ASSIGNEE' | 'TRANSPORT' | 'INTERNAL';
2
+ export type QuadErrorCode = 'SCOPE_NOT_GRANTED' | 'UNDECLARED_TYPE' | 'NOT_FOUND' | 'VALIDATION' | 'FORBIDDEN' | 'NOT_PERMITTED' | 'NOT_ASSIGNEE' | 'TRANSPORT' | 'INTERNAL';
3
3
  export declare class QuadError extends Error {
4
4
  readonly code: QuadErrorCode;
5
5
  readonly details: unknown;
@@ -5,14 +5,8 @@ import type { ContactPurpose, Role } from '@quad-education/canonical';
5
5
  */
6
6
  export type Recipient = {
7
7
  readonly person: string;
8
- }
9
- /** A school role, or (outside a school) one of the app's own roles (V2c, D100). */
10
- | {
11
- readonly role: Role | (string & {});
12
- }
13
- /** A Quad account, outside a school (V2c, D100). */
14
- | {
15
- readonly account: string;
8
+ } | {
9
+ readonly role: Role;
16
10
  } | {
17
11
  readonly relationship: 'parentsOf';
18
12
  readonly studentId: string;
@@ -1,7 +1,7 @@
1
- import type { CanonicalRecord, ContactPurpose, Entities, EntityName, RecordWithFields, RecordWithTiers, Role, TiersFor } from '@quad-education/canonical';
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.2.0";
4
+ export declare const SDK_VERSION = "0.4.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.
@@ -31,7 +31,6 @@ type Field<T, K extends string, Fallback> = T extends {
31
31
  export type CurrentApp = {
32
32
  roles: Field<QuadApp, 'roles', Role> & string;
33
33
  scopes: Field<QuadApp, 'scopes', never> & string;
34
- data: Field<QuadApp, 'data', undefined>;
35
34
  notifications: Field<QuadApp, 'notifications', never> & string;
36
35
  tasks: Field<QuadApp, 'tasks', never> & string;
37
36
  scheduled: Field<QuadApp, 'scheduled', never> & string;
@@ -78,22 +77,6 @@ export type Me = {
78
77
  readonly kind: 'user';
79
78
  readonly role: Role;
80
79
  readonly person: PersonSummary;
81
- }
82
- /** A Quad account using an app, with the app-defined roles it was assigned (V2c, D100). */
83
- | {
84
- readonly kind: 'account';
85
- readonly account: {
86
- readonly id: string;
87
- readonly displayName: string;
88
- };
89
- readonly roles: readonly string[];
90
- }
91
- /** Someone not signed in, identified only by a device id (V2c, D100). */
92
- | {
93
- readonly kind: 'guest';
94
- readonly guest: {
95
- readonly id: string;
96
- };
97
80
  } | {
98
81
  readonly kind: 'system';
99
82
  readonly appId: string;
@@ -101,22 +84,11 @@ export type Me = {
101
84
  export type AuthApi = {
102
85
  me(): Promise<Me>;
103
86
  };
104
- /** What an app declared for one entity: required and optional field names (V2s D85). */
105
- type DeclaredFor<A, N extends string, K extends 'required' | 'optional'> = A extends {
106
- data: infer D;
107
- } ? D extends {
108
- [P in N]: {
109
- [Q in K]: infer F extends string;
110
- };
111
- } ? F : never : never;
112
87
  /**
113
- * A school record as this app sees it. A version 1 manifest (with `data`) gets the core fields and the
114
- * fields it declared, optional ones typed `?`; a version 0 manifest gets every field of its granted
115
- * tiers (D23).
88
+ * A school record as this app sees it: the fields of the tiers its scopes grant (D23). Fields beyond ids, references
89
+ * and names are typed optional, because a school's system may not supply them (D103).
116
90
  */
117
- export type SchoolRecord<A extends AppConfig, N extends EntityName> = A extends {
118
- data: infer D;
119
- } ? [D] extends [undefined] ? RecordWithTiers<Entities[N], TiersFor<A['scopes'], N>> : RecordWithFields<Entities[N], TiersFor<A['scopes'], N>, DeclaredFor<A, N, 'required'>, DeclaredFor<A, N, 'optional'>> : RecordWithTiers<Entities[N], TiersFor<A['scopes'], N>>;
91
+ export type SchoolRecord<A extends AppConfig, N extends EntityName> = RecordWithTiers<Entities[N], TiersFor<A['scopes'], N>>;
120
92
  export type Collection<R> = {
121
93
  /** A record the current user can see; NOT_FOUND otherwise. */
122
94
  get(id: string): Promise<R>;
@@ -155,16 +127,7 @@ export type SchoolApi<A extends AppConfig> = {
155
127
  info(): Promise<SchoolInfo>;
156
128
  } & {
157
129
  readonly [N in Readable<A>]: Collection<SchoolRecord<A, N>> & Helpers<A, N>;
158
- } & ('calendarEvents' extends Readable<A> ? {
159
- readonly calendar: {
160
- /** Events overlapping [from, to); with `cohortId`, that cohort's events plus whole-school ones. */
161
- list(options: {
162
- from: string;
163
- to: string;
164
- cohortId?: string;
165
- }): Promise<readonly SchoolRecord<A, 'calendarEvents'>[]>;
166
- };
167
- } : unknown);
130
+ };
168
131
  type Simplify<T> = {
169
132
  [K in keyof T]: T[K];
170
133
  };
@@ -257,7 +220,7 @@ export type NotifyInput<A extends AppConfig> = {
257
220
  };
258
221
  export type ResolvedRecipient = {
259
222
  readonly personId: string;
260
- readonly role: Role | (string & {});
223
+ readonly role: Role;
261
224
  };
262
225
  export type NotifyApi<A extends AppConfig> = {
263
226
  /**
@@ -1 +1 @@
1
- export const SDK_VERSION = '0.2.0';
1
+ export const SDK_VERSION = '0.4.0';
@@ -1,26 +1,18 @@
1
- import { type Dataset } from '@quad-education/canonical';
1
+ import { type Dataset, type Principal } from '@quad-education/canonical';
2
2
  import { type AppSchema, type Manifest } from '@quad-education/schema';
3
- import { type AccountDirectory, type AccountRecord, type ActingAs } from '../contract/principals.ts';
4
3
  import type { OutboxEntry } from '../platform-types.ts';
5
4
  import { type ClockState, type Method, type SessionInfo } from '../protocol.ts';
6
5
  import { type RunRequest, type RunResult } from './runner.ts';
7
6
  import { type App, type Ctx, type Kernel, type ResolvedRecipients, type StoredRun } from './state.ts';
8
7
  import type { AppStore, PlatformKind } from './store.ts';
9
8
  export type EngineOptions = {
10
- /** The school. Absent, with `school: false`, for an engine that has none, like the data API (V2c). */
11
- dataset?: Dataset;
9
+ dataset: Dataset;
12
10
  /** Where in-browser state is kept; ignored when `store` is given. */
13
11
  indexedDB?: IDBFactory;
14
- /** Quad accounts a standalone app may notify and assign to, with their app roles (V2c, D100). */
15
- accounts?: readonly AccountRecord[] | AccountDirectory;
16
- /** False for an environment with no school: every school call is NOT_AVAILABLE (V2c, D100). */
17
- school?: boolean;
18
12
  /** IndexedDB database name; defaults to `quad-engine`. */
19
13
  dbName?: string;
20
- /** Keep state here instead of in memory and IndexedDB (the data API's DynamoDB store). */
14
+ /** Keep state here instead of in memory and IndexedDB (a school instance's data API, Phase 4). */
21
15
  store?: AppStore;
22
- /** The timezone of an engine with no school; defaults to Australia/Sydney. */
23
- timezone?: string;
24
16
  };
25
17
  export declare class Engine implements Kernel {
26
18
  private readonly apps;
@@ -28,33 +20,14 @@ export declare class Engine implements Kernel {
28
20
  private readonly byId;
29
21
  readonly dataset: Dataset;
30
22
  readonly store: AppStore;
31
- private readonly zone;
32
- private directory;
33
- private readonly hasSchool;
34
23
  private constructor();
35
24
  static create(options: EngineOptions): Promise<Engine>;
36
25
  close(): void;
37
26
  /** Load an app, resetting its store if the schema changed since last time (D12). */
38
- installApp({ manifest, schema, timezone, }: {
27
+ installApp({ manifest, schema }: {
39
28
  manifest: unknown;
40
29
  schema: AppSchema;
41
- timezone?: string;
42
30
  }): Promise<void>;
43
- /**
44
- * Register an app that is already installed in the store, without touching its state. A stateless server
45
- * (the data API) does this on each cold start; `installApp` is for publishing and reinstalling.
46
- */
47
- attachApp({ manifest, schema, timezone, }: {
48
- manifest: unknown;
49
- schema: AppSchema;
50
- timezone?: string;
51
- }): void;
52
- /** Whether an app is registered with this engine. */
53
- hasApp(appId: string): boolean;
54
- /** Replace the account directory (the shell rebuilds it when a previewed app's roles change). */
55
- setAccounts(accounts: readonly AccountRecord[]): void;
56
- /** Whether the app has no school here, so its people are accounts and guests (V2c, D100). */
57
- isStandalone(appId: string): boolean;
58
31
  manifest(appId: string): Manifest;
59
32
  /** Clear an app's data and outbox (the shell's reset button). */
60
33
  resetApp(appId: string): Promise<void>;
@@ -84,7 +57,7 @@ export declare class Engine implements Kernel {
84
57
  [k: string]: import("../index.ts").TaskStatus;
85
58
  };
86
59
  schedules: {
87
- [k: string]: "pending" | "done" | "cancelled";
60
+ [k: string]: "done" | "cancelled" | "pending";
88
61
  };
89
62
  audit: number;
90
63
  records: Record<string, Record<string, string>>;
@@ -95,7 +68,7 @@ export declare class Engine implements Kernel {
95
68
  * Who a call from the jobs host acts as: the principal of the run it belongs to (D7).
96
69
  * Only runs that are currently running count, so a finished run's id can't be replayed.
97
70
  */
98
- runPrincipal(appId: string, runId: string | undefined): Promise<ActingAs>;
71
+ runPrincipal(appId: string, runId: string | undefined): Promise<Principal>;
99
72
  /** Start due runs; the caller executes each as its `runAs` principal and calls finishRun. */
100
73
  startDueRuns(appId: string): Promise<RunRequest[]>;
101
74
  finishRun(appId: string, runId: string, result: RunResult): Promise<void>;
@@ -103,7 +76,7 @@ export declare class Engine implements Kernel {
103
76
  redeliverRun(appId: string, runId: string): Promise<void>;
104
77
  /** Move the demo clock forward (D14). Works whether it's frozen or running. */
105
78
  advanceClock(ms: number): void;
106
- timezone(app?: App): string;
79
+ timezone(): string;
107
80
  persistPlatform(app: App, kind: PlatformKind, items: readonly {
108
81
  id: string;
109
82
  }[]): Promise<void>;
@@ -118,17 +91,14 @@ export declare class Engine implements Kernel {
118
91
  nowMs(): number;
119
92
  sessionInfo(appId: string): SessionInfo;
120
93
  /** Handle one SDK call from `appId` on behalf of `principal`. */
121
- handle(appId: string, principal: ActingAs, method: Method, params: unknown): Promise<unknown>;
94
+ handle(appId: string, principal: Principal, method: Method, params: unknown): Promise<unknown>;
122
95
  private appFor;
123
- /** A school app, in an environment that has a school: standalone apps and school-less environments have none. */
124
- private usesSchool;
125
96
  private context;
126
97
  private me;
127
98
  private schoolInfo;
128
99
  private entityName;
129
100
  /**
130
- * Tiers the app is granted and the principal may see, with the fields it declared (V2s D85);
131
- * SCOPE_NOT_GRANTED if the app has no scope for the entity.
101
+ * Tiers the app is granted and the principal may see; SCOPE_NOT_GRANTED if the app has no scope for the entity.
132
102
  */
133
103
  private tiers;
134
104
  private allowedFields;
@@ -147,7 +117,6 @@ export declare class Engine implements Kernel {
147
117
  private cohortMembers;
148
118
  private studentParents;
149
119
  private parentChildren;
150
- private calendar;
151
120
  private dbEntity;
152
121
  nowIso(): string;
153
122
  /** The rules for an operation: 'system' (full access), or the role's rules (empty = denied). */
@@ -180,15 +149,10 @@ export declare class Engine implements Kernel {
180
149
  * reached about a specific student, and only through an eligible, unrestricted link.
181
150
  */
182
151
  resolveRecipients(ctx: Ctx, to: unknown, regarding: string | undefined): Promise<ResolvedRecipients>;
183
- /**
184
- * Recipients outside a school (V2c, D100): Quad accounts, by id or by one of the app's own roles, from the
185
- * account directory. Everything that needs a school is NOT_AVAILABLE. Guests have no address, so never appear.
186
- */
187
- private resolveAppRecipients;
188
152
  private suppressionReason;
189
153
  /** D17/D19: each suppressed parent is recorded, visible only to the CIO view. */
190
154
  private auditSuppressed;
191
155
  /** D19: one CIO-visible entry per call that returns pastoral or medical fields. */
192
156
  private auditSensitiveRead;
193
157
  }
194
- export declare const ENGINE_SDK_VERSION = "0.2.0";
158
+ export declare const ENGINE_SDK_VERSION = "0.4.0";