all files / lib/ server-types.ts

100% Statements 26/26
75% Branches 3/4
100% Functions 6/6
100% Lines 19/19
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86                                                                                                                                     
"use strict";
 
import * as express from "express"; 
 
/**
 * The supported HTTP methods.
 */
export enum HttpMethod {
	GET,
	POST,
	PUT,
	DELETE,
	HEAD,
	OPTIONS,
	PATCH
}
 
/**
 * Represents the current context of the request being handled.
 */
export class ServiceContext {
	/**
	 * The resolved language to be used in the current request handling. 
	 */
	language: string;
	/**
	 * The preferred media type to be used in the current request handling. 
	 */
	accept: string;
	/**
	 * The request object. 
	 */
	request: express.Request;
	/**
	 * The response object 
	 */
	response: express.Response; 
	/**
	 * The next function. It can be used to delegate to the next middleware
	 * registered the processing of the current request. 
	 */
	next: express.NextFunction;
}
 
/**
 * The Base class for all HTTP errors
 */
export abstract class HttpError extends Error {
  constructor(name: string, 
  			  public statusCode: number, 
  			  public message: string) {
    super(message);
    this.name = name;
  }
}
 
/**
 * Used to create a reference to a resource.
 */
export abstract class ReferencedResource {
	/**
	 * Constructor. Receives the location of the resource.
	 * @param location To be added to the Location header on response
	 * @param statusCode the response status code to be sent
	 * @param body the body to be sent
	 */
	constructor(public location: string, public statusCode: number, public body?: any) {}
}
 
/**
 * The factory used to instantiate the object services
 */
export interface ServiceFactory {
	/**
	 * Create a new service object. Called before each request handling.
	 */
	create: (serviceClass: Function) => any,
	/**
	 * Return the type used to handle requests to the target service.
	 * By default, returns the serviceClass received, but you can use this
	 * to implement IoC integrations.
	 */
	getTargetClass: (serviceClass: Function) => FunctionConstructor;
}