@scayle/storefront-nuxt
Version:
Nuxt integration for the SCAYLE Commerce Engine and Storefront API
58 lines (57 loc) • 1.89 kB
JavaScript
import { defineNitroPlugin } from "nitropack/runtime/plugin";
import { useRuntimeConfig } from "#imports";
import { RuntimeConfigSchema } from "../../utils/zodSchema.js";
import { purifySensitiveValue } from "@scayle/storefront-core";
import consola from "consola";
export const sensitiveKeys = [
"token",
"password",
"clientSecret",
"apiToken",
"secret"
];
export const getValue = (object, pathArray) => {
return JSON.stringify(
pathArray.reduce(
(acc, key) => typeof acc === "object" ? acc[key] : void 0,
object
)
);
};
export const formatZodError = (issues, runtimeConfig) => {
const errorMessages = [];
issues.forEach((issue) => {
if (issue.code === "invalid_union") {
return issue.errors.forEach((unionError) => {
errorMessages.push(...formatZodError(unionError, runtimeConfig));
});
}
const key = issue.path[issue.path.length - 1];
let value = "";
if (sensitiveKeys.includes(key)) {
value = purifySensitiveValue(getValue(runtimeConfig, issue.path), true);
} else if (issue.code !== "too_small") {
value = getValue(runtimeConfig, issue.path);
}
const errorMessage = {
code: issue.code,
path: issue.path.join("."),
message: issue.message
};
if (value) {
errorMessage.value = value;
}
errorMessages.push(errorMessage);
});
return errorMessages;
};
export default defineNitroPlugin(() => {
const runtimeConfig = useRuntimeConfig();
const { success, error } = RuntimeConfigSchema.safeParse(runtimeConfig);
if (!success) {
const errorMessages = formatZodError(error.issues, runtimeConfig);
consola.error("[storefront-nuxt] configValidation:", errorMessages);
throw Error("[storefront-nuxt] configValidation: Runtime config invalid");
}
consola.success("[storefront-nuxt] configValidation: Runtime config valid");
});