Skip to content

Commit 34d8404

Browse files
committed
fix: serialize and deserialize methods to support null, undefined, boolean, NaN types
1 parent 44668ee commit 34d8404

File tree

9 files changed

+393
-155
lines changed

9 files changed

+393
-155
lines changed

example/components/client-comp.tsx

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,11 @@ export const ClientComp: React.FC = () => {
2222
organizationId: "qlmskjdqslmdf",
2323
endDate: "03-10-2025",
2424
where: {
25-
lksdf: true,
26-
what: [
27-
"do",
28-
"you",
29-
{
30-
sdf: "string",
31-
test: 5,
32-
},
33-
],
25+
null: null,
26+
false: false,
27+
undefined: undefined,
28+
nan: NaN,
29+
number: 34,
3430
},
3531
});
3632
};

example/trpc/router.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,24 @@ export const appRouter = ctx.router({
9595
.input(analyticsFetcherSchema)
9696
.action(async (inputs) => {
9797
console.log("analyticsFetcher");
98+
99+
// null: null,
100+
// false: false,
101+
// undefined: undefined,
102+
// nan: NaN,
103+
// number: 34,
104+
105+
console.log( {where: inputs.where} )
106+
console.log( typeof inputs.where.null )
107+
console.log( {null :typeof inputs.where.null} )
108+
console.log( {false :typeof inputs.where.false} )
109+
console.log( {undefined :typeof inputs.where.undefined} )
110+
console.log( inputs.where.nan )
111+
console.log( {nan :typeof inputs.where.nan} )
112+
console.log( {number :typeof inputs.where.number} )
113+
114+
// if(typeof K === '')
115+
98116
console.log({ inputs });
99117
return { test: true };
100118
}),

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@creatorem/next-trpc",
3-
"version": "1.0.12",
3+
"version": "1.0.13",
44
"repository": {
55
"type": "git",
66
"url": "git+https://github.com/creatorem/next-trpc"

src/__tests__/create-trpc-api.test.ts

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,20 @@
11
import { describe, it, expect, jest } from '@jest/globals';
22
import z from 'zod';
33

4+
// Helper function to create mock request with Base64 encoded input
5+
const createMockRequest = (input: any) => {
6+
const serializedInput = JSON.stringify(input, (key, value) => {
7+
if (Number.isNaN(value)) {
8+
return '__NAN__';
9+
}
10+
return value;
11+
});
12+
const encoded = btoa(serializedInput);
13+
const searchParams = new URLSearchParams();
14+
searchParams.set('input', encoded);
15+
return { nextUrl: { searchParams } };
16+
};
17+
418
// Mock the dependencies we can't import in test environment
519
jest.mock('next/server', () => ({
620
NextResponse: {
@@ -119,8 +133,7 @@ describe('create-trpc-api', () => {
119133
getUser: endpoint.input(schema).action(mockAction)
120134
});
121135

122-
const searchParams = new URLSearchParams('id=123');
123-
const mockRequest = { nextUrl: { searchParams } };
136+
const mockRequest = createMockRequest({ id: '123' });
124137
const handler = createTrpcAPI({ router: testRouter });
125138

126139
await handler(mockRequest as any, { params: Promise.resolve({ trpc: 'get-user' }) });
@@ -162,8 +175,7 @@ describe('create-trpc-api', () => {
162175
getUser: endpoint.input(schema).action(mockAction)
163176
});
164177

165-
const searchParams = new URLSearchParams('id=');
166-
const mockRequest = { nextUrl: { searchParams } };
178+
const mockRequest = createMockRequest({ id: '' });
167179
const handler = createTrpcAPI({ router: testRouter });
168180

169181
await handler(mockRequest as any, { params: Promise.resolve({ trpc: 'get-user' }) });
@@ -220,8 +232,7 @@ describe('create-trpc-api', () => {
220232
createUser: endpoint.input(schema).action(mockAction)
221233
});
222234

223-
const searchParams = new URLSearchParams('name=John&age=30');
224-
const mockRequest = { nextUrl: { searchParams } };
235+
const mockRequest = createMockRequest({ name: 'John', age: '30' });
225236
const handler = createTrpcAPI({ router: testRouter });
226237

227238
await handler(mockRequest as any, { params: Promise.resolve({ trpc: 'create-user' }) });

src/__tests__/create-trpc-client.test.ts

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,18 @@ import { createTrpcClient, getTrpcFetch } from '../create-trpc-client';
33
import { endpoint, router } from '../core';
44
import z from 'zod';
55

6+
// Helper function to create expected URL with Base64 encoding
7+
const createExpectedUrl = (baseUrl: string, input: any) => {
8+
const serializedInput = JSON.stringify(input, (key, value) => {
9+
if (Number.isNaN(value)) {
10+
return '__NAN__';
11+
}
12+
return value;
13+
});
14+
const encoded = btoa(serializedInput);
15+
return `${baseUrl}?input=${encodeURIComponent(encoded)}`;
16+
};
17+
618
// Mock fetch
719
const mockFetch = jest.fn() as jest.MockedFunction<typeof fetch>;
820
global.fetch = mockFetch;
@@ -58,7 +70,7 @@ describe('create-trpc-client', () => {
5870
const result = await fetchFn({ id: '123', active: true });
5971

6072
expect(mockFetch).toHaveBeenCalledWith(
61-
'http://localhost:3000/api/trpc/get-user?id=123&active=true',
73+
createExpectedUrl('http://localhost:3000/api/trpc/get-user', { id: '123', active: true }),
6274
{
6375
method: 'GET',
6476
headers: {
@@ -206,7 +218,7 @@ describe('create-trpc-client', () => {
206218
await fetchFn(complexInput);
207219

208220
expect(mockFetch).toHaveBeenCalledWith(
209-
'http://localhost:3000/api/trpc/create-user?name=John+Doe&age=30&active=true&metadata=%7B%22key%22%3A%22value%22%7D',
221+
createExpectedUrl('http://localhost:3000/api/trpc/create-user', complexInput),
210222
expect.any(Object)
211223
);
212224
});
@@ -302,7 +314,7 @@ describe('create-trpc-client', () => {
302314
// Call with parameters
303315
await (client as any).withParams.fetch({ id: '123' });
304316
expect(mockFetch).toHaveBeenCalledWith(
305-
'http://localhost:3000/api/trpc/with-params?id=123',
317+
createExpectedUrl('http://localhost:3000/api/trpc/with-params', { id: '123' }),
306318
expect.any(Object)
307319
);
308320
});

src/__tests__/create-trpc-query-client.test.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,18 @@ import { createTrpcQueryClient } from '../create-trpc-query-client';
33
import { endpoint, router } from '../core';
44
import z from 'zod';
55

6+
// Helper function to create expected URL with Base64 encoding
7+
const createExpectedUrl = (baseUrl: string, input: any) => {
8+
const serializedInput = JSON.stringify(input, (key, value) => {
9+
if (Number.isNaN(value)) {
10+
return '__NAN__';
11+
}
12+
return value;
13+
});
14+
const encoded = btoa(serializedInput);
15+
return `${baseUrl}?input=${encodeURIComponent(encoded)}`;
16+
};
17+
618
// Mock fetch
719
const mockFetch = jest.fn() as jest.MockedFunction<typeof fetch>;
820
global.fetch = mockFetch;
@@ -62,7 +74,7 @@ describe('create-trpc-query-client', () => {
6274
const result = await (client as any).getUser.fetch({ id: '123' });
6375

6476
expect(mockFetch).toHaveBeenCalledWith(
65-
'http://localhost:3000/api/trpc/get-user?id=123',
77+
createExpectedUrl('http://localhost:3000/api/trpc/get-user', { id: '123' }),
6678
{
6779
method: 'GET',
6880
headers: {

0 commit comments

Comments
 (0)