UNPKG

@schnaq/strapi4-utils

Version:

Utility functions for working with Strapi v4

186 lines (184 loc) 4.98 kB
"use strict"; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); // src/index.ts var src_exports = {}; __export(src_exports, { flattenAttributes: () => flattenAttributes, flattenData: () => flattenData, getImageUrlForSize: () => getImageUrlForSize, queryAPI: () => queryAPI, queryAPIUser: () => queryAPIUser }); module.exports = __toCommonJS(src_exports); async function queryAPI([ path, body, method, token = process.env.NEXT_PUBLIC_STRAPI_API_TOKEN || process.env.STRAPI_API_TOKEN, apiUrl = process.env.NEXT_PUBLIC_STRAPI_API_URL || process.env.STRAPI_API_URL, cache = "no-cache" ]) { const headers = { Authorization: token ? `Bearer ${token}` : "", "Content-Type": body ? "application/json" : "text/plain" }; const validCacheValues = [ "default", "no-store", "reload", "no-cache", "force-cache", "only-if-cached" ]; if (!validCacheValues.includes(cache)) { cache = "no-cache"; } const result = await fetch(`${apiUrl}${path}`, { method: method ? method : body ? "POST" : "GET", headers, body: body ? JSON.stringify(body) : void 0, cache }); if (result.ok) { try { const json = await result.json(); flattenData(json); return json; } catch (e) { console.warn("Could not parse JSON from API response."); } } else { console.warn("API request failed. Status code:", result.status); try { const json = await result.json(); console.warn(json); return json; } catch (e) { console.warn(result); } } } function flattenData(json) { if (Array.isArray(json.data)) { json.data = json.data.map(flattenAttributes); } else { json.data = flattenAttributes(json.data); } } async function queryAPIUser([url, token]) { const headers = { Authorization: token ? `Bearer ${token}` : "" }; if (!token) { return; } const strapiApiUrl = process.env.NEXT_PUBLIC_STRAPI_API_URL || process.env.STRAPI_API_URL; headers["Authorization"] = `Bearer ${token}`; const result = await fetch(`${strapiApiUrl}${url}`, { headers }); if (result.ok) { return result.json(); } } function flattenAttributes(obj) { if (!obj) { return null; } let result = {}; for (const key of Object.keys(obj)) { const value = obj[key]; if (key === "attributes" || key === "data") { if (Array.isArray(value)) { return value.map((value2) => flattenAttributes(value2)); } else { Object.assign(result, flattenAttributes(value)); } } else { if (key === "body" || key === "items") { if (Array.isArray(value)) { result[key] = value.map((value2) => flattenAttributes(value2)); } } else { result[key] = typeof value === "object" ? flattenAttributes(value) : value; } } } return result; } function getImageUrlForSize(image, size) { if (!image) { return; } if (!size) { return { url: image.url, width: image.width, height: image.height }; } if (image.formats) { const formats = image.formats; const selectedFormat = formats[size]; if (selectedFormat) { return { url: selectedFormat.url, width: selectedFormat.width, height: selectedFormat.height }; } else if (formats.large) { return { url: formats.large.url, width: formats.large.width, height: formats.large.height }; } else if (formats.medium) { return { url: formats.medium.url, width: formats.medium.width, height: formats.medium.height }; } else if (formats.small) { return { url: formats.small.url, width: formats.small.width, height: formats.small.height }; } else if (formats.thumbnail) { return { url: formats.thumbnail.url, width: formats.thumbnail.width, height: formats.thumbnail.height }; } } return { url: image.url, width: image.width, height: image.height }; } // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { flattenAttributes, flattenData, getImageUrlForSize, queryAPI, queryAPIUser });