From 44e1c00a1a4780728f51b1fd28b1fabca356ce23 Mon Sep 17 00:00:00 2001 From: jrea Date: Wed, 16 Oct 2024 14:29:53 -0400 Subject: [PATCH] fix(server): pass query params along --- packages/react/package.json | 2 +- packages/server/src/api/routes/users/POST.ts | 3 ++- .../src/api/routes/users/apiUsers.test.ts | 20 +++++++++++++++++ .../server/src/api/utils/routes/apiRoutes.ts | 4 ++-- .../src/api/utils/routes/makeRestUrl.ts | 22 ++++++++++++++++--- yarn.lock | 2 +- 6 files changed, 45 insertions(+), 8 deletions(-) diff --git a/packages/react/package.json b/packages/react/package.json index 38c91e44..50d59505 100644 --- a/packages/react/package.json +++ b/packages/react/package.json @@ -30,7 +30,7 @@ "test": "yarn jest" }, "peerDependencies": { - "@tanstack/react-query": "^4.23.0", + "@tanstack/react-query": "^5.23.0", "next-auth": "^4.24.7", "react": "^18.0.0", "react-dom": "^18.0.0" diff --git a/packages/server/src/api/routes/users/POST.ts b/packages/server/src/api/routes/users/POST.ts index 139ce140..99198f2e 100644 --- a/packages/server/src/api/routes/users/POST.ts +++ b/packages/server/src/api/routes/users/POST.ts @@ -73,9 +73,10 @@ export async function POST( init.method = 'POST'; const yurl = new URL(init.request.url); const tenantId = yurl.searchParams.get('tenantId'); + const newTenantName = yurl.searchParams.get('newTenantName'); const tenant = tenantId ?? getTenantFromHttp(init.request.headers); - const url = apiRoutes(config).USERS(tenant ? tenant : undefined); + const url = apiRoutes(config).USERS({ tenantId: tenant, newTenantName }); return await request(url, init, config); } diff --git a/packages/server/src/api/routes/users/apiUsers.test.ts b/packages/server/src/api/routes/users/apiUsers.test.ts index 7cedb4e5..8b4a4861 100644 --- a/packages/server/src/api/routes/users/apiUsers.test.ts +++ b/packages/server/src/api/routes/users/apiUsers.test.ts @@ -38,6 +38,26 @@ describe('users route', () => { expect.objectContaining({}) ); }); + + it('should post to v2 users with tenantId and newTenantName', async () => { + const _res = new Request( + 'http://thenile.dev?tenantId=123&newTenantName=456', + { method: 'POST' } + ); + await route( + _res, + new Config({ + api: { + basePath: 'http://thenile.dev/v2/databases/testdb', + }, + }) + ); + expect(utilRequest).toHaveBeenCalledWith( + 'http://thenile.dev/v2/databases/testdb/users?tenantId=123&newTenantName=456', + expect.objectContaining({ method: 'POST' }), + expect.objectContaining({}) + ); + }); it('should GET to v2 tenant users with params', async () => { const _res = new Request('http://localhost:3000?tenantId=123', {}); await route( diff --git a/packages/server/src/api/utils/routes/apiRoutes.ts b/packages/server/src/api/utils/routes/apiRoutes.ts index 4e5bb621..67a20c43 100644 --- a/packages/server/src/api/utils/routes/apiRoutes.ts +++ b/packages/server/src/api/utils/routes/apiRoutes.ts @@ -6,8 +6,8 @@ type ApiRouteKeys = keyof typeof apiRoutes; export type ApiRoutePaths = (typeof apiRoutes)[ApiRouteKeys]; export const apiRoutes = (config: Config) => ({ ME: makeRestUrl(config, '/me'), - USERS: (tenantId?: string) => - makeRestUrl(config, '/users', tenantId ? { tenantId } : undefined), + USERS: (qp: { tenantId?: null | string; newTenantName?: null | string }) => + makeRestUrl(config, '/users', qp), USER: (userId: string) => makeRestUrl(config, `/users/${userId}`), TENANTS: makeRestUrl(config, '/tenants'), TENANT: (tenantId: string) => makeRestUrl(config, `/tenants/${tenantId}`), diff --git a/packages/server/src/api/utils/routes/makeRestUrl.ts b/packages/server/src/api/utils/routes/makeRestUrl.ts index 156a31ad..2938b35b 100644 --- a/packages/server/src/api/utils/routes/makeRestUrl.ts +++ b/packages/server/src/api/utils/routes/makeRestUrl.ts @@ -2,10 +2,23 @@ import { Config } from '../../../utils/Config'; const NILEDB_API_URL = process.env.NILEDB_API_URL; +function filterNullUndefined( + obj?: Record +): { [k: string]: string | null } | undefined { + if (!obj) { + return undefined; + } + return Object.fromEntries( + Object.entries(obj).filter( + ([, value]) => value !== null && value !== undefined + ) + ); +} + export function makeRestUrl( config: Config, path: string, - qp?: Record + qp?: Record ) { const url = config.api.basePath || NILEDB_API_URL; if (!url) { @@ -13,8 +26,11 @@ export function makeRestUrl( 'An API url is required. Set it via NILEDB_API_URL. Was auto configuration run?' ); } - const params = new URLSearchParams(qp); + const params = new URLSearchParams( + filterNullUndefined(qp) as Record + ); + const strParams = params.toString(); return `${[url, path.substring(1, path.length)].join('/')}${ - qp ? `?${params.toString()}` : '' + strParams ? `?${strParams}` : '' }`; } diff --git a/yarn.lock b/yarn.lock index a6d64d7d..fd642302 100644 --- a/yarn.lock +++ b/yarn.lock @@ -3050,7 +3050,7 @@ __metadata: typescript: "npm:^5.3.2" zod: "npm:^3.23.8" peerDependencies: - "@tanstack/react-query": ^4.23.0 + "@tanstack/react-query": ^5.23.0 next-auth: ^4.24.7 react: ^18.0.0 react-dom: ^18.0.0