-
-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathauth.js
47 lines (40 loc) · 904 Bytes
/
auth.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
'use strict';
/**
* Set arbitrary authorization tokens on Engine API requests.
*
* @example
*
* Worker(engineEndpoint, {
* use: [
* Auth('Bearer', 'BEARER_TOKEN')
* ]
* });
*/
function createAuth(type, token) {
if (!type) {
throw new Error('<type> required');
}
if (!token) {
throw new Error('<token> required');
}
/**
* Actual middleware that appends a `Authorization: "${type} ${token}"`
* header to the request options.
*/
return function(worker) {
const workerOptions = worker.options;
const requestOptions = workerOptions.requestOptions || {};
const headers = requestOptions.headers || {};
worker.options = {
...workerOptions,
requestOptions: {
...requestOptions,
headers: {
...headers,
Authorization: `${type} ${token}`
}
}
};
};
}
module.exports = createAuth;