@ankhorage/contracts 1.11.0 → 1.13.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/src/bindings.ts CHANGED
@@ -1,4 +1,7 @@
1
- import type { DbFilter, DbPage, DbSort } from './db';
1
+ import type { DataSourceId, EndpointId, OperationId } from './data';
2
+
3
+ export type ComponentInstanceId = string;
4
+ export type ComponentTypeId = string;
2
5
 
3
6
  export type BindingValue =
4
7
  | string
@@ -10,45 +13,118 @@ export type BindingValue =
10
13
  readonly [key: string]: BindingValue;
11
14
  };
12
15
 
13
- export type BindingRefreshMode = 'focus' | 'live' | 'poll' | 'static';
16
+ export type BindingDataPath = string;
17
+
18
+ export type BindingValueTransform = 'lowercase' | 'trim' | 'uppercase';
14
19
 
15
- export interface DbCollectionQueryBinding {
16
- readonly collection: string;
17
- readonly schema?: string;
18
- readonly columns?: readonly string[];
19
- readonly filters?: readonly DbFilter[];
20
- readonly sort?: readonly DbSort[];
21
- readonly page?: DbPage;
22
- readonly refresh?: BindingRefreshMode;
23
- readonly pollIntervalMs?: number;
20
+ export interface BindingOperationRef {
21
+ readonly dataSourceId: DataSourceId;
22
+ readonly operationId: OperationId;
23
+ readonly endpointId?: EndpointId;
24
24
  }
25
25
 
26
26
  export type BindingValueSource =
27
27
  | {
28
- readonly source: 'context';
29
- readonly path: string;
28
+ readonly kind: 'context';
29
+ readonly path: BindingDataPath;
30
30
  }
31
31
  | {
32
- readonly source: 'db.collection';
33
- readonly query: DbCollectionQueryBinding;
32
+ readonly kind: 'event';
33
+ readonly path: BindingDataPath;
34
34
  }
35
35
  | {
36
- readonly source: 'event';
37
- readonly path: string;
36
+ readonly kind: 'literal';
37
+ readonly value: BindingValue;
38
+ }
39
+ | {
40
+ readonly kind: 'operation';
41
+ readonly operation: BindingOperationRef;
42
+ readonly path?: BindingDataPath;
43
+ }
44
+ | {
45
+ readonly kind: 'state';
46
+ readonly path: BindingDataPath;
47
+ };
48
+
49
+ export interface BindingValueExpression {
50
+ readonly source: BindingValueSource;
51
+ readonly transforms?: readonly BindingValueTransform[];
52
+ }
53
+
54
+ export interface BindingFallback {
55
+ readonly value?: BindingValue;
56
+ readonly source?: BindingValueSource;
57
+ }
58
+
59
+ export type BindingLifecycleState = 'empty' | 'error' | 'loading';
60
+
61
+ export interface BindingLifecycleBehavior {
62
+ readonly state: BindingLifecycleState;
63
+ readonly fallback?: BindingFallback;
64
+ readonly message?: string;
65
+ }
66
+
67
+ export interface PropBinding {
68
+ readonly source: BindingValueSource;
69
+ readonly fallback?: BindingFallback;
70
+ readonly loading?: BindingLifecycleBehavior;
71
+ readonly error?: BindingLifecycleBehavior;
72
+ readonly empty?: BindingLifecycleBehavior;
73
+ readonly transforms?: readonly BindingValueTransform[];
74
+ }
75
+
76
+ export type BindingInputValue =
77
+ | {
78
+ readonly kind: 'array';
79
+ readonly items: readonly BindingInputValue[];
38
80
  }
39
81
  | {
40
- readonly source: 'literal';
82
+ readonly kind: 'literal';
41
83
  readonly value: BindingValue;
42
84
  }
43
85
  | {
44
- readonly source: 'state';
45
- readonly path: string;
86
+ readonly kind: 'object';
87
+ readonly fields: Readonly<Record<string, BindingInputValue>>;
88
+ }
89
+ | {
90
+ readonly kind: 'source';
91
+ readonly source: BindingValueSource;
92
+ readonly transforms?: readonly BindingValueTransform[];
46
93
  };
47
94
 
48
- export type BindingValueTransform = 'lowercase' | 'trim' | 'uppercase';
95
+ export type BindingInputMap = Readonly<Record<string, BindingInputValue>>;
96
+
97
+ export type BindingConditionOperator = 'eq' | 'exists' | 'neq' | 'notExists';
49
98
 
50
- export interface NodePropBinding {
51
- readonly prop: string;
52
- readonly valueFrom: BindingValueSource;
53
- readonly transform?: BindingValueTransform | readonly BindingValueTransform[];
99
+ export interface BindingCondition {
100
+ readonly source: BindingValueSource;
101
+ readonly operator: BindingConditionOperator;
102
+ readonly value?: BindingValue;
54
103
  }
104
+
105
+ export type EventBindingTarget =
106
+ | {
107
+ readonly kind: 'action';
108
+ readonly type: string;
109
+ }
110
+ | {
111
+ readonly kind: 'operation';
112
+ readonly operation: BindingOperationRef;
113
+ };
114
+
115
+ export interface EventBinding {
116
+ readonly target: EventBindingTarget;
117
+ readonly input?: BindingInputMap;
118
+ readonly when?: BindingCondition;
119
+ }
120
+
121
+ export interface ComponentDataBinding {
122
+ readonly componentId: ComponentInstanceId;
123
+ readonly componentType?: ComponentTypeId;
124
+ readonly props?: Readonly<Record<string, PropBinding>>;
125
+ readonly events?: Readonly<Record<string, readonly EventBinding[]>>;
126
+ }
127
+
128
+ export type ComponentDataBindingRegistry = Readonly<
129
+ Record<ComponentInstanceId, ComponentDataBinding>
130
+ >;
@@ -5,8 +5,6 @@ import { COLOR_HARMONIES } from '@ankhorage/color-theory';
5
5
  import { describe, expect, it } from 'bun:test';
6
6
 
7
7
  import {
8
- type ActionBinding,
9
- type ActionValueSource,
10
8
  APP_CATEGORIES,
11
9
  type AppCategory,
12
10
  AUTH_PROVIDERS,
@@ -21,22 +19,16 @@ import {
21
19
  type DbAdapter,
22
20
  type DbAdminAdapter,
23
21
  type DbChangeEvent,
24
- type DbPersistActionBinding,
25
- type DbPersistCommand,
26
22
  type DbRealtimeAdapter,
27
23
  DEPLOYMENT_TARGETS,
28
- type EmailSendActionBinding,
29
24
  type FormSubmitEventDto,
30
25
  type ImageAssetSource,
31
- type KnownActionBinding,
32
- type KnownCommandDto,
33
26
  NAVIGATOR_TYPES,
34
27
  type StoragePublicUrlResult,
35
28
  type StorageResult,
36
29
  type StorageUploadResult,
37
30
  type ThemeConfig,
38
31
  type ThemeModeConfig,
39
- type UiNode,
40
32
  } from './index';
41
33
 
42
34
  async function collectTypeScriptFiles(directory: string): Promise<string[]> {
@@ -236,55 +228,6 @@ describe('contracts', () => {
236
228
  expect(JSON.parse(JSON.stringify(source))).toEqual(source);
237
229
  });
238
230
 
239
- it('serializes node event bindings for submit actions', () => {
240
- const node: UiNode = {
241
- id: 'contact-form',
242
- type: 'Form',
243
- props: {
244
- title: 'Contact us',
245
- },
246
- events: {
247
- submit: [
248
- {
249
- type: 'email.send',
250
- payload: {
251
- to: 'info@example.com',
252
- subject: 'New contact message',
253
- bodyFrom: 'payload.values.message',
254
- },
255
- },
256
- {
257
- type: 'db.persist',
258
- payload: {
259
- collection: 'contact_messages',
260
- mode: 'columns',
261
- },
262
- },
263
- ],
264
- },
265
- };
266
-
267
- expect(JSON.parse(JSON.stringify(node))).toEqual(node);
268
- expect(node.events?.submit?.map((action) => action.type)).toEqual(['email.send', 'db.persist']);
269
- });
270
-
271
- it('accepts conditional node event bindings', () => {
272
- const binding: ActionBinding = {
273
- type: 'db.persist',
274
- payload: {
275
- collection: 'contact_messages',
276
- },
277
- when: {
278
- source: 'event',
279
- path: 'payload.values.email',
280
- operator: 'exists',
281
- },
282
- };
283
-
284
- expect(binding.when?.operator).toBe('exists');
285
- expect(binding.payload?.collection).toBe('contact_messages');
286
- });
287
-
288
231
  it('serializes normalized form submit event DTOs', () => {
289
232
  const event: FormSubmitEventDto = {
290
233
  type: 'form.submit',
@@ -344,121 +287,6 @@ describe('contracts', () => {
344
287
  expect(knownEventType).toBe('form.submit');
345
288
  });
346
289
 
347
- it('serializes provider-neutral value sources for action payloads', () => {
348
- const sources: readonly ActionValueSource[] = [
349
- { source: 'event', path: 'payload.values.email' },
350
- { source: 'literal', value: 'website-contact-form' },
351
- { source: 'context', path: 'auth.user.id' },
352
- { source: 'state', path: 'forms.contact.isSubmitting' },
353
- ];
354
-
355
- expect(JSON.parse(JSON.stringify(sources))).toEqual(sources);
356
- expect(sources.map((source) => source.source)).toEqual([
357
- 'event',
358
- 'literal',
359
- 'context',
360
- 'state',
361
- ]);
362
- });
363
-
364
- it('serializes a typed db.persist action binding', () => {
365
- const binding: DbPersistActionBinding = {
366
- type: 'db.persist',
367
- payload: {
368
- collection: 'contact_messages',
369
- mode: 'columns',
370
- values: [
371
- {
372
- field: 'firstname',
373
- valueFrom: { source: 'event', path: 'payload.values.firstname' },
374
- transform: 'trim',
375
- },
376
- {
377
- field: 'message',
378
- valueFrom: { source: 'event', path: 'payload.values.message' },
379
- },
380
- {
381
- field: 'source',
382
- valueFrom: { source: 'literal', value: 'website-contact-form' },
383
- },
384
- ],
385
- },
386
- };
387
-
388
- expect(JSON.parse(JSON.stringify(binding))).toEqual(binding);
389
- expect(binding.payload.values.map((value) => value.field)).toEqual([
390
- 'firstname',
391
- 'message',
392
- 'source',
393
- ]);
394
- });
395
-
396
- it('serializes a typed email.send action binding', () => {
397
- const binding: EmailSendActionBinding = {
398
- type: 'email.send',
399
- payload: {
400
- to: 'info@example.com',
401
- subject: 'New contact message',
402
- bodyFrom: { source: 'event', path: 'payload.values.message' },
403
- },
404
- };
405
-
406
- expect(JSON.parse(JSON.stringify(binding))).toEqual(binding);
407
- expect(binding.payload.bodyFrom?.source).toBe('event');
408
- });
409
-
410
- it('accepts typed action bindings in node event bindings', () => {
411
- const persist: DbPersistActionBinding = {
412
- type: 'db.persist',
413
- payload: {
414
- collection: 'contact_messages',
415
- values: [
416
- {
417
- field: 'message',
418
- valueFrom: { source: 'event', path: 'payload.values.message' },
419
- },
420
- ],
421
- },
422
- };
423
- const email: EmailSendActionBinding = {
424
- type: 'email.send',
425
- payload: {
426
- to: 'info@example.com',
427
- bodyFrom: { source: 'event', path: 'payload.values.message' },
428
- },
429
- };
430
- const node: UiNode = {
431
- id: 'contact-form',
432
- type: 'Form',
433
- events: {
434
- submit: [persist, email],
435
- },
436
- };
437
- const known: KnownActionBinding = persist;
438
-
439
- expect(node.events?.submit?.map((action) => action.type)).toEqual(['db.persist', 'email.send']);
440
- expect(known.type).toBe('db.persist');
441
- });
442
-
443
- it('serializes a normalized db.persist command DTO', () => {
444
- const command: DbPersistCommand = {
445
- type: 'db.persist.command',
446
- payload: {
447
- operation: 'insert',
448
- collection: 'contact_messages',
449
- mode: 'columns',
450
- values: {
451
- firstname: 'Fabio',
452
- message: 'This is my contact message',
453
- },
454
- },
455
- };
456
- const knownCommand: KnownCommandDto = command;
457
-
458
- expect(JSON.parse(JSON.stringify(command))).toEqual(command);
459
- expect(knownCommand.type).toBe('db.persist.command');
460
- });
461
-
462
290
  it('accepts a provider-neutral CRUD database adapter', async () => {
463
291
  const adapter: DbAdapter = {
464
292
  capabilities: {
package/src/types.ts CHANGED
@@ -1,7 +1,8 @@
1
1
  import type { ColorHarmony } from '@ankhorage/color-theory';
2
2
 
3
- import type { AuthFlowConfig, AuthIdentifierKind, AuthSignUpField } from './auth';
4
- import type { NodePropBinding } from './bindings';
3
+ import type { AuthFlowConfig, AuthIdentifierKind, AuthOAuthConfig, AuthSignUpField } from './auth';
4
+ import type { ComponentDataBindingRegistry } from './bindings';
5
+ import type { DataSourceRegistry } from './data';
5
6
 
6
7
  export interface ThemeModeConfig {
7
8
  primaryColor: string;
@@ -89,121 +90,6 @@ export type ManifestValue =
89
90
  | readonly ManifestValue[]
90
91
  | { readonly [key: string]: ManifestValue };
91
92
 
92
- export type ActionValue = ManifestValue;
93
-
94
- export type ActionBindingPayload = Record<string, ActionValue>;
95
-
96
- export type ActionConditionSource = 'context' | 'event' | 'state';
97
-
98
- export type ActionConditionOperator = 'eq' | 'exists' | 'neq' | 'notExists';
99
-
100
- export interface ActionCondition {
101
- readonly source: ActionConditionSource;
102
- readonly path: string;
103
- readonly operator: ActionConditionOperator;
104
- readonly value?: ActionValue;
105
- }
106
-
107
- export type ActionValueSource =
108
- | {
109
- readonly source: 'context';
110
- readonly path: string;
111
- }
112
- | {
113
- readonly source: 'event';
114
- readonly path: string;
115
- }
116
- | {
117
- readonly source: 'literal';
118
- readonly value: ActionValue;
119
- }
120
- | {
121
- readonly source: 'state';
122
- readonly path: string;
123
- };
124
-
125
- export type ActionValueTransform = 'lowercase' | 'trim' | 'uppercase';
126
-
127
- export interface ActionValueBinding {
128
- readonly valueFrom: ActionValueSource;
129
- readonly transform?: ActionValueTransform | readonly ActionValueTransform[];
130
- }
131
-
132
- export interface ActionBinding<
133
- TType extends string = string,
134
- TPayload extends object = ActionBindingPayload,
135
- > {
136
- readonly type: TType;
137
- readonly payload?: TPayload;
138
- readonly when?: ActionCondition;
139
- }
140
-
141
- export type DbPersistMode = 'columns' | 'json';
142
-
143
- export interface DbPersistFieldBinding {
144
- readonly field: string;
145
- readonly valueFrom: ActionValueSource;
146
- readonly transform?: ActionValueTransform | readonly ActionValueTransform[];
147
- }
148
-
149
- export interface DbPersistActionPayload {
150
- readonly collection: string;
151
- readonly kind?: string;
152
- readonly mode?: DbPersistMode;
153
- readonly jsonField?: string;
154
- readonly values: readonly DbPersistFieldBinding[];
155
- }
156
-
157
- export interface DbPersistActionBinding extends ActionBinding<
158
- 'db.persist',
159
- DbPersistActionPayload
160
- > {
161
- readonly payload: DbPersistActionPayload;
162
- }
163
-
164
- export interface EmailSendActionPayload {
165
- readonly to: string;
166
- readonly subject?: string;
167
- readonly body?: string;
168
- readonly bodyFrom?: ActionValueSource;
169
- }
170
-
171
- export interface EmailSendActionBinding extends ActionBinding<
172
- 'email.send',
173
- EmailSendActionPayload
174
- > {
175
- readonly payload: EmailSendActionPayload;
176
- }
177
-
178
- export type KnownActionBinding = DbPersistActionBinding | EmailSendActionBinding;
179
-
180
- export type UiNodeEventBindings = Record<string, readonly ActionBinding<string, object>[]>;
181
-
182
- export interface CommandDto<
183
- TType extends string = string,
184
- TPayload extends object = Record<string, ActionValue>,
185
- > {
186
- readonly type: TType;
187
- readonly payload: TPayload;
188
- }
189
-
190
- export type DbPersistOperation = 'insert';
191
-
192
- export type DbPersistCommandValues = Record<string, ActionValue>;
193
-
194
- export interface DbPersistCommandPayload {
195
- readonly operation: DbPersistOperation;
196
- readonly collection: string;
197
- readonly kind?: string;
198
- readonly mode?: DbPersistMode;
199
- readonly jsonField?: string;
200
- readonly values: DbPersistCommandValues;
201
- }
202
-
203
- export type DbPersistCommand = CommandDto<'db.persist.command', DbPersistCommandPayload>;
204
-
205
- export type KnownCommandDto = DbPersistCommand;
206
-
207
93
  export type ComponentEventPayloadValue = ManifestValue;
208
94
 
209
95
  export interface ComponentEventDto<
@@ -331,10 +217,8 @@ export interface UiNode {
331
217
  type: string;
332
218
  alias?: string;
333
219
  props?: Record<string, unknown>;
334
- bindings?: readonly NodePropBinding[];
335
220
  children?: UiNode[];
336
221
  style?: Record<string, number | string>;
337
- events?: UiNodeEventBindings;
338
222
  }
339
223
 
340
224
  export interface ScreenSpec {
@@ -404,6 +288,7 @@ export interface AuthSpec {
404
288
  flow?: AuthFlowConfig;
405
289
  signIn?: AuthSignInSpec;
406
290
  signUp?: AuthSignUpSpec;
291
+ oauth?: AuthOAuthConfig;
407
292
  profile?: AuthProfileSpec;
408
293
  }
409
294
 
@@ -437,6 +322,8 @@ export interface AppManifest {
437
322
  infra: InfraManifest;
438
323
  navigator: NavigatorSpec;
439
324
  screens: Record<string, ScreenSpec>;
325
+ dataSources?: DataSourceRegistry;
326
+ dataBindings?: ComponentDataBindingRegistry;
440
327
  settings: {
441
328
  apiBaseUrl?: string;
442
329
  localization: {