@creatorem/next-trpc 1.0.11 → 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 -6
- package/dist/create-trpc-client.js +11 -5
- package/package.json +1 -1
package/dist/create-trpc-api.js
CHANGED
|
@@ -4,17 +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
|
-
paramsObj[key] = value;
|
|
23
|
+
const input = searchParams.get('input');
|
|
24
|
+
if (!input) {
|
|
25
|
+
throw new Error('"input" not defined in the search params.');
|
|
15
26
|
}
|
|
16
|
-
|
|
17
|
-
return endpoint.input.parse(paramsObj);
|
|
27
|
+
return endpoint.input.parse(deserialize(input));
|
|
18
28
|
};
|
|
19
29
|
const createTrpcAPI = ({ router, ctx, }) => {
|
|
20
30
|
return async function handler(request, context) {
|
|
@@ -2,16 +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
|
-
searchParams.append(key, String(value));
|
|
13
|
-
}
|
|
14
|
-
requestUrl += `?${searchParams.toString()}`;
|
|
20
|
+
requestUrl += `?input=${serialize(input)}`;
|
|
15
21
|
}
|
|
16
22
|
const headerObject = typeof headers === "function" ? await headers() : headers || {};
|
|
17
23
|
const response = await fetch(requestUrl, {
|