This repository has been archived by the owner on Nov 11, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 23
/
api.js
221 lines (206 loc) · 7.12 KB
/
api.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
/*
Zapier App to automate ERPNext.
Copyright (C) 2018 Raffael Meyer
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
'use strict';
const {
CLIENT_ID,
BASE_URL,
METHOD_ENDPOINT,
RESOURCE_ENDPOINT,
OAUTH_AUTHORIZE_ENDPOINT,
} = require('./constants');
const { uniqueItems } = require('./util');
/**
* get a specific document
*
* @param {*} z Zapier object for requests with advanced logging
* @param {string} docType ERPNext DocType
* @param {string} docName Unique name (ID) of the document
* @returns {object} Document object
*/
const getDocument = (z, docType, docName) => {
return z
.request({
method: 'GET',
url: BASE_URL + RESOURCE_ENDPOINT + `/${docType}/${docName}`,
})
.then(response => response.json.data);
};
/**
* modify a specific document
*
* @param {*} z Zapier object for requests with advanced logging
* @param {string} docType ERPNext DocType
* @param {string} docName Unique name (ID) of the document
* @param {object} docBody Object with fieldnames and new values
* @returns {object} Document object
*/
const putDocument = (z, docType, docName, docBody) => {
return z
.request({
method: 'PUT',
url: BASE_URL + RESOURCE_ENDPOINT + `/${docType}/${docName}`,
body: docBody,
headers: { 'Content-Type': 'application/json' },
})
.then(response => response.json.data);
};
/**
* delete a specific document
*
* @param {*} z Zapier object for requests with advanced logging
* @param {string} docType ERPNext DocType
* @param {string} docName Unique name (ID) of the document
* @returns {string} Message (usually 'ok')
*/
const deleteDocument = (z, docType, docName) => {
return z
.request({
url: BASE_URL + RESOURCE_ENDPOINT + `/${docType}/${docName}`,
method: 'DELETE',
})
.then(response => response.json.message);
};
/**
* create a new document of given DocType
*
* @param {*} z Zapier object for requests with advanced logging
* @param {string} docType ERPNext DocType
* @param {object} docBody Object with fieldnames and new values
* @returns {object} Document object
*/
const postDocument = (z, docType, docBody) => {
return z
.request({
method: 'POST',
url: BASE_URL + RESOURCE_ENDPOINT + `/${docType}`,
body: docBody,
headers: { 'Content-Type': 'application/json' },
})
.then(response => response.json.data);
};
/**
* Get a list of documents for a given DocType
*
* @param {*} z Zapier object for requests with advanced logging
* @param {string} docType ERPNext DocType
* @param {object} params Object with fieldnames and new values
* @param {number} params.limitPageLength Number of documents for paginiation
* @param {number} params.limitStart multiple of `limitPageLength` to get successive pages
* @param {Array} params.fields fields of the DocType that should be returned, defaults to 'name'
* @param {Array} params.filters list of SQL-filters in format
* [[DocType, DocName, fieldname, operator, value]]
* @returns {Object[]} Array of document objects
*/
const listDocuments = (z, docType, params) => {
if (params.hasOwnProperty('fields') && Array.isArray(params.fields)) {
if (!params.fields.includes('name')) {
params.fields.push('name');
}
params.fields = uniqueItems(params.fields);
}
return z
.request({
method: 'GET',
url: BASE_URL + RESOURCE_ENDPOINT + `/${docType}`,
params: {
// stringify fields so the param will not be duplicated for every field
fields: JSON.stringify(params.fields) || '["name"]',
filters: JSON.stringify(params.filters) || '[]',
limit_page_length: params.limitPageLength || 20,
limit_start: params.limitStart || 0,
},
})
.then(response => {
// Expected response: {"data": [{"lead_name": "Mustermann", ...}]}
if (!response.json.hasOwnProperty('data')) {
throw new Error('Search did not return any data: ' + response.json);
}
return response.json.data;
});
};
/**
* Send data to a whitelisted method
*
* @param {*} z Zapier object for requests with advanced logging
* @param {string} dottedPathToMethod Dotted path to method, e.g. 'frappe.auth.get_logged_user'
* @param {object} postData Object containing the data to post
* @returns {object} Parsed json response
*/
const postMethod = (z, dottedPathToMethod, postData) => {
return z
.request({
method: 'POST',
url: BASE_URL + METHOD_ENDPOINT + `/${dottedPathToMethod}`,
body: postData,
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
},
})
.then(response => response.json);
};
/**
* Get data from a whitelisted method.
*
* This is a separate function because authorize URL in authentication.js
* needs only the object returned by this function.
*
* @param {string} dottedPathToMethod Dotted path to method, e.g. 'frappe.auth.get_logged_user'
* @param {object} params Object containing query parameters
* @returns {object} GET request object
*/
const buildMethodGetRequestObject = (dottedPathToMethod, params) => {
return {
method: 'GET',
url: BASE_URL + METHOD_ENDPOINT + `/${dottedPathToMethod}`,
params: params || {},
};
};
/**
* Get data from a whitelisted method
*
* @param {*} z Zapier object for requests with advanced logging
* @param {string} dottedPathToMethod Dotted path to method, e.g. 'frappe.auth.get_logged_user'
* @param {object} params Object containing query parameters
* @returns {object} Parsed json response
*/
const getMethod = (z, dottedPathToMethod, params) => {
return z
.request(buildMethodGetRequestObject(dottedPathToMethod, params))
.then(response => response.json);
};
const getAuthorizeRequest = () => {
return {
method: 'GET',
url: BASE_URL + '/' + OAUTH_AUTHORIZE_ENDPOINT,
params: {
client_id: CLIENT_ID,
state: '{{bundle.inputData.state}}',
response_type: 'code',
scope: 'all',
redirect_uri: '{{bundle.inputData.redirect_uri}}',
},
};
};
module.exports = {
getDocument,
putDocument,
deleteDocument,
postDocument,
listDocuments,
getMethod,
postMethod,
buildMethodGetRequestObject,
getAuthorizeRequest,
};