@live-state/sync 0.0.4-beta.1 → 0.0.4-beta.10
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/chunk-E7NFSHVD.js +1 -0
- package/dist/chunk-GUSCESQZ.js +1 -0
- package/dist/client.d.ts +3 -1
- package/dist/client.js +1 -1
- package/dist/fetch-client.d.ts +4 -13
- package/dist/fetch-client.js +1 -1
- package/dist/{index-C1FzxdnB.d.ts → index-C5AAiO4g.d.ts} +143 -72
- package/dist/index-COp5Ib0a.d.cts +282 -0
- package/dist/index-COp5Ib0a.d.ts +282 -0
- package/dist/index.cjs +1 -1
- package/dist/index.d.cts +1 -236
- package/dist/index.d.ts +1 -236
- package/dist/index.js +1 -1
- package/dist/server.cjs +2 -2
- package/dist/server.d.cts +113 -59
- package/dist/server.d.ts +113 -59
- package/dist/server.js +2 -2
- package/package.json +1 -1
- package/dist/chunk-G5SVBG4B.js +0 -1
- package/dist/chunk-LLHCJUB6.js +0 -1
package/dist/server.d.cts
CHANGED
|
@@ -1,27 +1,49 @@
|
|
|
1
|
-
import { z
|
|
2
|
-
import { LiveObjectAny,
|
|
1
|
+
import { z } from 'zod';
|
|
2
|
+
import { L as LiveObjectAny, W as WhereClause, S as Simplify, I as InferLiveObjectWithRelationalIds, M as MaterializedLiveType, a as Schema, b as IncludeClause, c as InferLiveObject, d as InferInsert, e as InferUpdate, f as Logger, g as LogLevel } from './index-COp5Ib0a.cjs';
|
|
3
|
+
import * as z3 from 'zod/v3';
|
|
4
|
+
import * as z4 from 'zod/v4/core';
|
|
3
5
|
import { PostgresPool } from 'kysely';
|
|
4
6
|
import { Application } from 'express-ws';
|
|
5
7
|
|
|
6
|
-
declare const
|
|
7
|
-
id: z.ZodOptional<z.ZodString>;
|
|
8
|
-
type: z.ZodLiteral<"MUTATE">;
|
|
8
|
+
declare const querySchema: z.ZodObject<{
|
|
9
9
|
resource: z.ZodString;
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
10
|
+
where: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodAny>>;
|
|
11
|
+
include: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodAny>>;
|
|
12
|
+
lastSyncedAt: z.ZodOptional<z.ZodString>;
|
|
13
|
+
limit: z.ZodOptional<z.ZodCoercedNumber<unknown>>;
|
|
14
|
+
sort: z.ZodOptional<z.ZodArray<z.ZodObject<{
|
|
15
|
+
key: z.ZodString;
|
|
16
|
+
direction: z.ZodEnum<{
|
|
17
|
+
asc: "asc";
|
|
18
|
+
desc: "desc";
|
|
19
|
+
}>;
|
|
20
|
+
}, z.core.$strip>>>;
|
|
21
|
+
}, z.core.$strip>;
|
|
22
|
+
type RawQueryRequest = z.infer<typeof querySchema>;
|
|
23
|
+
declare const defaultMutationSchema: z.ZodObject<{
|
|
13
24
|
id: z.ZodOptional<z.ZodString>;
|
|
14
25
|
type: z.ZodLiteral<"MUTATE">;
|
|
15
26
|
resource: z.ZodString;
|
|
16
|
-
resourceId: z.ZodString
|
|
27
|
+
resourceId: z.ZodOptional<z.ZodString>;
|
|
28
|
+
procedure: z.ZodEnum<{
|
|
29
|
+
INSERT: "INSERT";
|
|
30
|
+
UPDATE: "UPDATE";
|
|
31
|
+
}>;
|
|
17
32
|
payload: z.ZodRecord<z.ZodString, z.ZodObject<{
|
|
18
33
|
value: z.ZodNullable<z.ZodUnion<[z.ZodUnion<[z.ZodUnion<[z.ZodString, z.ZodNumber]>, z.ZodBoolean]>, z.ZodDate]>>;
|
|
19
34
|
_meta: z.ZodOptional<z.ZodObject<{
|
|
20
35
|
timestamp: z.ZodNullable<z.ZodOptional<z.ZodString>>;
|
|
21
36
|
}, z.core.$strip>>;
|
|
22
37
|
}, z.core.$strip>>;
|
|
23
|
-
}, z.core.$strip
|
|
24
|
-
type
|
|
38
|
+
}, z.core.$strip>;
|
|
39
|
+
type DefaultMutation = Omit<z.infer<typeof defaultMutationSchema>, "resourceId"> & {
|
|
40
|
+
resourceId: string;
|
|
41
|
+
};
|
|
42
|
+
|
|
43
|
+
type Awaitable<T> = T | Promise<T>;
|
|
44
|
+
|
|
45
|
+
/** biome-ignore-all lint/suspicious/noExplicitAny: false positive */
|
|
46
|
+
/** biome-ignore-all lint/style/noNonNullAssertion: false positive */
|
|
25
47
|
|
|
26
48
|
type RouteRecord = Record<string, AnyRoute>;
|
|
27
49
|
declare class Router<TRoutes extends RouteRecord> {
|
|
@@ -31,11 +53,11 @@ declare class Router<TRoutes extends RouteRecord> {
|
|
|
31
53
|
routes: TRoutes;
|
|
32
54
|
}): Router<TRoutes>;
|
|
33
55
|
}
|
|
34
|
-
declare const router: <TSchema extends Schema<any>, TRoutes extends Record<keyof TSchema,
|
|
56
|
+
declare const router: <TSchema extends Schema<any>, TRoutes extends Record<keyof TSchema, Route<any, any, any>>>(opts: {
|
|
35
57
|
schema: TSchema;
|
|
36
58
|
routes: TRoutes;
|
|
37
59
|
}) => Router<TRoutes>;
|
|
38
|
-
type AnyRouter = Router<
|
|
60
|
+
type AnyRouter = Router<any>;
|
|
39
61
|
type QueryResult<TShape extends LiveObjectAny> = {
|
|
40
62
|
data: Record<string, MaterializedLiveType<TShape>>;
|
|
41
63
|
};
|
|
@@ -43,51 +65,55 @@ type MutationResult<TShape extends LiveObjectAny> = {
|
|
|
43
65
|
data: MaterializedLiveType<TShape>;
|
|
44
66
|
acceptedValues: Record<string, any> | null;
|
|
45
67
|
};
|
|
46
|
-
type
|
|
47
|
-
|
|
48
|
-
db: Storage;
|
|
49
|
-
schema: TSchema;
|
|
50
|
-
}) => Promise<TResult>;
|
|
51
|
-
type Mutation<TInputValidator extends ZodTypeAny, // TODO use StandardSchema instead
|
|
52
|
-
THandler extends RequestHandler<z.infer<TInputValidator>, any, any>> = {
|
|
68
|
+
type Mutation<TInputValidator extends z3.ZodTypeAny | z4.$ZodType, // TODO use StandardSchema instead
|
|
69
|
+
TOutput> = {
|
|
53
70
|
inputValidator: TInputValidator;
|
|
54
|
-
handler:
|
|
71
|
+
handler: (opts: {
|
|
72
|
+
req: MutationRequest<z.infer<TInputValidator>>;
|
|
73
|
+
db: Storage;
|
|
74
|
+
}) => TOutput;
|
|
55
75
|
};
|
|
56
|
-
declare const mutationCreator: <TInputValidator extends ZodTypeAny>(validator?: TInputValidator) => {
|
|
57
|
-
handler: <THandler extends
|
|
76
|
+
declare const mutationCreator: <TInputValidator extends z3.ZodTypeAny | z4.$ZodType>(validator?: TInputValidator) => {
|
|
77
|
+
handler: <THandler extends Mutation<TInputValidator, any>["handler"]>(handler: THandler) => Mutation<TInputValidator, ReturnType<THandler>>;
|
|
58
78
|
};
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
79
|
+
type ReadAuthorizationHandler<TShape extends LiveObjectAny> = (opts: {
|
|
80
|
+
ctx: BaseRequest["context"];
|
|
81
|
+
}) => WhereClause<TShape> | boolean;
|
|
82
|
+
type MutationAuthorizationHandler<TShape extends LiveObjectAny> = (opts: {
|
|
83
|
+
ctx: BaseRequest["context"];
|
|
84
|
+
value: Simplify<InferLiveObjectWithRelationalIds<TShape>>;
|
|
85
|
+
}) => WhereClause<TShape> | boolean;
|
|
86
|
+
type Authorization<TShape extends LiveObjectAny> = {
|
|
87
|
+
read?: ReadAuthorizationHandler<TShape>;
|
|
88
|
+
insert?: MutationAuthorizationHandler<TShape>;
|
|
89
|
+
update?: {
|
|
90
|
+
preMutation?: MutationAuthorizationHandler<TShape>;
|
|
91
|
+
postMutation?: MutationAuthorizationHandler<TShape>;
|
|
92
|
+
};
|
|
93
|
+
};
|
|
94
|
+
declare class Route<TResourceSchema extends LiveObjectAny, TMiddleware extends Middleware<any>, TCustomMutations extends Record<string, Mutation<any, any>>> {
|
|
95
|
+
readonly resourceSchema: TResourceSchema;
|
|
62
96
|
readonly middlewares: Set<TMiddleware>;
|
|
63
97
|
readonly customMutations: TCustomMutations;
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
private handleSet;
|
|
67
|
-
handleRequest(opts: {
|
|
68
|
-
req: ParsedRequest;
|
|
69
|
-
db: Storage;
|
|
70
|
-
schema: Schema<any>;
|
|
71
|
-
}): Promise<any>;
|
|
98
|
+
readonly authorization?: Authorization<TResourceSchema>;
|
|
99
|
+
constructor(resourceSchema: TResourceSchema, customMutations?: TCustomMutations, authorization?: Authorization<TResourceSchema>);
|
|
72
100
|
use(...middlewares: TMiddleware[]): this;
|
|
73
|
-
withMutations<T extends Record<string, Mutation<any,
|
|
101
|
+
withMutations<T extends Record<string, Mutation<any, any>>>(mutationFactory: (opts: {
|
|
74
102
|
mutation: typeof mutationCreator;
|
|
75
103
|
}) => T): Route<TResourceSchema, TMiddleware, T>;
|
|
104
|
+
private handleSet;
|
|
105
|
+
private wrapInMiddlewares;
|
|
76
106
|
}
|
|
77
107
|
declare class RouteFactory {
|
|
78
108
|
private middlewares;
|
|
79
109
|
private constructor();
|
|
80
|
-
|
|
110
|
+
collectionRoute<T extends LiveObjectAny>(shape: T, authorization?: Authorization<T>): Route<T, Middleware<any>, Record<string, never>>;
|
|
81
111
|
use(...middlewares: Middleware<any>[]): RouteFactory;
|
|
82
112
|
static create(): RouteFactory;
|
|
83
113
|
}
|
|
84
114
|
declare const routeFactory: typeof RouteFactory.create;
|
|
85
115
|
type AnyRoute = Route<LiveObjectAny, Middleware<any>, Record<string, any>>;
|
|
86
116
|
|
|
87
|
-
type Simplify<T> = T extends Record<string, unknown> ? {
|
|
88
|
-
[K in keyof T]: Simplify<T[K]>;
|
|
89
|
-
} : T;
|
|
90
|
-
|
|
91
117
|
/** biome-ignore-all lint/suspicious/noExplicitAny: false positive */
|
|
92
118
|
|
|
93
119
|
declare abstract class Storage {
|
|
@@ -98,8 +124,13 @@ declare abstract class Storage {
|
|
|
98
124
|
where?: WhereClause<T>;
|
|
99
125
|
include?: IncludeClause<T>;
|
|
100
126
|
}): Promise<Record<string, InferLiveObject<T>>>;
|
|
101
|
-
insert<T extends LiveObjectAny>(resource: T, value: Simplify<
|
|
102
|
-
update<T extends LiveObjectAny>(resource: T, resourceId: string, value:
|
|
127
|
+
insert<T extends LiveObjectAny>(resource: T, value: Simplify<InferInsert<T>>): Promise<InferLiveObject<T>>;
|
|
128
|
+
update<T extends LiveObjectAny>(resource: T, resourceId: string, value: InferUpdate<T>): Promise<InferLiveObject<T>>;
|
|
129
|
+
abstract transaction<T>(fn: (opts: {
|
|
130
|
+
trx: Storage;
|
|
131
|
+
commit: () => Promise<void>;
|
|
132
|
+
rollback: () => Promise<void>;
|
|
133
|
+
}) => Promise<T>): Promise<T>;
|
|
103
134
|
}
|
|
104
135
|
|
|
105
136
|
/** biome-ignore-all lint/suspicious/noExplicitAny: false positive */
|
|
@@ -107,6 +138,7 @@ declare abstract class Storage {
|
|
|
107
138
|
declare class SQLStorage extends Storage {
|
|
108
139
|
private db;
|
|
109
140
|
private schema?;
|
|
141
|
+
private logger?;
|
|
110
142
|
constructor(pool: PostgresPool);
|
|
111
143
|
findOne<T extends LiveObjectAny>(resource: T, id: string, options?: {
|
|
112
144
|
include?: IncludeClause<T>;
|
|
@@ -114,35 +146,51 @@ declare class SQLStorage extends Storage {
|
|
|
114
146
|
find<T extends LiveObjectAny>(resource: T, options?: {
|
|
115
147
|
where?: WhereClause<T>;
|
|
116
148
|
include?: IncludeClause<T>;
|
|
149
|
+
limit?: number;
|
|
150
|
+
sort?: {
|
|
151
|
+
key: string;
|
|
152
|
+
direction: "asc" | "desc";
|
|
153
|
+
}[];
|
|
117
154
|
}): Promise<Record<string, InferLiveObject<T>>>;
|
|
155
|
+
transaction<T>(fn: (opts: {
|
|
156
|
+
trx: Storage;
|
|
157
|
+
commit: () => Promise<void>;
|
|
158
|
+
rollback: () => Promise<void>;
|
|
159
|
+
}) => Promise<T>): Promise<T>;
|
|
118
160
|
private convertToMaterializedLiveType;
|
|
161
|
+
private isKyselyLike;
|
|
119
162
|
}
|
|
120
163
|
|
|
121
164
|
declare const expressAdapter: (app: Application, server: Server<AnyRouter>, options?: {
|
|
122
165
|
basePath?: string;
|
|
123
166
|
}) => void;
|
|
124
167
|
|
|
125
|
-
|
|
168
|
+
/** biome-ignore-all lint/suspicious/noExplicitAny: any's are actually used correctly */
|
|
169
|
+
|
|
170
|
+
interface BaseRequest {
|
|
126
171
|
headers: Record<string, string>;
|
|
127
172
|
cookies: Record<string, string>;
|
|
128
|
-
|
|
129
|
-
resourceName: string;
|
|
130
|
-
procedure?: string;
|
|
173
|
+
queryParams: Record<string, string>;
|
|
131
174
|
context: Record<string, any>;
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
type: "QUERY"
|
|
175
|
+
}
|
|
176
|
+
interface QueryRequest extends BaseRequest, RawQueryRequest {
|
|
177
|
+
type: "QUERY";
|
|
178
|
+
}
|
|
179
|
+
interface MutationRequest<TInput = any> extends BaseRequest {
|
|
180
|
+
type: "MUTATE";
|
|
181
|
+
input: TInput;
|
|
182
|
+
resource: string;
|
|
135
183
|
resourceId?: string;
|
|
136
|
-
|
|
137
|
-
}
|
|
138
|
-
type
|
|
184
|
+
procedure: string;
|
|
185
|
+
}
|
|
186
|
+
type Request = QueryRequest | MutationRequest;
|
|
187
|
+
type ContextProvider = (req: Omit<BaseRequest, "context"> & {
|
|
139
188
|
transport: "HTTP" | "WEBSOCKET";
|
|
140
189
|
}) => Record<string, any>;
|
|
141
|
-
type
|
|
142
|
-
type
|
|
143
|
-
type NextFunction<T> = (req: ParsedRequest) => Promise<T> | T;
|
|
190
|
+
type MutationHandler = (mutation: DefaultMutation) => void;
|
|
191
|
+
type NextFunction<O, R = Request> = (req: R) => Awaitable<O>;
|
|
144
192
|
type Middleware<T = any> = (opts: {
|
|
145
|
-
req:
|
|
193
|
+
req: Request;
|
|
146
194
|
next: NextFunction<T>;
|
|
147
195
|
}) => ReturnType<NextFunction<T>>;
|
|
148
196
|
declare class Server<TRouter extends AnyRouter> {
|
|
@@ -150,6 +198,7 @@ declare class Server<TRouter extends AnyRouter> {
|
|
|
150
198
|
readonly storage: Storage;
|
|
151
199
|
readonly schema: Schema<any>;
|
|
152
200
|
readonly middlewares: Set<Middleware<any>>;
|
|
201
|
+
readonly logger: Logger;
|
|
153
202
|
contextProvider?: ContextProvider;
|
|
154
203
|
private mutationSubscriptions;
|
|
155
204
|
private constructor();
|
|
@@ -159,14 +208,19 @@ declare class Server<TRouter extends AnyRouter> {
|
|
|
159
208
|
schema: Schema<any>;
|
|
160
209
|
middlewares?: Middleware<any>[];
|
|
161
210
|
contextProvider?: ContextProvider;
|
|
211
|
+
logLevel?: LogLevel;
|
|
162
212
|
}): Server<TRouter>;
|
|
163
213
|
subscribeToMutations(handler: MutationHandler): () => void;
|
|
164
|
-
|
|
165
|
-
req:
|
|
214
|
+
handleQuery(opts: {
|
|
215
|
+
req: QueryRequest;
|
|
216
|
+
}): Promise<QueryResult<any>>;
|
|
217
|
+
handleMutation(opts: {
|
|
218
|
+
req: MutationRequest;
|
|
166
219
|
}): Promise<any>;
|
|
167
220
|
use(middleware: Middleware<any>): this;
|
|
168
221
|
context(contextProvider: ContextProvider): this;
|
|
222
|
+
private wrapInMiddlewares;
|
|
169
223
|
}
|
|
170
224
|
declare const server: typeof Server.create;
|
|
171
225
|
|
|
172
|
-
export { type AnyRoute, type AnyRouter, type ContextProvider, type Middleware, type Mutation, type MutationHandler, type MutationResult, type NextFunction, type
|
|
226
|
+
export { type AnyRoute, type AnyRouter, type Authorization, type BaseRequest, type ContextProvider, type Middleware, type Mutation, type MutationAuthorizationHandler, type MutationHandler, type MutationRequest, type MutationResult, type NextFunction, type QueryRequest, type QueryResult, type ReadAuthorizationHandler, type Request, Route, RouteFactory, type RouteRecord, Router, SQLStorage, Server, Storage, expressAdapter, routeFactory, router, server };
|
package/dist/server.d.ts
CHANGED
|
@@ -1,27 +1,49 @@
|
|
|
1
|
-
import { z
|
|
2
|
-
import { LiveObjectAny,
|
|
1
|
+
import { z } from 'zod';
|
|
2
|
+
import { L as LiveObjectAny, W as WhereClause, S as Simplify, I as InferLiveObjectWithRelationalIds, M as MaterializedLiveType, a as Schema, b as IncludeClause, c as InferLiveObject, d as InferInsert, e as InferUpdate, f as Logger, g as LogLevel } from './index-COp5Ib0a.js';
|
|
3
|
+
import * as z3 from 'zod/v3';
|
|
4
|
+
import * as z4 from 'zod/v4/core';
|
|
3
5
|
import { PostgresPool } from 'kysely';
|
|
4
6
|
import { Application } from 'express-ws';
|
|
5
7
|
|
|
6
|
-
declare const
|
|
7
|
-
id: z.ZodOptional<z.ZodString>;
|
|
8
|
-
type: z.ZodLiteral<"MUTATE">;
|
|
8
|
+
declare const querySchema: z.ZodObject<{
|
|
9
9
|
resource: z.ZodString;
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
10
|
+
where: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodAny>>;
|
|
11
|
+
include: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodAny>>;
|
|
12
|
+
lastSyncedAt: z.ZodOptional<z.ZodString>;
|
|
13
|
+
limit: z.ZodOptional<z.ZodCoercedNumber<unknown>>;
|
|
14
|
+
sort: z.ZodOptional<z.ZodArray<z.ZodObject<{
|
|
15
|
+
key: z.ZodString;
|
|
16
|
+
direction: z.ZodEnum<{
|
|
17
|
+
asc: "asc";
|
|
18
|
+
desc: "desc";
|
|
19
|
+
}>;
|
|
20
|
+
}, z.core.$strip>>>;
|
|
21
|
+
}, z.core.$strip>;
|
|
22
|
+
type RawQueryRequest = z.infer<typeof querySchema>;
|
|
23
|
+
declare const defaultMutationSchema: z.ZodObject<{
|
|
13
24
|
id: z.ZodOptional<z.ZodString>;
|
|
14
25
|
type: z.ZodLiteral<"MUTATE">;
|
|
15
26
|
resource: z.ZodString;
|
|
16
|
-
resourceId: z.ZodString
|
|
27
|
+
resourceId: z.ZodOptional<z.ZodString>;
|
|
28
|
+
procedure: z.ZodEnum<{
|
|
29
|
+
INSERT: "INSERT";
|
|
30
|
+
UPDATE: "UPDATE";
|
|
31
|
+
}>;
|
|
17
32
|
payload: z.ZodRecord<z.ZodString, z.ZodObject<{
|
|
18
33
|
value: z.ZodNullable<z.ZodUnion<[z.ZodUnion<[z.ZodUnion<[z.ZodString, z.ZodNumber]>, z.ZodBoolean]>, z.ZodDate]>>;
|
|
19
34
|
_meta: z.ZodOptional<z.ZodObject<{
|
|
20
35
|
timestamp: z.ZodNullable<z.ZodOptional<z.ZodString>>;
|
|
21
36
|
}, z.core.$strip>>;
|
|
22
37
|
}, z.core.$strip>>;
|
|
23
|
-
}, z.core.$strip
|
|
24
|
-
type
|
|
38
|
+
}, z.core.$strip>;
|
|
39
|
+
type DefaultMutation = Omit<z.infer<typeof defaultMutationSchema>, "resourceId"> & {
|
|
40
|
+
resourceId: string;
|
|
41
|
+
};
|
|
42
|
+
|
|
43
|
+
type Awaitable<T> = T | Promise<T>;
|
|
44
|
+
|
|
45
|
+
/** biome-ignore-all lint/suspicious/noExplicitAny: false positive */
|
|
46
|
+
/** biome-ignore-all lint/style/noNonNullAssertion: false positive */
|
|
25
47
|
|
|
26
48
|
type RouteRecord = Record<string, AnyRoute>;
|
|
27
49
|
declare class Router<TRoutes extends RouteRecord> {
|
|
@@ -31,11 +53,11 @@ declare class Router<TRoutes extends RouteRecord> {
|
|
|
31
53
|
routes: TRoutes;
|
|
32
54
|
}): Router<TRoutes>;
|
|
33
55
|
}
|
|
34
|
-
declare const router: <TSchema extends Schema<any>, TRoutes extends Record<keyof TSchema,
|
|
56
|
+
declare const router: <TSchema extends Schema<any>, TRoutes extends Record<keyof TSchema, Route<any, any, any>>>(opts: {
|
|
35
57
|
schema: TSchema;
|
|
36
58
|
routes: TRoutes;
|
|
37
59
|
}) => Router<TRoutes>;
|
|
38
|
-
type AnyRouter = Router<
|
|
60
|
+
type AnyRouter = Router<any>;
|
|
39
61
|
type QueryResult<TShape extends LiveObjectAny> = {
|
|
40
62
|
data: Record<string, MaterializedLiveType<TShape>>;
|
|
41
63
|
};
|
|
@@ -43,51 +65,55 @@ type MutationResult<TShape extends LiveObjectAny> = {
|
|
|
43
65
|
data: MaterializedLiveType<TShape>;
|
|
44
66
|
acceptedValues: Record<string, any> | null;
|
|
45
67
|
};
|
|
46
|
-
type
|
|
47
|
-
|
|
48
|
-
db: Storage;
|
|
49
|
-
schema: TSchema;
|
|
50
|
-
}) => Promise<TResult>;
|
|
51
|
-
type Mutation<TInputValidator extends ZodTypeAny, // TODO use StandardSchema instead
|
|
52
|
-
THandler extends RequestHandler<z.infer<TInputValidator>, any, any>> = {
|
|
68
|
+
type Mutation<TInputValidator extends z3.ZodTypeAny | z4.$ZodType, // TODO use StandardSchema instead
|
|
69
|
+
TOutput> = {
|
|
53
70
|
inputValidator: TInputValidator;
|
|
54
|
-
handler:
|
|
71
|
+
handler: (opts: {
|
|
72
|
+
req: MutationRequest<z.infer<TInputValidator>>;
|
|
73
|
+
db: Storage;
|
|
74
|
+
}) => TOutput;
|
|
55
75
|
};
|
|
56
|
-
declare const mutationCreator: <TInputValidator extends ZodTypeAny>(validator?: TInputValidator) => {
|
|
57
|
-
handler: <THandler extends
|
|
76
|
+
declare const mutationCreator: <TInputValidator extends z3.ZodTypeAny | z4.$ZodType>(validator?: TInputValidator) => {
|
|
77
|
+
handler: <THandler extends Mutation<TInputValidator, any>["handler"]>(handler: THandler) => Mutation<TInputValidator, ReturnType<THandler>>;
|
|
58
78
|
};
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
79
|
+
type ReadAuthorizationHandler<TShape extends LiveObjectAny> = (opts: {
|
|
80
|
+
ctx: BaseRequest["context"];
|
|
81
|
+
}) => WhereClause<TShape> | boolean;
|
|
82
|
+
type MutationAuthorizationHandler<TShape extends LiveObjectAny> = (opts: {
|
|
83
|
+
ctx: BaseRequest["context"];
|
|
84
|
+
value: Simplify<InferLiveObjectWithRelationalIds<TShape>>;
|
|
85
|
+
}) => WhereClause<TShape> | boolean;
|
|
86
|
+
type Authorization<TShape extends LiveObjectAny> = {
|
|
87
|
+
read?: ReadAuthorizationHandler<TShape>;
|
|
88
|
+
insert?: MutationAuthorizationHandler<TShape>;
|
|
89
|
+
update?: {
|
|
90
|
+
preMutation?: MutationAuthorizationHandler<TShape>;
|
|
91
|
+
postMutation?: MutationAuthorizationHandler<TShape>;
|
|
92
|
+
};
|
|
93
|
+
};
|
|
94
|
+
declare class Route<TResourceSchema extends LiveObjectAny, TMiddleware extends Middleware<any>, TCustomMutations extends Record<string, Mutation<any, any>>> {
|
|
95
|
+
readonly resourceSchema: TResourceSchema;
|
|
62
96
|
readonly middlewares: Set<TMiddleware>;
|
|
63
97
|
readonly customMutations: TCustomMutations;
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
private handleSet;
|
|
67
|
-
handleRequest(opts: {
|
|
68
|
-
req: ParsedRequest;
|
|
69
|
-
db: Storage;
|
|
70
|
-
schema: Schema<any>;
|
|
71
|
-
}): Promise<any>;
|
|
98
|
+
readonly authorization?: Authorization<TResourceSchema>;
|
|
99
|
+
constructor(resourceSchema: TResourceSchema, customMutations?: TCustomMutations, authorization?: Authorization<TResourceSchema>);
|
|
72
100
|
use(...middlewares: TMiddleware[]): this;
|
|
73
|
-
withMutations<T extends Record<string, Mutation<any,
|
|
101
|
+
withMutations<T extends Record<string, Mutation<any, any>>>(mutationFactory: (opts: {
|
|
74
102
|
mutation: typeof mutationCreator;
|
|
75
103
|
}) => T): Route<TResourceSchema, TMiddleware, T>;
|
|
104
|
+
private handleSet;
|
|
105
|
+
private wrapInMiddlewares;
|
|
76
106
|
}
|
|
77
107
|
declare class RouteFactory {
|
|
78
108
|
private middlewares;
|
|
79
109
|
private constructor();
|
|
80
|
-
|
|
110
|
+
collectionRoute<T extends LiveObjectAny>(shape: T, authorization?: Authorization<T>): Route<T, Middleware<any>, Record<string, never>>;
|
|
81
111
|
use(...middlewares: Middleware<any>[]): RouteFactory;
|
|
82
112
|
static create(): RouteFactory;
|
|
83
113
|
}
|
|
84
114
|
declare const routeFactory: typeof RouteFactory.create;
|
|
85
115
|
type AnyRoute = Route<LiveObjectAny, Middleware<any>, Record<string, any>>;
|
|
86
116
|
|
|
87
|
-
type Simplify<T> = T extends Record<string, unknown> ? {
|
|
88
|
-
[K in keyof T]: Simplify<T[K]>;
|
|
89
|
-
} : T;
|
|
90
|
-
|
|
91
117
|
/** biome-ignore-all lint/suspicious/noExplicitAny: false positive */
|
|
92
118
|
|
|
93
119
|
declare abstract class Storage {
|
|
@@ -98,8 +124,13 @@ declare abstract class Storage {
|
|
|
98
124
|
where?: WhereClause<T>;
|
|
99
125
|
include?: IncludeClause<T>;
|
|
100
126
|
}): Promise<Record<string, InferLiveObject<T>>>;
|
|
101
|
-
insert<T extends LiveObjectAny>(resource: T, value: Simplify<
|
|
102
|
-
update<T extends LiveObjectAny>(resource: T, resourceId: string, value:
|
|
127
|
+
insert<T extends LiveObjectAny>(resource: T, value: Simplify<InferInsert<T>>): Promise<InferLiveObject<T>>;
|
|
128
|
+
update<T extends LiveObjectAny>(resource: T, resourceId: string, value: InferUpdate<T>): Promise<InferLiveObject<T>>;
|
|
129
|
+
abstract transaction<T>(fn: (opts: {
|
|
130
|
+
trx: Storage;
|
|
131
|
+
commit: () => Promise<void>;
|
|
132
|
+
rollback: () => Promise<void>;
|
|
133
|
+
}) => Promise<T>): Promise<T>;
|
|
103
134
|
}
|
|
104
135
|
|
|
105
136
|
/** biome-ignore-all lint/suspicious/noExplicitAny: false positive */
|
|
@@ -107,6 +138,7 @@ declare abstract class Storage {
|
|
|
107
138
|
declare class SQLStorage extends Storage {
|
|
108
139
|
private db;
|
|
109
140
|
private schema?;
|
|
141
|
+
private logger?;
|
|
110
142
|
constructor(pool: PostgresPool);
|
|
111
143
|
findOne<T extends LiveObjectAny>(resource: T, id: string, options?: {
|
|
112
144
|
include?: IncludeClause<T>;
|
|
@@ -114,35 +146,51 @@ declare class SQLStorage extends Storage {
|
|
|
114
146
|
find<T extends LiveObjectAny>(resource: T, options?: {
|
|
115
147
|
where?: WhereClause<T>;
|
|
116
148
|
include?: IncludeClause<T>;
|
|
149
|
+
limit?: number;
|
|
150
|
+
sort?: {
|
|
151
|
+
key: string;
|
|
152
|
+
direction: "asc" | "desc";
|
|
153
|
+
}[];
|
|
117
154
|
}): Promise<Record<string, InferLiveObject<T>>>;
|
|
155
|
+
transaction<T>(fn: (opts: {
|
|
156
|
+
trx: Storage;
|
|
157
|
+
commit: () => Promise<void>;
|
|
158
|
+
rollback: () => Promise<void>;
|
|
159
|
+
}) => Promise<T>): Promise<T>;
|
|
118
160
|
private convertToMaterializedLiveType;
|
|
161
|
+
private isKyselyLike;
|
|
119
162
|
}
|
|
120
163
|
|
|
121
164
|
declare const expressAdapter: (app: Application, server: Server<AnyRouter>, options?: {
|
|
122
165
|
basePath?: string;
|
|
123
166
|
}) => void;
|
|
124
167
|
|
|
125
|
-
|
|
168
|
+
/** biome-ignore-all lint/suspicious/noExplicitAny: any's are actually used correctly */
|
|
169
|
+
|
|
170
|
+
interface BaseRequest {
|
|
126
171
|
headers: Record<string, string>;
|
|
127
172
|
cookies: Record<string, string>;
|
|
128
|
-
|
|
129
|
-
resourceName: string;
|
|
130
|
-
procedure?: string;
|
|
173
|
+
queryParams: Record<string, string>;
|
|
131
174
|
context: Record<string, any>;
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
type: "QUERY"
|
|
175
|
+
}
|
|
176
|
+
interface QueryRequest extends BaseRequest, RawQueryRequest {
|
|
177
|
+
type: "QUERY";
|
|
178
|
+
}
|
|
179
|
+
interface MutationRequest<TInput = any> extends BaseRequest {
|
|
180
|
+
type: "MUTATE";
|
|
181
|
+
input: TInput;
|
|
182
|
+
resource: string;
|
|
135
183
|
resourceId?: string;
|
|
136
|
-
|
|
137
|
-
}
|
|
138
|
-
type
|
|
184
|
+
procedure: string;
|
|
185
|
+
}
|
|
186
|
+
type Request = QueryRequest | MutationRequest;
|
|
187
|
+
type ContextProvider = (req: Omit<BaseRequest, "context"> & {
|
|
139
188
|
transport: "HTTP" | "WEBSOCKET";
|
|
140
189
|
}) => Record<string, any>;
|
|
141
|
-
type
|
|
142
|
-
type
|
|
143
|
-
type NextFunction<T> = (req: ParsedRequest) => Promise<T> | T;
|
|
190
|
+
type MutationHandler = (mutation: DefaultMutation) => void;
|
|
191
|
+
type NextFunction<O, R = Request> = (req: R) => Awaitable<O>;
|
|
144
192
|
type Middleware<T = any> = (opts: {
|
|
145
|
-
req:
|
|
193
|
+
req: Request;
|
|
146
194
|
next: NextFunction<T>;
|
|
147
195
|
}) => ReturnType<NextFunction<T>>;
|
|
148
196
|
declare class Server<TRouter extends AnyRouter> {
|
|
@@ -150,6 +198,7 @@ declare class Server<TRouter extends AnyRouter> {
|
|
|
150
198
|
readonly storage: Storage;
|
|
151
199
|
readonly schema: Schema<any>;
|
|
152
200
|
readonly middlewares: Set<Middleware<any>>;
|
|
201
|
+
readonly logger: Logger;
|
|
153
202
|
contextProvider?: ContextProvider;
|
|
154
203
|
private mutationSubscriptions;
|
|
155
204
|
private constructor();
|
|
@@ -159,14 +208,19 @@ declare class Server<TRouter extends AnyRouter> {
|
|
|
159
208
|
schema: Schema<any>;
|
|
160
209
|
middlewares?: Middleware<any>[];
|
|
161
210
|
contextProvider?: ContextProvider;
|
|
211
|
+
logLevel?: LogLevel;
|
|
162
212
|
}): Server<TRouter>;
|
|
163
213
|
subscribeToMutations(handler: MutationHandler): () => void;
|
|
164
|
-
|
|
165
|
-
req:
|
|
214
|
+
handleQuery(opts: {
|
|
215
|
+
req: QueryRequest;
|
|
216
|
+
}): Promise<QueryResult<any>>;
|
|
217
|
+
handleMutation(opts: {
|
|
218
|
+
req: MutationRequest;
|
|
166
219
|
}): Promise<any>;
|
|
167
220
|
use(middleware: Middleware<any>): this;
|
|
168
221
|
context(contextProvider: ContextProvider): this;
|
|
222
|
+
private wrapInMiddlewares;
|
|
169
223
|
}
|
|
170
224
|
declare const server: typeof Server.create;
|
|
171
225
|
|
|
172
|
-
export { type AnyRoute, type AnyRouter, type ContextProvider, type Middleware, type Mutation, type MutationHandler, type MutationResult, type NextFunction, type
|
|
226
|
+
export { type AnyRoute, type AnyRouter, type Authorization, type BaseRequest, type ContextProvider, type Middleware, type Mutation, type MutationAuthorizationHandler, type MutationHandler, type MutationRequest, type MutationResult, type NextFunction, type QueryRequest, type QueryResult, type ReadAuthorizationHandler, type Request, Route, RouteFactory, type RouteRecord, Router, SQLStorage, Server, Storage, expressAdapter, routeFactory, router, server };
|