Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(server): pass query params along #847

Merged
merged 1 commit into from
Oct 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/react/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
3 changes: 2 additions & 1 deletion packages/server/src/api/routes/users/POST.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
20 changes: 20 additions & 0 deletions packages/server/src/api/routes/users/apiUsers.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
4 changes: 2 additions & 2 deletions packages/server/src/api/utils/routes/apiRoutes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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}`),
Expand Down
22 changes: 19 additions & 3 deletions packages/server/src/api/utils/routes/makeRestUrl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,35 @@ import { Config } from '../../../utils/Config';

const NILEDB_API_URL = process.env.NILEDB_API_URL;

function filterNullUndefined(
obj?: Record<string, string | null>
): { [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<string, string>
qp?: Record<string, string | null>
) {
const url = config.api.basePath || NILEDB_API_URL;
if (!url) {
throw new Error(
'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<string, string>
);
const strParams = params.toString();
return `${[url, path.substring(1, path.length)].join('/')}${
qp ? `?${params.toString()}` : ''
strParams ? `?${strParams}` : ''
}`;
}
2 changes: 1 addition & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down