(
apiPath: string,
options: TanStackStartOptions<SchemaDef>,
qArg?: unknown,
otherArgs?: any,
)
| 22 | } |
| 23 | |
| 24 | function makeTestClient( |
| 25 | apiPath: string, |
| 26 | options: TanStackStartOptions<SchemaDef>, |
| 27 | qArg?: unknown, |
| 28 | otherArgs?: any, |
| 29 | ): TestClient { |
| 30 | const pathParts = apiPath.split('/').filter((p) => p); |
| 31 | const path = pathParts.join('/'); |
| 32 | |
| 33 | const handler = TanStackStartHandler(options); |
| 34 | |
| 35 | const params = { |
| 36 | _splat: path, |
| 37 | ...otherArgs, |
| 38 | }; |
| 39 | |
| 40 | const buildUrl = (method: string) => { |
| 41 | const baseUrl = `http://localhost${apiPath}`; |
| 42 | if (method === 'GET' || method === 'DELETE') { |
| 43 | const url = new URL(baseUrl); |
| 44 | if (qArg) { |
| 45 | url.searchParams.set('q', JSON.stringify(qArg)); |
| 46 | } |
| 47 | if (otherArgs) { |
| 48 | Object.entries(otherArgs).forEach(([key, value]) => { |
| 49 | url.searchParams.set(key, String(value)); |
| 50 | }); |
| 51 | } |
| 52 | return url.toString(); |
| 53 | } |
| 54 | return baseUrl; |
| 55 | }; |
| 56 | |
| 57 | const executeRequest = async (method: string, body?: any) => { |
| 58 | const url = buildUrl(method); |
| 59 | const request = makeRequest(method, url, body); |
| 60 | const response = await handler({ request, params }); |
| 61 | const responseBody = await unmarshal(response); |
| 62 | return { |
| 63 | status: response.status, |
| 64 | body: responseBody, |
| 65 | }; |
| 66 | }; |
| 67 | |
| 68 | return { |
| 69 | get: async () => executeRequest('GET'), |
| 70 | post: () => ({ |
| 71 | send: async (data: any) => executeRequest('POST', data), |
| 72 | }), |
| 73 | put: () => ({ |
| 74 | send: async (data: any) => executeRequest('PUT', data), |
| 75 | }), |
| 76 | del: async () => executeRequest('DELETE'), |
| 77 | }; |
| 78 | } |
| 79 | |
| 80 | describe('TanStack Start adapter tests - rpc handler', () => { |
| 81 | it('simple crud', async () => { |
no test coverage detected