UNPKG

@cisco-meraki/dashboard-api-tools

Version:

Typescript library for interacting with Meraki's public API

110 lines (109 loc) 4.84 kB
"use strict"; 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()); }); }; var __rest = (this && this.__rest) || function (s, e) { var t = {}; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0) t[p] = s[p]; if (s != null && typeof Object.getOwnPropertySymbols === "function") for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) { if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i])) t[p[i]] = s[p[i]]; } return t; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.fetchBaseQuery = void 0; const toolkit_1 = require("@reduxjs/toolkit"); const __1 = require(".."); const apiUtils_1 = require("../apiUtils"); /** * This exists because the redux helper has the wrong type. Alternatively, we * could have changed the return type of isPlainObject. */ function isHeaderObject(obj) { return (0, toolkit_1.isPlainObject)(obj); } function stripUndefined(headers) { if (!isHeaderObject(headers)) { return headers !== null && headers !== void 0 ? headers : {}; } const copy = Object.assign({}, headers); Object.entries(copy).forEach(([k, v]) => { if (typeof v === "undefined") delete copy[k]; }); return copy; } /** * fetchBaseQuery returns a fetch-like wrapper that is used internally by Redux. * * **NOTE:** This version of fetchBaseQuery does not support responseHandler or * validateStatus. The reason is because apiRequest already performs this * functionality. See: * https://redux-toolkit.js.org/rtk-query/api/fetchBaseQuery#parsing-a-Response * and * https://redux-toolkit.js.org/rtk-query/api/fetchBaseQuery#handling-non-standard-response-status-codes */ function fetchBaseQuery(baseOpts) { const { baseUrl, transformHeaders = (x) => x, pauseUntilResolved, paramsSerializer } = baseOpts; return (...args) => __awaiter(this, void 0, void 0, function* () { var _a; const [fetchArg, api] = args; const { signal, getState, extra, endpoint, forced = false, type } = api; let { url } = typeof fetchArg === "string" ? { url: fetchArg } : fetchArg; const _b = typeof fetchArg === "string" ? { url: fetchArg } : fetchArg, { method = "GET", headers: rawHeaders = {}, body = undefined, params = undefined, // Not supported. Note: it's not possible to easily extra types so disabling instead // eslint-disable-next-line @typescript-eslint/no-unused-vars responseHandler = "json", // Not supported. Note: it's not possible to easily extra types so disabling instead // eslint-disable-next-line @typescript-eslint/no-unused-vars validateStatus = () => true } = _b, rest = __rest(_b, ["method", "headers", "body", "params", "responseHandler", "validateStatus"]); const headers = yield transformHeaders(new Headers(stripUndefined(rawHeaders)), { getState, extra, endpoint, forced, type, }); const config = Object.assign({ method, signal, body, headers }, rest); if (params) { url += paramsSerializer(params); } if (pauseUntilResolved) yield pauseUntilResolved((_a = config.headers) !== null && _a !== void 0 ? _a : {}, { getState, extra, endpoint, forced, type }); try { const _c = yield (0, apiUtils_1.apiRequest)(method, `${baseUrl}${url}`, body, { fetchOptions: config, }), { data } = _c, meta = __rest(_c, ["data"]); return { data, meta, }; } catch (error) { if (!(0, __1.isApiError)(error)) { console.warn("The following is not a recognized error returned by an endpoint", error); return { error: { errors: [JSON.stringify(error, null, " ")], }, }; } return { error, }; } }); } exports.fetchBaseQuery = fetchBaseQuery;