0
Fork 0
mirror of https://github.com/logto-io/logto.git synced 2025-01-20 21:32:31 -05:00

refactor(core): overwrite response with parsed result (#4759)

* refactor(core): overwrite response with parsed result

* refactor: fix legacy schema issues
This commit is contained in:
Gao Sun 2023-11-08 16:01:23 +08:00 committed by GitHub
parent a3de4fa6bb
commit 397c0146e7
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 13 additions and 18 deletions

View file

@ -166,7 +166,11 @@ export default function koaGuard<
if (response !== undefined) {
const result = response.safeParse(ctx.body);
if (!result.success) {
if (result.success) {
// Overwrite the response body with the parsed one, since it will strip out
// the properties that are not defined in the schema.
ctx.body = result.data;
} else {
if (!EnvSet.values.isProduction) {
consoleLog.error('Invalid response:', result.error);
}

View file

@ -9,7 +9,7 @@ const { jest } = import.meta;
const mockBody = { key: 'a', payload: { key: 'a', result: LogResult.Success }, createdAt: 123 };
const mockLog: Log = { tenantId: 'fake_tenant', id: '1', ...mockBody };
const mockLogs = [mockLog, { id: '2', ...mockBody }];
const mockLogs = [mockLog, { tenantId: 'fake_tenant', id: '2', ...mockBody }];
const logs = {
countLogs: jest.fn().mockResolvedValue({

View file

@ -21,7 +21,7 @@ export default function logRoutes<T extends AuthedRouter>(
applicationId: string().optional(),
logKey: string().optional(),
}),
response: Logs.guard.omit({ tenantId: true }).array(),
response: Logs.guard.array(),
status: 200,
}),
async (ctx, next) => {

View file

@ -5,10 +5,9 @@ import {
featuredUserGuard,
userWithOrganizationRolesGuard,
} from '@logto/schemas';
import { type Optional, cond, yes } from '@silverhand/essentials';
import { yes } from '@silverhand/essentials';
import { z } from 'zod';
import { type SearchOptions } from '#src/database/utils.js';
import { EnvSet } from '#src/env-set/index.js';
import RequestError from '#src/errors/RequestError/index.js';
import koaGuard from '#src/middleware/koa-guard.js';
@ -94,13 +93,7 @@ export default function organizationRoutes<T extends AuthedRouter>(...args: Rout
status: [200, 404],
}),
async (ctx, next) => {
const { q } = ctx.guard.query;
const search: Optional<SearchOptions<(typeof userSearchKeys)[number]>> = cond(
q && {
fields: userSearchKeys,
keyword: q,
}
);
const search = parseSearchOptions(userSearchKeys, ctx.guard.query);
const [totalCount, entities] = await organizations.relations.users.getUsersByOrganizationId(
ctx.guard.params.id,

View file

@ -6,7 +6,7 @@ export const roleNamesGuard = z.string().array();
const identityGuard = z.object({
userId: z.string(),
details: z.object({}).optional(), // Connector's userinfo details, schemaless
details: z.record(z.unknown()).optional(), // Connector's userinfo details, schemaless
});
export const identitiesGuard = z.record(identityGuard);

View file

@ -4,12 +4,10 @@ import {
type OrganizationRole,
OrganizationRoles,
type Organization,
type User,
Organizations,
Users,
} from '../db-entries/index.js';
import { type FeaturedUser } from './user.js';
import { type UserInfo, type FeaturedUser, userInfoGuard } from './user.js';
export type OrganizationRoleWithScopes = OrganizationRole & {
scopes: Array<{
@ -60,13 +58,13 @@ export const organizationWithOrganizationRolesGuard: z.ZodType<OrganizationWithR
* The user entity with the `organizationRoles` field that contains the roles of
* the user in a specific organization.
*/
export type UserWithOrganizationRoles = User & {
export type UserWithOrganizationRoles = UserInfo & {
/** The roles of the user in a specific organization. */
organizationRoles: OrganizationRoleEntity[];
};
export const userWithOrganizationRolesGuard: z.ZodType<UserWithOrganizationRoles> =
Users.guard.extend({
userInfoGuard.extend({
organizationRoles: organizationRoleEntityGuard.array(),
});