@critical-path/core 0.27.0 → 0.28.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 +22 -0
- package/README.md +2 -0
- package/dist/domain/custom-fields.d.ts +7 -2
- package/dist/domain/custom-fields.d.ts.map +1 -1
- package/dist/domain/custom-fields.js +23 -1
- package/dist/domain/custom-fields.js.map +1 -1
- package/dist/engine/index.d.ts +1 -0
- package/dist/engine/index.d.ts.map +1 -1
- package/dist/engine/index.js +31 -21
- package/dist/engine/index.js.map +1 -1
- package/dist/plugins/index.d.ts +13 -3
- package/dist/plugins/index.d.ts.map +1 -1
- package/dist/plugins/index.js +44 -15
- package/dist/plugins/index.js.map +1 -1
- package/dist/plugins/plugins.test.d.ts +2 -0
- package/dist/plugins/plugins.test.d.ts.map +1 -0
- package/dist/plugins/plugins.test.js +137 -0
- package/dist/plugins/plugins.test.js.map +1 -0
- package/dist/schemas/index.d.ts +94 -91
- package/dist/schemas/index.d.ts.map +1 -1
- package/dist/schemas/index.js +2 -1
- package/dist/schemas/index.js.map +1 -1
- package/dist/types/index.d.ts +51 -5
- package/dist/types/index.d.ts.map +1 -1
- package/package.json +1 -1
- package/src/domain/custom-fields.ts +43 -2
- package/src/engine/index.ts +46 -25
- package/src/plugins/index.ts +51 -16
- package/src/plugins/plugins.test.ts +161 -0
- package/src/schemas/index.ts +2 -1
- package/src/types/index.ts +57 -5
- package/tsconfig.tsbuildinfo +1 -1
|
@@ -0,0 +1,161 @@
|
|
|
1
|
+
import { describe, it, expect, vi } from 'vitest';
|
|
2
|
+
import {
|
|
3
|
+
CriticalPathEngine,
|
|
4
|
+
CustomFieldValidationError,
|
|
5
|
+
ValidationError,
|
|
6
|
+
WorkflowValidationError,
|
|
7
|
+
type CriticalPathPlugin
|
|
8
|
+
} from '../index.js';
|
|
9
|
+
|
|
10
|
+
const strictWorkflow = {
|
|
11
|
+
name: 'Strict',
|
|
12
|
+
defaultStatusKey: 'todo',
|
|
13
|
+
statuses: [
|
|
14
|
+
{ key: 'todo', label: 'To Do', category: 'not_started' as const },
|
|
15
|
+
{ key: 'doing', label: 'Doing', category: 'in_progress' as const },
|
|
16
|
+
{ key: 'done', label: 'Done', category: 'completed' as const }
|
|
17
|
+
],
|
|
18
|
+
transitions: [
|
|
19
|
+
{ fromStatusKey: 'todo', toStatusKey: 'doing' },
|
|
20
|
+
{ fromStatusKey: 'doing', toStatusKey: 'done' }
|
|
21
|
+
]
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
describe('plugin system', () => {
|
|
25
|
+
it('runs init once with the engine before ready resolves', async () => {
|
|
26
|
+
const init = vi.fn(async (engine: CriticalPathEngine) => {
|
|
27
|
+
await engine.createProject({ name: 'Seeded by plugin' });
|
|
28
|
+
});
|
|
29
|
+
const engine = new CriticalPathEngine({ plugins: [{ id: 'seed', name: 'Seed', version: '1', init }] });
|
|
30
|
+
await engine.ready;
|
|
31
|
+
|
|
32
|
+
expect(init).toHaveBeenCalledTimes(1);
|
|
33
|
+
expect(init).toHaveBeenCalledWith(engine);
|
|
34
|
+
expect(await engine.getProjects()).toHaveLength(1);
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
it('surfaces init failures through ready without unhandled rejections', async () => {
|
|
38
|
+
const engine = new CriticalPathEngine({
|
|
39
|
+
plugins: [{ id: 'bad', name: 'Bad', version: '1', init: () => Promise.reject(new Error('boom')) }]
|
|
40
|
+
});
|
|
41
|
+
await expect(engine.ready).rejects.toThrow('boom');
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
it('validates values of plugin-registered custom field types and rejects unknown types', async () => {
|
|
45
|
+
const urlField: CriticalPathPlugin = {
|
|
46
|
+
id: 'url-field',
|
|
47
|
+
name: 'URL field',
|
|
48
|
+
version: '1',
|
|
49
|
+
customFieldTypes: [
|
|
50
|
+
{ type: 'url', validate: (value) => (typeof value === 'string' && /^https?:\/\//.test(value) ? null : 'must be an http(s) URL') }
|
|
51
|
+
]
|
|
52
|
+
};
|
|
53
|
+
const engine = new CriticalPathEngine({ plugins: [urlField] });
|
|
54
|
+
const project = await engine.createProject({
|
|
55
|
+
name: 'Links',
|
|
56
|
+
customFieldDefinitions: [{ id: 'f1', key: 'spec', label: 'Spec', type: 'url' }]
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
await engine.createTask({ projectId: project.id, title: 'ok', customFields: { spec: 'https://example.com' } });
|
|
60
|
+
await expect(engine.createTask({ projectId: project.id, title: 'bad', customFields: { spec: 'ftp://x' } })).rejects.toThrow(
|
|
61
|
+
/must be an http\(s\) URL/
|
|
62
|
+
);
|
|
63
|
+
await expect(
|
|
64
|
+
engine.createProject({ name: 'Unknown', customFieldDefinitions: [{ id: 'f', key: 'x', label: 'X', type: 'nope' }] })
|
|
65
|
+
).rejects.toThrow(CustomFieldValidationError);
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
it('refuses custom field types that clash with built-in or already registered types', () => {
|
|
69
|
+
const clash = (type: string): CriticalPathPlugin => ({
|
|
70
|
+
id: `p-${type}-${Math.random()}`,
|
|
71
|
+
name: 'Clash',
|
|
72
|
+
version: '1',
|
|
73
|
+
customFieldTypes: [{ type, validate: () => null }]
|
|
74
|
+
});
|
|
75
|
+
expect(() => new CriticalPathEngine({ plugins: [clash('number')] })).toThrow(/already exists/);
|
|
76
|
+
expect(() => new CriticalPathEngine({ plugins: [clash('money'), clash('money')] })).toThrow(/already exists/);
|
|
77
|
+
});
|
|
78
|
+
|
|
79
|
+
it('validates the output of before-hooks, so plugins cannot bypass workflow rules', async () => {
|
|
80
|
+
const skipAhead: CriticalPathPlugin = {
|
|
81
|
+
id: 'skip',
|
|
82
|
+
name: 'Skip ahead',
|
|
83
|
+
version: '1',
|
|
84
|
+
hooks: { beforeTaskUpdate: (_id, updates) => ({ ...updates, status: 'done' }) }
|
|
85
|
+
};
|
|
86
|
+
const engine = new CriticalPathEngine({ plugins: [skipAhead] });
|
|
87
|
+
const workflow = await engine.createWorkflow(strictWorkflow);
|
|
88
|
+
const project = await engine.createProject({ name: 'Strict', workflowId: workflow.id });
|
|
89
|
+
const task = await engine.createTask({ projectId: project.id, title: 'T' });
|
|
90
|
+
|
|
91
|
+
await expect(engine.updateTask(task.id, { title: 'renamed' })).rejects.toThrow(WorkflowValidationError);
|
|
92
|
+
expect((await engine.getTask(task.id))?.status).toBe('todo');
|
|
93
|
+
});
|
|
94
|
+
|
|
95
|
+
it('prevents before-hooks from moving tasks between projects', async () => {
|
|
96
|
+
let otherProjectId = '';
|
|
97
|
+
const mover: CriticalPathPlugin = {
|
|
98
|
+
id: 'mover',
|
|
99
|
+
name: 'Mover',
|
|
100
|
+
version: '1',
|
|
101
|
+
hooks: {
|
|
102
|
+
beforeTaskCreate: (task) => (task.title === 'move me' ? { ...task, projectId: otherProjectId } : task),
|
|
103
|
+
beforeTaskUpdate: (_id, updates) => ({ ...updates, projectId: otherProjectId })
|
|
104
|
+
}
|
|
105
|
+
};
|
|
106
|
+
const engine = new CriticalPathEngine({ plugins: [mover] });
|
|
107
|
+
const home = await engine.createProject({ name: 'Home' });
|
|
108
|
+
otherProjectId = (await engine.createProject({ name: 'Other' })).id;
|
|
109
|
+
|
|
110
|
+
await expect(engine.createTask({ projectId: home.id, title: 'move me' })).rejects.toThrow(ValidationError);
|
|
111
|
+
const task = await engine.createTask({ projectId: home.id, title: 'stay' });
|
|
112
|
+
await engine.updateTask(task.id, { title: 'still here' });
|
|
113
|
+
expect((await engine.getTask(task.id))?.projectId).toBe(home.id);
|
|
114
|
+
});
|
|
115
|
+
|
|
116
|
+
it('enforces required custom fields even when none are supplied', async () => {
|
|
117
|
+
const engine = new CriticalPathEngine();
|
|
118
|
+
const project = await engine.createProject({
|
|
119
|
+
name: 'Required',
|
|
120
|
+
customFieldDefinitions: [{ id: 'f', key: 'client', label: 'Client', type: 'text', required: true }]
|
|
121
|
+
});
|
|
122
|
+
await expect(engine.createTask({ projectId: project.id, title: 'missing' })).rejects.toThrow(/required/);
|
|
123
|
+
});
|
|
124
|
+
|
|
125
|
+
it('logs after-hook failures without failing the write or skipping events', async () => {
|
|
126
|
+
const consoleError = vi.spyOn(console, 'error').mockImplementation(() => {});
|
|
127
|
+
const engine = new CriticalPathEngine({
|
|
128
|
+
plugins: [{ id: 'flaky', name: 'Flaky', version: '1', hooks: { afterTaskCreate: () => { throw new Error('downstream'); } } }]
|
|
129
|
+
});
|
|
130
|
+
const published: string[] = [];
|
|
131
|
+
engine.events.subscribe('task.created', (e) => void published.push(e.name));
|
|
132
|
+
|
|
133
|
+
const project = await engine.createProject({ name: 'After' });
|
|
134
|
+
const task = await engine.createTask({ projectId: project.id, title: 'stored' });
|
|
135
|
+
expect(await engine.getTask(task.id)).not.toBeNull();
|
|
136
|
+
expect(published).toEqual(['task.created']);
|
|
137
|
+
expect(consoleError).toHaveBeenCalledWith(expect.stringContaining('"flaky" afterTaskCreate'), expect.any(Error));
|
|
138
|
+
consoleError.mockRestore();
|
|
139
|
+
});
|
|
140
|
+
|
|
141
|
+
it('passes the deleted task to delete hooks', async () => {
|
|
142
|
+
const seen: string[] = [];
|
|
143
|
+
const engine = new CriticalPathEngine({
|
|
144
|
+
plugins: [
|
|
145
|
+
{
|
|
146
|
+
id: 'audit',
|
|
147
|
+
name: 'Audit',
|
|
148
|
+
version: '1',
|
|
149
|
+
hooks: {
|
|
150
|
+
beforeTaskDelete: (_id, task) => void seen.push(`before:${task.title}`),
|
|
151
|
+
afterTaskDelete: (_id, task) => void seen.push(`after:${task.title}`)
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
]
|
|
155
|
+
});
|
|
156
|
+
const project = await engine.createProject({ name: 'Del' });
|
|
157
|
+
const task = await engine.createTask({ projectId: project.id, title: 'gone' });
|
|
158
|
+
await engine.deleteTask(task.id);
|
|
159
|
+
expect(seen).toEqual(['before:gone', 'after:gone']);
|
|
160
|
+
});
|
|
161
|
+
});
|
package/src/schemas/index.ts
CHANGED
|
@@ -65,7 +65,8 @@ export const CustomFieldDefinitionSchema = strictObject({
|
|
|
65
65
|
id: z.string(),
|
|
66
66
|
key: nonEmpty,
|
|
67
67
|
label: z.string(),
|
|
68
|
-
type
|
|
68
|
+
/** Built-in type or one registered by a plugin (checked by the engine). */
|
|
69
|
+
type: nonEmpty,
|
|
69
70
|
options: z.array(z.string()).optional(),
|
|
70
71
|
required: z.boolean().optional(),
|
|
71
72
|
defaultValue: z.unknown().optional()
|
package/src/types/index.ts
CHANGED
|
@@ -88,7 +88,8 @@ export interface CustomFieldDefinition {
|
|
|
88
88
|
id: string;
|
|
89
89
|
key: string;
|
|
90
90
|
label: string;
|
|
91
|
-
|
|
91
|
+
/** A built-in type, or a type registered by a plugin's `customFieldTypes`. */
|
|
92
|
+
type: 'text' | 'number' | 'date' | 'boolean' | 'single_select' | 'multi_select' | 'user' | (string & {});
|
|
92
93
|
options?: string[];
|
|
93
94
|
required?: boolean;
|
|
94
95
|
defaultValue?: unknown;
|
|
@@ -448,23 +449,74 @@ export type PublicWebhook = Omit<Webhook, 'secret'> & { hasSecret: boolean };
|
|
|
448
449
|
*/
|
|
449
450
|
export type WebhookEvent = import('../domain/events.js').CriticalPathDomainEvent['name'] | '*';
|
|
450
451
|
|
|
452
|
+
/**
|
|
453
|
+
* Lifecycle hooks, run in plugin registration order.
|
|
454
|
+
*
|
|
455
|
+
* - `before*` hooks may transform the input or throw to abort the operation. Their output is
|
|
456
|
+
* validated (workflow transitions, custom fields) exactly like caller input, and they cannot
|
|
457
|
+
* move a task to another project.
|
|
458
|
+
* - `after*` hooks run once the change is stored. Errors are logged and do not fail the call.
|
|
459
|
+
*/
|
|
451
460
|
export interface PluginHooks {
|
|
452
461
|
beforeTaskCreate?: (task: Partial<Task>) => Promise<Partial<Task>> | Partial<Task>;
|
|
453
462
|
afterTaskCreate?: (task: Task) => Promise<void> | void;
|
|
454
463
|
beforeTaskUpdate?: (id: string, updates: Partial<Task>) => Promise<Partial<Task>> | Partial<Task>;
|
|
455
464
|
afterTaskUpdate?: (task: Task, previousState: Task) => Promise<void> | void;
|
|
456
|
-
beforeTaskDelete?: (id: string) => Promise<void> | void;
|
|
457
|
-
afterTaskDelete?: (id: string) => Promise<void> | void;
|
|
465
|
+
beforeTaskDelete?: (id: string, task: Task) => Promise<void> | void;
|
|
466
|
+
afterTaskDelete?: (id: string, task: Task) => Promise<void> | void;
|
|
467
|
+
}
|
|
468
|
+
|
|
469
|
+
/** A custom field type contributed by a plugin, e.g. `url` or `currency`. */
|
|
470
|
+
export interface CustomFieldType {
|
|
471
|
+
/** The `type` value used in `CustomFieldDefinition`s. Must not clash with a built-in type. */
|
|
472
|
+
type: string;
|
|
473
|
+
label?: string;
|
|
474
|
+
/** Returns an error message for an invalid value, or nothing when valid. Not called for empty values. */
|
|
475
|
+
validate: (value: unknown, definition: CustomFieldDefinition) => string | null | undefined | void;
|
|
476
|
+
}
|
|
477
|
+
|
|
478
|
+
/** Context passed to plugin routes and middleware. */
|
|
479
|
+
export interface PluginRequestContext {
|
|
480
|
+
/** The engine as the calling actor (authorization and tenancy apply). */
|
|
481
|
+
engine: import('../engine/index.js').CriticalPathEngine;
|
|
482
|
+
/** The caller resolved by the router's `getContext`, if any. */
|
|
483
|
+
context?: Record<string, unknown> & { userId?: string };
|
|
484
|
+
url: URL;
|
|
485
|
+
/** Path parameters from the route pattern, e.g. `{ projectId: 'p1' }` for `/reports/:projectId`. */
|
|
486
|
+
params: Record<string, string>;
|
|
458
487
|
}
|
|
459
488
|
|
|
489
|
+
export interface PluginRoute {
|
|
490
|
+
method: 'GET' | 'POST' | 'PUT' | 'PATCH' | 'DELETE';
|
|
491
|
+
/** Path below the router's base, with `:name` parameters, e.g. `/reports/:projectId`. */
|
|
492
|
+
path: string;
|
|
493
|
+
handler: (request: Request, ctx: PluginRequestContext) => Response | Promise<Response>;
|
|
494
|
+
}
|
|
495
|
+
|
|
496
|
+
/**
|
|
497
|
+
* Wraps every routed request (after authentication). Call `next()` to continue, or return a
|
|
498
|
+
* `Response` to short-circuit (e.g. rate limiting).
|
|
499
|
+
*/
|
|
500
|
+
export type PluginMiddleware = (
|
|
501
|
+
request: Request,
|
|
502
|
+
ctx: Omit<PluginRequestContext, 'params'>,
|
|
503
|
+
next: () => Promise<Response>
|
|
504
|
+
) => Response | Promise<Response>;
|
|
505
|
+
|
|
460
506
|
export interface CriticalPathPlugin {
|
|
461
507
|
id: string;
|
|
462
508
|
name: string;
|
|
463
509
|
version: string;
|
|
464
510
|
description?: string;
|
|
465
511
|
hooks?: PluginHooks;
|
|
466
|
-
|
|
467
|
-
|
|
512
|
+
/** Additional custom field types projects can use in `customFieldDefinitions`. */
|
|
513
|
+
customFieldTypes?: CustomFieldType[];
|
|
514
|
+
/** Runs once when the engine starts; `engine.ready` resolves after every plugin's `init`. */
|
|
515
|
+
init?: (engine: import('../engine/index.js').CriticalPathEngine) => Promise<void> | void;
|
|
516
|
+
/** HTTP routes served by `@critical-path/server`, matched before built-in routes. */
|
|
517
|
+
routes?: PluginRoute[];
|
|
518
|
+
/** Middleware run by `@critical-path/server` around every routed request. */
|
|
519
|
+
middleware?: PluginMiddleware;
|
|
468
520
|
}
|
|
469
521
|
|
|
470
522
|
export interface CriticalPathConfig {
|
package/tsconfig.tsbuildinfo
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"root":["./src/deliverables.test.ts","./src/index.test.ts","./src/index.ts","./src/status.test.ts","./src/task-state.test.ts","./src/use-case-validation.test.ts","./src/domain/authorization.ts","./src/domain/calendar.test.ts","./src/domain/calendar.ts","./src/domain/cpm.test.ts","./src/domain/cpm.ts","./src/domain/custom-fields.ts","./src/domain/ddd.test.ts","./src/domain/entities.ts","./src/domain/errors.ts","./src/domain/events.ts","./src/domain/graph.ts","./src/domain/index.ts","./src/domain/ladder.test.ts","./src/domain/ladder.ts","./src/domain/metrics.test.ts","./src/domain/metrics.ts","./src/domain/task-duration.test.ts","./src/domain/workload.test.ts","./src/domain/workload.ts","./src/engine/actor.test.ts","./src/engine/authorization.test.ts","./src/engine/index.ts","./src/plugins/index.ts","./src/schemas/index.ts","./src/schemas/schemas.test.ts","./src/storage/file-storage.ts","./src/storage/firebase-storage.ts","./src/storage/index.ts","./src/storage/s3.ts","./src/storage/storage.test.ts","./src/store/firebase.test.ts","./src/store/firebase.ts","./src/store/index.ts","./src/store/sqlite.test.ts","./src/store/sqlite.ts","./src/types/index.ts","./src/utils/fractional-index.test.ts","./src/utils/fractional-index.ts","./src/utils/index.ts","./src/utils/key.test.ts","./src/utils/key.ts","./src/utils/mentions.test.ts","./src/utils/mentions.ts","./src/utils/status.ts","./src/utils/workflow.ts","./src/webhooks/dispatcher.ts","./src/webhooks/index.ts","./src/webhooks/signature.ts","./src/webhooks/webhooks.test.ts"],"version":"5.9.3"}
|
|
1
|
+
{"root":["./src/deliverables.test.ts","./src/index.test.ts","./src/index.ts","./src/status.test.ts","./src/task-state.test.ts","./src/use-case-validation.test.ts","./src/domain/authorization.ts","./src/domain/calendar.test.ts","./src/domain/calendar.ts","./src/domain/cpm.test.ts","./src/domain/cpm.ts","./src/domain/custom-fields.ts","./src/domain/ddd.test.ts","./src/domain/entities.ts","./src/domain/errors.ts","./src/domain/events.ts","./src/domain/graph.ts","./src/domain/index.ts","./src/domain/ladder.test.ts","./src/domain/ladder.ts","./src/domain/metrics.test.ts","./src/domain/metrics.ts","./src/domain/task-duration.test.ts","./src/domain/workload.test.ts","./src/domain/workload.ts","./src/engine/actor.test.ts","./src/engine/authorization.test.ts","./src/engine/index.ts","./src/plugins/index.ts","./src/plugins/plugins.test.ts","./src/schemas/index.ts","./src/schemas/schemas.test.ts","./src/storage/file-storage.ts","./src/storage/firebase-storage.ts","./src/storage/index.ts","./src/storage/s3.ts","./src/storage/storage.test.ts","./src/store/firebase.test.ts","./src/store/firebase.ts","./src/store/index.ts","./src/store/sqlite.test.ts","./src/store/sqlite.ts","./src/types/index.ts","./src/utils/fractional-index.test.ts","./src/utils/fractional-index.ts","./src/utils/index.ts","./src/utils/key.test.ts","./src/utils/key.ts","./src/utils/mentions.test.ts","./src/utils/mentions.ts","./src/utils/status.ts","./src/utils/workflow.ts","./src/webhooks/dispatcher.ts","./src/webhooks/index.ts","./src/webhooks/signature.ts","./src/webhooks/webhooks.test.ts"],"version":"5.9.3"}
|