@schnaq/strapi4-utils
Version:
Utility functions for working with Strapi v4
157 lines (156 loc) • 3.81 kB
JavaScript
// src/index.ts
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
};
}
export {
flattenAttributes,
flattenData,
getImageUrlForSize,
queryAPI,
queryAPIUser
};