@codeinkit/dom-trigger 0.0.0-stage → 0.1.0-alpha.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/CHANGELOG.md +16 -0
- package/LICENSE +21 -0
- package/README.md +314 -2
- package/dist/cjs/dom-trigger.js +308 -0
- package/dist/cjs/emitter.js +48 -0
- package/dist/cjs/index.js +12 -0
- package/dist/cjs/modules.js +48 -0
- package/dist/cjs/names.js +22 -0
- package/dist/cjs/package.json +1 -0
- package/dist/cjs/types.js +2 -0
- package/dist/esm/dom-trigger.js +304 -0
- package/dist/esm/emitter.js +44 -0
- package/dist/esm/index.js +3 -0
- package/dist/esm/modules.js +45 -0
- package/dist/esm/names.js +16 -0
- package/dist/esm/package.json +1 -0
- package/dist/esm/types.js +1 -0
- package/dist/types/dom-trigger.d.ts +52 -0
- package/dist/types/emitter.d.ts +24 -0
- package/dist/types/index.d.ts +5 -0
- package/dist/types/modules.d.ts +10 -0
- package/dist/types/names.d.ts +8 -0
- package/dist/types/types.d.ts +88 -0
- package/package.json +61 -4
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
/** `domain.verb`: lowercase segments separated by dots, at least two of them */
|
|
2
|
+
export declare const EVENT_NAME_RE: RegExp;
|
|
3
|
+
/** the DOM event type is `prefix + name`, e.g. `flows:booking.submit` */
|
|
4
|
+
export declare const DEFAULT_PREFIX = "flows:";
|
|
5
|
+
export declare function isEventName(name: unknown): name is string;
|
|
6
|
+
export declare function assertEventName(name: unknown): asserts name is string;
|
|
7
|
+
/** `globalThis.document`, undefined on the server. Never read at import time */
|
|
8
|
+
export declare function getDocument(): Document | undefined;
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
export type Concurrency = 'drop' | 'latest' | 'queue' | 'parallel';
|
|
2
|
+
/** one item of a flow module's `triggers` export */
|
|
3
|
+
export interface TriggerDef {
|
|
4
|
+
type: 'dom';
|
|
5
|
+
/** event name without the prefix, e.g. `booking.submit` */
|
|
6
|
+
event: string;
|
|
7
|
+
/** what to do with an event that arrives while a run of this trigger is active. Default `'drop'` */
|
|
8
|
+
concurrency?: Concurrency;
|
|
9
|
+
}
|
|
10
|
+
/** anything zod-like: `safeParse` is preferred, `parse` (throws when invalid) is the fallback */
|
|
11
|
+
export interface SchemaLike<T = unknown> {
|
|
12
|
+
safeParse?(value: unknown): {
|
|
13
|
+
success: boolean;
|
|
14
|
+
data?: T;
|
|
15
|
+
error?: unknown;
|
|
16
|
+
};
|
|
17
|
+
parse?(value: unknown): T;
|
|
18
|
+
}
|
|
19
|
+
export interface ExecuteOptions {
|
|
20
|
+
signal?: AbortSignal;
|
|
21
|
+
runId?: string;
|
|
22
|
+
}
|
|
23
|
+
/** the part of a `Flows` instance the trigger uses. Both `@codeinkit/flows` and `@codeinkit/flows/core` fit */
|
|
24
|
+
export interface FlowsLike {
|
|
25
|
+
execute(name: string, input: any, scope?: any, options?: ExecuteOptions): Promise<unknown>;
|
|
26
|
+
registerModules?(modules: Record<string, unknown>, options?: {
|
|
27
|
+
base?: string;
|
|
28
|
+
}): string[];
|
|
29
|
+
}
|
|
30
|
+
/** the data a UI flow starts with */
|
|
31
|
+
export interface DomTriggerData<D = unknown> {
|
|
32
|
+
event: string;
|
|
33
|
+
detail: D;
|
|
34
|
+
runId: string;
|
|
35
|
+
source: {
|
|
36
|
+
flowId: string;
|
|
37
|
+
route?: string;
|
|
38
|
+
};
|
|
39
|
+
}
|
|
40
|
+
export type RunStatusName = 'running' | 'done' | 'error' | 'cancelled' | 'dropped' | 'invalid';
|
|
41
|
+
export interface RunStatus {
|
|
42
|
+
status: RunStatusName;
|
|
43
|
+
flowId: string;
|
|
44
|
+
event: string;
|
|
45
|
+
runId: string;
|
|
46
|
+
/** the flow's output, on `done` */
|
|
47
|
+
output?: unknown;
|
|
48
|
+
/** the rejection on `error`, the schema error on `invalid`, the abort reason on `cancelled` */
|
|
49
|
+
error?: unknown;
|
|
50
|
+
}
|
|
51
|
+
export interface RunInfo {
|
|
52
|
+
status: 'error' | 'invalid';
|
|
53
|
+
flowId: string;
|
|
54
|
+
event: string;
|
|
55
|
+
runId: string;
|
|
56
|
+
}
|
|
57
|
+
export interface DomTriggerOptions {
|
|
58
|
+
/** where to listen. Default: `globalThis.document`, resolved in `start()`; without one `start()` is a no-op (SSR) */
|
|
59
|
+
target?: EventTarget;
|
|
60
|
+
/** default `'flows:'` */
|
|
61
|
+
prefix?: string;
|
|
62
|
+
/** the unsafe scope of each run, default `{}`. Never put ports here, they belong to `new Flows({ ports })` */
|
|
63
|
+
scope?: () => Record<string, unknown> | undefined;
|
|
64
|
+
/** called for `error` and `invalid`. Default: `console.error` */
|
|
65
|
+
onError?: (error: unknown, info: RunInfo) => void;
|
|
66
|
+
/** default: `globalThis.location?.pathname` */
|
|
67
|
+
route?: () => string | undefined;
|
|
68
|
+
/** default: `crypto.randomUUID()` when available */
|
|
69
|
+
runId?: () => string;
|
|
70
|
+
}
|
|
71
|
+
export interface RegisterOptions {
|
|
72
|
+
/** validates (and may transform) the event's `detail` before the flow runs */
|
|
73
|
+
input?: SchemaLike;
|
|
74
|
+
}
|
|
75
|
+
export interface RegisterModulesOptions {
|
|
76
|
+
/** directory prefix removed from every key, e.g. './flows'. Default: the longest directory common to all keys */
|
|
77
|
+
base?: string;
|
|
78
|
+
/** also call `flows.registerModules(modules, { base })` */
|
|
79
|
+
registerFlows?: boolean;
|
|
80
|
+
}
|
|
81
|
+
export type EventMap = Record<string, unknown>;
|
|
82
|
+
/**
|
|
83
|
+
* DOM event types of an event map, to augment `DocumentEventMap`:
|
|
84
|
+
* `declare global { interface DocumentEventMap extends DomEventMap<AppEvents> {} }`
|
|
85
|
+
*/
|
|
86
|
+
export type DomEventMap<M, P extends string = 'flows:'> = {
|
|
87
|
+
[K in keyof M & string as `${P}${K}`]: CustomEvent<M[K]>;
|
|
88
|
+
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,63 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@codeinkit/dom-trigger",
|
|
3
|
-
"version": "0.0.0
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.1.0-alpha.0",
|
|
4
|
+
"description": "DOM CustomEvent trigger for @codeinkit/flows: run UI flows from `flows:domain.verb` events dispatched on the document.",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"author": "codeinkit dev team",
|
|
7
|
+
"repository": {
|
|
8
|
+
"type": "git",
|
|
9
|
+
"url": "git+https://github.com/amitmtrn/dom-trigger.git"
|
|
10
|
+
},
|
|
11
|
+
"bugs": {
|
|
12
|
+
"url": "https://github.com/amitmtrn/dom-trigger/issues"
|
|
13
|
+
},
|
|
14
|
+
"homepage": "https://github.com/amitmtrn/dom-trigger#readme",
|
|
15
|
+
"keywords": [
|
|
16
|
+
"flows",
|
|
17
|
+
"trigger",
|
|
18
|
+
"dom",
|
|
19
|
+
"customevent",
|
|
20
|
+
"ui"
|
|
21
|
+
],
|
|
22
|
+
"sideEffects": false,
|
|
23
|
+
"main": "./dist/cjs/index.js",
|
|
24
|
+
"module": "./dist/esm/index.js",
|
|
25
|
+
"types": "./dist/types/index.d.ts",
|
|
26
|
+
"exports": {
|
|
27
|
+
".": {
|
|
28
|
+
"types": "./dist/types/index.d.ts",
|
|
29
|
+
"import": "./dist/esm/index.js",
|
|
30
|
+
"require": "./dist/cjs/index.js"
|
|
31
|
+
},
|
|
32
|
+
"./package.json": "./package.json"
|
|
33
|
+
},
|
|
34
|
+
"files": [
|
|
35
|
+
"dist",
|
|
36
|
+
"README.md",
|
|
37
|
+
"LICENSE",
|
|
38
|
+
"CHANGELOG.md"
|
|
39
|
+
],
|
|
40
|
+
"engines": {
|
|
41
|
+
"node": ">=18"
|
|
42
|
+
},
|
|
43
|
+
"scripts": {
|
|
44
|
+
"clean": "node scripts/clean.cjs",
|
|
45
|
+
"build": "npm run clean && tsc -p tsconfig.cjs.json && tsc -p tsconfig.esm.json && node scripts/postbuild.cjs",
|
|
46
|
+
"typecheck": "tsc --noEmit -p . && tsc --noEmit -p tsconfig.test.json",
|
|
47
|
+
"test": "jest",
|
|
48
|
+
"prepublishOnly": "npm run typecheck && npm run build && npm test"
|
|
49
|
+
},
|
|
50
|
+
"peerDependencies": {
|
|
51
|
+
"@codeinkit/flows": "^4.0.0-alpha.1"
|
|
52
|
+
},
|
|
53
|
+
"devDependencies": {
|
|
54
|
+
"@codeinkit/flows": "4.0.0-alpha.1",
|
|
55
|
+
"@codeinkit/flows-hoa": "0.2.0-alpha.0",
|
|
56
|
+
"@types/jest": "^29.5.14",
|
|
57
|
+
"@types/node": "^22.10.0",
|
|
58
|
+
"jest": "^29.7.0",
|
|
59
|
+
"jest-environment-jsdom": "^29.7.0",
|
|
60
|
+
"ts-jest": "^29.2.5",
|
|
61
|
+
"typescript": "^5.6.3"
|
|
62
|
+
}
|
|
63
|
+
}
|