@cashfarm/tractor
Version:
A Hapi server with superpowers
83 lines (82 loc) • 3.29 kB
TypeScript
import * as Hapi from 'hapi';
export declare const EndpointMetadataKey: unique symbol;
export declare type EPMethods = Hapi.HTTP_METHODS_PARTIAL | '*';
/**
* Defines possible options for the `@Endpoint` decorator
*/
export interface IEndpointOptions {
/**
* An optional domain string or an array of domain strings for limiting the route to only
* requests with a matching host header field. Matching is done against the hostname part
* of the header only (excluding the port). Defaults to all hosts.
*/
vhost?: string;
description?: string;
notes?: string;
tags?: string[];
handler?: string | ((req: any, reply: any) => any);
validate?: {
query?: any;
params?: any;
payload?: any;
};
response?: {
schema: any;
};
auth?: false | string | Hapi.AuthOptions;
}
/**
* The endpoint metadata class
*
* When an endpoint is declared in Tractor using the `@Endpoint` decorator,
* an instance of EndpointMetadata will be created to hold the metadata
* and stored using `Reflect.defineMetada` in an array of endpoints referenced
* by the metadata key `'tractor:controller:endpoint'`.
*/
export declare class EndpointMetadata implements IEndpointOptions {
/**
* the HTTP method. Typically one of 'GET', 'POST', 'PUT', 'PATCH', 'DELETE', or 'OPTIONS'.
*
* Any HTTP method is allowed, except for 'HEAD'. Use '*' to match against any HTTP method
* (only when an exact match was not found, and any match with a specific method will be given
* a higher priority over a wildcard match). Can be assigned an array of methods which has the
* same result as adding the same route with different methods manually.
*/
method: Hapi.HTTP_METHODS_PARTIAL | '*' | (Hapi.HTTP_METHODS_PARTIAL | '*')[];
/**
* the absolute path used to match incoming requests (must begin with '/').
* Incoming requests are compared to the configured paths based on the connection
* router configuration option. The path can include named parameters enclosed in {}
* which will be matched against literal values in the request as described in Path parameters.
*/
path: string;
/**
* an optional domain string or an array of domain strings for limiting the route to only
* requests with a matching host header field. Matching is done against the hostname part
* of the header only (excluding the port). Defaults to all hosts.
*/
vhost?: string;
description: string;
notes: string;
tags: string[];
validate: {
query?: any;
params?: any;
payload?: any;
};
response: {
schema: any;
};
auth: false | string | Hapi.AuthOptions;
constructor(method: Hapi.HTTP_METHODS_PARTIAL | '*' | (Hapi.HTTP_METHODS_PARTIAL | '*')[], path: string, { vhost, description, notes, tags, validate, response, auth }?: IEndpointOptions);
}
/**
* The `@Endpoint` decorator
*
* Marks a controller method as an API endpoint
*
* @param method The http methods this endpoint will respond to
* @param path The endpoint's path
* @param options Hapi endpoint options
*/
export declare function Endpoint(method: EPMethods | EPMethods[], path: string, options: IEndpointOptions): MethodDecorator;