@distilled.cloud/core 1.0.0-rc.13 → 1.0.0-rc.15
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/lib/api.d.ts +4 -4
- package/lib/api.d.ts.map +1 -1
- package/lib/api.js +3 -5
- package/lib/api.js.map +1 -1
- package/lib/category.d.ts.map +1 -1
- package/lib/category.js +3 -5
- package/lib/category.js.map +1 -1
- package/lib/codegen/boolean-string-enums.d.ts.map +1 -1
- package/lib/codegen/boolean-string-enums.js +2 -6
- package/lib/codegen/boolean-string-enums.js.map +1 -1
- package/lib/codegen/boolean-string-enums.test.js +3 -5
- package/lib/codegen/boolean-string-enums.test.js.map +1 -1
- package/lib/codegen/cli.d.ts +5 -4
- package/lib/codegen/cli.d.ts.map +1 -1
- package/lib/codegen/cli.js +12 -9
- package/lib/codegen/cli.js.map +1 -1
- package/lib/codegen/defer-naming.test.d.ts +2 -0
- package/lib/codegen/defer-naming.test.d.ts.map +1 -0
- package/lib/codegen/defer-naming.test.js +105 -0
- package/lib/codegen/defer-naming.test.js.map +1 -0
- package/lib/codegen/emit.d.ts.map +1 -1
- package/lib/codegen/emit.js +1 -3
- package/lib/codegen/emit.js.map +1 -1
- package/lib/codegen/format.d.ts +0 -12
- package/lib/codegen/format.d.ts.map +1 -1
- package/lib/codegen/format.js +10 -6
- package/lib/codegen/format.js.map +1 -1
- package/lib/codegen/generated-at.d.ts +29 -0
- package/lib/codegen/generated-at.d.ts.map +1 -0
- package/lib/codegen/generated-at.js +78 -0
- package/lib/codegen/generated-at.js.map +1 -0
- package/lib/codegen/generated-at.test.d.ts +2 -0
- package/lib/codegen/generated-at.test.d.ts.map +1 -0
- package/lib/codegen/generated-at.test.js +81 -0
- package/lib/codegen/generated-at.test.js.map +1 -0
- package/lib/codegen/generator.d.ts +4 -2
- package/lib/codegen/generator.d.ts.map +1 -1
- package/lib/codegen/generator.js +37 -75
- package/lib/codegen/generator.js.map +1 -1
- package/lib/codegen/generator.test.js +4 -9
- package/lib/codegen/generator.test.js.map +1 -1
- package/lib/codegen/graphql-client.d.ts.map +1 -1
- package/lib/codegen/graphql-client.js +12 -33
- package/lib/codegen/graphql-client.js.map +1 -1
- package/lib/codegen/graphql-client.test.js +4 -12
- package/lib/codegen/graphql-client.test.js.map +1 -1
- package/lib/codegen/graphql.d.ts.map +1 -1
- package/lib/codegen/graphql.js +7 -20
- package/lib/codegen/graphql.js.map +1 -1
- package/lib/codegen/model-diff.d.ts +10 -0
- package/lib/codegen/model-diff.d.ts.map +1 -0
- package/lib/codegen/model-diff.js +67 -0
- package/lib/codegen/model-diff.js.map +1 -0
- package/lib/codegen/naming.d.ts.map +1 -1
- package/lib/codegen/naming.js.map +1 -1
- package/lib/codegen/openapi-binary.test.js +1 -1
- package/lib/codegen/openapi-binary.test.js.map +1 -1
- package/lib/codegen/openapi-cli.d.ts +11 -11
- package/lib/codegen/openapi-cli.d.ts.map +1 -1
- package/lib/codegen/openapi-cli.js +26 -51
- package/lib/codegen/openapi-cli.js.map +1 -1
- package/lib/codegen/openapi.d.ts +9 -1
- package/lib/codegen/openapi.d.ts.map +1 -1
- package/lib/codegen/openapi.js +48 -42
- package/lib/codegen/openapi.js.map +1 -1
- package/lib/codegen/operations.d.ts.map +1 -1
- package/lib/codegen/operations.js.map +1 -1
- package/lib/codegen/pagination.d.ts.map +1 -1
- package/lib/codegen/pagination.js +2 -4
- package/lib/codegen/pagination.js.map +1 -1
- package/lib/codegen/patch-audit.d.ts +84 -0
- package/lib/codegen/patch-audit.d.ts.map +1 -0
- package/lib/codegen/patch-audit.js +366 -0
- package/lib/codegen/patch-audit.js.map +1 -0
- package/lib/codegen/patch-audit.test.d.ts +2 -0
- package/lib/codegen/patch-audit.test.d.ts.map +1 -0
- package/lib/codegen/patch-audit.test.js +141 -0
- package/lib/codegen/patch-audit.test.js.map +1 -0
- package/lib/codegen/patches.d.ts +59 -10
- package/lib/codegen/patches.d.ts.map +1 -1
- package/lib/codegen/patches.js +233 -63
- package/lib/codegen/patches.js.map +1 -1
- package/lib/codegen/patches.test.js +8 -4
- package/lib/codegen/patches.test.js.map +1 -1
- package/lib/codegen/prelude.js +1 -1
- package/lib/codegen/prelude.js.map +1 -1
- package/lib/codegen/proto.d.ts.map +1 -1
- package/lib/codegen/proto.js +2 -4
- package/lib/codegen/proto.js.map +1 -1
- package/lib/codegen/protocol-imports.test.d.ts +2 -0
- package/lib/codegen/protocol-imports.test.d.ts.map +1 -0
- package/lib/codegen/protocol-imports.test.js +36 -0
- package/lib/codegen/protocol-imports.test.js.map +1 -0
- package/lib/codegen/rewrite-operation-ids.d.ts +23 -0
- package/lib/codegen/rewrite-operation-ids.d.ts.map +1 -1
- package/lib/codegen/rewrite-operation-ids.js +97 -59
- package/lib/codegen/rewrite-operation-ids.js.map +1 -1
- package/lib/codegen/rewrite-operation-ids.test.js +19 -101
- package/lib/codegen/rewrite-operation-ids.test.js.map +1 -1
- package/lib/codegen/spec-path.d.ts +1 -1
- package/lib/codegen/spec-path.d.ts.map +1 -1
- package/lib/codegen/spec-path.js +1 -1
- package/lib/codegen/spec-path.js.map +1 -1
- package/lib/codegen/untagged-unions.test.js +1 -1
- package/lib/codegen/untagged-unions.test.js.map +1 -1
- package/lib/error-category.d.ts.map +1 -1
- package/lib/error-category.js.map +1 -1
- package/lib/errors.d.ts.map +1 -1
- package/lib/errors.js +7 -5
- package/lib/errors.js.map +1 -1
- package/lib/graphql.d.ts +4 -8
- package/lib/graphql.d.ts.map +1 -1
- package/lib/graphql.js +20 -48
- package/lib/graphql.js.map +1 -1
- package/lib/graphql.test.js +6 -6
- package/lib/graphql.test.js.map +1 -1
- package/lib/graphql.types.d.ts.map +1 -1
- package/lib/graphql.types.js.map +1 -1
- package/lib/json-patch.d.ts +13 -6
- package/lib/json-patch.d.ts.map +1 -1
- package/lib/json-patch.js +34 -19
- package/lib/json-patch.js.map +1 -1
- package/lib/json-patch.test.d.ts +2 -0
- package/lib/json-patch.test.d.ts.map +1 -0
- package/lib/json-patch.test.js +65 -0
- package/lib/json-patch.test.js.map +1 -0
- package/lib/pagination.d.ts.map +1 -1
- package/lib/pagination.js +6 -16
- package/lib/pagination.js.map +1 -1
- package/lib/pagination.test.js +3 -6
- package/lib/pagination.test.js.map +1 -1
- package/lib/protocol-http.d.ts +20 -1
- package/lib/protocol-http.d.ts.map +1 -1
- package/lib/protocol-http.js +41 -47
- package/lib/protocol-http.js.map +1 -1
- package/lib/protocol-http.test.js +20 -10
- package/lib/protocol-http.test.js.map +1 -1
- package/lib/protocol-rest.d.ts.map +1 -1
- package/lib/protocol-rest.js +9 -18
- package/lib/protocol-rest.js.map +1 -1
- package/lib/protocol-rest.test.d.ts +2 -0
- package/lib/protocol-rest.test.d.ts.map +1 -0
- package/lib/protocol-rest.test.js +498 -0
- package/lib/protocol-rest.test.js.map +1 -0
- package/lib/response-validation.d.ts.map +1 -1
- package/lib/response-validation.js.map +1 -1
- package/lib/response-validation.test.js +10 -5
- package/lib/response-validation.test.js.map +1 -1
- package/lib/retry-after.d.ts.map +1 -1
- package/lib/retry-after.js +1 -1
- package/lib/retry-after.js.map +1 -1
- package/lib/retry-after.test.d.ts +2 -0
- package/lib/retry-after.test.d.ts.map +1 -0
- package/lib/retry-after.test.js +204 -0
- package/lib/retry-after.test.js.map +1 -0
- package/lib/retry.d.ts.map +1 -1
- package/lib/retry.js +1 -2
- package/lib/retry.js.map +1 -1
- package/lib/retry.test.d.ts +2 -0
- package/lib/retry.test.d.ts.map +1 -0
- package/lib/retry.test.js +407 -0
- package/lib/retry.test.js.map +1 -0
- package/lib/sdks.test.d.ts +2 -0
- package/lib/sdks.test.d.ts.map +1 -0
- package/lib/sdks.test.js +76 -0
- package/lib/sdks.test.js.map +1 -0
- package/lib/testing.d.ts +2 -2
- package/lib/testing.d.ts.map +1 -1
- package/lib/testing.js +1 -1
- package/lib/testing.js.map +1 -1
- package/lib/trait.d.ts.map +1 -1
- package/lib/trait.js.map +1 -1
- package/lib/xml.d.ts.map +1 -1
- package/lib/xml.js +3 -7
- package/lib/xml.js.map +1 -1
- package/lib/xml.test.js +11 -12
- package/lib/xml.test.js.map +1 -1
- package/package.json +7 -10
- package/src/api.ts +38 -55
- package/src/category.ts +21 -50
- package/src/codegen/boolean-string-enums.test.ts +12 -28
- package/src/codegen/boolean-string-enums.ts +2 -10
- package/src/codegen/cli.ts +29 -47
- package/src/codegen/defer-naming.test.ts +113 -0
- package/src/codegen/emit.ts +2 -5
- package/src/codegen/format.ts +12 -10
- package/src/codegen/generated-at.test.ts +89 -0
- package/src/codegen/generated-at.ts +88 -0
- package/src/codegen/generator.test.ts +8 -22
- package/src/codegen/generator.ts +72 -191
- package/src/codegen/graphql-client.test.ts +12 -42
- package/src/codegen/graphql-client.ts +30 -95
- package/src/codegen/graphql.ts +22 -94
- package/src/codegen/model-diff.ts +67 -0
- package/src/codegen/naming.ts +1 -2
- package/src/codegen/openapi-binary.test.ts +5 -12
- package/src/codegen/openapi-cli.ts +38 -79
- package/src/codegen/openapi.ts +79 -136
- package/src/codegen/operations.ts +2 -5
- package/src/codegen/pagination.ts +4 -11
- package/src/codegen/patch-audit.test.ts +160 -0
- package/src/codegen/patch-audit.ts +480 -0
- package/src/codegen/patches.test.ts +14 -21
- package/src/codegen/patches.ts +313 -103
- package/src/codegen/prelude.ts +1 -1
- package/src/codegen/proto.ts +18 -54
- package/src/codegen/protocol-imports.test.ts +39 -0
- package/src/codegen/rewrite-operation-ids.test.ts +23 -114
- package/src/codegen/rewrite-operation-ids.ts +111 -98
- package/src/codegen/spec-path.ts +3 -6
- package/src/codegen/untagged-unions.test.ts +3 -7
- package/src/error-category.ts +5 -17
- package/src/errors.ts +16 -31
- package/src/graphql.test.ts +6 -8
- package/src/graphql.ts +76 -219
- package/src/graphql.types.ts +10 -25
- package/src/json-patch.test.ts +77 -0
- package/src/json-patch.ts +54 -60
- package/src/pagination.test.ts +13 -35
- package/src/pagination.ts +44 -111
- package/src/protocol-http.test.ts +36 -30
- package/src/protocol-http.ts +73 -138
- package/src/protocol-rest.test.ts +703 -0
- package/src/protocol-rest.ts +24 -62
- package/src/response-validation.test.ts +12 -18
- package/src/response-validation.ts +3 -12
- package/src/retry-after.test.ts +267 -0
- package/src/retry-after.ts +4 -10
- package/src/retry.test.ts +566 -0
- package/src/retry.ts +6 -21
- package/src/sdks.test.ts +87 -0
- package/src/testing.ts +4 -10
- package/src/trait.ts +21 -67
- package/src/xml.test.ts +15 -33
- package/src/xml.ts +9 -25
package/src/graphql.types.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
|
|
2
|
-
import type
|
|
1
|
+
import type * as Effect from "effect/Effect";
|
|
2
|
+
import type * as Stream from "effect/Stream";
|
|
3
3
|
import { mapQuery } from "./graphql.ts";
|
|
4
4
|
import type {
|
|
5
5
|
itemsQuery,
|
|
@@ -7,8 +7,8 @@ import type {
|
|
|
7
7
|
GraphQLTransportError,
|
|
8
8
|
UnknownGraphQLError,
|
|
9
9
|
} from "./graphql.ts";
|
|
10
|
-
|
|
11
|
-
import type
|
|
10
|
+
/** Compile-only: Query.fn unwraps Query/Effect fields on the returned plan. */
|
|
11
|
+
import type { PlanError, Query, QueryError, UnwrapPlan } from "./query.ts";
|
|
12
12
|
|
|
13
13
|
type Plan = {
|
|
14
14
|
readonly email: Query<string>;
|
|
@@ -18,11 +18,7 @@ type Plan = {
|
|
|
18
18
|
|
|
19
19
|
type Unwrapped = UnwrapPlan<Plan>;
|
|
20
20
|
type Assert<T extends true> = T;
|
|
21
|
-
type Equal<Left, Right> = [Left] extends [Right]
|
|
22
|
-
? [Right] extends [Left]
|
|
23
|
-
? true
|
|
24
|
-
: false
|
|
25
|
-
: false;
|
|
21
|
+
type Equal<Left, Right> = [Left] extends [Right] ? ([Right] extends [Left] ? true : false) : false;
|
|
26
22
|
|
|
27
23
|
type _Email = Assert<Equal<Unwrapped["email"], string>>;
|
|
28
24
|
type _Projects = Assert<
|
|
@@ -56,17 +52,13 @@ type ErrorPlan = {
|
|
|
56
52
|
type _FieldsKeepRootErrors = Assert<
|
|
57
53
|
Equal<(typeof project.name)["errorType"], NotFound | RateLimited>
|
|
58
54
|
>;
|
|
59
|
-
type _PlanError = Assert<
|
|
60
|
-
Equal<PlanError<ErrorPlan>, NotFound | RateLimited | "lookup-failed">
|
|
61
|
-
>;
|
|
55
|
+
type _PlanError = Assert<Equal<PlanError<ErrorPlan>, NotFound | RateLimited | "lookup-failed">>;
|
|
62
56
|
type _NoErrors = Assert<Equal<PlanError<{ literal: 1 }>, never>>;
|
|
63
57
|
type _QueryError = Assert<
|
|
64
58
|
Equal<
|
|
65
59
|
Exclude<
|
|
66
60
|
QueryError<NotFound>,
|
|
67
|
-
|
|
68
|
-
| UnknownGraphQLError
|
|
69
|
-
| GraphQLTransportError
|
|
61
|
+
GraphQLFailure<NotFound | UnknownGraphQLError> | UnknownGraphQLError | GraphQLTransportError
|
|
70
62
|
>,
|
|
71
63
|
NotFound
|
|
72
64
|
>
|
|
@@ -78,10 +70,7 @@ type PagedItems = ReturnType<typeof itemsQuery<{ name: string }, NotFound>>;
|
|
|
78
70
|
type _ItemsValue = Assert<Equal<Stream.Success<PagedItems>, { name: string }>>;
|
|
79
71
|
type _ItemsError = Assert<
|
|
80
72
|
Equal<
|
|
81
|
-
Extract<
|
|
82
|
-
Stream.Error<PagedItems>,
|
|
83
|
-
{ _tag: "NotFound" | "GraphQLPaginationError" }
|
|
84
|
-
>["_tag"],
|
|
73
|
+
Extract<Stream.Error<PagedItems>, { _tag: "NotFound" | "GraphQLPaginationError" }>["_tag"],
|
|
85
74
|
"NotFound" | "GraphQLPaginationError"
|
|
86
75
|
>
|
|
87
76
|
>;
|
|
@@ -91,9 +80,7 @@ export type _Services = typeof services;
|
|
|
91
80
|
declare const nullable: Query<{
|
|
92
81
|
owner: { name: string; tags: ReadonlyArray<string> | null } | null;
|
|
93
82
|
}>;
|
|
94
|
-
type _NullParent = Assert<
|
|
95
|
-
Equal<UnwrapPlan<typeof nullable.owner.name>, string | null>
|
|
96
|
-
>;
|
|
83
|
+
type _NullParent = Assert<Equal<UnwrapPlan<typeof nullable.owner.name>, string | null>>;
|
|
97
84
|
|
|
98
85
|
// ── Query.map on objects ────────────────────────────────────────────────────
|
|
99
86
|
declare const service: Query<{
|
|
@@ -102,9 +89,7 @@ declare const service: Query<{
|
|
|
102
89
|
latestDeployment: { id: string; status: string } | null;
|
|
103
90
|
tags: ReadonlyArray<{ label: string }>;
|
|
104
91
|
}>;
|
|
105
|
-
const deployment = service.latestDeployment.pipe(
|
|
106
|
-
mapQuery((d) => ({ id: d.id, status: d.status })),
|
|
107
|
-
);
|
|
92
|
+
const deployment = service.latestDeployment.pipe(mapQuery((d) => ({ id: d.id, status: d.status })));
|
|
108
93
|
type _ObjectMap = Assert<
|
|
109
94
|
Equal<UnwrapPlan<typeof deployment>, { id: string; status: string } | null>
|
|
110
95
|
>;
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
import { describe, expect, test } from "vitest";
|
|
2
|
+
import { applyOperation, StaleTargetError } from "./json-patch.ts";
|
|
3
|
+
|
|
4
|
+
describe("RFC 6902 arrays", () => {
|
|
5
|
+
test("add at an index inserts before it", () => {
|
|
6
|
+
const doc = { errors: ["a", "c"] };
|
|
7
|
+
applyOperation(doc, { op: "add", path: "/errors/1", value: "b" });
|
|
8
|
+
expect(doc.errors).toEqual(["a", "b", "c"]);
|
|
9
|
+
});
|
|
10
|
+
|
|
11
|
+
test("add at the length appends, like `-`", () => {
|
|
12
|
+
const doc = { errors: ["a"] };
|
|
13
|
+
applyOperation(doc, { op: "add", path: "/errors/1", value: "b" });
|
|
14
|
+
applyOperation(doc, { op: "add", path: "/errors/-", value: "c" });
|
|
15
|
+
expect(doc.errors).toEqual(["a", "b", "c"]);
|
|
16
|
+
});
|
|
17
|
+
|
|
18
|
+
test("add past the end is a stale target", () => {
|
|
19
|
+
expect(() =>
|
|
20
|
+
applyOperation({ errors: [] }, { op: "add", path: "/errors/2", value: "x" }),
|
|
21
|
+
).toThrow(StaleTargetError);
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
test("replace at an index overwrites", () => {
|
|
25
|
+
const doc = { errors: ["a", "x", "c"] };
|
|
26
|
+
applyOperation(doc, { op: "replace", path: "/errors/1", value: "b" });
|
|
27
|
+
expect(doc.errors).toEqual(["a", "b", "c"]);
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
test("replace keeps an object key in place", () => {
|
|
31
|
+
const doc: Record<string, unknown> = { a: 1, b: 2, c: 3 };
|
|
32
|
+
applyOperation(doc, { op: "replace", path: "/b", value: 4 });
|
|
33
|
+
expect(Object.keys(doc)).toEqual(["a", "b", "c"]);
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
test("move into an array inserts", () => {
|
|
37
|
+
const doc = { from: "b", list: ["a", "c"] };
|
|
38
|
+
applyOperation(doc, { op: "move", from: "/from", path: "/list/1" });
|
|
39
|
+
expect(doc).toEqual({ list: ["a", "b", "c"] });
|
|
40
|
+
});
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
describe("`before` on object keys", () => {
|
|
44
|
+
test("add inserts a new key before the named sibling", () => {
|
|
45
|
+
const doc: Record<string, unknown> = { input: 1, output: 2, traits: 3 };
|
|
46
|
+
applyOperation(doc, { op: "add", path: "/errors", value: [], before: "traits" });
|
|
47
|
+
expect(Object.keys(doc)).toEqual(["input", "output", "errors", "traits"]);
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
test("add keeps an existing key where it is", () => {
|
|
51
|
+
const doc: Record<string, unknown> = { a: 1, b: 2, c: 3 };
|
|
52
|
+
applyOperation(doc, { op: "add", path: "/c", value: 4, before: "a" });
|
|
53
|
+
expect(doc).toEqual({ a: 1, b: 2, c: 4 });
|
|
54
|
+
expect(Object.keys(doc)).toEqual(["a", "b", "c"]);
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
test("add appends when the sibling is absent", () => {
|
|
58
|
+
const doc: Record<string, unknown> = { a: 1 };
|
|
59
|
+
applyOperation(doc, { op: "add", path: "/b", value: 2, before: "missing" });
|
|
60
|
+
expect(Object.keys(doc)).toEqual(["a", "b"]);
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
test("add keeps the parent object's identity", () => {
|
|
64
|
+
const shapes: Record<string, unknown> = { a: 1, c: 3 };
|
|
65
|
+
const doc = { shapes };
|
|
66
|
+
applyOperation(doc, { op: "add", path: "/shapes/b", value: 2, before: "c" });
|
|
67
|
+
expect(doc.shapes).toBe(shapes);
|
|
68
|
+
expect(Object.keys(shapes)).toEqual(["a", "b", "c"]);
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
test("move onto itself reorders a key", () => {
|
|
72
|
+
const doc: Record<string, unknown> = { a: 1, b: 2, c: 3 };
|
|
73
|
+
applyOperation(doc, { op: "move", from: "/c", path: "/c", before: "a" });
|
|
74
|
+
expect(Object.keys(doc)).toEqual(["c", "a", "b"]);
|
|
75
|
+
expect(doc).toEqual({ a: 1, b: 2, c: 3 });
|
|
76
|
+
});
|
|
77
|
+
});
|
package/src/json-patch.ts
CHANGED
|
@@ -1,11 +1,10 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* JSON Patch (RFC 6902) Implementation
|
|
3
3
|
*
|
|
4
|
-
*
|
|
5
|
-
* `convert` so `.generated-specs` is the patched Smithy
|
|
6
|
-
* not patch.
|
|
7
|
-
*
|
|
8
|
-
* paths.
|
|
4
|
+
* The patch engine for every SDK. Patches are Smithy ops applied in
|
|
5
|
+
* `convert` (`finalizeConvert`), so `.generated-specs` is the patched Smithy
|
|
6
|
+
* model; generate does not patch. Operation names are convert policy
|
|
7
|
+
* (`operationNames`), never a patch.
|
|
9
8
|
*
|
|
10
9
|
* Pure functions only — callers load patch files themselves (the generators
|
|
11
10
|
* use Effect's FileSystem) and hand the parsed operations to `applyPatch`.
|
|
@@ -20,6 +19,14 @@ export interface JsonPatchOperation {
|
|
|
20
19
|
path: string;
|
|
21
20
|
value?: unknown;
|
|
22
21
|
from?: string;
|
|
22
|
+
/**
|
|
23
|
+
* Extension for `add` and `move`: when the op creates a new object key,
|
|
24
|
+
* insert it just before this sibling key instead of at the end, so the
|
|
25
|
+
* patched model keeps the order the converter would have written. A `move`
|
|
26
|
+
* whose `from` equals `path` reorders a key. Ignored when the key already
|
|
27
|
+
* exists, the parent is an array, or the sibling is absent.
|
|
28
|
+
*/
|
|
29
|
+
before?: string;
|
|
23
30
|
}
|
|
24
31
|
|
|
25
32
|
export type JsonPatch = JsonPatchOperation[];
|
|
@@ -59,11 +66,7 @@ export function getValueAtPath(obj: unknown, pointer: string): unknown {
|
|
|
59
66
|
let current: unknown = obj;
|
|
60
67
|
|
|
61
68
|
for (const segment of segments) {
|
|
62
|
-
if (
|
|
63
|
-
current === null ||
|
|
64
|
-
current === undefined ||
|
|
65
|
-
typeof current !== "object"
|
|
66
|
-
) {
|
|
69
|
+
if (current === null || current === undefined || typeof current !== "object") {
|
|
67
70
|
throw new StaleTargetError(
|
|
68
71
|
`JSON pointer ${pointer} missing (at '${segment}'): not an object`,
|
|
69
72
|
);
|
|
@@ -71,17 +74,13 @@ export function getValueAtPath(obj: unknown, pointer: string): unknown {
|
|
|
71
74
|
if (Array.isArray(current)) {
|
|
72
75
|
const index = segment === "-" ? current.length : parseInt(segment, 10);
|
|
73
76
|
if (index < 0 || index >= current.length) {
|
|
74
|
-
throw new StaleTargetError(
|
|
75
|
-
`JSON pointer ${pointer} missing index '${segment}'`,
|
|
76
|
-
);
|
|
77
|
+
throw new StaleTargetError(`JSON pointer ${pointer} missing index '${segment}'`);
|
|
77
78
|
}
|
|
78
79
|
current = current[index];
|
|
79
80
|
} else {
|
|
80
81
|
const record = current as Record<string, unknown>;
|
|
81
82
|
if (!Object.prototype.hasOwnProperty.call(record, segment)) {
|
|
82
|
-
throw new StaleTargetError(
|
|
83
|
-
`JSON pointer ${pointer} missing key '${segment}'`,
|
|
84
|
-
);
|
|
83
|
+
throw new StaleTargetError(`JSON pointer ${pointer} missing key '${segment}'`);
|
|
85
84
|
}
|
|
86
85
|
current = record[segment];
|
|
87
86
|
}
|
|
@@ -95,6 +94,8 @@ export function setValueAtPath(
|
|
|
95
94
|
obj: unknown,
|
|
96
95
|
pointer: string,
|
|
97
96
|
value: unknown,
|
|
97
|
+
mode: "add" | "replace" = "add",
|
|
98
|
+
before?: string,
|
|
98
99
|
): void {
|
|
99
100
|
const segments = parseJsonPointer(pointer);
|
|
100
101
|
if (segments.length === 0) {
|
|
@@ -106,9 +107,7 @@ export function setValueAtPath(
|
|
|
106
107
|
for (let i = 0; i < segments.length - 1; i++) {
|
|
107
108
|
const segment = segments[i]!;
|
|
108
109
|
if (current === null || typeof current !== "object") {
|
|
109
|
-
throw new StaleTargetError(
|
|
110
|
-
`Cannot traverse path ${pointer}: not an object`,
|
|
111
|
-
);
|
|
110
|
+
throw new StaleTargetError(`Cannot traverse path ${pointer}: not an object`);
|
|
112
111
|
}
|
|
113
112
|
if (Array.isArray(current)) {
|
|
114
113
|
current = current[parseInt(segment, 10)];
|
|
@@ -119,19 +118,38 @@ export function setValueAtPath(
|
|
|
119
118
|
|
|
120
119
|
const lastSegment = segments[segments.length - 1]!;
|
|
121
120
|
if (current === null || typeof current !== "object") {
|
|
122
|
-
throw new StaleTargetError(
|
|
123
|
-
`Cannot set value at path ${pointer}: parent is not an object`,
|
|
124
|
-
);
|
|
121
|
+
throw new StaleTargetError(`Cannot set value at path ${pointer}: parent is not an object`);
|
|
125
122
|
}
|
|
126
123
|
|
|
127
124
|
if (Array.isArray(current)) {
|
|
128
125
|
if (lastSegment === "-") {
|
|
129
126
|
current.push(value);
|
|
130
|
-
} else {
|
|
127
|
+
} else if (mode === "replace") {
|
|
131
128
|
current[parseInt(lastSegment, 10)] = value;
|
|
129
|
+
} else {
|
|
130
|
+
// RFC 6902 §4.1: `add` at an array index inserts before it.
|
|
131
|
+
const index = parseInt(lastSegment, 10);
|
|
132
|
+
if (Number.isNaN(index) || index < 0 || index > current.length) {
|
|
133
|
+
throw new StaleTargetError(
|
|
134
|
+
`JSON pointer ${pointer} index '${lastSegment}' is out of bounds`,
|
|
135
|
+
);
|
|
136
|
+
}
|
|
137
|
+
current.splice(index, 0, value);
|
|
132
138
|
}
|
|
133
139
|
} else {
|
|
134
|
-
|
|
140
|
+
const record = current as Record<string, unknown>;
|
|
141
|
+
if (before === undefined || lastSegment in record || !(before in record)) {
|
|
142
|
+
record[lastSegment] = value;
|
|
143
|
+
return;
|
|
144
|
+
}
|
|
145
|
+
// Rebuild the keys in place: the parent object stays the same object,
|
|
146
|
+
// since callers and later ops may hold a reference to it.
|
|
147
|
+
const entries = Object.entries(record);
|
|
148
|
+
for (const [key] of entries) delete record[key];
|
|
149
|
+
for (const [key, v] of entries) {
|
|
150
|
+
if (key === before) record[lastSegment] = value;
|
|
151
|
+
record[key] = v;
|
|
152
|
+
}
|
|
135
153
|
}
|
|
136
154
|
}
|
|
137
155
|
|
|
@@ -147,9 +165,7 @@ export function removeValueAtPath(obj: unknown, pointer: string): void {
|
|
|
147
165
|
for (let i = 0; i < segments.length - 1; i++) {
|
|
148
166
|
const segment = segments[i]!;
|
|
149
167
|
if (current === null || typeof current !== "object") {
|
|
150
|
-
throw new StaleTargetError(
|
|
151
|
-
`Cannot traverse path ${pointer}: not an object`,
|
|
152
|
-
);
|
|
168
|
+
throw new StaleTargetError(`Cannot traverse path ${pointer}: not an object`);
|
|
153
169
|
}
|
|
154
170
|
if (Array.isArray(current)) {
|
|
155
171
|
current = current[parseInt(segment, 10)];
|
|
@@ -160,26 +176,20 @@ export function removeValueAtPath(obj: unknown, pointer: string): void {
|
|
|
160
176
|
|
|
161
177
|
const lastSegment = segments[segments.length - 1]!;
|
|
162
178
|
if (current === null || typeof current !== "object") {
|
|
163
|
-
throw new StaleTargetError(
|
|
164
|
-
`Cannot remove at path ${pointer}: parent is not an object`,
|
|
165
|
-
);
|
|
179
|
+
throw new StaleTargetError(`Cannot remove at path ${pointer}: parent is not an object`);
|
|
166
180
|
}
|
|
167
181
|
|
|
168
182
|
// RFC 6902 §4.2: the target location MUST exist.
|
|
169
183
|
if (Array.isArray(current)) {
|
|
170
184
|
const index = parseInt(lastSegment, 10);
|
|
171
185
|
if (Number.isNaN(index) || index < 0 || index >= current.length) {
|
|
172
|
-
throw new StaleTargetError(
|
|
173
|
-
`JSON pointer ${pointer} missing index '${lastSegment}'`,
|
|
174
|
-
);
|
|
186
|
+
throw new StaleTargetError(`JSON pointer ${pointer} missing index '${lastSegment}'`);
|
|
175
187
|
}
|
|
176
188
|
current.splice(index, 1);
|
|
177
189
|
} else {
|
|
178
190
|
const record = current as Record<string, unknown>;
|
|
179
191
|
if (!Object.prototype.hasOwnProperty.call(record, lastSegment)) {
|
|
180
|
-
throw new StaleTargetError(
|
|
181
|
-
`JSON pointer ${pointer} missing key '${lastSegment}'`,
|
|
182
|
-
);
|
|
192
|
+
throw new StaleTargetError(`JSON pointer ${pointer} missing key '${lastSegment}'`);
|
|
183
193
|
}
|
|
184
194
|
delete record[lastSegment];
|
|
185
195
|
}
|
|
@@ -190,13 +200,10 @@ export function removeValueAtPath(obj: unknown, pointer: string): void {
|
|
|
190
200
|
// ============================================================================
|
|
191
201
|
|
|
192
202
|
/** Apply a single JSON Patch operation (mutates `obj` in place). */
|
|
193
|
-
export function applyOperation(
|
|
194
|
-
obj: unknown,
|
|
195
|
-
operation: JsonPatchOperation,
|
|
196
|
-
): void {
|
|
203
|
+
export function applyOperation(obj: unknown, operation: JsonPatchOperation): void {
|
|
197
204
|
switch (operation.op) {
|
|
198
205
|
case "add":
|
|
199
|
-
setValueAtPath(obj, operation.path, operation.value);
|
|
206
|
+
setValueAtPath(obj, operation.path, operation.value, "add", operation.before);
|
|
200
207
|
break;
|
|
201
208
|
case "remove":
|
|
202
209
|
removeValueAtPath(obj, operation.path);
|
|
@@ -204,33 +211,25 @@ export function applyOperation(
|
|
|
204
211
|
case "replace": {
|
|
205
212
|
const existing = getValueAtPath(obj, operation.path);
|
|
206
213
|
if (existing === undefined) {
|
|
207
|
-
throw new StaleTargetError(
|
|
208
|
-
`JSON pointer ${operation.path} does not exist`,
|
|
209
|
-
);
|
|
214
|
+
throw new StaleTargetError(`JSON pointer ${operation.path} does not exist`);
|
|
210
215
|
}
|
|
211
|
-
setValueAtPath(obj, operation.path, operation.value);
|
|
216
|
+
setValueAtPath(obj, operation.path, operation.value, "replace");
|
|
212
217
|
break;
|
|
213
218
|
}
|
|
214
219
|
case "move": {
|
|
215
220
|
if (!operation.from) throw new Error("move operation requires 'from'");
|
|
216
221
|
const moveValue = getValueAtPath(obj, operation.from);
|
|
217
222
|
if (moveValue === undefined) {
|
|
218
|
-
throw new StaleTargetError(
|
|
219
|
-
`Cannot move from path ${operation.from}: not an object`,
|
|
220
|
-
);
|
|
223
|
+
throw new StaleTargetError(`Cannot move from path ${operation.from}: not an object`);
|
|
221
224
|
}
|
|
222
225
|
removeValueAtPath(obj, operation.from);
|
|
223
|
-
setValueAtPath(obj, operation.path, moveValue);
|
|
226
|
+
setValueAtPath(obj, operation.path, moveValue, "add", operation.before);
|
|
224
227
|
break;
|
|
225
228
|
}
|
|
226
229
|
case "copy": {
|
|
227
230
|
if (!operation.from) throw new Error("copy operation requires 'from'");
|
|
228
231
|
const copyValue = getValueAtPath(obj, operation.from);
|
|
229
|
-
setValueAtPath(
|
|
230
|
-
obj,
|
|
231
|
-
operation.path,
|
|
232
|
-
JSON.parse(JSON.stringify(copyValue)),
|
|
233
|
-
);
|
|
232
|
+
setValueAtPath(obj, operation.path, JSON.parse(JSON.stringify(copyValue)));
|
|
234
233
|
break;
|
|
235
234
|
}
|
|
236
235
|
case "test": {
|
|
@@ -263,12 +262,7 @@ export function applyPatch(obj: unknown, patch: JsonPatch): void {
|
|
|
263
262
|
*/
|
|
264
263
|
export function isStaleTargetError(error: unknown): boolean {
|
|
265
264
|
if (error instanceof StaleTargetError) return true;
|
|
266
|
-
const message =
|
|
267
|
-
typeof error === "string"
|
|
268
|
-
? error
|
|
269
|
-
: error instanceof Error
|
|
270
|
-
? error.message
|
|
271
|
-
: "";
|
|
265
|
+
const message = typeof error === "string" ? error : error instanceof Error ? error.message : "";
|
|
272
266
|
return (
|
|
273
267
|
message.includes("not an object") ||
|
|
274
268
|
message.includes("parent is not an object") ||
|
package/src/pagination.test.ts
CHANGED
|
@@ -1,11 +1,7 @@
|
|
|
1
|
-
import { describe, expect, test } from "bun:test";
|
|
2
1
|
import * as Effect from "effect/Effect";
|
|
3
2
|
import * as Stream from "effect/Stream";
|
|
4
|
-
import {
|
|
5
|
-
|
|
6
|
-
paginateCursor,
|
|
7
|
-
type PaginatedTrait,
|
|
8
|
-
} from "./pagination.ts";
|
|
3
|
+
import { describe, expect, test } from "vitest";
|
|
4
|
+
import { extractItems, paginateCursor, type PaginatedTrait } from "./pagination.ts";
|
|
9
5
|
|
|
10
6
|
const pagination: PaginatedTrait = {
|
|
11
7
|
mode: "cursor",
|
|
@@ -106,8 +102,7 @@ const fixture = ({ pages, start }: Scenario) => {
|
|
|
106
102
|
Effect.sync(() => {
|
|
107
103
|
const page = pages[requests.length];
|
|
108
104
|
requests.push(request);
|
|
109
|
-
if (!page)
|
|
110
|
-
throw new Error("Paginator made an unexpected extra request");
|
|
105
|
+
if (!page) throw new Error("Paginator made an unexpected extra request");
|
|
111
106
|
return page;
|
|
112
107
|
}),
|
|
113
108
|
input,
|
|
@@ -126,9 +121,7 @@ describe("cursor pagination", () => {
|
|
|
126
121
|
const { stream, requests, input } = fixture(scenario);
|
|
127
122
|
const pages = yield* collect(stream);
|
|
128
123
|
expect(Array.from(pages)).toEqual(scenario.pages);
|
|
129
|
-
expect(requests.map((request) => request.cursor)).toEqual(
|
|
130
|
-
scenario.cursors,
|
|
131
|
-
);
|
|
124
|
+
expect(requests.map((request) => request.cursor)).toEqual(scenario.cursors);
|
|
132
125
|
expect(requests.every((request) => request.limit === 1)).toBe(true);
|
|
133
126
|
expect(input.cursor).toBe(scenario.start);
|
|
134
127
|
}),
|
|
@@ -139,15 +132,9 @@ describe("cursor pagination", () => {
|
|
|
139
132
|
Effect.runPromise(
|
|
140
133
|
Effect.gen(function* () {
|
|
141
134
|
const { stream, requests } = fixture(scenario);
|
|
142
|
-
const items = yield* collect(
|
|
143
|
-
|
|
144
|
-
);
|
|
145
|
-
expect(Array.from(items)).toEqual(
|
|
146
|
-
scenario.pages.flatMap((page) => page.items ?? []),
|
|
147
|
-
);
|
|
148
|
-
expect(requests.map((request) => request.cursor)).toEqual(
|
|
149
|
-
scenario.cursors,
|
|
150
|
-
);
|
|
135
|
+
const items = yield* collect(extractItems<Page, number, never, never>(stream, "items"));
|
|
136
|
+
expect(Array.from(items)).toEqual(scenario.pages.flatMap((page) => page.items ?? []));
|
|
137
|
+
expect(requests.map((request) => request.cursor)).toEqual(scenario.cursors);
|
|
151
138
|
}),
|
|
152
139
|
),
|
|
153
140
|
);
|
|
@@ -164,27 +151,18 @@ describe("cursor pagination", () => {
|
|
|
164
151
|
const stream = paginateCursor(
|
|
165
152
|
(input: { cursor?: string }) =>
|
|
166
153
|
Effect.sync(() => {
|
|
167
|
-
if (++calls > 12)
|
|
168
|
-
|
|
169
|
-
return pages[
|
|
170
|
-
input.cursor === "a" ? 1 : input.cursor === "b" ? 2 : 0
|
|
171
|
-
]!;
|
|
154
|
+
if (++calls > 12) throw new Error("Paginator exceeded the request bound");
|
|
155
|
+
return pages[input.cursor === "a" ? 1 : input.cursor === "b" ? 2 : 0]!;
|
|
172
156
|
}),
|
|
173
157
|
{},
|
|
174
158
|
pagination,
|
|
175
159
|
);
|
|
176
160
|
expect(Array.from(yield* collect(stream))).toEqual(pages);
|
|
177
161
|
expect(Array.from(yield* collect(stream))).toEqual(pages);
|
|
178
|
-
const concurrent = yield* Effect.all(
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
},
|
|
183
|
-
);
|
|
184
|
-
expect(concurrent.map((result) => Array.from(result))).toEqual([
|
|
185
|
-
pages,
|
|
186
|
-
pages,
|
|
187
|
-
]);
|
|
162
|
+
const concurrent = yield* Effect.all([collect(stream), collect(stream)], {
|
|
163
|
+
concurrency: "unbounded",
|
|
164
|
+
});
|
|
165
|
+
expect(concurrent.map((result) => Array.from(result))).toEqual([pages, pages]);
|
|
188
166
|
expect(calls).toBe(12);
|
|
189
167
|
}),
|
|
190
168
|
));
|