MCPcopy
hub / github.com/parse-community/parse-server / wrapToHTTPRequest

Function wrapToHTTPRequest

src/Controllers/HooksController.js:187–255  ·  view source on GitHub ↗
(hook, key)

Source from the content-addressed store, hash-verified

185}
186
187function wrapToHTTPRequest(hook, key) {
188 return req => {
189 const jsonBody = {};
190 for (var i in req) {
191 // Parse Server config is not serializable
192 if (i === 'config') { continue; }
193 jsonBody[i] = req[i];
194 }
195 if (req.object) {
196 jsonBody.object = req.object.toJSON();
197 jsonBody.object.className = req.object.className;
198 }
199 if (req.original) {
200 jsonBody.original = req.original.toJSON();
201 jsonBody.original.className = req.original.className;
202 }
203 const jsonRequest: any = {
204 url: hook.url,
205 headers: {
206 'Content-Type': 'application/json',
207 },
208 body: jsonBody,
209 method: 'POST',
210 };
211
212 const agent = hook.url.startsWith('https') ? HTTPAgents['https'] : HTTPAgents['http'];
213 jsonRequest.agent = agent;
214
215 if (key) {
216 jsonRequest.headers['X-Parse-Webhook-Key'] = key;
217 } else {
218 logger.warn('Making outgoing webhook request without webhookKey being set!');
219 }
220 return request(jsonRequest).then(response => {
221 let err;
222 let result;
223 let body = response.data;
224 if (body) {
225 if (typeof body === 'string') {
226 try {
227 body = JSON.parse(body);
228 } catch {
229 err = {
230 error: 'Malformed response',
231 code: -1,
232 partialResponse: body.substring(0, 100),
233 };
234 }
235 }
236 if (!err) {
237 result = body.success;
238 err = body.error;
239 }
240 }
241 if (err) {
242 throw err;
243 } else if (hook.triggerName === 'beforeSave') {
244 if (typeof result === 'object') {

Callers 1

addHookToTriggersMethod · 0.85

Calls 2

warnMethod · 0.80
requestFunction · 0.50

Tested by

no test coverage detected