effect-zero 0.4.0 → 0.4.2

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.
@@ -5,7 +5,7 @@ export declare const TransformRequestMessage: Schema.Tuple2<Schema.Literal<["tra
5
5
  args: Schema.Array$<typeof Schema.Unknown>;
6
6
  }>>>;
7
7
  export type TransformRequestMessage = typeof TransformRequestMessage.Type;
8
- export declare const TransformResponseMessage: Schema.Tuple2<Schema.Literal<["transformed"]>, Schema.Array$<Schema.Union<[Schema.Struct<{
8
+ export declare const TransformResponseMessage: Schema.Union<[Schema.Tuple2<Schema.Literal<["transformed"]>, Schema.Array$<Schema.Union<[Schema.Struct<{
9
9
  id: typeof Schema.String;
10
10
  name: typeof Schema.String;
11
11
  ast: typeof Schema.Unknown;
@@ -13,17 +13,36 @@ export declare const TransformResponseMessage: Schema.Tuple2<Schema.Literal<["tr
13
13
  error: Schema.Literal<["app"]>;
14
14
  id: typeof Schema.String;
15
15
  name: typeof Schema.String;
16
- details: typeof Schema.Unknown;
16
+ message: Schema.optional<typeof Schema.String>;
17
+ details: Schema.optional<typeof Schema.Unknown>;
17
18
  }>, Schema.Struct<{
18
- error: Schema.Literal<["http"]>;
19
+ error: Schema.Literal<["parse"]>;
19
20
  id: typeof Schema.String;
20
21
  name: typeof Schema.String;
22
+ message: typeof Schema.String;
23
+ details: Schema.optional<typeof Schema.Unknown>;
24
+ }>]>]>>>, Schema.Tuple2<Schema.Literal<["transformFailed"]>, Schema.Union<[Schema.Struct<{
25
+ origin: Schema.Literal<["server"]>;
26
+ reason: Schema.Literal<["database", "parse", "internal"]>;
27
+ kind: Schema.Literal<["TransformFailed"]>;
28
+ details: Schema.optional<typeof Schema.Unknown>;
29
+ queryIDs: Schema.Array$<typeof Schema.String>;
30
+ message: typeof Schema.String;
31
+ }>, Schema.Struct<{
32
+ origin: Schema.Literal<["zeroCache"]>;
33
+ reason: Schema.Literal<["http"]>;
21
34
  status: typeof Schema.Number;
22
- details: typeof Schema.Unknown;
35
+ bodyPreview: Schema.optional<typeof Schema.String>;
36
+ kind: Schema.Literal<["TransformFailed"]>;
37
+ details: Schema.optional<typeof Schema.Unknown>;
38
+ queryIDs: Schema.Array$<typeof Schema.String>;
39
+ message: typeof Schema.String;
23
40
  }>, Schema.Struct<{
24
- error: Schema.Literal<["zero"]>;
25
- id: typeof Schema.String;
26
- name: typeof Schema.String;
27
- details: typeof Schema.Unknown;
28
- }>]>]>>>;
41
+ origin: Schema.Literal<["zeroCache"]>;
42
+ reason: Schema.Literal<["timeout", "parse", "internal"]>;
43
+ kind: Schema.Literal<["TransformFailed"]>;
44
+ details: Schema.optional<typeof Schema.Unknown>;
45
+ queryIDs: Schema.Array$<typeof Schema.String>;
46
+ message: typeof Schema.String;
47
+ }>]>>]>;
29
48
  //# sourceMappingURL=queries.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"queries.d.ts","sourceRoot":"","sources":["../../src/types/queries.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,MAAM,MAAM,eAAe,CAAC;AAiDxC,eAAO,MAAM,uBAAuB;;;;IAAkE,CAAC;AACvG,MAAM,MAAM,uBAAuB,GAAG,OAAO,uBAAuB,CAAC,IAAI,CAAC;AAK1E,eAAO,MAAM,wBAAwB;;;;;;;;;;;;;;;;;;;;QAAqE,CAAC"}
1
+ {"version":3,"file":"queries.d.ts","sourceRoot":"","sources":["../../src/types/queries.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,MAAM,MAAM,eAAe,CAAC;AA6CxC,eAAO,MAAM,uBAAuB;;;;IAAkE,CAAC;AACvG,MAAM,MAAM,uBAAuB,GAAG,OAAO,uBAAuB,CAAC,IAAI,CAAC;AAS1E,eAAO,MAAM,wBAAwB;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;OAA2D,CAAC"}
@@ -1,7 +1,8 @@
1
1
  import * as Schema from "effect/Schema";
2
2
  import { JsonSchema } from "./common.js";
3
+ import { TransformFailedBody } from "./error.js";
3
4
  // The code below was converted to Effect Schema from:
4
- // https://github.com/rocicorp/mono/blob/9fe2b3cb770bad269ecaf12296c64a0739c23e4e/packages/zero-protocol/src/custom-queries.ts#L14
5
+ // https://github.com/rocicorp/mono/blob/8e0f600fb3a9185facf60cfd4971d260b266690e/packages/zero-protocol/src/custom-queries.ts
5
6
  /** TODO: Defined as unknown for now */
6
7
  const Ast = Schema.Unknown;
7
8
  const TransformRequestBody = Schema.Array(Schema.Struct({
@@ -14,29 +15,27 @@ const TransformedQuery = Schema.Struct({
14
15
  name: Schema.String,
15
16
  ast: Ast,
16
17
  });
17
- const AppQueryError = Schema.Struct({
18
+ const AppErroredQuery = Schema.Struct({
18
19
  error: Schema.Literal("app"),
19
20
  id: Schema.String,
20
21
  name: Schema.String,
21
- details: JsonSchema,
22
+ // optional for backwards compatibility
23
+ message: Schema.optional(Schema.String),
24
+ details: Schema.optional(JsonSchema),
22
25
  });
23
- const ErrorSchema = Schema.Struct({
24
- error: Schema.Literal("zero"),
26
+ const ParseErroredQuery = Schema.Struct({
27
+ error: Schema.Literal("parse"),
25
28
  id: Schema.String,
26
29
  name: Schema.String,
27
- details: JsonSchema,
30
+ message: Schema.String,
31
+ details: Schema.optional(JsonSchema),
28
32
  });
29
- const HttpQueryError = Schema.Struct({
30
- error: Schema.Literal("http"),
31
- id: Schema.String,
32
- name: Schema.String,
33
- status: Schema.Number,
34
- details: JsonSchema,
35
- });
36
- const ErroredQuery = Schema.Union(AppQueryError, HttpQueryError, ErrorSchema);
33
+ const ErroredQuery = Schema.Union(AppErroredQuery, ParseErroredQuery);
37
34
  const TransformResponseBody = Schema.Array(Schema.Union(TransformedQuery, ErroredQuery));
38
35
  export const TransformRequestMessage = Schema.Tuple(Schema.Literal("transform"), TransformRequestBody);
39
36
  // biome-ignore lint/correctness/noUnusedVariables: borrowed code
40
37
  const TransformErrorMessage = Schema.Tuple(Schema.Literal("transformError"), Schema.Array(ErroredQuery));
41
- export const TransformResponseMessage = Schema.Tuple(Schema.Literal("transformed"), TransformResponseBody);
38
+ const TransformFailedMessage = Schema.Tuple(Schema.Literal("transformFailed"), TransformFailedBody);
39
+ const TransformOkMessage = Schema.Tuple(Schema.Literal("transformed"), TransformResponseBody);
40
+ export const TransformResponseMessage = Schema.Union(TransformOkMessage, TransformFailedMessage);
42
41
  //# sourceMappingURL=queries.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"queries.js","sourceRoot":"","sources":["../../src/types/queries.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,MAAM,MAAM,eAAe,CAAC;AACxC,OAAO,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AAEzC,sDAAsD;AACtD,kIAAkI;AAElI,uCAAuC;AACvC,MAAM,GAAG,GAAG,MAAM,CAAC,OAAO,CAAC;AAE3B,MAAM,oBAAoB,GAAG,MAAM,CAAC,KAAK,CACvC,MAAM,CAAC,MAAM,CAAC;IACZ,EAAE,EAAE,MAAM,CAAC,MAAM;IACjB,IAAI,EAAE,MAAM,CAAC,MAAM;IACnB,IAAI,EAAE,MAAM,CAAC,KAAK,CAAC,UAAU,CAAC;CAC/B,CAAC,CACH,CAAC;AAEF,MAAM,gBAAgB,GAAG,MAAM,CAAC,MAAM,CAAC;IACrC,EAAE,EAAE,MAAM,CAAC,MAAM;IACjB,IAAI,EAAE,MAAM,CAAC,MAAM;IACnB,GAAG,EAAE,GAAG;CACT,CAAC,CAAC;AAEH,MAAM,aAAa,GAAG,MAAM,CAAC,MAAM,CAAC;IAClC,KAAK,EAAE,MAAM,CAAC,OAAO,CAAC,KAAK,CAAC;IAC5B,EAAE,EAAE,MAAM,CAAC,MAAM;IACjB,IAAI,EAAE,MAAM,CAAC,MAAM;IACnB,OAAO,EAAE,UAAU;CACpB,CAAC,CAAC;AAEH,MAAM,WAAW,GAAG,MAAM,CAAC,MAAM,CAAC;IAChC,KAAK,EAAE,MAAM,CAAC,OAAO,CAAC,MAAM,CAAC;IAC7B,EAAE,EAAE,MAAM,C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C;AAEzF,MAAM,CAAC,MAAM,uBAAuB,GAAG,MAAM,CAAC,KAAK,CAAC,MAAM,CAAC,OAAO,CAAC,WAAW,CAAC,EAAE,oBAAoB,CAAC,CAAC;AAGvG,iEAAiE;AACjE,MAAM,qBAAqB,GAAG,MAAM,CAAC,KAAK,CAAC,MAAM,CAAC,OAAO,CAAC,gBAAgB,CAAC,EAAE,MAAM,CAAC,KAAK,CAAC,YAAY,CAAC,CAAC,CAAC;AAEzG,MAAM,sBAAsB,GAAG,MAAM,CAAC,KAAK,CAAC,MAAM,CAAC,OAAO,CAAC,iBAAiB,CAAC,EAAE,mBAAmB,CAAC,CAAC;AAEpG,MAAM,kBAAkB,GAAG,MAAM,CAAC,KAAK,CAAC,MAAM,CAAC,OAAO,CAAC,aAAa,CAAC,EAAE,qBAAqB,CAAC,CAAC;AAE9F,MAAM,CAAC,MAAM,wBAAwB,GAAG,MAAM,CAAC,KAAK,CAAC,kBAAkB,EAAE,sBAAsB,CAAC,CAAC"}
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "effect-zero",
3
3
  "type": "module",
4
- "version": "0.4.0",
4
+ "version": "0.4.2",
5
5
  "license": "MIT",
6
6
  "repository": {
7
7
  "url": "https://github.com/realms-labs/effect-zero"
@@ -19,7 +19,7 @@
19
19
  "@effect-atom/atom": "^0.4.3",
20
20
  "@effect/language-service": "^0.55.2",
21
21
  "@effect/vitest": "^0.27.0",
22
- "@rocicorp/zero": "^0.26.1",
22
+ "@rocicorp/zero": "^1.3.0",
23
23
  "@types/bun": "latest",
24
24
  "@types/fs-extra": "^11.0.4",
25
25
  "effect": "^3.19.3",
@@ -34,16 +34,16 @@
34
34
  "@effect-atom/atom": "*"
35
35
  },
36
36
  "exports": {
37
- "./server": {
37
+ "./client-transaction": {
38
38
  "import": {
39
- "types": "./dist/server.d.ts",
40
- "default": "./dist/server.js"
39
+ "types": "./dist/client-transaction.d.ts",
40
+ "default": "./dist/client-transaction.js"
41
41
  }
42
42
  },
43
- "./query": {
43
+ "./server-transaction": {
44
44
  "import": {
45
- "types": "./dist/query.d.ts",
46
- "default": "./dist/query.js"
45
+ "types": "./dist/server-transaction.d.ts",
46
+ "default": "./dist/server-transaction.js"
47
47
  }
48
48
  },
49
49
  "./mutators": {
@@ -58,16 +58,16 @@
58
58
  "default": "./dist/snapshot.js"
59
59
  }
60
60
  },
61
- "./client-transaction": {
61
+ "./query": {
62
62
  "import": {
63
- "types": "./dist/client-transaction.d.ts",
64
- "default": "./dist/client-transaction.js"
63
+ "types": "./dist/query.d.ts",
64
+ "default": "./dist/query.js"
65
65
  }
66
66
  },
67
- "./server-transaction": {
67
+ "./server": {
68
68
  "import": {
69
- "types": "./dist/server-transaction.d.ts",
70
- "default": "./dist/server-transaction.js"
69
+ "types": "./dist/server.d.ts",
70
+ "default": "./dist/server.js"
71
71
  }
72
72
  },
73
73
  "./result": {
@@ -88,16 +88,22 @@
88
88
  "default": "./dist/types/push.js"
89
89
  }
90
90
  },
91
+ "./types/common": {
92
+ "import": {
93
+ "types": "./dist/types/common.d.ts",
94
+ "default": "./dist/types/common.js"
95
+ }
96
+ },
91
97
  "./types/queries": {
92
98
  "import": {
93
99
  "types": "./dist/types/queries.d.ts",
94
100
  "default": "./dist/types/queries.js"
95
101
  }
96
102
  },
97
- "./types/common": {
103
+ "./types/error": {
98
104
  "import": {
99
- "types": "./dist/types/common.d.ts",
100
- "default": "./dist/types/common.js"
105
+ "types": "./dist/types/error.d.ts",
106
+ "default": "./dist/types/error.js"
101
107
  }
102
108
  }
103
109
  }
package/src/query.ts CHANGED
@@ -91,11 +91,20 @@ export const stream = <T extends keyof S["tables"] & string, S extends ZeroSchem
91
91
  return Stream.asyncEffect<Parameters<Parameters<(typeof view)["addListener"]>[0]>>((emit) =>
92
92
  Effect.sync(() => view.addListener((...args) => emit.single(args))),
93
93
  ).pipe(
94
- Stream.mapEffect(([data, resultType]) =>
94
+ Stream.mapEffect(([data, resultType, error]) =>
95
95
  Effect.sync(() => {
96
- // logic here borrowed from: https://github.com/rocicorp/mono/blob/288b00ec94f5a9ae6e988513423af25c281dbb2a/packages/zero-react/src/use-query.tsx#L295
96
+ // logic here borrowed from: https://github.com/rocicorp/mono/blob/8e0f600fb3a9185facf60cfd4971d260b266690e/packages/zero-react/src/use-query.tsx#L543
97
97
  const cloned = data === undefined ? data : (deepClone(data as ReadonlyJSONValue) as HumanReadable<R>);
98
- return getSnapshot<R>(asQueryInternals(query).format.singular, cloned, resultType);
98
+ return getSnapshot<R>(
99
+ asQueryInternals(query).format.singular,
100
+ cloned,
101
+ resultType,
102
+ // TODO: Look into this. It seems like this is difficult but not impossible to model in the Effect paradigm.
103
+ // Likely need some kind of stateful piece between the publisher and and subscriber, allowing the subscriber to swap out
104
+ // the publisher when this function is called.
105
+ () => Effect.dieMessage("retry not available in `effect-zero`"),
106
+ error,
107
+ );
99
108
  }),
100
109
  ),
101
110
  Stream.map(QueryResult.make),
package/src/snapshot.ts CHANGED
@@ -1,19 +1,22 @@
1
1
  // Code borrowed from:
2
- // https://github.com/rocicorp/mono/blob/30f209f2946b4cf2cd2dee459849351498f11308/packages/zero-react/src/use-query.tsx
3
- // https://github.com/rocicorp/mono/blob/30f209f2946b4cf2cd2dee459849351498f11308/packages/shared/src/deep-clone.ts#L4
2
+ // https://github.com/rocicorp/mono/blob/8e0f600fb3a9185facf60cfd4971d260b266690e/packages/zero-react/src/use-query.tsx
3
+ // https://github.com/rocicorp/mono/blob/8e0f600fb3a9185facf60cfd4971d260b266690e/packages/shared/src/deep-clone.ts
4
4
 
5
- import type { HumanReadable, JSONValue, ReadonlyJSONValue } from "@rocicorp/zero";
5
+ import type { ErroredQuery, HumanReadable, JSONValue, QueryErrorDetails, ReadonlyJSONValue, ResultType } from "@rocicorp/zero";
6
6
  import type { QueryResult } from "@rocicorp/zero/react";
7
7
 
8
8
  const emptyArray: unknown[] = [];
9
9
 
10
10
  const resultTypeUnknown = { type: "unknown" } as const;
11
11
  const resultTypeComplete = { type: "complete" } as const;
12
+ const resultTypeError = {type: 'error'} as const;
12
13
 
13
14
  const emptySnapshotSingularUnknown = [undefined, resultTypeUnknown] as const;
14
15
  const emptySnapshotSingularComplete = [undefined, resultTypeComplete] as const;
16
+ const emptySnapshotSingularErrorUnknown = [undefined, resultTypeError] as const;
15
17
  const emptySnapshotPluralUnknown = [emptyArray, resultTypeUnknown] as const;
16
18
  const emptySnapshotPluralComplete = [emptyArray, resultTypeComplete] as const;
19
+ const emptySnapshotErrorUnknown = [emptyArray, resultTypeError] as const;
17
20
 
18
21
  export function getDefaultSnapshot<TReturn>(singular: boolean): QueryResult<TReturn> {
19
22
  return (singular ? emptySnapshotSingularUnknown : emptySnapshotPluralUnknown) as QueryResult<TReturn>;
@@ -26,21 +29,77 @@ export function getDefaultSnapshot<TReturn>(singular: boolean): QueryResult<TRet
26
29
  export function getSnapshot<TReturn>(
27
30
  singular: boolean,
28
31
  data: HumanReadable<TReturn>,
29
- resultType: string,
32
+ resultType: ResultType,
33
+ retryFn: () => void,
34
+ error?: ErroredQuery,
30
35
  ): QueryResult<TReturn> {
31
36
  if (singular && data === undefined) {
32
- return (resultType === "complete"
33
- ? emptySnapshotSingularComplete
34
- : emptySnapshotSingularUnknown) as unknown as QueryResult<TReturn>;
37
+ switch (resultType) {
38
+ case 'error':
39
+ if (error) {
40
+ return [
41
+ undefined,
42
+ makeError(retryFn, error),
43
+ ] as unknown as QueryResult<TReturn>;
44
+ }
45
+ return emptySnapshotSingularErrorUnknown as unknown as QueryResult<TReturn>;
46
+ case 'complete':
47
+ return emptySnapshotSingularComplete as unknown as QueryResult<TReturn>;
48
+ case 'unknown':
49
+ return emptySnapshotSingularUnknown as unknown as QueryResult<TReturn>;
50
+ }
35
51
  }
36
52
 
37
53
  if (!singular && (data as unknown[]).length === 0) {
38
- return (
39
- resultType === "complete" ? emptySnapshotPluralComplete : emptySnapshotPluralUnknown
40
- ) as QueryResult<TReturn>;
54
+ switch (resultType) {
55
+ case 'error':
56
+ if (error) {
57
+ return [
58
+ emptyArray,
59
+ makeError(retryFn, error),
60
+ ] as unknown as QueryResult<TReturn>;
61
+ }
62
+ return emptySnapshotErrorUnknown as unknown as QueryResult<TReturn>;
63
+ case 'complete':
64
+ return emptySnapshotPluralComplete as unknown as QueryResult<TReturn>;
65
+ case 'unknown':
66
+ return emptySnapshotPluralUnknown as unknown as QueryResult<TReturn>;
67
+ }
41
68
  }
42
69
 
43
- return [data, resultType === "complete" ? resultTypeComplete : resultTypeUnknown];
70
+ switch (resultType) {
71
+ case 'error':
72
+ if (error) {
73
+ return [data, makeError(retryFn, error)];
74
+ }
75
+ return [
76
+ data,
77
+ makeError(retryFn, {
78
+ error: 'app',
79
+ id: 'unknown',
80
+ name: 'unknown',
81
+ message: 'An unknown error occurred',
82
+ }),
83
+ ];
84
+ case 'complete':
85
+ return [data, resultTypeComplete];
86
+ case 'unknown':
87
+ return [data, resultTypeUnknown];
88
+ }
89
+ }
90
+
91
+ function makeError(retry: () => void, error: ErroredQuery): QueryErrorDetails {
92
+ const message = error.message ?? 'An unknown error occurred';
93
+ return {
94
+ type: 'error',
95
+ retry,
96
+ refetch: retry,
97
+ error: {
98
+ type: error.error,
99
+ message,
100
+ ...(error.details ? {details: error.details} : {}),
101
+ },
102
+ };
44
103
  }
45
104
 
46
105
  export function deepClone(value: ReadonlyJSONValue): JSONValue {
@@ -0,0 +1,71 @@
1
+ import * as Schema from "effect/Schema";
2
+ import { JsonSchema } from "./common.js";
3
+
4
+ // The code below was converted to Effect Schema from:
5
+ // https://github.com/rocicorp/mono/blob/8e0f600fb3a9185facf60cfd4971d260b266690e/packages/zero-protocol/src/error.ts
6
+
7
+ const MutationId = Schema.Struct({
8
+ id: Schema.Number,
9
+ clientID: Schema.String,
10
+ });
11
+
12
+ const PushFailedBase = Schema.Struct({
13
+ kind: Schema.Literal("PushFailed"),
14
+ details: Schema.optional(JsonSchema),
15
+ /**
16
+ * The mutationIDs of the mutations that failed to process.
17
+ * This can be a subset of the mutationIDs in the request.
18
+ */
19
+ mutationIDs: Schema.Array(MutationId),
20
+ message: Schema.String,
21
+ });
22
+
23
+ export const PushFailedBody = Schema.Union(
24
+ Schema.Struct({
25
+ ...PushFailedBase.fields,
26
+ origin: Schema.Literal("server"),
27
+ reason: Schema.Literal("database", "parse", "oooMutation", "unsupportedPushVersion", "internal"),
28
+ }),
29
+ Schema.Struct({
30
+ ...PushFailedBase.fields,
31
+ origin: Schema.Literal("zeroCache"),
32
+ reason: Schema.Literal("http"),
33
+ status: Schema.Number,
34
+ bodyPreview: Schema.optional(Schema.String),
35
+ }),
36
+ Schema.Struct({
37
+ ...PushFailedBase.fields,
38
+ origin: Schema.Literal("zeroCache"),
39
+ reason: Schema.Literal("timeout", "parse", "internal"),
40
+ }),
41
+ );
42
+
43
+ const TransformFailedBase = Schema.Struct({
44
+ kind: Schema.Literal("TransformFailed"),
45
+ details: Schema.optional(JsonSchema),
46
+ /**
47
+ * The queryIDs of the queries that failed to transform.
48
+ */
49
+ queryIDs: Schema.Array(Schema.String),
50
+ message: Schema.String,
51
+ });
52
+
53
+ export const TransformFailedBody = Schema.Union(
54
+ Schema.Struct({
55
+ ...TransformFailedBase.fields,
56
+ origin: Schema.Literal("server"),
57
+ reason: Schema.Literal("database", "parse", "internal"),
58
+ }),
59
+ Schema.Struct({
60
+ ...TransformFailedBase.fields,
61
+ origin: Schema.Literal("zeroCache"),
62
+ reason: Schema.Literal("http"),
63
+ status: Schema.Number,
64
+ bodyPreview: Schema.optional(Schema.String),
65
+ }),
66
+ Schema.Struct({
67
+ ...TransformFailedBase.fields,
68
+ origin: Schema.Literal("zeroCache"),
69
+ reason: Schema.Literal("timeout", "parse", "internal"),
70
+ }),
71
+ );
package/src/types/push.ts CHANGED
@@ -1,8 +1,9 @@
1
1
  import * as Schema from "effect/Schema";
2
2
  import { JsonSchema } from "./common.js";
3
+ import { PushFailedBody } from "./error.js";
3
4
 
4
5
  // The code below was converted to Effect Schema from:
5
- // https://github.com/rocicorp/mono/blob/30f209f2946b4cf2cd2dee459849351498f11308/packages/zero-protocol/src/push.ts
6
+ // https://github.com/rocicorp/mono/blob/8e0f600fb3a9185facf60cfd4971d260b266690e/packages/zero-protocol/src/push.ts
6
7
 
7
8
  const PrimaryKey = Schema.Array(Schema.String);
8
9
 
@@ -15,6 +16,32 @@ const Row = Schema.Record({ key: Schema.String, value: Value });
15
16
 
16
17
  const CRUD_MUTATION_NAME = "_zero_crud";
17
18
 
19
+ // biome-ignore lint/correctness/noUnusedVariables: borrowed code
20
+ const CLEANUP_RESULTS_MUTATION_NAME = "_zero_cleanupResults";
21
+
22
+ // biome-ignore lint/correctness/noUnusedVariables: borrowed code
23
+ const CleanupResultsArg = Schema.Union(
24
+ // Legacy format (no type field) - treat as single
25
+ Schema.Struct({
26
+ clientGroupID: Schema.String,
27
+ clientID: Schema.String,
28
+ upToMutationID: Schema.Number,
29
+ }),
30
+ // Explicit single: delete up to a specific mutation ID for one client
31
+ Schema.Struct({
32
+ type: Schema.Literal("single"),
33
+ clientGroupID: Schema.String,
34
+ clientID: Schema.String,
35
+ upToMutationID: Schema.Number,
36
+ }),
37
+ // Bulk: delete all mutations for multiple clients
38
+ Schema.Struct({
39
+ type: Schema.Literal("bulk"),
40
+ clientGroupID: Schema.String,
41
+ clientIDs: Schema.NonEmptyArray(Schema.String),
42
+ }),
43
+ );
44
+
18
45
  /**
19
46
  * Inserts if entity with id does not already exist.
20
47
  */
@@ -97,6 +124,11 @@ export const PushBody = Schema.Struct({
97
124
  schemaVersion: Schema.optional(Schema.Number),
98
125
  timestamp: Schema.Number,
99
126
  requestID: Schema.String,
127
+ /**
128
+ * @deprecated auth is managed at client-group scope via connect/updateAuth
129
+ * and should not be included in push messages.
130
+ */
131
+ auth: Schema.optional(Schema.String),
100
132
  });
101
133
  export type PushBody = typeof PushBody.Type;
102
134
 
@@ -111,13 +143,18 @@ const MutationId = Schema.Struct({
111
143
  export const AppError = Schema.Struct({
112
144
  error: Schema.Literal("app"),
113
145
  // The user can return any additional data here
146
+ message: Schema.optional(Schema.String),
114
147
  details: Schema.optional(JsonSchema),
115
148
  });
116
149
 
117
150
  export type AppError = typeof AppError.Type;
118
151
 
119
152
  export const ZeroError = Schema.Struct({
120
- error: Schema.Union(Schema.Literal("oooMutation"), Schema.Literal("alreadyProcessed")),
153
+ error: Schema.Union(
154
+ /** @deprecated push oooMutation errors are now represented as ['error', { ... }] messages */
155
+ Schema.Literal("oooMutation"),
156
+ Schema.Literal("alreadyProcessed"),
157
+ ),
121
158
  details: Schema.optional(JsonSchema),
122
159
  });
123
160
 
@@ -147,42 +184,64 @@ const PushOk = Schema.Struct({
147
184
  mutations: Schema.Array(MutationResponse),
148
185
  });
149
186
 
187
+ /**
188
+ * @deprecated push errors are now represented as ['error', { ... }] messages
189
+ */
150
190
  const UnsupportedPushVersion = Schema.Struct({
191
+ /** @deprecated */
151
192
  error: Schema.Literal("unsupportedPushVersion"),
152
- // optional for backwards compatibility
153
- // This field is included so the client knows which mutations
154
- // were not processed by the server.
193
+ /** @deprecated */
155
194
  mutationIDs: Schema.optional(Schema.Array(MutationId)),
156
195
  });
157
196
 
197
+ /**
198
+ * @deprecated push errors are now represented as ['error', { ... }] messages
199
+ */
158
200
  const UnsupportedSchemaVersion = Schema.Struct({
201
+ /** @deprecated */
159
202
  error: Schema.Literal("unsupportedSchemaVersion"),
160
- // optional for backwards compatibility
161
- // This field is included so the client knows which mutations
162
- // were not processed by the server.
203
+ /** @deprecated */
163
204
  mutationIDs: Schema.optional(Schema.Array(MutationId)),
164
205
  });
165
206
 
207
+ /**
208
+ * @deprecated push http errors are now represented as ['error', { ... }] messages
209
+ */
166
210
  const HttpError = Schema.Struct({
211
+ /** @deprecated */
167
212
  error: Schema.Literal("http"),
213
+ /** @deprecated */
168
214
  status: Schema.Number,
215
+ /** @deprecated */
169
216
  details: Schema.String,
217
+ /** @deprecated */
170
218
  mutationIDs: Schema.optional(Schema.Array(MutationId)),
171
219
  });
172
220
 
221
+ /**
222
+ * @deprecated push zero errors are now represented as ['error', { ... }] messages
223
+ */
173
224
  const ZeroPusherError = Schema.Struct({
225
+ /** @deprecated */
174
226
  error: Schema.Literal("zeroPusher"),
227
+ /** @deprecated */
175
228
  details: Schema.String,
229
+ /** @deprecated */
176
230
  mutationIDs: Schema.optional(Schema.Array(MutationId)),
177
231
  });
178
232
 
233
+ /**
234
+ * @deprecated push errors are now represented as ['error', { ... }] messages
235
+ */
179
236
  const PushError = Schema.Union(UnsupportedPushVersion, UnsupportedSchemaVersion, HttpError, ZeroPusherError);
180
237
 
181
- export const PushResponse = Schema.Union(PushOk, PushError);
238
+ const PushResponseBody = Schema.Union(PushOk, PushError);
239
+
240
+ export const PushResponse = Schema.Union(PushResponseBody, PushFailedBody);
182
241
  export type PushResponse = typeof PushResponse.Type;
183
242
 
184
243
  // biome-ignore lint/correctness/noUnusedVariables: borrowed code
185
- const PushResponseMessage = Schema.Tuple(Schema.Literal("pushResponse"), PushResponse);
244
+ const PushResponseMessage = Schema.Tuple(Schema.Literal("pushResponse"), PushResponseBody);
186
245
 
187
246
  // biome-ignore lint/correctness/noUnusedVariables: borrowed code
188
247
  const AckMutationResponsesMessage = Schema.Tuple(Schema.Literal("ackMutationResponses"), MutationId);
@@ -1,8 +1,9 @@
1
1
  import * as Schema from "effect/Schema";
2
2
  import { JsonSchema } from "./common.js";
3
+ import { TransformFailedBody } from "./error.js";
3
4
 
4
5
  // The code below was converted to Effect Schema from:
5
- // https://github.com/rocicorp/mono/blob/9fe2b3cb770bad269ecaf12296c64a0739c23e4e/packages/zero-protocol/src/custom-queries.ts#L14
6
+ // https://github.com/rocicorp/mono/blob/8e0f600fb3a9185facf60cfd4971d260b266690e/packages/zero-protocol/src/custom-queries.ts
6
7
 
7
8
  /** TODO: Defined as unknown for now */
8
9
  const Ast = Schema.Unknown;
@@ -21,29 +22,24 @@ const TransformedQuery = Schema.Struct({
21
22
  ast: Ast,
22
23
  });
23
24
 
24
- const AppQueryError = Schema.Struct({
25
+ const AppErroredQuery = Schema.Struct({
25
26
  error: Schema.Literal("app"),
26
27
  id: Schema.String,
27
28
  name: Schema.String,
28
- details: JsonSchema,
29
+ // optional for backwards compatibility
30
+ message: Schema.optional(Schema.String),
31
+ details: Schema.optional(JsonSchema),
29
32
  });
30
33
 
31
- const ErrorSchema = Schema.Struct({
32
- error: Schema.Literal("zero"),
34
+ const ParseErroredQuery = Schema.Struct({
35
+ error: Schema.Literal("parse"),
33
36
  id: Schema.String,
34
37
  name: Schema.String,
35
- details: JsonSchema,
38
+ message: Schema.String,
39
+ details: Schema.optional(JsonSchema),
36
40
  });
37
41
 
38
- const HttpQueryError = Schema.Struct({
39
- error: Schema.Literal("http"),
40
- id: Schema.String,
41
- name: Schema.String,
42
- status: Schema.Number,
43
- details: JsonSchema,
44
- });
45
-
46
- const ErroredQuery = Schema.Union(AppQueryError, HttpQueryError, ErrorSchema);
42
+ const ErroredQuery = Schema.Union(AppErroredQuery, ParseErroredQuery);
47
43
 
48
44
  const TransformResponseBody = Schema.Array(Schema.Union(TransformedQuery, ErroredQuery));
49
45
 
@@ -53,4 +49,8 @@ export type TransformRequestMessage = typeof TransformRequestMessage.Type;
53
49
  // biome-ignore lint/correctness/noUnusedVariables: borrowed code
54
50
  const TransformErrorMessage = Schema.Tuple(Schema.Literal("transformError"), Schema.Array(ErroredQuery));
55
51
 
56
- export const TransformResponseMessage = Schema.Tuple(Schema.Literal("transformed"), TransformResponseBody);
52
+ const TransformFailedMessage = Schema.Tuple(Schema.Literal("transformFailed"), TransformFailedBody);
53
+
54
+ const TransformOkMessage = Schema.Tuple(Schema.Literal("transformed"), TransformResponseBody);
55
+
56
+ export const TransformResponseMessage = Schema.Union(TransformOkMessage, TransformFailedMessage);