Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adding ability to pass in global module configurations #199

Merged
merged 3 commits into from
Mar 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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