Skip to content

Commit

Permalink
Merge pull request #199 from nuxt-modules/feat/195-global-module-config
Browse files Browse the repository at this point in the history
adding ability to pass in global module configurations
  • Loading branch information
Baroshem authored Mar 24, 2024
2 parents 19f0bee + 81c6690 commit 7874007
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 8 deletions.
6 changes: 4 additions & 2 deletions src/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ export type ModuleOptions = {
uploadPreset?: string;
apiKey?: string;
analytics?: boolean;
config?: CloudinaryConfigurationOptions;
cloud?: ConfigOptions["cloud"];
url?: ConfigOptions["url"];
};

export default defineNuxtModule<ModuleOptions>({
Expand All @@ -36,7 +37,8 @@ export default defineNuxtModule<ModuleOptions>({
process.env.CLOUDINARY_UPLOAD_PRESET || options.uploadPreset,
apiKey: process.env.CLOUDINARY_API_KEY || options.apiKey,
analytics: options.analytics,
config: options.config,
cloud: options.cloud,
url: options.url,
}
);

Expand Down
22 changes: 16 additions & 6 deletions src/runtime/composables/useCldImageUrl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +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 config = props.config || useRuntimeConfig().public.cloudinary.config
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')

Expand All @@ -17,15 +25,17 @@ export const useCldImageUrl = (props: ConstructUrlProps) => {
...props.options
},
config: {
...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({
Expand Down

0 comments on commit 7874007

Please sign in to comment.