-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
71 lines (57 loc) · 2.07 KB
/
index.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
const platform = require('zapier-platform-core');
const authentication = require('./authentication');
const CallNotificationTrigger = require('./triggers/call_notification');
const zapierApp = require('./package.json');
/**
* Adds the authentication header to the request.
* @param request The request data.
* @param z The Z object.
* @param bundle An object containing the metadata.
* @returns {*}
*/
const addAuthorizationHeader = (request, z, bundle) => {
if (bundle.authData.email && bundle.authData.token) {
request.headers.Authorization = `Token ${bundle.authData.email}:${bundle.authData.token}`;
}
return request;
};
/**
* Checks the response code and throws any errors if something is wrong.
* @param response The response that was returned
* @param z The Z object
* @param bundle An object containing the metadata
* @returns {*}
*/
const processResponseCode = (response, z, bundle) => {
z.console.log(`processResponseCode status: ${response.status}`);
if (bundle.authData.token && bundle.authData.email) {
if (response.status === 200 || response.status === 201) {
// Do nothing, this is expected behaviour.
} else if (response.status === 401) {
z.console.log('401 Unauthorized status code detected.');
throw new z.errors.HaltedError(`${bundle.authData.email} doesn't have the right credentials.`);
} else if (response.status === 403) {
z.console.log('403 Forbidden status code detected.');
throw new z.errors.HaltedError(`${bundle.authData.email} access is forbidden.`);
} else {
z.console.log(`${response.status} status code remains unhandled.`);
throw new z.errors.HaltedError('An unhandled exception occurred.');
}
}
z.console.log(response);
return response;
};
const App = {
version: zapierApp.version,
platformVersion: platform.version,
authentication,
beforeRequest: [addAuthorizationHeader],
afterResponse: [processResponseCode],
resources: {},
triggers: {
[CallNotificationTrigger.key]: CallNotificationTrigger,
},
searches: {},
creates: {},
};
module.exports = App;