@dxos/functions 0.5.3-main.c3feabc → 0.5.3-main.cb47aab
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/dist/lib/browser/index.mjs +663 -422
- package/dist/lib/browser/index.mjs.map +4 -4
- package/dist/lib/browser/meta.json +1 -1
- package/dist/lib/node/index.cjs +648 -419
- package/dist/lib/node/index.cjs.map +4 -4
- package/dist/lib/node/meta.json +1 -1
- package/dist/types/src/handler.d.ts +32 -12
- package/dist/types/src/handler.d.ts.map +1 -1
- package/dist/types/src/index.d.ts +2 -0
- package/dist/types/src/index.d.ts.map +1 -1
- package/dist/types/src/registry/function-registry.d.ts +24 -0
- package/dist/types/src/registry/function-registry.d.ts.map +1 -0
- package/dist/types/src/registry/function-registry.test.d.ts +2 -0
- package/dist/types/src/registry/function-registry.test.d.ts.map +1 -0
- package/dist/types/src/registry/index.d.ts +2 -0
- package/dist/types/src/registry/index.d.ts.map +1 -0
- package/dist/types/src/runtime/dev-server.d.ts +7 -10
- package/dist/types/src/runtime/dev-server.d.ts.map +1 -1
- package/dist/types/src/runtime/scheduler.d.ts +10 -59
- package/dist/types/src/runtime/scheduler.d.ts.map +1 -1
- package/dist/types/src/testing/functions-integration.test.d.ts +2 -0
- package/dist/types/src/testing/functions-integration.test.d.ts.map +1 -0
- package/dist/types/src/testing/index.d.ts +4 -0
- package/dist/types/src/testing/index.d.ts.map +1 -0
- package/dist/types/src/testing/setup.d.ts +5 -0
- package/dist/types/src/testing/setup.d.ts.map +1 -0
- package/dist/types/src/testing/test/handler.d.ts +1 -0
- package/dist/types/src/testing/test/handler.d.ts.map +1 -1
- package/dist/types/src/testing/types.d.ts +9 -0
- package/dist/types/src/testing/types.d.ts.map +1 -0
- package/dist/types/src/testing/util.d.ts +3 -0
- package/dist/types/src/testing/util.d.ts.map +1 -0
- package/dist/types/src/trigger/index.d.ts +2 -0
- package/dist/types/src/trigger/index.d.ts.map +1 -0
- package/dist/types/src/trigger/trigger-registry.d.ts +40 -0
- package/dist/types/src/trigger/trigger-registry.d.ts.map +1 -0
- package/dist/types/src/trigger/trigger-registry.test.d.ts +2 -0
- package/dist/types/src/trigger/trigger-registry.test.d.ts.map +1 -0
- package/dist/types/src/trigger/type/index.d.ts +5 -0
- package/dist/types/src/trigger/type/index.d.ts.map +1 -0
- package/dist/types/src/trigger/type/subscription-trigger.d.ts +4 -0
- package/dist/types/src/trigger/type/subscription-trigger.d.ts.map +1 -0
- package/dist/types/src/trigger/type/timer-trigger.d.ts +4 -0
- package/dist/types/src/trigger/type/timer-trigger.d.ts.map +1 -0
- package/dist/types/src/trigger/type/webhook-trigger.d.ts +4 -0
- package/dist/types/src/trigger/type/webhook-trigger.d.ts.map +1 -0
- package/dist/types/src/trigger/type/websocket-trigger.d.ts +13 -0
- package/dist/types/src/trigger/type/websocket-trigger.d.ts.map +1 -0
- package/dist/types/src/types.d.ts +118 -112
- package/dist/types/src/types.d.ts.map +1 -1
- package/package.json +16 -13
- package/schema/functions.json +122 -103
- package/src/handler.ts +50 -27
- package/src/index.ts +2 -0
- package/src/registry/function-registry.test.ts +105 -0
- package/src/registry/function-registry.ts +84 -0
- package/src/registry/index.ts +5 -0
- package/src/runtime/dev-server.test.ts +15 -35
- package/src/runtime/dev-server.ts +40 -22
- package/src/runtime/scheduler.test.ts +54 -75
- package/src/runtime/scheduler.ts +67 -300
- package/src/testing/functions-integration.test.ts +99 -0
- package/src/testing/index.ts +7 -0
- package/src/testing/setup.ts +45 -0
- package/src/testing/test/handler.ts +8 -2
- package/src/testing/types.ts +9 -0
- package/src/testing/util.ts +16 -0
- package/src/trigger/index.ts +5 -0
- package/src/trigger/trigger-registry.test.ts +229 -0
- package/src/trigger/trigger-registry.ts +176 -0
- package/src/trigger/type/index.ts +8 -0
- package/src/trigger/type/subscription-trigger.ts +73 -0
- package/src/trigger/type/timer-trigger.ts +44 -0
- package/src/trigger/type/webhook-trigger.ts +47 -0
- package/src/trigger/type/websocket-trigger.ts +91 -0
- package/src/types.ts +56 -40
package/src/types.ts
CHANGED
|
@@ -2,27 +2,21 @@
|
|
|
2
2
|
// Copyright 2023 DXOS.org
|
|
3
3
|
//
|
|
4
4
|
|
|
5
|
-
import
|
|
5
|
+
import { AST, S, TypedObject } from '@dxos/echo-schema';
|
|
6
6
|
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
});
|
|
10
|
-
|
|
11
|
-
const WebhookTriggerSchema = S.mutable(
|
|
12
|
-
S.struct({
|
|
13
|
-
method: S.string,
|
|
14
|
-
// Assigned port.
|
|
15
|
-
port: S.optional(S.number),
|
|
16
|
-
}),
|
|
17
|
-
);
|
|
7
|
+
// TODO(burdon): Factor out.
|
|
8
|
+
const omitEchoId = <T>(schema: S.Schema<T>): S.Schema<Omit<T, 'id'>> => S.make(AST.omit(schema.ast, ['id']));
|
|
18
9
|
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
10
|
+
/**
|
|
11
|
+
* Type discriminator for TriggerSpec.
|
|
12
|
+
* Every spec has a type field of type FunctionTriggerType that we can use to understand which
|
|
13
|
+
* type we're working with.
|
|
14
|
+
* https://www.typescriptlang.org/docs/handbook/2/narrowing.html#discriminated-unions
|
|
15
|
+
*/
|
|
16
|
+
export type FunctionTriggerType = 'subscription' | 'timer' | 'webhook' | 'websocket';
|
|
23
17
|
|
|
24
18
|
const SubscriptionTriggerSchema = S.struct({
|
|
25
|
-
|
|
19
|
+
type: S.literal('subscription'),
|
|
26
20
|
// TODO(burdon): Define query DSL.
|
|
27
21
|
filter: S.array(
|
|
28
22
|
S.struct({
|
|
@@ -39,47 +33,69 @@ const SubscriptionTriggerSchema = S.struct({
|
|
|
39
33
|
}),
|
|
40
34
|
),
|
|
41
35
|
});
|
|
36
|
+
export type SubscriptionTrigger = S.Schema.Type<typeof SubscriptionTriggerSchema>;
|
|
42
37
|
|
|
43
|
-
const
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
// Context passed to function.
|
|
47
|
-
context: S.optional(S.record(S.string, S.any)),
|
|
48
|
-
|
|
49
|
-
// Triggers.
|
|
50
|
-
timer: S.optional(TimerTriggerSchema),
|
|
51
|
-
webhook: S.optional(WebhookTriggerSchema),
|
|
52
|
-
websocket: S.optional(WebsocketTriggerSchema),
|
|
53
|
-
subscription: S.optional(SubscriptionTriggerSchema),
|
|
38
|
+
const TimerTriggerSchema = S.struct({
|
|
39
|
+
type: S.literal('timer'),
|
|
40
|
+
cron: S.string,
|
|
54
41
|
});
|
|
55
|
-
|
|
56
42
|
export type TimerTrigger = S.Schema.Type<typeof TimerTriggerSchema>;
|
|
43
|
+
|
|
44
|
+
const WebhookTriggerSchema = S.mutable(
|
|
45
|
+
S.struct({
|
|
46
|
+
type: S.literal('webhook'),
|
|
47
|
+
method: S.string,
|
|
48
|
+
// Assigned port.
|
|
49
|
+
port: S.optional(S.number),
|
|
50
|
+
}),
|
|
51
|
+
);
|
|
57
52
|
export type WebhookTrigger = S.Schema.Type<typeof WebhookTriggerSchema>;
|
|
53
|
+
|
|
54
|
+
const WebsocketTriggerSchema = S.struct({
|
|
55
|
+
type: S.literal('websocket'),
|
|
56
|
+
url: S.string,
|
|
57
|
+
init: S.optional(S.record(S.string, S.any)),
|
|
58
|
+
});
|
|
58
59
|
export type WebsocketTrigger = S.Schema.Type<typeof WebsocketTriggerSchema>;
|
|
59
|
-
|
|
60
|
-
|
|
60
|
+
|
|
61
|
+
const TriggerSpecSchema = S.union(
|
|
62
|
+
TimerTriggerSchema,
|
|
63
|
+
WebhookTriggerSchema,
|
|
64
|
+
WebsocketTriggerSchema,
|
|
65
|
+
SubscriptionTriggerSchema,
|
|
66
|
+
);
|
|
67
|
+
export type TriggerSpec = TimerTrigger | WebhookTrigger | WebsocketTrigger | SubscriptionTrigger;
|
|
61
68
|
|
|
62
69
|
/**
|
|
63
70
|
* Function definition.
|
|
64
71
|
*/
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
72
|
+
export class FunctionDef extends TypedObject({
|
|
73
|
+
typename: 'dxos.org/type/FunctionDef',
|
|
74
|
+
version: '0.1.0',
|
|
75
|
+
})({
|
|
76
|
+
uri: S.string,
|
|
69
77
|
description: S.optional(S.string),
|
|
70
|
-
|
|
78
|
+
route: S.string,
|
|
71
79
|
// TODO(burdon): NPM/GitHub/Docker/CF URL?
|
|
72
80
|
handler: S.string,
|
|
73
|
-
})
|
|
81
|
+
}) {}
|
|
74
82
|
|
|
75
|
-
export
|
|
83
|
+
export class FunctionTrigger extends TypedObject({
|
|
84
|
+
typename: 'dxos.org/type/FunctionTrigger',
|
|
85
|
+
version: '0.1.0',
|
|
86
|
+
})({
|
|
87
|
+
function: S.string.pipe(S.description('Function ID/URI.')),
|
|
88
|
+
// Context passed to a function.
|
|
89
|
+
meta: S.optional(S.record(S.string, S.any)),
|
|
90
|
+
spec: TriggerSpecSchema,
|
|
91
|
+
}) {}
|
|
76
92
|
|
|
77
93
|
/**
|
|
78
94
|
* Function manifest file.
|
|
79
95
|
*/
|
|
80
96
|
export const FunctionManifestSchema = S.struct({
|
|
81
|
-
functions: S.mutable(S.array(
|
|
82
|
-
triggers: S.optional(S.mutable(S.array(
|
|
97
|
+
functions: S.optional(S.mutable(S.array(omitEchoId(FunctionDef)))),
|
|
98
|
+
triggers: S.optional(S.mutable(S.array(omitEchoId(FunctionTrigger)))),
|
|
83
99
|
});
|
|
84
100
|
|
|
85
101
|
export type FunctionManifest = S.Schema.Type<typeof FunctionManifestSchema>;
|