-
Notifications
You must be signed in to change notification settings - Fork 0
/
kepi.js
135 lines (109 loc) · 3.14 KB
/
kepi.js
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
128
129
130
131
132
133
134
135
const DEFAULTS = require('./defaults');
const Header = require('./header').Header;
class Kepi {
constructor(data, customOptions) {
data = data || {};
this.options = Object.assign({}, DEFAULTS, customOptions);
this.resetData = this.options.resetAfterApply ? data : null;
this.headers = {};
if (this.options.setupNicknames) {
this._setupNicknames(this.options.NICKNAMES);
this._setupNicknames(this.options.nicknames);
}
this.add(data);
}
/**
* add to existing headers, creating if necessary
* @param {*} data "safe", or a { } of key/value pairs.
*/
add(data) {
if (data === 'safe')
return this.safe();
let headerNames = Object.keys(data);
headerNames.forEach((headerName) => {
let fullName = this._fullName(headerName);
if (this.headers[headerName])
this.headers[headerName].add(data[headerName]);
else
this.headers[headerName] = Header.create(this._headerType(fullName), fullName, data[headerName], this.options);
});
return this;
}
/**
* Write the headers into the http.ServerResponse, and possibly reset
* @param {*} response http.ServerResponse
*/
applyTo(response) {
let headerNames = Object.keys(this.headers);
headerNames.forEach((headerName) => {
this.headers[headerName].applyTo(response);
});
if (this.resetData) {
this.headers = {};
this.add(this.resetData);
}
return this;
}
/**
* Find the header, creating if needed
* @param {string} headerName
* @param {*} data only used if creating
*/
header(headerName, data = undefined) {
let fullName = this._fullName(headerName);
let header = this.headers[fullName];
if (header)
return header;
header = Header.create(this._headerType(fullName), fullName, data, this.options);
this.headers[fullName] = header;
return header;
}
/**
* Creates micro "middleware"
* @param {*} handler
*/
micro(handler) {
let kepi = this;
return function(req, res, ...restArgs) {
kepi.applyTo(res);
return handler(req, res, ...restArgs)
}
}
/**
* Creates express middleware. Usually used via `app.use(kepi.middleware());`
*/
middleware() {
let kepi = this;
return function(req, res, next) {
kepi.applyTo(res);
next();
}
}
safe() {
let allSafe = Object.assign({}, this.options.SAFE, this.options.safe);
Object.keys(allSafe).forEach((headerName) => {
this.header(headerName).safe();
});
return this;
}
_fullName(name) {
return this.options.nicknames[name] ||
this.options.NICKNAMES[name] ||
name;
}
_headerType(fullName) {
return this.options.headerClasses[fullName] || this.options.HEADER_CLASSES[fullName];
}
_setupNicknames(data, where = this) {
Object.keys(data).forEach((nickname) => {
let value = data[nickname];
if (typeof value === 'string')
where[nickname] = () => this.header(value);
else {
where[nickname] = {};
this._setupNicknames(value, where[nickname]);
}
});
}
}
module.exports = function(data, options) { return new Kepi(data, options); }