Skip to content

Commit

Permalink
adding ability to pass in global module configurations
Browse files Browse the repository at this point in the history
  • Loading branch information
colbyfayock committed Mar 22, 2024
1 parent 8db8612 commit 5a21810
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 7 deletions.
8 changes: 7 additions & 1 deletion playground/nuxt.config.ts
Original file line number Diff line number Diff line change
@@ -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
}
}
})
7 changes: 6 additions & 1 deletion src/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<ModuleOptions>({
Expand All @@ -33,7 +36,9 @@ export default defineNuxtModule<ModuleOptions>({
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,
}
);

Expand Down
21 changes: 16 additions & 5 deletions src/runtime/composables/useCldImageUrl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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')

Expand All @@ -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({
Expand Down

0 comments on commit 5a21810

Please sign in to comment.