import { Request } from 'express' ; import { Connection } from 'mongoose' ; import { SchemaOptions } from 'mongoose' ; import { RestRegistry } from '../rest-registry' ; import { RestgooseModel } from '../restgoose-model' ; import { Constructor , HttpMethod , MiddlewareFetch , MiddlewarePersist , MiddlewarePostFetch , MiddlewarePreFetch , MiddlewarePreSave , RestMethodName , RestRequest , } from '../types' ; export function rest < T extends RestgooseModel > ( config ) { return ( target , propertyKey ? ) => { if ( ! propertyKey ) { RestRegistry . registerModel ( target as Constructor < T > , config ) ; } else { RestRegistry . registerSubrest ( target . constructor as Constructor < T > , propertyKey , config ) ; } } ; } function defaultMethod < T extends RestgooseModel > ( name , path , config ) { return Object . assign ( { } , config , { path : path , method : name , } ) as RestConfigurationMethodWithPath < T > ; } export function all < T extends RestgooseModel > ( config = { } ) { return defaultMethod ( 'all' , '/' , config ) ; } export function one < T extends RestgooseModel > ( config = { } ) { return defaultMethod ( 'one' , '/:id' , config ) ; } export function create < T extends RestgooseModel > ( config = { } ) { return defaultMethod ( 'create' , '/' , config ) ; } export function update < T extends RestgooseModel > ( config = { } ) { return defaultMethod ( 'update' , '/:id' , config ) ; } export function remove < T extends RestgooseModel > ( config = { } ) { return defaultMethod ( 'remove' , '/:id' , config ) ; } export function removeAll < T extends RestgooseModel > ( config = { } ) { return defaultMethod ( 'removeAll' , '/' , config ) ; } export function custom < T extends RestgooseModel > ( httpMethod , path , config = { } ) { return defaultMethod ( 'custom' , path , config ) ; } export interface RestConfiguration < T extends RestgooseModel > { route : string ; schemaOptions ? : SchemaOptions ; getConnection ? : ( req : Request ) => Promise < Connection > ; methods ? : RestConfigurationMethodWithPath < T > [ ] ; } export interface RestConfigurationMethod < T extends RestgooseModel > { preFetch ? : MiddlewarePreFetch | MiddlewarePostFetch < T > | MiddlewarePreSave < T > ; fetch ? : MiddlewareFetch < T > ; postFetch ? : MiddlewarePostFetch < T > | MiddlewarePreSave < T > ; preSave ? : MiddlewarePostFetch < T > | MiddlewarePreSave < T > ; persist ? : MiddlewarePersist < T > ; preSend ? : MiddlewarePostFetch < T > | MiddlewarePreSave < T > ; onError ? : ( req : RestRequest , error : any ) => Promise < any > ; } export interface RestConfigurationMethodWithPath < T extends RestgooseModel > extends RestConfigurationMethod < T > { path : string ; method : RestMethodName ; } export class RestError extends Error { constructor ( public httpCode , public errorData = { } ) { super ( 'RestError' ) ; } }