0
Fork 0
mirror of https://github.com/logto-io/logto.git synced 2024-12-30 20:33:54 -05:00

refactor(cloud): guard api response

This commit is contained in:
Gao Sun 2023-02-18 18:54:07 +08:00
parent 1808a25570
commit 9a3b3f4bae
No known key found for this signature in database
GPG key ID: 13EBE123E4773688
3 changed files with 19 additions and 11 deletions

View file

@ -28,7 +28,8 @@
"dotenv": "^16.0.0",
"find-up": "^6.3.0",
"http-proxy": "^1.18.1",
"mime-types": "^2.1.35"
"mime-types": "^2.1.35",
"zod": "^3.20.2"
},
"devDependencies": {
"@silverhand/eslint-config": "2.0.1",

View file

@ -1,4 +1,5 @@
import { Router } from '@withtyped/server';
import { z } from 'zod';
import { client } from '#src/queries/index.js';
import { createTenantsQueries } from '#src/queries/tenants.js';
@ -6,15 +7,19 @@ import { getTenantIdFromManagementApiIndicator } from '#src/utils/tenant.js';
const { getManagementApiLikeIndicatorsForUser } = createTenantsQueries(client);
export const tenants = new Router('/tenants').get('/', {}, async (context, next) => {
const { rows } = await getManagementApiLikeIndicatorsForUser('some_user_id');
export const tenants = new Router('/tenants').get(
'/',
{ response: z.object({ id: z.string(), indicator: z.string() }).array() },
async (context, next) => {
const { rows } = await getManagementApiLikeIndicatorsForUser('some_user_id');
const tenants = rows
.map(({ indicator }) => ({
id: getTenantIdFromManagementApiIndicator(indicator),
indicator,
}))
.filter((tenant): tenant is { id: string; indicator: string } => Boolean(tenant.id));
const tenants = rows
.map(({ indicator }) => ({
id: getTenantIdFromManagementApiIndicator(indicator),
indicator,
}))
.filter((tenant): tenant is { id: string; indicator: string } => Boolean(tenant.id));
return next({ ...context, json: tenants });
});
return next({ ...context, json: tenants });
}
);

View file

@ -126,6 +126,7 @@ importers:
nodemon: ^2.0.19
prettier: ^2.8.1
typescript: ^4.9.4
zod: ^3.20.2
dependencies:
'@logto/schemas': link:../schemas
'@logto/shared': link:../shared
@ -137,6 +138,7 @@ importers:
find-up: 6.3.0
http-proxy: 1.18.1
mime-types: 2.1.35
zod: 3.20.2
devDependencies:
'@silverhand/eslint-config': 2.0.1_kjzxg5porcw5dx54sezsklj5cy
'@silverhand/ts-config': 2.0.2_typescript@4.9.4