Skip to content

Commit 35c64db

Browse files
committed
feat: move next path utils out of the core lib
1 parent c5717c8 commit 35c64db

8 files changed

Lines changed: 88 additions & 177 deletions

File tree

‎.changeset/clean-phones-push.md‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
'@ts-rest/core': patch
3+
'@ts-rest/next': patch
4+
---
5+
6+
Move next lib utils out of @ts-rest/core to reduce bundle size

‎libs/ts-rest/core/src/index.ts‎

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
1-
export * from './lib/dsl';
21
export * from './lib/client';
3-
export * from './lib/server-utils';
4-
export * from './lib/type-utils';
5-
export * from './lib/server-utils';
6-
export * from './lib/zod-utils';
7-
export * from './lib/status-codes';
2+
export * from './lib/dsl';
83
export * from './lib/paths';
94
export * from './lib/query';
5+
export * from './lib/status-codes';
6+
export * from './lib/type-utils';
7+
export * from './lib/zod-utils';

‎libs/ts-rest/core/src/lib/server-utils.spec.ts‎

Lines changed: 0 additions & 128 deletions
This file was deleted.
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import { initContract } from '@ts-rest/core';
2+
import { getPathParamsFromArray } from './path-utils';
3+
4+
const c = initContract();
5+
6+
describe('getPathParamsFromArray', () => {
7+
it('should extract params from array', () => {
8+
const appRoute = c.query({
9+
method: 'GET',
10+
path: `/posts/:id`,
11+
query: null,
12+
responses: {
13+
200: c.response<{ message: string }>(),
14+
},
15+
});
16+
17+
const pathParams = getPathParamsFromArray(['posts', '1'], appRoute);
18+
19+
expect(pathParams).toStrictEqual({ id: '1' });
20+
});
21+
22+
it('should extract params from array with many path params', () => {
23+
const appRoute = c.query({
24+
method: 'GET',
25+
path: `/posts/:id/comments/:commentId`,
26+
query: null,
27+
responses: {
28+
200: c.response<{ message: string }>(),
29+
},
30+
});
31+
32+
const pathParams = getPathParamsFromArray(
33+
['posts', '1', 'comments', '2'],
34+
appRoute
35+
);
36+
37+
expect(pathParams).toStrictEqual({ id: '1', commentId: '2' });
38+
});
39+
it('should ignore any query params', () => {
40+
const appRoute = c.query({
41+
method: 'GET',
42+
path: `/posts/:id`,
43+
query: null,
44+
responses: {
45+
200: c.response<{ message: string }>(),
46+
},
47+
});
48+
49+
const pathParams = getPathParamsFromArray(['posts', '1'], appRoute);
50+
51+
expect(pathParams).toStrictEqual({ id: '1' });
52+
});
53+
});
Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,4 @@
1-
import { AppRoute } from './dsl';
2-
3-
/**
4-
* Extract path params from url
5-
* @param url e.g. /posts/1/comments/2?unwanted-query-param=true
6-
* @param appRoute
7-
* @returns
8-
*/
9-
export const getPathParamsFromUrl = (
10-
url: string,
11-
appRoute: AppRoute
12-
): Record<string, string> => {
13-
const baseUrl = url.split('?')[0];
14-
const baseUrlAsArr = baseUrl.split('/').slice(1);
15-
16-
return getPathParamsFromArray(baseUrlAsArr, appRoute);
17-
};
1+
import { AppRoute } from '@ts-rest/core';
182

193
/**
204
* Get path params from array of url segments

‎libs/ts-rest/next/src/lib/test-helpers.ts‎

Lines changed: 0 additions & 23 deletions
This file was deleted.

‎libs/ts-rest/next/src/lib/ts-rest-next.spec.ts‎

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import { initContract } from '@ts-rest/core';
2-
import { NextApiResponse } from 'next';
2+
import { NextApiRequest, NextApiResponse } from 'next';
33
import { createNextRoute, createNextRouter } from './ts-rest-next';
4-
import { mockReq } from './test-helpers';
54

65
const c = initContract();
76

@@ -150,3 +149,25 @@ describe('createNextRouter', () => {
150149
});
151150
});
152151
});
152+
153+
export const mockReq = (
154+
url: string,
155+
args: {
156+
query?: Record<string, string>;
157+
body?: unknown;
158+
method: string;
159+
}
160+
): NextApiRequest => {
161+
const paramArray = url.split('/').splice(1);
162+
163+
const req = {
164+
query: {
165+
...args.query,
166+
['ts-rest']: paramArray,
167+
},
168+
body: args.body,
169+
method: args.method,
170+
} as unknown as NextApiRequest;
171+
172+
return req;
173+
};

‎libs/ts-rest/next/src/lib/ts-rest-next.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,11 @@ import {
1212
AppRouteQuery,
1313
AppRouter,
1414
checkZodSchema,
15-
getPathParamsFromArray,
1615
isAppRoute,
1716
PathParamsWithCustomValidators,
1817
ZodInferOrType,
1918
} from '@ts-rest/core';
19+
import { getPathParamsFromArray } from './path-utils';
2020

2121
type RouteToQueryFunctionImplementation<T extends AppRouteQuery> = (args: {
2222
params: PathParamsWithCustomValidators<T>;

0 commit comments

Comments
 (0)