UNPKG

@cronstamp/clientlib

Version:

Client library for cronstamp, a blockchain-based document timestamping and verification service.

45 lines 1.53 kB
import { ErrorTypes, ProcessError } from '../lifecycle/manager.js'; export async function postRequest(route, content, config) { return request(route, content, 'POST', config); } export async function getRequest(route, content, config) { return request(route, content, 'GET', config); } /** * HTTP Request * @param route * @param content * @param method * @param config * @returns response json and status code */ async function request(route, content, method, config) { if (config.API_URL === undefined) throw new ProcessError('Define API_URL as the endpoint for the cronstamp api!', ErrorTypes.UNEXPECTED); const headers = { Accept: 'application/json', 'Content-Type': 'application/json' }; const response = await fetch(config.API_URL + route + (method == 'GET' ? '?' + new URLSearchParams(content) : ''), method == 'POST' ? { method: method, headers: headers, body: JSON.stringify(content) } : { method: method, headers: headers }); //TODO: add retries? //only 200 and 202 are valid responses currently if (response.status !== 200 && response.status !== 202) { var detail = undefined; try { detail = await response.json(); } catch { } throw new ProcessError(`Request to ${route} endpoint failed with status ${response.status}`, ErrorTypes.UNEXPECTED, detail); } return response; } //# sourceMappingURL=api.js.map