UNPKG

tinacms

Version:

> The Fastest Way to Edit Next.js Content

83 lines (82 loc) 3.41 kB
(function(global, factory) { typeof exports === "object" && typeof module !== "undefined" ? factory(exports, require("fetch-ponyfill")) : typeof define === "function" && define.amd ? define(["exports", "fetch-ponyfill"], factory) : (global = typeof globalThis !== "undefined" ? globalThis : global || self, factory(global.tinacms = {}, global.NOOP)); })(this, function(exports2, fetchPonyfill) { "use strict"; const { fetch: fetchPonyfillFN, Headers: HeadersPonyfill } = fetchPonyfill(); const fetchDefined = typeof fetch === "undefined" ? fetchPonyfillFN : fetch; const HeadersDefined = typeof Headers === "undefined" ? HeadersPonyfill : Headers; const TINA_HOST = "content.tinajs.io"; class TinaClient { constructor({ token, url, queries, errorPolicy }) { this.apiUrl = url; this.readonlyToken = token == null ? void 0 : token.trim(); this.queries = queries(this); this.errorPolicy = errorPolicy || "throw"; } async request({ errorPolicy, ...args }, options) { var _a; const errorPolicyDefined = errorPolicy || this.errorPolicy; const headers = new HeadersDefined(); if (this.readonlyToken) { headers.append("X-API-KEY", this.readonlyToken); } headers.append("Content-Type", "application/json"); if (options == null ? void 0 : options.fetchOptions) { if ((_a = options == null ? void 0 : options.fetchOptions) == null ? void 0 : _a.headers) { Object.entries(options.fetchOptions.headers).forEach(([key, value]) => { headers.append(key, value); }); } } const { headers: _, ...providedFetchOptions } = (options == null ? void 0 : options.fetchOptions) || {}; const bodyString = JSON.stringify({ query: args.query, variables: (args == null ? void 0 : args.variables) || {} }); const url = (args == null ? void 0 : args.url) || this.apiUrl; const optionsObject = { method: "POST", headers, body: bodyString, redirect: "follow", ...providedFetchOptions }; const res = await fetchDefined(url, optionsObject); if (!res.ok) { let additionalInfo = ""; if (res.status === 401) { additionalInfo = "Please check that your client ID, URL and read only token are configured properly."; } throw new Error( `Server responded with status code ${res.status}, ${res.statusText}. ${additionalInfo ? additionalInfo : ""} Please see our FAQ for more information: https://tina.io/docs/errors/faq/` ); } const json = await res.json(); if (json.errors && errorPolicyDefined === "throw") { throw new Error( `Unable to fetch, please see our FAQ for more information: https://tina.io/docs/errors/faq/ Errors: ${json.errors.map((error) => error.message).join("\n")}` ); } return { data: json == null ? void 0 : json.data, errors: (json == null ? void 0 : json.errors) || null, query: args.query }; } } function createClient(args) { const client = new TinaClient(args); return client; } exports2.TINA_HOST = TINA_HOST; exports2.TinaClient = TinaClient; exports2.createClient = createClient; Object.defineProperty(exports2, Symbol.toStringTag, { value: "Module" }); });