mirror of
https://github.com/logto-io/logto.git
synced 2024-12-30 20:33:54 -05:00
refactor(core): use const pagination key name for swagger (#5175)
This commit is contained in:
parent
4022e4dbc7
commit
d469e9499c
2 changed files with 14 additions and 9 deletions
|
@ -27,6 +27,8 @@ export const isPaginationMiddleware = <Type extends IMiddleware>(
|
|||
): function_ is WithPaginationContext<Type> => function_.name === 'paginationMiddleware';
|
||||
|
||||
export const fallbackDefaultPageSize = 20;
|
||||
export const pageNumberKey = 'page';
|
||||
export const pageSizeKey = 'page_size';
|
||||
|
||||
export default function koaPagination<StateT, ContextT, ResponseBodyT>({
|
||||
defaultPageSize = fallbackDefaultPageSize,
|
||||
|
@ -42,16 +44,16 @@ export default function koaPagination<StateT, ContextT, ResponseBodyT>({
|
|||
try {
|
||||
const {
|
||||
request: {
|
||||
query: { page, page_size },
|
||||
query: { [pageNumberKey]: rawPageNumber, [pageSizeKey]: rawPageSize },
|
||||
},
|
||||
} = ctx;
|
||||
// If isOptional is set to true, user can disable pagination by
|
||||
// set both `page` and `page_size` to empty
|
||||
const disabled = !page && !page_size && isOptional;
|
||||
const disabled = !rawPageNumber && !rawPageSize && isOptional;
|
||||
// Query values are all string, need to convert to number first.
|
||||
const pageNumber = page ? number().positive().parse(Number(page)) : 1;
|
||||
const pageSize = page_size
|
||||
? number().positive().max(maxPageSize).parse(Number(page_size))
|
||||
const pageNumber = rawPageNumber ? number().positive().parse(Number(rawPageNumber)) : 1;
|
||||
const pageSize = rawPageSize
|
||||
? number().positive().max(maxPageSize).parse(Number(rawPageSize))
|
||||
: defaultPageSize;
|
||||
|
||||
ctx.pagination = { offset: (pageNumber - 1) * pageSize, limit: pageSize, disabled };
|
||||
|
|
|
@ -4,7 +4,11 @@ import { type OpenAPIV3 } from 'openapi-types';
|
|||
import pluralize from 'pluralize';
|
||||
import { z } from 'zod';
|
||||
|
||||
import { fallbackDefaultPageSize } from '#src/middleware/koa-pagination.js';
|
||||
import {
|
||||
fallbackDefaultPageSize,
|
||||
pageNumberKey,
|
||||
pageSizeKey,
|
||||
} from '#src/middleware/koa-pagination.js';
|
||||
import assertThat from '#src/utils/assert-that.js';
|
||||
import { zodTypeToSwagger } from '#src/utils/zod.js';
|
||||
|
||||
|
@ -12,10 +16,9 @@ import { getRootComponent } from './general.js';
|
|||
|
||||
export type ParameterArray = Array<OpenAPIV3.ReferenceObject | OpenAPIV3.ParameterObject>;
|
||||
|
||||
// TODO: Generate pagination parameters according to the config.
|
||||
export const paginationParameters: OpenAPIV3.ParameterObject[] = [
|
||||
{
|
||||
name: 'page',
|
||||
name: pageNumberKey,
|
||||
in: 'query',
|
||||
description: 'Page number (starts from 1).',
|
||||
required: false,
|
||||
|
@ -26,7 +29,7 @@ export const paginationParameters: OpenAPIV3.ParameterObject[] = [
|
|||
},
|
||||
},
|
||||
{
|
||||
name: 'page_size',
|
||||
name: pageSizeKey,
|
||||
in: 'query',
|
||||
description: 'Entries per page.',
|
||||
required: false,
|
||||
|
|
Loading…
Reference in a new issue