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';
|
): function_ is WithPaginationContext<Type> => function_.name === 'paginationMiddleware';
|
||||||
|
|
||||||
export const fallbackDefaultPageSize = 20;
|
export const fallbackDefaultPageSize = 20;
|
||||||
|
export const pageNumberKey = 'page';
|
||||||
|
export const pageSizeKey = 'page_size';
|
||||||
|
|
||||||
export default function koaPagination<StateT, ContextT, ResponseBodyT>({
|
export default function koaPagination<StateT, ContextT, ResponseBodyT>({
|
||||||
defaultPageSize = fallbackDefaultPageSize,
|
defaultPageSize = fallbackDefaultPageSize,
|
||||||
|
@ -42,16 +44,16 @@ export default function koaPagination<StateT, ContextT, ResponseBodyT>({
|
||||||
try {
|
try {
|
||||||
const {
|
const {
|
||||||
request: {
|
request: {
|
||||||
query: { page, page_size },
|
query: { [pageNumberKey]: rawPageNumber, [pageSizeKey]: rawPageSize },
|
||||||
},
|
},
|
||||||
} = ctx;
|
} = ctx;
|
||||||
// If isOptional is set to true, user can disable pagination by
|
// If isOptional is set to true, user can disable pagination by
|
||||||
// set both `page` and `page_size` to empty
|
// 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.
|
// Query values are all string, need to convert to number first.
|
||||||
const pageNumber = page ? number().positive().parse(Number(page)) : 1;
|
const pageNumber = rawPageNumber ? number().positive().parse(Number(rawPageNumber)) : 1;
|
||||||
const pageSize = page_size
|
const pageSize = rawPageSize
|
||||||
? number().positive().max(maxPageSize).parse(Number(page_size))
|
? number().positive().max(maxPageSize).parse(Number(rawPageSize))
|
||||||
: defaultPageSize;
|
: defaultPageSize;
|
||||||
|
|
||||||
ctx.pagination = { offset: (pageNumber - 1) * pageSize, limit: pageSize, disabled };
|
ctx.pagination = { offset: (pageNumber - 1) * pageSize, limit: pageSize, disabled };
|
||||||
|
|
|
@ -4,7 +4,11 @@ import { type OpenAPIV3 } from 'openapi-types';
|
||||||
import pluralize from 'pluralize';
|
import pluralize from 'pluralize';
|
||||||
import { z } from 'zod';
|
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 assertThat from '#src/utils/assert-that.js';
|
||||||
import { zodTypeToSwagger } from '#src/utils/zod.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>;
|
export type ParameterArray = Array<OpenAPIV3.ReferenceObject | OpenAPIV3.ParameterObject>;
|
||||||
|
|
||||||
// TODO: Generate pagination parameters according to the config.
|
|
||||||
export const paginationParameters: OpenAPIV3.ParameterObject[] = [
|
export const paginationParameters: OpenAPIV3.ParameterObject[] = [
|
||||||
{
|
{
|
||||||
name: 'page',
|
name: pageNumberKey,
|
||||||
in: 'query',
|
in: 'query',
|
||||||
description: 'Page number (starts from 1).',
|
description: 'Page number (starts from 1).',
|
||||||
required: false,
|
required: false,
|
||||||
|
@ -26,7 +29,7 @@ export const paginationParameters: OpenAPIV3.ParameterObject[] = [
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: 'page_size',
|
name: pageSizeKey,
|
||||||
in: 'query',
|
in: 'query',
|
||||||
description: 'Entries per page.',
|
description: 'Entries per page.',
|
||||||
required: false,
|
required: false,
|
||||||
|
|
Loading…
Reference in a new issue