fastify-mongoose-rest
Version:
Rest API generator tools for fastify and mongoose
211 lines (210 loc) • 10.8 kB
TypeScript
/// <reference types="node" />
import { Model } from 'mongoose';
import { FastifyMongooseRestOptions } from './types';
/**
* Create a new FastifyMongooseRest instance
*
* @param basePath Base path for the routes. It will be prefixed to all routes
* @param model Mongoose model to use
* @param options Options for the routes
* @returns FastifyMongooseRest instance
*
* @example
* ```ts
* import FastifyMongooseRest from 'fastify-mongoose-rest';
* import Cat from './models/cat';
*
* // If the base path does not include a slash at the start, it will be added
* const fastifyMongooseRest = FastifyMongooseRest('/cats', Cat);
* ```
*/
export default function FastifyMongooseRest<T>(basePath: string, model: Model<T>, options?: FastifyMongooseRestOptions): {
/**
* Route for creating a new resource
*
* @description
* Validates input with given {@link FastifyMongooseRestOptions.validationSchema validation schema} and returns the created resource
*
* @see {@link Model.create Mongooose create method}
*/
create: {
method: "POST";
url: string;
schema: {
summary: string;
tags?: string[] | undefined;
body: object;
response: object;
};
handler: (request: import("fastify").FastifyRequest<import("fastify").RouteGenericInterface, import("fastify").RawServerDefault, import("http").IncomingMessage, import("fastify").FastifySchema, import("fastify").FastifyTypeProviderDefault, unknown, import("fastify").FastifyBaseLogger, import("fastify/types/type-provider").ResolveFastifyRequestType<import("fastify").FastifyTypeProviderDefault, import("fastify").FastifySchema, import("fastify").RouteGenericInterface>>, reply: import("fastify").FastifyReply<import("fastify").RawServerDefault, import("http").IncomingMessage, import("http").ServerResponse<import("http").IncomingMessage>, import("fastify").RouteGenericInterface, unknown, import("fastify").FastifySchema, import("fastify").FastifyTypeProviderDefault, unknown>) => Promise<any>;
};
/**
* Route for deleting a resource by it's identifier
*
* @description
* Deletes a resource by _id or {@link FastifyMongooseRestOptions.findProperty findProperty option}
*
* @see {@link Model.deleteOne Mongooose deleteOne method}
*/
delete: {
method: "DELETE";
url: string;
schema: {
summary: string;
tags?: string[] | undefined;
params: object;
response: object;
};
handler: (request: import("fastify").FastifyRequest<{
Params: {
id: string;
};
}, import("fastify").RawServerDefault, import("http").IncomingMessage, import("fastify").FastifySchema, import("fastify").FastifyTypeProviderDefault, unknown, import("fastify").FastifyBaseLogger, import("fastify/types/type-provider").ResolveFastifyRequestType<import("fastify").FastifyTypeProviderDefault, import("fastify").FastifySchema, {
Params: {
id: string;
};
}>>, reply: import("fastify").FastifyReply<import("fastify").RawServerDefault, import("http").IncomingMessage, import("http").ServerResponse<import("http").IncomingMessage>, import("fastify").RouteGenericInterface, unknown, import("fastify").FastifySchema, import("fastify").FastifyTypeProviderDefault, unknown>) => Promise<any>;
};
/**
* Route for displaying a single resource by it's identifier
*
* @description
* Returns a resource by _id or {@link FastifyMongooseRestOptions.findProperty findProperty option}
*
* @see {@link Model.findOne Mongooose findOne method}
*/
details: {
method: "GET";
url: string;
schema: {
summary: string;
tags?: string[] | undefined;
params: object;
querystring: object;
response: object;
};
handler: (request: import("fastify").FastifyRequest<{
Params: {
id: string;
};
Querystring: {
populate?: string | undefined;
projection?: string | undefined;
select?: string | undefined;
};
}, import("fastify").RawServerDefault, import("http").IncomingMessage, import("fastify").FastifySchema, import("fastify").FastifyTypeProviderDefault, unknown, import("fastify").FastifyBaseLogger, import("fastify/types/type-provider").ResolveFastifyRequestType<import("fastify").FastifyTypeProviderDefault, import("fastify").FastifySchema, {
Params: {
id: string;
};
Querystring: {
populate?: string | undefined;
projection?: string | undefined;
select?: string | undefined;
};
}>>, reply: import("fastify").FastifyReply<import("fastify").RawServerDefault, import("http").IncomingMessage, import("http").ServerResponse<import("http").IncomingMessage>, import("fastify").RouteGenericInterface, unknown, import("fastify").FastifySchema, import("fastify").FastifyTypeProviderDefault, unknown>) => Promise<any>;
};
/**
* Route for displaying modifying a resource by it's identifier
*
* @description
* Validates input with given {@link FastifyMongooseRestOptions.validationSchema validation schema} and returns the modified resource
*
* @see {@link Model.findOne Mongooose findOne method}
* @see {@link Model.updateOne Mongooose updateOne method}
*/
modify: {
method: "PATCH";
url: string;
schema: {
summary: string;
tags?: string[] | undefined;
params: object;
body: object;
response: object;
};
handler: (request: import("fastify").FastifyRequest<{
Params: {
id: string;
};
Body: {
[index: string]: any;
};
}, import("fastify").RawServerDefault, import("http").IncomingMessage, import("fastify").FastifySchema, import("fastify").FastifyTypeProviderDefault, unknown, import("fastify").FastifyBaseLogger, import("fastify/types/type-provider").ResolveFastifyRequestType<import("fastify").FastifyTypeProviderDefault, import("fastify").FastifySchema, {
Params: {
id: string;
};
Body: {
[index: string]: any;
};
}>>, reply: import("fastify").FastifyReply<import("fastify").RawServerDefault, import("http").IncomingMessage, import("http").ServerResponse<import("http").IncomingMessage>, import("fastify").RouteGenericInterface, unknown, import("fastify").FastifySchema, import("fastify").FastifyTypeProviderDefault, unknown>) => Promise<any>;
};
/**
* Route for querying a list of resources
*
* @description
* Returns a list of resources defined by the query parameters used in the request
*
* @see {@link Model.find Mongooose find method}
*/
list: {
method: "GET";
url: string;
schema: {
summary: string;
tags?: string[] | undefined;
querystring: object;
response: object;
};
handler: (request: import("fastify").FastifyRequest<{
Querystring: import("./types").FindQueryOptions;
}, import("fastify").RawServerDefault, import("http").IncomingMessage, import("fastify").FastifySchema, import("fastify").FastifyTypeProviderDefault, unknown, import("fastify").FastifyBaseLogger, import("fastify/types/type-provider").ResolveFastifyRequestType<import("fastify").FastifyTypeProviderDefault, import("fastify").FastifySchema, {
Querystring: import("./types").FindQueryOptions;
}>>, reply: import("fastify").FastifyReply<import("fastify").RawServerDefault, import("http").IncomingMessage, import("http").ServerResponse<import("http").IncomingMessage>, import("fastify").RouteGenericInterface, unknown, import("fastify").FastifySchema, import("fastify").FastifyTypeProviderDefault, unknown>) => Promise<any>;
};
/**
* Route for searching a list of resources
*
* @description
* Returns a list of resources defined by the body used in the request
*
* @see {@link Model.find Mongooose find method}
*/
search: {
method: "POST";
url: string;
schema: {
summary: string;
tags?: string[] | undefined;
body: object;
response: object;
};
handler: (request: import("fastify").FastifyRequest<{
Body: import("./types").FindOptions;
}, import("fastify").RawServerDefault, import("http").IncomingMessage, import("fastify").FastifySchema, import("fastify").FastifyTypeProviderDefault, unknown, import("fastify").FastifyBaseLogger, import("fastify/types/type-provider").ResolveFastifyRequestType<import("fastify").FastifyTypeProviderDefault, import("fastify").FastifySchema, {
Body: import("./types").FindOptions;
}>>, reply: import("fastify").FastifyReply<import("fastify").RawServerDefault, import("http").IncomingMessage, import("http").ServerResponse<import("http").IncomingMessage>, import("fastify").RouteGenericInterface, unknown, import("fastify").FastifySchema, import("fastify").FastifyTypeProviderDefault, unknown>) => Promise<any>;
};
/**
* Route for inserting many resources
*
* @description
* Validates input with given {@link FastifyMongooseRestOptions.validationSchema validation schema} and returns the created resources
*
* @see {@link Model.insertMany Mongooose insertMany method}
*/
insertMany: {
method: "POST";
url: string;
schema: {
summary: string;
tags?: string[] | undefined;
body: object[];
response: object;
};
handler: (request: import("fastify").FastifyRequest<{
Body: object[];
}, import("fastify").RawServerDefault, import("http").IncomingMessage, import("fastify").FastifySchema, import("fastify").FastifyTypeProviderDefault, unknown, import("fastify").FastifyBaseLogger, import("fastify/types/type-provider").ResolveFastifyRequestType<import("fastify").FastifyTypeProviderDefault, import("fastify").FastifySchema, {
Body: object[];
}>>, reply: import("fastify").FastifyReply<import("fastify").RawServerDefault, import("http").IncomingMessage, import("http").ServerResponse<import("http").IncomingMessage>, import("fastify").RouteGenericInterface, unknown, import("fastify").FastifySchema, import("fastify").FastifyTypeProviderDefault, unknown>) => Promise<any>;
};
};