UNPKG

@nopwdio/sdk-js

Version:
49 lines 2.3 kB
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; import { AbortError, ApiError, BadRequestError, ForbiddenError, InternalError, NetworkError, NotFoundError, TooManyRequestsError, UnauthorizedError, } from "./errors.js"; const API_BASE = "https://api.nopwd.io/v0"; export const endpoint = function (params) { return __awaiter(this, void 0, void 0, function* () { try { const resp = yield fetch(`${API_BASE}${params.ressource}`, { method: params.method, body: params.data ? JSON.stringify(params.data) : undefined, signal: params.signal, }); if (!resp.ok) { throw new ApiError(resp.status, yield resp.json()); } return resp.json(); } catch (e) { if (e.name === "AbortError") { throw new AbortError(); } if (e instanceof ApiError) { switch (e.getStatus()) { case 400: throw new BadRequestError(e.getData()); case 401: throw new UnauthorizedError(e.getData()); case 403: throw new ForbiddenError(e.getData()); case 404: throw new NotFoundError(e.getData()); case 429: throw new TooManyRequestsError(e.getData()); } throw new InternalError(e.getData()); } // should be a network error throw new NetworkError(); } }); }; //# sourceMappingURL=endpoint.js.map