@curvenote/cli
Version:
CLI Client library for Curvenote
55 lines (54 loc) • 1.78 kB
JavaScript
/**
* Perform json GET request to `url`
*
* If request is successful, return the response json.
* If request fails, throw an error.
*/
export async function getFromUrl(session, url) {
session.log.debug('Getting from', url);
const headers = await session.getHeaders();
const response = await session.fetch(url, {
headers: {
'Content-Type': 'application/json',
...headers,
},
});
if (response.ok) {
const json = (await response.json());
return json;
}
else {
session.log.debug('GET FAILED', url, response.status, response.statusText);
throw new Error(`GET FAILED ${url}: ${response.status}\n\n${response.statusText}
Please contact support@curvenote.com`);
}
}
/**
* Perform json GET request to `pathname` on the journals API
*
* If request is successful, return the response json.
* If request fails, throw an error.
*/
export async function getFromJournals(session, pathname) {
// TODO this could/should now just use session.get? and so
const url = `${session.config.apiUrl}${pathname}`;
return getFromUrl(session, url);
}
export async function postToUrl(session, url, body, opts = {}) {
session.log.debug(`${opts?.method ?? 'POST'}ing to`, url);
const method = opts?.method ?? 'POST';
const headers = await session.getHeaders();
return session.fetch(url, {
method,
body: JSON.stringify(body),
headers: {
'Content-Type': 'application/json',
...headers,
},
});
}
export async function postToJournals(session, pathname, body, opts = {}) {
const url = `${session.config?.apiUrl}${pathname}`;
const resp = await postToUrl(session, url, body, opts);
return resp;
}