/
index.ts
99 lines (89 loc) · 2.04 KB
/
index.ts
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
87
88
89
90
91
92
93
94
95
96
97
98
99
import { OutgoingHttpHeaders } from 'http';
import { RequestOptions } from 'https';
import { MoxyRequest, MoxyResponse, MoxyServer } from '..';
/**
* Path and query params
*/
export type HandlerVariables = Record<string, string | string[]>;
/**
* Common http verbs
*/
export type Method = 'connect' | 'delete' | 'get' | 'head' | 'options' | 'patch' | 'post' | 'put' | 'trace';
/**
* Manual request handler
*/
export type RequestHandler = (
req: MoxyRequest,
res: MoxyResponse,
variables: HandlerVariables,
server: MoxyServer
) => void;
export interface PathSettings {
/**
* If set, will proxy all requests to the target
*/
proxy?: string;
/**
* Options to pass through proxy
*/
proxyOptions?: RequestOptions;
/**
* Method-level delay (in milliseconds)
*/
delay?: number;
/**
* If true, will not parse route as regex
*/
exact?: true;
}
export interface MethodSettings extends PathSettings {
/**
* status code to return (defaults to 200)
*/
status?: number;
/**
* response payload
*/
body?: any;
/**
* headers to add (Content-Type is added automatically)
*/
headers?: OutgoingHttpHeaders;
/**
* HTTP request handler function
*/
handler?: RequestHandler;
}
/**
* Configuration for a method.
* Would be configured as { get: MethodConfig, post: MethodConfig, ... }
*
* examples
*
* Standard http response:
* {
* status: 200,
* body: { message: 'hello' },
* }
*
* Manual request listener method:
* async (req, res, vars) => res.sendJson({ url: req.url, date: Date.now() }, { status: 201 });
*
* Static file:
* '/static/:file'
*
*/
export type MethodConfig = MethodSettings | string | RequestHandler;
/**
* Configuration for a path.
*/
export type PathConfig = PathSettings & { all?: MethodConfig } & { [key in Method]?: MethodConfig };
/**
* Configuration for a route.
*/
export type RouteConfig = string | RequestHandler | PathConfig;
/**
* Configuration for multiple routes.
*/
export type Routes = Record<string, RouteConfig>;
export * from './router';