orpc-nuxt 0.5.0 → 0.6.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +9 -8
- package/dist/module.json +1 -1
- package/dist/runtime/testing.d.ts +24 -22
- package/dist/runtime/testing.js +19 -5
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -557,19 +557,20 @@ test("renders the posts", async () => {
|
|
|
557
557
|
})
|
|
558
558
|
```
|
|
559
559
|
|
|
560
|
-
|
|
560
|
+
The second handler argument provides typed error constructors derived from that procedure's `.errors()` map:
|
|
561
561
|
|
|
562
562
|
```ts
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
563
|
+
procedures.blog.posts.update.handle((input, { errors }) => {
|
|
564
|
+
if (input.title === "taken") {
|
|
565
|
+
throw errors.CONFLICT({
|
|
566
|
+
message: "Already exists",
|
|
567
|
+
data: { field: "title" },
|
|
568
|
+
})
|
|
569
|
+
}
|
|
570
|
+
return { id: 1, ...input }
|
|
567
571
|
})
|
|
568
572
|
```
|
|
569
573
|
|
|
570
|
-
Because this helper is standalone, it cannot infer which fake procedure will throw the error.
|
|
571
|
-
Its code and data are therefore not checked against that procedure's `.errors()` map.
|
|
572
|
-
|
|
573
574
|
### Outside Vue components
|
|
574
575
|
|
|
575
576
|
You can call `.useQuery()` and `.useMutation()` outside a component, for example in a script or in a test that never mounts one.
|
package/dist/module.json
CHANGED
|
@@ -1,33 +1,35 @@
|
|
|
1
|
-
import type { AnyNestedClient, Client
|
|
1
|
+
import type { AnyORPCError, AnyNestedClient, Client } from "@orpc/client";
|
|
2
|
+
import { ORPCError } from "@orpc/client";
|
|
2
3
|
import { QueryClient } from "@tanstack/vue-query";
|
|
3
4
|
import type { Mock } from "vitest";
|
|
4
5
|
import type { ORPCNuxtClient, ORPCNuxtClientOptions } from "./types.js";
|
|
5
6
|
type MaybePromise<T> = T | Promise<T>;
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
7
|
+
type ProcedureError<TProcedure> = TProcedure extends Client<any, any, any, infer Error> ? Extract<Error, AnyORPCError> : never;
|
|
8
|
+
type ErrorConstructorOptions<Data> = ErrorOptions & {
|
|
9
|
+
message?: string;
|
|
10
|
+
} & (undefined extends Data ? {
|
|
11
|
+
data?: Data;
|
|
12
|
+
} : {
|
|
13
|
+
data: Data;
|
|
14
|
+
});
|
|
15
|
+
type ErrorConstructor<ErrorType extends AnyORPCError> = ErrorType extends ORPCError<infer _Code, infer Data> ? (...args: undefined extends Data ? [options?: ErrorConstructorOptions<Data>] : [options: ErrorConstructorOptions<Data>]) => ErrorType : never;
|
|
16
|
+
type TestORPCErrors<TProcedure> = {
|
|
17
|
+
[Code in ProcedureError<TProcedure>["code"] & string]: ErrorConstructor<Extract<ProcedureError<TProcedure>, {
|
|
18
|
+
code: Code;
|
|
19
|
+
}>>;
|
|
20
|
+
};
|
|
21
|
+
/** Error constructors derived from the procedure's declared error union. */
|
|
22
|
+
export interface TestORPCHandlerOptions<TProcedure> {
|
|
23
|
+
/** Construct an error declared by this procedure, with its corresponding data type. */
|
|
24
|
+
errors: TestORPCErrors<TProcedure>;
|
|
25
|
+
}
|
|
25
26
|
/** A procedure implementation registered for a test client. */
|
|
26
|
-
export type TestORPCHandler<TProcedure> = TProcedure extends Client<infer _Context, infer Input, infer Output, infer _Error> ? (input: Input) => MaybePromise<Awaited<Output>> : never;
|
|
27
|
+
export type TestORPCHandler<TProcedure> = TProcedure extends Client<infer _Context, infer Input, infer Output, infer _Error> ? (input: Input, options: TestORPCHandlerOptions<TProcedure>) => MaybePromise<Awaited<Output>> : never;
|
|
28
|
+
type TestORPCMockHandler<TProcedure> = TProcedure extends Client<infer _Context, infer Input, infer Output, infer _Error> ? (input: Input) => MaybePromise<Awaited<Output>> : never;
|
|
27
29
|
/** Registration methods for one procedure in a test client. */
|
|
28
30
|
export interface TestORPCProcedure<TProcedure> {
|
|
29
31
|
/** Register the implementation used by subsequent calls and return its Vitest mock. */
|
|
30
|
-
handle(handler: TestORPCHandler<TProcedure>): Mock<
|
|
32
|
+
handle(handler: TestORPCHandler<TProcedure>): Mock<TestORPCMockHandler<TProcedure>>;
|
|
31
33
|
}
|
|
32
34
|
/** A router-shaped tree whose procedure leaves register test implementations. */
|
|
33
35
|
export type TestORPCProcedures<T extends AnyNestedClient> = T extends Client<infer Context, infer Input, infer Output, infer Error> ? TestORPCProcedure<Client<Context, Input, Output, Error>> : {
|
package/dist/runtime/testing.js
CHANGED
|
@@ -1,10 +1,7 @@
|
|
|
1
|
-
import { createORPCClient, createORPCErrorFromJson } from "@orpc/client";
|
|
1
|
+
import { createORPCClient, createORPCErrorFromJson, ORPCError } from "@orpc/client";
|
|
2
2
|
import { QueryClient } from "@tanstack/vue-query";
|
|
3
3
|
import { vi } from "vitest";
|
|
4
4
|
import { createORPCNuxtClient } from "./client/create.js";
|
|
5
|
-
export function createORPCError(code, message, data) {
|
|
6
|
-
return createORPCErrorFromJson({ defined: true, code, message, data });
|
|
7
|
-
}
|
|
8
5
|
export function createTestORPCClient(options = {}) {
|
|
9
6
|
const registrations = /* @__PURE__ */ new Map();
|
|
10
7
|
const queryClient = options.queryClient ?? new QueryClient({
|
|
@@ -14,7 +11,8 @@ export function createTestORPCClient(options = {}) {
|
|
|
14
11
|
}
|
|
15
12
|
});
|
|
16
13
|
function registerHandler(path, handler) {
|
|
17
|
-
const
|
|
14
|
+
const options2 = { errors: createErrorConstructors() };
|
|
15
|
+
const mock = vi.fn((input) => handler(input, options2));
|
|
18
16
|
registrations.set(path, mock);
|
|
19
17
|
return mock;
|
|
20
18
|
}
|
|
@@ -53,6 +51,22 @@ export function createTestORPCClient(options = {}) {
|
|
|
53
51
|
}
|
|
54
52
|
};
|
|
55
53
|
}
|
|
54
|
+
function createErrorConstructors() {
|
|
55
|
+
const constructors = /* @__PURE__ */ new Map();
|
|
56
|
+
return new Proxy(/* @__PURE__ */ Object.create(null), {
|
|
57
|
+
get(target, property, receiver) {
|
|
58
|
+
if (typeof property !== "string") return Reflect.get(target, property, receiver);
|
|
59
|
+
const cached = constructors.get(property);
|
|
60
|
+
if (cached) return cached;
|
|
61
|
+
const constructor = (options) => {
|
|
62
|
+
const error = new ORPCError(property, options);
|
|
63
|
+
return createORPCErrorFromJson({ ...error.toJSON(), defined: true }, { cause: error.cause });
|
|
64
|
+
};
|
|
65
|
+
constructors.set(property, constructor);
|
|
66
|
+
return constructor;
|
|
67
|
+
}
|
|
68
|
+
});
|
|
69
|
+
}
|
|
56
70
|
function createRecursiveProxy(path, call) {
|
|
57
71
|
const target = () => {
|
|
58
72
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "orpc-nuxt",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.6.0",
|
|
4
4
|
"description": "oRPC integration for Nuxt with TanStack Vue Query composables.",
|
|
5
5
|
"homepage": "https://github.com/IlyaSemenov/orpc-nuxt#readme",
|
|
6
6
|
"bugs": "https://github.com/IlyaSemenov/orpc-nuxt/issues",
|