@scayle/storefront-nuxt
Version:
Nuxt integration for the SCAYLE Commerce Engine and Storefront API
58 lines (57 loc) • 2.01 kB
JavaScript
import { createConsola } from "consola";
import {
STORAGE_MOUNT_CACHE,
STORAGE_MOUNT_SESSION
} from "../../server/utils/cacheStorage.js";
import createLog from "../../createLog.js";
import { JSONReporter } from "../../JSONReporter.js";
import { defineNitroPlugin } from "nitropack/runtime/plugin";
import { useStorage } from "nitropack/runtime/storage";
import { useRuntimeConfig } from "#imports";
import { getAvailableMounts, mountStorage } from "../../utils/storage.js";
export default defineNitroPlugin(async () => {
const storage = useStorage();
const config = useRuntimeConfig();
const { shops } = config.storefront;
const logConfig = config.public.storefront.log;
const log = createLog(
(opts) => createConsola({
...logConfig.json ? { reporters: [new JSONReporter()] } : void 0,
stderr: logConfig.output === "stdout" ? process.stdout : process.stderr,
stdout: logConfig.output === "stderr" ? process.stderr : process.stdout,
...opts
}),
logConfig.name,
logConfig.level
);
const availableMounts = getAvailableMounts(storage);
if (!availableMounts.includes(STORAGE_MOUNT_SESSION)) {
log.warn(
`${STORAGE_MOUNT_SESSION} storage mount not provided. Falling back to memory driver.`
);
}
if (!availableMounts.includes(STORAGE_MOUNT_CACHE)) {
log.warn(
`${STORAGE_MOUNT_CACHE} storage mount not provided. Falling back to memory driver.`
);
}
const mountShopStorage = async (_shopId, mount) => {
try {
if (!availableMounts.includes(mount)) {
await mountStorage(storage, mount);
}
} catch (error) {
log.error(
`[storefront-nuxt] nitroStorageConfig: Unable to create required storage mount '${mount}'.`,
error
);
}
};
for (const shop of Object.values(shops)) {
await mountShopStorage(shop.shopId, `${STORAGE_MOUNT_CACHE}:${shop.shopId}`);
await mountShopStorage(
shop.shopId,
`${STORAGE_MOUNT_SESSION}:${shop.shopId}`
);
}
});