@gramlot/gramlot 0.2.12 → 0.2.14

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.
@@ -1,12 +1,19 @@
1
1
  /**
2
- * Server-side page base class and the `registerSource` marker.
2
+ * Server-side page base class and the `registerSource` and `registerEndpoint` markers.
3
3
  *
4
4
  * @module
5
5
  */
6
6
  import type {GramlotBuilder} from '../builder/gramlot-builder.js';
7
7
 
8
+ /** The marker of a Source method: `{auth}` on the method. */
9
+ export const SOURCE_METHOD: unique symbol;
10
+ /** The marker of an endpoint: `{auth}` on the method. */
11
+ export const ENDPOINT_METHOD: unique symbol;
12
+
8
13
  /** The registered Source method `name` of `page`, or null. */
9
14
  export function sourceMethod(page: Page, name: string): Function | null;
15
+ /** The registered endpoint `name` of `page`, or null. */
16
+ export function endpointMethod(page: Page, name: string): Function | null;
10
17
 
11
18
  /** Server-side page base; unrelated to browser view components. */
12
19
  export class Page {
@@ -30,7 +37,10 @@ export class Page {
30
37
  * its `root` argument and returns nothing.
31
38
  *
32
39
  * Source methods (`@source`, `registerSource`, `remoteSource`) are not yet part of the page-writing API:
33
- * they arrive together with the `remote` grammar attribute and `@endpoint`.
40
+ * they arrive together with the `remote` grammar attribute.
34
41
  */
35
- static registerSource(methodName: string): void;
42
+ static registerSource(methodName: string, options?: {auth?: string | null}): void;
43
+ /** Mark the own method `methodName` of this page class as an endpoint called with
44
+ * `contentType: 'data'`, equivalent to the Python `@endpoint`; call it after the class. */
45
+ static registerEndpoint(methodName: string, options?: {auth?: string | null}): void;
36
46
  }
@@ -1,22 +1,50 @@
1
1
  /* @ts-self-types="./page.d.ts" */
2
2
  /**
3
- * Server-side page base class and the `registerSource` marker.
3
+ * Server-side page base class and the `registerSource` and `registerEndpoint` markers.
4
4
  *
5
5
  * @module
6
6
  */
7
7
  import {GramlotBuilder} from '../builder/gramlot-builder.js';
8
8
 
9
- const SOURCE_METHOD = Symbol('gramlot.source');
9
+ /** The marker of a Source method and of an endpoint: `{auth}` on the method, as the Python
10
+ * `__gramlot_source__` and `__gramlot_endpoint__` attributes. */
11
+ export const SOURCE_METHOD = Symbol('gramlot.source');
12
+ export const ENDPOINT_METHOD = Symbol('gramlot.endpoint');
13
+ const MARKERS = new Map([[SOURCE_METHOD, 'registerSource'], [ENDPOINT_METHOD, 'registerEndpoint']]);
10
14
 
11
- export function sourceMethod(page, name) {
15
+ function declaredMethod(page, name, marker) {
12
16
  if (typeof name !== 'string' || !/^[A-Za-z][\w]*$/.test(name)) return null;
13
17
  for (let proto = Object.getPrototypeOf(page); proto; proto = Object.getPrototypeOf(proto)) {
14
18
  const descriptor = Object.getOwnPropertyDescriptor(proto, name);
15
- if (descriptor) return descriptor.value?.[SOURCE_METHOD] ? descriptor.value : null;
19
+ if (descriptor) return descriptor.value?.[marker] ? descriptor.value : null;
16
20
  }
17
21
  return null;
18
22
  }
19
23
 
24
+ export function sourceMethod(page, name) {
25
+ return declaredMethod(page, name, SOURCE_METHOD);
26
+ }
27
+
28
+ export function endpointMethod(page, name) {
29
+ return declaredMethod(page, name, ENDPOINT_METHOD);
30
+ }
31
+
32
+ function declare(PageClass, methodName, marker, auth) {
33
+ const register = MARKERS.get(marker);
34
+ if (methodName === 'main') throw new TypeError(`${register} cannot declare main`);
35
+ const method = Object.getOwnPropertyDescriptor(PageClass.prototype, methodName)?.value;
36
+ if (typeof method !== 'function') {
37
+ throw new TypeError(`${register} requires an own method of ${PageClass.name}: ${String(methodName)}`);
38
+ }
39
+ if (method[marker]) {
40
+ throw new TypeError(`${marker === SOURCE_METHOD ? 'Source method' : 'Endpoint'} already registered: ${methodName}`);
41
+ }
42
+ if (method[SOURCE_METHOD] || method[ENDPOINT_METHOD]) {
43
+ throw new TypeError(`${methodName} is declared both as Source method and endpoint`);
44
+ }
45
+ method[marker] = {auth};
46
+ }
47
+
20
48
  /** Server-side page base; unrelated to browser view components. */
21
49
  export class Page {
22
50
  static title = 'Gramlot';
@@ -34,14 +62,15 @@ export class Page {
34
62
  * equivalent to Python `@source`; call it after the class.
35
63
  *
36
64
  * Source methods (`@source`, `registerSource`, `remoteSource`) are not yet part of the page-writing API:
37
- * they arrive together with the `remote` grammar attribute and `@endpoint`.
65
+ * they arrive together with the `remote` grammar attribute.
38
66
  */
39
- static registerSource(methodName) {
40
- const method = Object.getOwnPropertyDescriptor(this.prototype, methodName)?.value;
41
- if (typeof method !== 'function') {
42
- throw new TypeError(`registerSource requires an own method of ${this.name}: ${String(methodName)}`);
43
- }
44
- if (method[SOURCE_METHOD]) throw new TypeError(`Source method already registered: ${methodName}`);
45
- method[SOURCE_METHOD] = true;
67
+ static registerSource(methodName, {auth = null} = {}) {
68
+ declare(this, methodName, SOURCE_METHOD, auth);
69
+ }
70
+
71
+ /** Mark the own method `methodName` of this page class as an endpoint called with
72
+ * `contentType: 'data'`, equivalent to Python `@endpoint`; call it after the class. */
73
+ static registerEndpoint(methodName, {auth = null} = {}) {
74
+ declare(this, methodName, ENDPOINT_METHOD, auth);
46
75
  }
47
76
  }
@@ -1,7 +1,7 @@
1
1
  /**
2
2
  * The click of a `<button>` (source plan §2 Button, §4.11, S12). One mechanism per button (P10): the
3
- * `dataController` nested in it, `action`, or the `fire`/`fire_*` family. The nested controller is a
4
- * normal ControllerProvider (B7); the click is one more trigger. Controller, `action` and counter use
3
+ * `dataController` or `dataRpc` nested in it, `action`, or the `fire`/`fire_*` family. The nested
4
+ * provider is a normal ControllerProvider or RpcProvider (B7); the click is one more trigger. Controller, `action` and counter use
5
5
  * the button's own node, also when the click hits a descendant (R11).
6
6
  *
7
7
  * R3, provisional: only a button with a mechanism gets `type="button"` when the author wrote no
@@ -31,22 +31,22 @@ export class ButtonBinding {
31
31
  get node() { return this.binding.node; }
32
32
  get element() { return this.#record.element; }
33
33
 
34
- /** The `dataController` children of the button, in Source order. */
34
+ /** The `dataController` and `dataRpc` children of the button, in Source order. */
35
35
  get controllers() {
36
36
  const value = this.node.value;
37
37
  if (!value?.getNodes) return [];
38
- return value.getNodes().filter(child => child.nodeTag === 'dataController');
38
+ return value.getNodes().filter(child => child.nodeTag === 'dataController' || child.nodeTag === 'dataRpc');
39
39
  }
40
40
 
41
41
  /**
42
- * 'controller' | 'action' | 'fire' | null, from the current Source. Several `dataController`
43
- * children, or two mechanisms together, are an error naming the button (P10).
42
+ * 'controller' | 'action' | 'fire' | null, from the current Source. Several `dataController` or
43
+ * `dataRpc` children, or two mechanisms together, are an error naming the button (P10).
44
44
  */
45
45
  get mechanism() {
46
46
  const node = this.node;
47
47
  const controllers = this.controllers;
48
48
  if (controllers.length > 1) {
49
- throw new Error(`${node.nodeTag} '${node.label}': several dataController children; a button has one click mechanism`);
49
+ throw new Error(`${node.nodeTag} '${node.label}': several dataController or dataRpc children; a button has one click mechanism`);
50
50
  }
51
51
  const attrs = node.getAttr() ?? {};
52
52
  const found = [];
@@ -1,34 +0,0 @@
1
- /**
2
- * Main and remote Source transport; application pages do not issue fetch calls.
3
- *
4
- * @module
5
- */
6
- import type {BeaconNavigator} from './dom.d.ts';
7
-
8
- /** The fetch function used by the transport. */
9
- export type Fetcher = (input: string, init?: RequestInit) => Promise<Response>;
10
-
11
- /** The page-to-server transport: loads `main` and remote Sources and closes the server page. */
12
- export class MainTransport {
13
- /** The URL of the `main` endpoint. */
14
- url: string;
15
- /** The function that sends the requests. */
16
- fetcher: Fetcher;
17
- /** The URL of the remote Source endpoint. */
18
- sourceUrl: string;
19
- /** The URL of the page close endpoint. */
20
- closeUrl: string;
21
- /** The navigator whose `sendBeacon` sends the close request on page hide. */
22
- navigator: BeaconNavigator;
23
- /** Create a transport with the endpoint URLs of the page. */
24
- constructor(url: string, fetcher?: Fetcher, sourceUrl?: string, closeUrl?: string,
25
- navigator?: BeaconNavigator);
26
- /** Request the `main` Source of `pageId`; resolves with the TYTX text. */
27
- main(pageId: string, signal?: AbortSignal): Promise<string>;
28
- /** Request the remote Source `method` of `pageId` with `params`; resolves with the TYTX text. */
29
- source(pageId: string, method: string, params: Record<string, unknown>, signal?: AbortSignal): Promise<string>;
30
- /** POST a JSON payload to `url`; resolves with the response text, rejects on a non-2xx status. */
31
- request(operation: string, url: string, payload: unknown, signal?: AbortSignal): Promise<string>;
32
- /** Ask the server to close the page, with a beacon or a best-effort fetch; delivery failure is ignored. */
33
- close(pageId: string, options?: {beacon?: boolean}): void;
34
- }