@creatorem/next-trpc 1.0.10 → 1.0.12
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/create-trpc-api.js
CHANGED
|
@@ -11,7 +11,21 @@ const parseInput = (request, endpoint) => {
|
|
|
11
11
|
const paramsObj = {};
|
|
12
12
|
// Convert URLSearchParams to object
|
|
13
13
|
for (const [key, value] of searchParams) {
|
|
14
|
-
|
|
14
|
+
try {
|
|
15
|
+
// Try to parse as JSON first, but only for complex types (arrays/objects)
|
|
16
|
+
// Skip JSON parsing for simple primitives to preserve string values
|
|
17
|
+
const parsed = JSON.parse(value);
|
|
18
|
+
if (Array.isArray(parsed) || (typeof parsed === 'object' && parsed !== null)) {
|
|
19
|
+
paramsObj[key] = parsed;
|
|
20
|
+
}
|
|
21
|
+
else {
|
|
22
|
+
paramsObj[key] = value;
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
catch (_a) {
|
|
26
|
+
// If JSON parsing fails, keep as string
|
|
27
|
+
paramsObj[key] = value;
|
|
28
|
+
}
|
|
15
29
|
}
|
|
16
30
|
// Validate input with endpoint schema
|
|
17
31
|
return endpoint.input.parse(paramsObj);
|
|
@@ -9,7 +9,18 @@ const getTrpcFetch = ({ endpointSlug, url, headers, }) => async (input) => {
|
|
|
9
9
|
if (input) {
|
|
10
10
|
const searchParams = new URLSearchParams();
|
|
11
11
|
for (const [key, value] of Object.entries(input)) {
|
|
12
|
-
|
|
12
|
+
if (Array.isArray(value)) {
|
|
13
|
+
// Handle arrays by JSON stringifying them
|
|
14
|
+
searchParams.append(key, JSON.stringify(value));
|
|
15
|
+
}
|
|
16
|
+
else if (value !== null && typeof value === 'object') {
|
|
17
|
+
// Handle objects by JSON stringifying them
|
|
18
|
+
searchParams.append(key, JSON.stringify(value));
|
|
19
|
+
}
|
|
20
|
+
else {
|
|
21
|
+
// Handle primitive values as strings
|
|
22
|
+
searchParams.append(key, String(value));
|
|
23
|
+
}
|
|
13
24
|
}
|
|
14
25
|
requestUrl += `?${searchParams.toString()}`;
|
|
15
26
|
}
|
|
@@ -1,9 +1,12 @@
|
|
|
1
1
|
import type { useQuery as useQueryType } from "@tanstack/react-query";
|
|
2
2
|
import { type EndpointClient, createTrpcClientOptions } from "./create-trpc-client";
|
|
3
3
|
import type { Router, Endpoint, router } from "./core";
|
|
4
|
+
import z from "zod";
|
|
4
5
|
type TrpcClientWithQuery<R extends Router<any>> = {
|
|
5
6
|
[K in keyof R]: R[K] extends Endpoint<infer Output, infer Input, any> ? EndpointClient<Input, Output> & {
|
|
6
|
-
useQuery: (queryOptions
|
|
7
|
+
useQuery: Input extends import("zod").Schema ? (queryOptions: Omit<Parameters<typeof useQueryType>[0], "queryKey" | "queryFn"> & {
|
|
8
|
+
input: z.infer<Input>;
|
|
9
|
+
}) => ReturnType<typeof useQueryType<Awaited<Output>, Error, Awaited<Output>, string[]>> : (queryOptions?: Omit<Parameters<typeof useQueryType>[0], "queryKey" | "queryFn">) => ReturnType<typeof useQueryType<Awaited<Output>, Error, Awaited<Output>, string[]>>;
|
|
7
10
|
} : never;
|
|
8
11
|
};
|
|
9
12
|
export declare const createTrpcQueryClient: <R extends ReturnType<typeof router<any, Router<any>>>>(opts: createTrpcClientOptions & {
|
|
@@ -11,7 +11,10 @@ const createTrpcQueryClient = (opts) => {
|
|
|
11
11
|
fetch: (0, create_trpc_client_1.getTrpcFetch)(Object.assign({ endpointSlug: prop }, opts)),
|
|
12
12
|
useQuery: (queryOptions) => {
|
|
13
13
|
const endpointName = prop.replace(/([A-Z])/g, "-$1").toLowerCase();
|
|
14
|
-
return opts.useQuery(Object.assign(Object.assign({}, queryOptions), { queryKey: [endpointName], queryFn: (
|
|
14
|
+
return opts.useQuery(Object.assign(Object.assign({}, queryOptions), { queryKey: [endpointName], queryFn: async () => {
|
|
15
|
+
const fetcher = (0, create_trpc_client_1.getTrpcFetch)(Object.assign({ endpointSlug: prop }, opts));
|
|
16
|
+
return await fetcher(queryOptions === null || queryOptions === void 0 ? void 0 : queryOptions.input);
|
|
17
|
+
} }));
|
|
15
18
|
},
|
|
16
19
|
};
|
|
17
20
|
}
|