0
Fork 0
mirror of https://github.com/logto-io/logto.git synced 2025-02-17 22:04:19 -05:00

refactor(core): use isTrue() to check params (#2321)

This commit is contained in:
Gao Sun 2022-11-04 22:25:58 +08:00 committed by GitHub
parent 68f2d56a24
commit 877eb892c9
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 12 additions and 8 deletions

View file

@ -1,2 +1,4 @@
export const isTrue = (value: string) =>
['1', 'true', 'y', 'yes', 'yep', 'yeah'].includes(value.toLowerCase());
export const isTrue = (value?: string) =>
// We need to leverage the native type guard
// eslint-disable-next-line no-implicit-coercion
!!value && ['1', 'true', 'y', 'yes', 'yep', 'yeah'].includes(value.toLowerCase());

View file

@ -3,7 +3,7 @@ import { has } from '@silverhand/essentials';
import type { MiddlewareType } from 'koa';
import koaBody from 'koa-body';
import type { IMiddleware, IRouterParamContext } from 'koa-router';
import type { ZodType } from 'zod';
import type { ZodType, ZodTypeDef } from 'zod';
import envSet from '@/env-set';
import RequestError from '@/errors/RequestError';
@ -48,7 +48,7 @@ export const isGuardMiddleware = <Type extends IMiddleware>(
): function_ is WithGuardConfig<Type> =>
function_.name === 'guardMiddleware' && has(function_, 'config');
const tryParse = <Output, Definition, Input>(
const tryParse = <Output, Definition extends ZodTypeDef, Input>(
type: 'query' | 'body' | 'params',
guard: Optional<ZodType<Output, Definition, Input>>,
data: unknown

View file

@ -4,6 +4,7 @@ import { has } from '@silverhand/essentials';
import pick from 'lodash.pick';
import { literal, object, string } from 'zod';
import { isTrue } from '@/env-set/parameters';
import RequestError from '@/errors/RequestError';
import { encryptUserPassword, generateUserId, insertUser } from '@/lib/user';
import koaGuard from '@/middleware/koa-guard';
@ -29,8 +30,9 @@ export default function adminUserRoutes<T extends AuthedRouter>(router: T) {
koaGuard({
query: object({
search: string().optional(),
hideAdminUser: literal('true').optional(),
isCaseSensitive: literal('true').optional(),
// Use `.transform()` once the type issue fixed
hideAdminUser: string().optional(),
isCaseSensitive: string().optional(),
}),
}),
async (ctx, next) => {
@ -39,8 +41,8 @@ export default function adminUserRoutes<T extends AuthedRouter>(router: T) {
query: { search, hideAdminUser: _hideAdminUser, isCaseSensitive: _isCaseSensitive },
} = ctx.guard;
const hideAdminUser = _hideAdminUser === 'true';
const isCaseSensitive = _isCaseSensitive === 'true';
const hideAdminUser = isTrue(_hideAdminUser);
const isCaseSensitive = isTrue(_isCaseSensitive);
const [{ count }, users] = await Promise.all([
countUsers(search, hideAdminUser, isCaseSensitive),
findUsers(limit, offset, search, hideAdminUser, isCaseSensitive),