-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwrap.ts
More file actions
127 lines (119 loc) · 3.51 KB
/
wrap.ts
File metadata and controls
127 lines (119 loc) · 3.51 KB
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
import { FunctionHandler, FunctionRequest, FunctionRouter,
HTTPContext, HTTPHandler, HTTPResponse, HTTPRouter } from 'handler.js';
import { Callback } from './callback';
import { Context } from './context';
import { Event, ProxyEvent, RouterEvent } from './event';
import { Handler } from './handler';
import * as logger from './logger';
function _sendError(cb: Callback, err: Error) {
logger.log(err.message, 'Error');
cb(err);
}
function _sendResult(cb: Callback, result: any) {
logger.log(result, 'Result');
cb(null, result);
}
function _toProxyResult(res: HTTPResponse) {
let body: string;
if (res.type === 'application/json') {
body = JSON.stringify(res.body);
} else {
body = res.body;
}
return {
body,
headers: {
'Access-Control-Allow-Origin': '*',
'Content-Type': res.type,
},
statusCode: res.status,
};
}
export function fromFunctionHandler(handler: FunctionHandler): Handler {
return async (event: Event, context: Context, callback: Callback) => {
try {
logger.log(event, 'Event');
const req = new FunctionRequest({
body: event,
context,
});
const res = await handler(req);
logger.log(res, 'Result');
callback(null, res);
} catch (err) {
_sendError(callback, err);
}
};
}
export function fromFunctionRouter(router: FunctionRouter): Handler {
return async (event: RouterEvent, context: Context, callback: Callback) => {
try {
logger.log(event, 'Event');
const req = new FunctionRequest({
body: event.body,
context,
path: event.path,
});
for (const middleware of router.middlewares) {
const handler = middleware as FunctionHandler;
const res = await handler(req);
logger.log(res, 'Result');
callback(null, res);
}
const match = router.matchRoute(req.path);
if (!match) {
throw new Error('route not matched');
}
const res = await match.handler(req);
logger.log(res, 'Result');
callback(null, res);
} catch (err) {
_sendError(callback, err);
}
};
}
export function fromHTTPHandler(handler: HTTPHandler): Handler {
const router = new HTTPRouter();
router.all('*', handler);
return fromHTTPRouter(router);
}
export function fromHTTPRouter(router: HTTPRouter): Handler {
return async (event: ProxyEvent, context: Context, callback: Callback) => {
try {
logger.log(event, 'Event');
const rewritePath = router._rewritePath(event.path);
if (rewritePath) {
return _sendResult(callback, {
headers: {
Location: rewritePath,
},
statusCode: 302,
});
}
const ctx = new HTTPContext({
body: JSON.parse(event.body),
context,
headers: event.headers || {},
method: event.httpMethod,
params: event.pathParameters || {},
path: event.path,
query: event.queryStringParameters || {},
});
for (const middleware of router.middlewares) {
const handler = middleware as HTTPHandler;
await handler(ctx);
}
const match = router.matchRoute(ctx.req.method, ctx.req.path);
if (!match) {
const res = new HTTPResponse({context: ctx});
res.body = 'Not Found';
res.status = 404;
return _sendResult(callback, _toProxyResult(res));
}
await match.handler(ctx);
_sendResult(callback, _toProxyResult(ctx.res));
} catch (err) {
_sendError(callback, err);
}
};
}