-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathazureEndpointHandler.js
More file actions
66 lines (59 loc) · 1.71 KB
/
azureEndpointHandler.js
File metadata and controls
66 lines (59 loc) · 1.71 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
const {Writable} = require('stream');
class Res extends Writable {
constructor(context){
super();
this.res = context.res;
this.res.isRaw = true;
}
_write(chunk, encoding, callback){
this.res.body += chunk;
}
writeHead(statusCode,headers={}){
this.res.status = statusCode;
this.res.headers = Object.assign(this.res.headers||{},headers);
}
}
class Finder{
constructor(method,path,context){
this.method = method;
this.path = path;
this.req = context.req;
this.res = new Res(context);
this.context = context;
}
check(method,path,handler){
if (method == this.method && path == this.path){
Promise.resolve(handler(this.req,this.res)).then(_=>this.context.done())
}
return this;
}
get(path,handler){
return this.check("get",path,handler);
}
post(path,handler){
return this.check("post",path,handler);
}
put(path,handler){
return this.check("put",path,handler);
}
delete(path,handler){
return this.check("delete",path,handler);
}
}
function invoker(method,path,context){
return new Finder(method,path,context);
}
/**
* Returns a method sutiable for use as an azure function export for doing things
*
* @param {string} method Http method to search for
* @param {string} path Express-like path to search for
* @param {string=} endpoints Endpoint file to require and bind to
*/
function bindFor(method,path,endpoints="./endpoints.js"){
process.on("unhandledRejection",console.error);
return function(context){
require(endpoints)(new Finder(method,path,context));
}
}
module.exports = {invoker,bindFor}