From 5a21810da7d835b33b25382ee6171add61d03cee Mon Sep 17 00:00:00 2001 From: Colby Fayock Date: Fri, 22 Mar 2024 14:18:24 -0300 Subject: [PATCH] adding ability to pass in global module configurations --- playground/nuxt.config.ts | 8 +++++++- src/module.ts | 7 ++++++- src/runtime/composables/useCldImageUrl.ts | 21 ++++++++++++++++----- 3 files changed, 29 insertions(+), 7 deletions(-) diff --git a/playground/nuxt.config.ts b/playground/nuxt.config.ts index 08819ad..b798e20 100644 --- a/playground/nuxt.config.ts +++ b/playground/nuxt.config.ts @@ -1,6 +1,12 @@ export default defineNuxtConfig({ modules: ['../src/module'], cloudinary: { - cloudName: 'nuxt-cloudinary' + cloudName: 'nuxt-cloudinary', + url: { + cname: 'spacejelly.dev', + secureDistribution: 'spacejelly.dev', + secure: true, + privateCdn: true + } } }) diff --git a/src/module.ts b/src/module.ts index 3d6d4e5..377fca5 100644 --- a/src/module.ts +++ b/src/module.ts @@ -6,12 +6,15 @@ import { } from "@nuxt/kit"; import { fileURLToPath } from "url"; import { defu } from "defu"; +import { ConfigOptions } from '@cloudinary-util/url-loader' export type ModuleOptions = { cloudName?: string; uploadPreset?: string; apiKey?: string; analytics?: boolean; + cloud?: ConfigOptions["cloud"]; + url?: ConfigOptions["url"]; }; export default defineNuxtModule({ @@ -33,7 +36,9 @@ export default defineNuxtModule({ uploadPreset: process.env.CLOUDINARY_UPLOAD_PRESET || options.uploadPreset, apiKey: process.env.CLOUDINARY_API_KEY || options.apiKey, - analytics: options.analytics + analytics: options.analytics, + cloud: options.cloud, + url: options.url, } ); diff --git a/src/runtime/composables/useCldImageUrl.ts b/src/runtime/composables/useCldImageUrl.ts index d91b78c..fddafe4 100644 --- a/src/runtime/composables/useCldImageUrl.ts +++ b/src/runtime/composables/useCldImageUrl.ts @@ -6,7 +6,16 @@ import pkg from '../../../package.json' export const useCldImageUrl = (props: ConstructUrlProps) => { if (!props.options.src) console.error("`[@nuxtjs/cloudinary]`: Property `src` is missing") - const cldCloudName = props.config?.cloud?.cloudName || useRuntimeConfig().public.cloudinary.cloudName + const moduleConfig = useRuntimeConfig().public.cloudinary; + + // There are a few ways to pass in the Cloud Name + // - Component/composable config prop + // - Top level module config + // - Top level module config cloud property + // While the top level module config is redundant, retaining it for convenience as most + // users won't need to pass in more advanced settings via the `cloud` property + + const cldCloudName = props.config?.cloud?.cloudName || moduleConfig.cloud?.cloudName || moduleConfig?.cloudName; if (!cldCloudName) console.warn('`[@nuxtjs/cloudinary]` Environment variable `CLOUDINARY_CLOUD_NAME` or property `cloudinary.cloudName` missing') @@ -16,15 +25,17 @@ export const useCldImageUrl = (props: ConstructUrlProps) => { ...props.options }, config: { - ...props.config, + url: moduleConfig.url, cloud: { - cloudName: cldCloudName - } + cloudName: cldCloudName, + ...moduleConfig.cloud + }, + ...props.config, }, analytics: false } - if (useRuntimeConfig().public.cloudinary.analytics) { + if (moduleConfig.analytics) { cldOptions = { ...cldOptions, analytics: Object.assign({