@confect/core 1.0.0-next.3 → 1.0.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/Ref.ts CHANGED
@@ -1,14 +1,15 @@
1
- import type { FunctionType, FunctionVisibility } from "convex/server";
1
+ import type { FunctionVisibility } from "convex/server";
2
2
  import type { Schema } from "effect";
3
3
  import type * as FunctionSpec from "./FunctionSpec";
4
+ import type * as RuntimeAndFunctionType from "./RuntimeAndFunctionType";
4
5
 
5
6
  export interface Ref<
6
- _FunctionType extends FunctionType,
7
+ _RuntimeAndFunctionType extends RuntimeAndFunctionType.RuntimeAndFunctionType,
7
8
  _FunctionVisibility extends FunctionVisibility,
8
9
  _Args extends Schema.Schema.AnyNoContext,
9
10
  _Returns extends Schema.Schema.AnyNoContext,
10
11
  > {
11
- readonly _FunctionType?: _FunctionType;
12
+ readonly _RuntimeAndFunctionType?: _RuntimeAndFunctionType;
12
13
  readonly _FunctionVisibility?: _FunctionVisibility;
13
14
  readonly _Args?: _Args;
14
15
  readonly _Returns?: _Returns;
@@ -21,60 +22,80 @@ export interface AnyInternal extends Ref<any, "internal", any, any> {}
21
22
  export interface AnyPublic extends Ref<any, "public", any, any> {}
22
23
 
23
24
  export interface AnyQuery extends Ref<
24
- "query",
25
+ RuntimeAndFunctionType.AnyQuery,
25
26
  FunctionVisibility,
26
27
  Schema.Schema.AnyNoContext,
27
28
  Schema.Schema.AnyNoContext
28
29
  > {}
29
30
 
30
31
  export interface AnyMutation extends Ref<
31
- "mutation",
32
+ RuntimeAndFunctionType.AnyMutation,
32
33
  FunctionVisibility,
33
34
  Schema.Schema.AnyNoContext,
34
35
  Schema.Schema.AnyNoContext
35
36
  > {}
36
37
 
37
38
  export interface AnyAction extends Ref<
38
- "action",
39
+ RuntimeAndFunctionType.AnyAction,
39
40
  FunctionVisibility,
40
41
  Schema.Schema.AnyNoContext,
41
42
  Schema.Schema.AnyNoContext
42
43
  > {}
43
44
 
44
45
  export interface AnyPublicQuery extends Ref<
45
- "query",
46
+ RuntimeAndFunctionType.AnyQuery,
46
47
  "public",
47
48
  Schema.Schema.AnyNoContext,
48
49
  Schema.Schema.AnyNoContext
49
50
  > {}
50
51
 
51
52
  export interface AnyPublicMutation extends Ref<
52
- "mutation",
53
+ RuntimeAndFunctionType.AnyMutation,
53
54
  "public",
54
55
  Schema.Schema.AnyNoContext,
55
56
  Schema.Schema.AnyNoContext
56
57
  > {}
57
58
 
58
59
  export interface AnyPublicAction extends Ref<
59
- "action",
60
+ RuntimeAndFunctionType.AnyAction,
60
61
  "public",
61
62
  Schema.Schema.AnyNoContext,
62
63
  Schema.Schema.AnyNoContext
63
64
  > {}
64
65
 
66
+ export type GetRuntimeAndFunctionType<Ref_> =
67
+ Ref_ extends Ref<
68
+ infer RuntimeAndFunctionType_,
69
+ infer _FunctionVisibility,
70
+ infer _Args,
71
+ infer _Returns
72
+ >
73
+ ? RuntimeAndFunctionType_
74
+ : never;
75
+
76
+ export type GetRuntime<Ref_> =
77
+ Ref_ extends Ref<
78
+ infer RuntimeAndFunctionType_,
79
+ infer _FunctionVisibility,
80
+ infer _Args,
81
+ infer _Returns
82
+ >
83
+ ? RuntimeAndFunctionType.GetRuntime<RuntimeAndFunctionType_>
84
+ : never;
85
+
65
86
  export type GetFunctionType<Ref_> =
66
87
  Ref_ extends Ref<
67
- infer FunctionType_,
88
+ infer RuntimeAndFunctionType_,
68
89
  infer _FunctionVisibility,
69
90
  infer _Args,
70
91
  infer _Returns
71
92
  >
72
- ? FunctionType_
93
+ ? RuntimeAndFunctionType.GetFunctionType<RuntimeAndFunctionType_>
73
94
  : never;
74
95
 
75
96
  export type GetFunctionVisibility<Ref_> =
76
97
  Ref_ extends Ref<
77
- infer _FunctionType,
98
+ infer _RuntimeAndFunctionType,
78
99
  infer FunctionVisibility_,
79
100
  infer _Args,
80
101
  infer _Returns
@@ -84,7 +105,7 @@ export type GetFunctionVisibility<Ref_> =
84
105
 
85
106
  export type Args<Ref_> =
86
107
  Ref_ extends Ref<
87
- infer _FunctionType,
108
+ infer _RuntimeAndFunctionType,
88
109
  infer _FunctionVisibility,
89
110
  infer Args_,
90
111
  infer _Returns
@@ -94,7 +115,7 @@ export type Args<Ref_> =
94
115
 
95
116
  export type Returns<Ref_> =
96
117
  Ref_ extends Ref<
97
- infer _FunctionType,
118
+ infer _RuntimeAndFunctionType,
98
119
  infer _FunctionVisibility,
99
120
  infer _Args,
100
121
  infer Returns_
@@ -103,14 +124,14 @@ export type Returns<Ref_> =
103
124
  : never;
104
125
 
105
126
  export type FromFunctionSpec<F extends FunctionSpec.AnyWithProps> = Ref<
106
- FunctionSpec.GetFunctionType<F>,
127
+ FunctionSpec.GetRuntimeAndFunctionType<F>,
107
128
  FunctionSpec.GetFunctionVisibility<F>,
108
129
  FunctionSpec.Args<F>,
109
130
  FunctionSpec.Returns<F>
110
131
  >;
111
132
 
112
133
  export const make = <
113
- FunctionType_ extends FunctionType,
134
+ RuntimeAndFunctionType_ extends RuntimeAndFunctionType.RuntimeAndFunctionType,
114
135
  FunctionVisibility_ extends FunctionVisibility,
115
136
  Args_ extends Schema.Schema.AnyNoContext,
116
137
  Returns_ extends Schema.Schema.AnyNoContext,
@@ -120,22 +141,22 @@ export const make = <
120
141
  */
121
142
  convexFunctionName: string,
122
143
  function_: FunctionSpec.FunctionSpec<
123
- FunctionType_,
144
+ RuntimeAndFunctionType_,
124
145
  FunctionVisibility_,
125
146
  string,
126
147
  Args_,
127
148
  Returns_
128
149
  >,
129
- ): Ref<FunctionType_, FunctionVisibility_, Args_, Returns_> =>
150
+ ): Ref<RuntimeAndFunctionType_, FunctionVisibility_, Args_, Returns_> =>
130
151
  ({
131
152
  [HiddenFunctionKey]: function_,
132
153
  [HiddenConvexFunctionNameKey]: convexFunctionName,
133
- }) as Ref<FunctionType_, FunctionVisibility_, Args_, Returns_>;
154
+ }) as Ref<RuntimeAndFunctionType_, FunctionVisibility_, Args_, Returns_>;
134
155
 
135
156
  const HiddenFunctionKey = "@confect/core/api/HiddenFunctionKey";
136
157
  type HiddenFunctionKey = typeof HiddenFunctionKey;
137
158
  type HiddenFunction<Ref_ extends Any> = FunctionSpec.FunctionSpec<
138
- GetFunctionType<Ref_>,
159
+ GetRuntimeAndFunctionType<Ref_>,
139
160
  GetFunctionVisibility<Ref_>,
140
161
  string,
141
162
  Args<Ref_>,
@@ -143,11 +164,16 @@ type HiddenFunction<Ref_ extends Any> = FunctionSpec.FunctionSpec<
143
164
  >;
144
165
 
145
166
  export const getFunction = <
146
- FunctionType_ extends FunctionType,
167
+ RuntimeAndFunctionType_ extends RuntimeAndFunctionType.RuntimeAndFunctionType,
147
168
  FunctionVisibility_ extends FunctionVisibility,
148
169
  Args_ extends Schema.Schema.AnyNoContext,
149
170
  Returns_ extends Schema.Schema.AnyNoContext,
150
- Ref_ extends Ref<FunctionType_, FunctionVisibility_, Args_, Returns_>,
171
+ Ref_ extends Ref<
172
+ RuntimeAndFunctionType_,
173
+ FunctionVisibility_,
174
+ Args_,
175
+ Returns_
176
+ >,
151
177
  >(
152
178
  ref: Ref_,
153
179
  ): HiddenFunction<Ref_> => (ref as any)[HiddenFunctionKey];
@@ -158,10 +184,10 @@ type HiddenConvexFunctionNameKey = typeof HiddenConvexFunctionNameKey;
158
184
  type HiddenConvexFunctionName = string;
159
185
 
160
186
  export const getConvexFunctionName = <
161
- FunctionType_ extends FunctionType,
187
+ RuntimeAndFunctionType_ extends RuntimeAndFunctionType.RuntimeAndFunctionType,
162
188
  FunctionVisibility_ extends FunctionVisibility,
163
189
  Args_ extends Schema.Schema.AnyNoContext,
164
190
  Returns_ extends Schema.Schema.AnyNoContext,
165
191
  >(
166
- ref: Ref<FunctionType_, FunctionVisibility_, Args_, Returns_>,
192
+ ref: Ref<RuntimeAndFunctionType_, FunctionVisibility_, Args_, Returns_>,
167
193
  ): HiddenConvexFunctionName => (ref as any)[HiddenConvexFunctionNameKey];
package/src/Refs.ts CHANGED
@@ -1,14 +1,30 @@
1
1
  import type { Types } from "effect";
2
- import { pipe, Record } from "effect";
2
+ import { Array, pipe, Record } from "effect";
3
3
  import type * as FunctionSpec from "./FunctionSpec";
4
- import type * as GroupSpec from "./GroupSpec";
4
+ import * as GroupSpec from "./GroupSpec";
5
5
  import * as Ref from "./Ref";
6
6
  import type * as Spec from "./Spec";
7
7
 
8
8
  export type Refs<
9
- Spec_ extends Spec.AnyWithProps,
9
+ ConvexSpec extends Spec.AnyWithPropsWithRuntime<"Convex">,
10
+ NodeSpec extends Spec.AnyWithPropsWithRuntime<"Node"> = never,
10
11
  Predicate extends Ref.Any = Ref.Any,
11
- > = Types.Simplify<OmitEmpty<Helper<Spec.Groups<Spec_>, Predicate>>>;
12
+ > = Types.Simplify<
13
+ OmitEmpty<
14
+ Helper<
15
+ | Spec.Groups<ConvexSpec>
16
+ | (NodeSpec extends never
17
+ ? never
18
+ : GroupSpec.GroupSpec<
19
+ "Node",
20
+ "node",
21
+ never,
22
+ NodeSpec["groups"][keyof NodeSpec["groups"]]
23
+ >),
24
+ Predicate
25
+ >
26
+ >
27
+ >;
12
28
 
13
29
  type GroupRefs<
14
30
  Group extends GroupSpec.AnyWithProps,
@@ -57,16 +73,31 @@ type Any =
57
73
  }
58
74
  | Ref.Any;
59
75
 
60
- export const make = <Spec_ extends Spec.AnyWithProps>(
61
- spec: Spec_,
76
+ export const make = <
77
+ ConvexSpec extends Spec.AnyWithPropsWithRuntime<"Convex">,
78
+ NodeSpec extends Spec.AnyWithPropsWithRuntime<"Node"> = never,
79
+ >(
80
+ convexSpec: ConvexSpec,
81
+ nodeSpec?: NodeSpec,
62
82
  ): {
63
- public: Refs<Spec_, Ref.AnyPublic>;
64
- internal: Refs<Spec_, Ref.AnyInternal>;
83
+ public: Refs<ConvexSpec, NodeSpec, Ref.AnyPublic>;
84
+ internal: Refs<ConvexSpec, NodeSpec, Ref.AnyInternal>;
65
85
  } => {
66
- const refs = makeHelper(spec.groups);
86
+ const nodeGroup = nodeSpec
87
+ ? Array.reduce(
88
+ Record.values(nodeSpec.groups),
89
+ GroupSpec.makeNode("node"),
90
+ (nodeGroupSpec, group) => nodeGroupSpec.addGroup(group),
91
+ )
92
+ : null;
93
+
94
+ const groups = nodeGroup
95
+ ? { ...convexSpec.groups, node: nodeGroup }
96
+ : convexSpec.groups;
97
+ const refs = makeHelper(groups);
67
98
  return {
68
- public: refs as Refs<Spec_, Ref.AnyPublic>,
69
- internal: refs as Refs<Spec_, Ref.AnyInternal>,
99
+ public: refs as Refs<ConvexSpec, NodeSpec, Ref.AnyPublic>,
100
+ internal: refs as Refs<ConvexSpec, NodeSpec, Ref.AnyInternal>,
70
101
  };
71
102
  };
72
103
 
@@ -0,0 +1,57 @@
1
+ export type RuntimeAndFunctionType =
2
+ | { readonly runtime: "Convex"; readonly functionType: "query" }
3
+ | { readonly runtime: "Convex"; readonly functionType: "mutation" }
4
+ | { readonly runtime: "Convex"; readonly functionType: "action" }
5
+ | { readonly runtime: "Node"; readonly functionType: "action" };
6
+
7
+ export type Runtime = RuntimeAndFunctionType["runtime"];
8
+
9
+ const make = <
10
+ Runtime_ extends Runtime,
11
+ FunctionType_ extends GetFunctionType<WithRuntime<Runtime_>>,
12
+ >(
13
+ runtime: Runtime_,
14
+ functionType: FunctionType_,
15
+ ): { readonly runtime: Runtime_; readonly functionType: FunctionType_ } => ({
16
+ runtime,
17
+ functionType,
18
+ });
19
+
20
+ export type AnyQuery = Extract<
21
+ RuntimeAndFunctionType,
22
+ { readonly functionType: "query" }
23
+ >;
24
+
25
+ export type AnyMutation = Extract<
26
+ RuntimeAndFunctionType,
27
+ { readonly functionType: "mutation" }
28
+ >;
29
+
30
+ export type AnyAction = Extract<
31
+ RuntimeAndFunctionType,
32
+ { readonly functionType: "action" }
33
+ >;
34
+
35
+ export type WithRuntime<Runtime_ extends Runtime> = Extract<
36
+ RuntimeAndFunctionType,
37
+ { readonly runtime: Runtime_ }
38
+ >;
39
+
40
+ export type GetRuntime<RuntimeAndFunctionType_ extends RuntimeAndFunctionType> =
41
+ RuntimeAndFunctionType_["runtime"];
42
+
43
+ export type GetFunctionType<
44
+ RuntimeAndFunctionType_ extends RuntimeAndFunctionType,
45
+ > = RuntimeAndFunctionType_["functionType"];
46
+
47
+ export const ConvexQuery = make("Convex", "query");
48
+ export type ConvexQuery = typeof ConvexQuery;
49
+
50
+ export const ConvexMutation = make("Convex", "mutation");
51
+ export type ConvexMutation = typeof ConvexMutation;
52
+
53
+ export const ConvexAction = make("Convex", "action");
54
+ export type ConvexAction = typeof ConvexAction;
55
+
56
+ export const NodeAction = make("Node", "action");
57
+ export type NodeAction = typeof NodeAction;
package/src/Spec.ts CHANGED
@@ -1,14 +1,31 @@
1
- import { Predicate, Record } from "effect";
2
- import type * as GroupSpec from "./GroupSpec";
1
+ import { Array, Predicate, Record } from "effect";
2
+ import * as GroupSpec from "./GroupSpec";
3
+ import type * as RuntimeAndFunctionType from "./RuntimeAndFunctionType";
3
4
 
4
- export const TypeId = "@confect/core/api/Spec";
5
+ export const TypeId = "@confect/core/Spec";
5
6
  export type TypeId = typeof TypeId;
6
7
 
7
8
  export const isSpec = (u: unknown): u is AnyWithProps =>
8
9
  Predicate.hasProperty(u, TypeId);
9
10
 
10
- export interface Spec<Groups_ extends GroupSpec.AnyWithProps = never> {
11
+ export const isConvexSpec = (
12
+ u: unknown,
13
+ ): u is AnyWithPropsWithRuntime<"Convex"> =>
14
+ Predicate.hasProperty(u, TypeId) &&
15
+ Predicate.hasProperty(u, "runtime") &&
16
+ u.runtime === "Convex";
17
+
18
+ export const isNodeSpec = (u: unknown): u is AnyWithPropsWithRuntime<"Node"> =>
19
+ Predicate.hasProperty(u, TypeId) &&
20
+ Predicate.hasProperty(u, "runtime") &&
21
+ u.runtime === "Node";
22
+
23
+ export interface Spec<
24
+ Runtime extends RuntimeAndFunctionType.Runtime,
25
+ Groups_ extends GroupSpec.AnyWithPropsWithRuntime<Runtime> = never,
26
+ > {
11
27
  readonly [TypeId]: TypeId;
28
+ readonly runtime: Runtime;
12
29
  readonly groups: {
13
30
  [GroupName in GroupSpec.Name<Groups_>]: GroupSpec.WithName<
14
31
  Groups_,
@@ -16,16 +33,23 @@ export interface Spec<Groups_ extends GroupSpec.AnyWithProps = never> {
16
33
  >;
17
34
  };
18
35
 
19
- add<Group extends GroupSpec.AnyWithProps>(
36
+ add<Group extends GroupSpec.AnyWithPropsWithRuntime<Runtime>>(
20
37
  group: Group,
21
- ): Spec<Groups_ | Group>;
38
+ ): Spec<Runtime, Groups_ | Group>;
22
39
  }
23
40
 
24
41
  export interface Any {
25
42
  readonly [TypeId]: TypeId;
26
43
  }
27
44
 
28
- export interface AnyWithProps extends Spec<GroupSpec.AnyWithProps> {}
45
+ export interface AnyWithProps extends Spec<
46
+ RuntimeAndFunctionType.Runtime,
47
+ GroupSpec.AnyWithProps
48
+ > {}
49
+
50
+ export interface AnyWithPropsWithRuntime<
51
+ Runtime extends RuntimeAndFunctionType.Runtime,
52
+ > extends Spec<Runtime, GroupSpec.AnyWithPropsWithRuntime<Runtime>> {}
29
53
 
30
54
  export type Groups<Spec_ extends AnyWithProps> =
31
55
  Spec_["groups"][keyof Spec_["groups"]];
@@ -35,18 +59,59 @@ const Proto = {
35
59
 
36
60
  add<Group extends GroupSpec.AnyWithProps>(this: AnyWithProps, group: Group) {
37
61
  return makeProto({
62
+ runtime: this.runtime,
38
63
  groups: Record.set(this.groups, group.name, group),
39
64
  });
40
65
  },
41
66
  };
42
67
 
43
- const makeProto = <Groups_ extends GroupSpec.AnyWithProps>({
68
+ const makeProto = <
69
+ Runtime extends RuntimeAndFunctionType.Runtime,
70
+ Groups_ extends GroupSpec.AnyWithPropsWithRuntime<Runtime>,
71
+ >({
72
+ runtime,
44
73
  groups,
45
74
  }: {
75
+ runtime: Runtime;
46
76
  groups: Record.ReadonlyRecord<string, Groups_>;
47
- }): Spec<Groups_> =>
77
+ }): Spec<Runtime, Groups_> =>
48
78
  Object.assign(Object.create(Proto), {
79
+ runtime,
49
80
  groups,
50
81
  });
51
82
 
52
- export const make = (): Spec => makeProto({ groups: {} });
83
+ export const make = (): Spec<"Convex"> =>
84
+ makeProto({ runtime: "Convex", groups: {} });
85
+
86
+ export const makeNode = (): Spec<"Node"> =>
87
+ makeProto({ runtime: "Node", groups: {} });
88
+
89
+ /**
90
+ * Merges a Convex spec with an optional Node spec for use with `Api.make`.
91
+ * When `nodeSpec` is provided, its groups are merged under a "node" namespace,
92
+ * mirroring the structure used by `Refs.make`.
93
+ */
94
+ export const merge = <
95
+ ConvexSpec extends AnyWithPropsWithRuntime<"Convex">,
96
+ NodeSpec extends AnyWithPropsWithRuntime<"Node">,
97
+ >(
98
+ convexSpec: ConvexSpec,
99
+ nodeSpec?: NodeSpec,
100
+ ): AnyWithProps => {
101
+ const nodeGroup = nodeSpec
102
+ ? Array.reduce(
103
+ Record.values(nodeSpec.groups),
104
+ GroupSpec.makeNode("node"),
105
+ (nodeGroupSpec, group) => nodeGroupSpec.addGroup(group),
106
+ )
107
+ : null;
108
+
109
+ const groups = nodeGroup
110
+ ? { ...convexSpec.groups, node: nodeGroup }
111
+ : convexSpec.groups;
112
+
113
+ return Object.assign(Object.create(Proto), {
114
+ runtime: "Convex" as const,
115
+ groups,
116
+ }) as AnyWithProps;
117
+ };
package/src/index.ts CHANGED
@@ -5,6 +5,7 @@ export * as GroupSpec from "./GroupSpec";
5
5
  export * as PaginationResult from "./PaginationResult";
6
6
  export * as Ref from "./Ref";
7
7
  export * as Refs from "./Refs";
8
+ export * as RuntimeAndFunctionType from "./RuntimeAndFunctionType";
8
9
  export * as Spec from "./Spec";
9
10
  export * as SystemFields from "./SystemFields";
10
11
  export * as Types from "./Types";