@nodeswork/sbase
Version:
Basic REST api foundation from Nodeswork.
127 lines (126 loc) • 4.61 kB
TypeScript
import { IMiddleware, IRouterContext } from 'koa-router';
import { ModelPopulateOptions } from 'mongoose';
import * as model from './model';
export declare const READONLY = "READONLY";
export declare const AUTOGEN = "AUTOGEN";
export declare type KoaMiddlewaresType = typeof KoaMiddlewares;
export declare class KoaMiddlewares extends model.DocumentModel {
static createMiddleware(options: CreateOptions): IMiddleware;
/**
* Returns Koa get middleware.
*
* Examples:
*
* 1. Load from ctx.params. This is the most common case where the url path
* stores the model id.
*
* @Post('/articles/:articleId')
* getArticle = models.Article.getMiddleware({ field: 'articleId' });
*
* 2. Load from ctx.request.
*
* // When there is a dot in the field path, it will load from ctx.request.
* @Middleware(models.Article.getMiddleware({ field: 'body.articleId' }));
*
* 3. No need to specify id.
* // Pass a star.
* @Middleware(models.Article.getMiddleware({ field: '*' }));
*
* @param options.field specifies which field to load the id key value.
* @param options.idFieldName specifies the field name in query.
* Default: '_id'.
* @param options.target specifies which field under ctx to set the target.
* Default: 'object'.
* @param options.triggerNext specifies whether to trigger next middleware.
* Default: false.
* @param options.transform a map function before send to ctx.body.
*/
static getMiddleware(options: GetOptions): IMiddleware;
/**
* Returns KOA find middleware.
*
* Examples:
*
* 1. Normal query.
*
* @Get('/articles')
* find = models.Article.findMiddleware();
*
* 2. Pagination. User query.size, query.page to calculate numbers to skip.
*
* @Get('/articles')
* find = models.Article.findMiddleware({
* pagination: {
* size: 50, // single page size
* sizeChoices: [50, 100, 200],
* // where to store the full IPaginationData<any>.
* target: 'articlesWithPagination',
* },
* })
*
* @param options.pagination.size specifies max number of returning records.
* @param options.pagination.sizeChoices
* @param options.pagination.target specifies where to store the full data.
* @param options.sort specifies the returning order.
* @param options.level specifies the data level.
* @param options.project specifies the projection.
* @param options.populate specifies the populates.
*/
static findMiddleware(options?: FindOptions): IMiddleware;
static updateMiddleware(options: UpdateOptions): IMiddleware;
static deleteMiddleware(options: DeleteOptions): IMiddleware;
}
export interface CommonOptions {
noBody?: boolean;
triggerNext?: boolean;
target?: string;
lean?: boolean;
transform?: (a: any, ctx: IRouterContext) => any | Promise<any>;
}
export interface CommonResponseOptions {
level?: string;
project?: string[];
populate?: ModelPopulateOptions | ModelPopulateOptions[];
}
export interface CommonReadOptions {
}
export interface CommonWriteOptions {
omits?: string[];
}
export interface CreateOptions extends CommonOptions, CommonResponseOptions, CommonWriteOptions {
}
/**
* When read/write/delete to a single item.
*/
export interface SingleItemOptions {
field: string;
idFieldName?: string;
nullable?: boolean;
}
/**
* Get Middleware Options.
*/
export interface GetOptions extends CommonOptions, CommonResponseOptions, CommonReadOptions, SingleItemOptions {
}
export interface FindOptions extends CommonOptions, CommonResponseOptions, CommonReadOptions {
pagination?: {
size?: number;
sizeChoices?: number[];
target?: string;
};
sort?: object;
}
export interface UpdateOptions extends CommonOptions, CommonResponseOptions, CommonWriteOptions, SingleItemOptions {
}
export interface DeleteOptions extends CommonOptions, SingleItemOptions {
}
export declare type INext = () => Promise<any>;
export declare function Autogen(schema?: any): PropertyDecorator;
export declare function Readonly(schema?: any): PropertyDecorator;
export interface PaginationData<T> {
pageSize: number;
page: number;
total: number;
data: T[];
}
export declare function isPaginationData<T>(data: PaginationData<T> | any): data is PaginationData<T>;