-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathutils.js
More file actions
31 lines (29 loc) · 1.51 KB
/
Copy pathutils.js
File metadata and controls
31 lines (29 loc) · 1.51 KB
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
const fetch = require('node-fetch');
const { onshapeApiUrl } = require('./config');
module.exports = {
/**
* Send a request to the Onshape API, and proxy the response back to the caller.
*
* @param {string} apiPath The API path to be called. This can be absolute or a path fragment.
* @param {Request} req The request being proxied.
* @param {Response} res The response being proxied.
*/
forwardRequestToOnshape: async (apiPath, req, res) => {
try {
const normalizedUrl = apiPath.indexOf(onshapeApiUrl) === 0 ? apiPath : `${onshapeApiUrl}/${apiPath}`;
if (req.method == 'POST') {
const resp = await fetch(normalizedUrl, { method: 'POST', body: JSON.stringify(req.body), headers: { Authorization: `Bearer ${req.user.accessToken}`, 'Content-Type':'application/json', Accept: 'application/vnd.onshape.v1+json'}});
const data = await resp.text();
const contentType = resp.headers.get('Content-Type');
res.status(resp.status).contentType(contentType).send(data);
} else {
const resp = await fetch(normalizedUrl, { headers: { Authorization: `Bearer ${req.user.accessToken}` }});
const data = await resp.text();
const contentType = resp.headers.get('Content-Type');
res.status(resp.status).contentType(contentType).send(data);
}
} catch (err) {
res.status(500).json({ error: err });
}
}
}