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