@creatorem/next-trpc 1.0.12 → 1.0.13
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 +16 -20
- package/dist/create-trpc-client.js +11 -16
- package/package.json +1 -1
package/dist/create-trpc-api.js
CHANGED
|
@@ -4,31 +4,27 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
4
4
|
exports.createTrpcAPI = void 0;
|
|
5
5
|
const server_1 = require("next/server");
|
|
6
6
|
const utils_1 = require("./utils");
|
|
7
|
+
function deserialize(str) {
|
|
8
|
+
return JSON.parse(decodeURIComponent(Array.prototype.map
|
|
9
|
+
.call(atob(str), function (c) {
|
|
10
|
+
return '%' + ('00' + c.charCodeAt(0).toString(16)).slice(-2);
|
|
11
|
+
})
|
|
12
|
+
.join('')), (key, value) => {
|
|
13
|
+
if (value === '__NAN__') {
|
|
14
|
+
return NaN;
|
|
15
|
+
}
|
|
16
|
+
return value;
|
|
17
|
+
});
|
|
18
|
+
}
|
|
7
19
|
const parseInput = (request, endpoint) => {
|
|
8
20
|
if (!endpoint.input)
|
|
9
21
|
return undefined;
|
|
10
22
|
const searchParams = request.nextUrl.searchParams;
|
|
11
|
-
const
|
|
12
|
-
|
|
13
|
-
|
|
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
|
-
}
|
|
23
|
+
const input = searchParams.get('input');
|
|
24
|
+
if (!input) {
|
|
25
|
+
throw new Error('"input" not defined in the search params.');
|
|
29
26
|
}
|
|
30
|
-
|
|
31
|
-
return endpoint.input.parse(paramsObj);
|
|
27
|
+
return endpoint.input.parse(deserialize(input));
|
|
32
28
|
};
|
|
33
29
|
const createTrpcAPI = ({ router, ctx, }) => {
|
|
34
30
|
return async function handler(request, context) {
|
|
@@ -2,27 +2,22 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.createTrpcClient = exports.getTrpcFetch = void 0;
|
|
4
4
|
const utils_1 = require("./utils");
|
|
5
|
+
function serialize(str) {
|
|
6
|
+
return btoa(encodeURIComponent(JSON.stringify(str, (key, value) => {
|
|
7
|
+
if (Number.isNaN(value)) {
|
|
8
|
+
return "__NAN__";
|
|
9
|
+
}
|
|
10
|
+
return value;
|
|
11
|
+
})).replace(/%([0-9A-F]{2})/g, function (match, p1) {
|
|
12
|
+
return String.fromCharCode(parseInt(p1, 16));
|
|
13
|
+
}));
|
|
14
|
+
}
|
|
5
15
|
const getTrpcFetch = ({ endpointSlug, url, headers, }) => async (input) => {
|
|
6
16
|
const endpointName = (0, utils_1.kebabize)(endpointSlug);
|
|
7
17
|
// Build URL with search params if input exists
|
|
8
18
|
let requestUrl = `${url}/${endpointName}`;
|
|
9
19
|
if (input) {
|
|
10
|
-
|
|
11
|
-
for (const [key, value] of Object.entries(input)) {
|
|
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
|
-
}
|
|
24
|
-
}
|
|
25
|
-
requestUrl += `?${searchParams.toString()}`;
|
|
20
|
+
requestUrl += `?input=${serialize(input)}`;
|
|
26
21
|
}
|
|
27
22
|
const headerObject = typeof headers === "function" ? await headers() : headers || {};
|
|
28
23
|
const response = await fetch(requestUrl, {
|