Skip to content
Open
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
7 changes: 7 additions & 0 deletions build.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/**
* @see https://github.com/nuxt/module-builder?tab=readme-ov-file#buildconfigts-optional
* Should just be extending and not overriding the default config
*/
export default {
entries: ['src/module', 'src/kit'],
}
7 changes: 7 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@
"types": "./dist/types.d.mts",
"import": "./dist/module.mjs"
},
"./kit": {
"types": "./dist/kit.d.mts",
"import": "./dist/kit.mjs"
},
"./runtime/*": "./dist/runtime/*"
},
"files": [
Expand Down Expand Up @@ -86,6 +90,9 @@
"*": {
".": [
"./dist/types.d.mts"
],
"kit": [
"./dist/kit.d.mts"
]
}
},
Expand Down
21 changes: 21 additions & 0 deletions playground/modules/auth.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { defineNuxtModule, hasNuxtModule } from '@nuxt/kit'
import {
addControlledCookie,
CookieType,
} from '@dargmuesli/nuxt-cookie-control/kit'

export default defineNuxtModule({
setup() {
if (hasNuxtModule('@dargmuesli/nuxt-cookie-control')) {
addControlledCookie(
{
id: 'module-added-cookie',
name: 'Module Added Cookie',
description: 'This cookie was added from the module.',
targetCookieIds: ['_module_added_cookie'],
},
CookieType.OPTIONAL,
)
}
},
})
50 changes: 50 additions & 0 deletions src/kit.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import { useNuxt } from '@nuxt/kit'
import type { Nuxt } from '@nuxt/schema'

import { CONFIG_KEY } from './constants'
import type { Cookie, ModuleOptions } from './types'
import { CookieType } from './types'

/**
* Adds a cookie to the Nuxt configuration under the specified type category.
*
* @param cookie - The cookie object to be added
* @param type - The category type for the cookie (defaults to OPTIONAL)
* @param nuxt - The Nuxt instance (defaults to current Nuxt context)
*
* @remarks
* - Initializes cookie arrays if they don't exist
* - Prevents duplicate cookies by checking existing cookie IDs
* - Silently returns if a cookie with the same ID already exists
*
* @example
* ```ts
* addControlledCookie({ id: 'analytics', name: 'Analytics' }, CookieType.OPTIONAL)
* ```
*/
export const addControlledCookie = (
cookie: Cookie,
type: CookieType = CookieType.OPTIONAL,
nuxt: Nuxt = useNuxt(),
) => {
nuxt.options[CONFIG_KEY] ||= {} as (typeof nuxt.options)[typeof CONFIG_KEY]
;(nuxt.options[CONFIG_KEY] as ModuleOptions).cookies ||= {
necessary: [],
optional: [],
}
;(nuxt.options[CONFIG_KEY] as ModuleOptions).cookies.necessary ||= []
;(nuxt.options[CONFIG_KEY] as ModuleOptions).cookies.optional ||= []

if (
(nuxt.options[CONFIG_KEY] as ModuleOptions).cookies[type].some(
(c) => c.id === cookie.id,
)
) {
return
}

;(nuxt.options[CONFIG_KEY] as ModuleOptions).cookies[type].push(cookie)
}

export { CookieType } from './types'
export type { Cookie } from './types'
9 changes: 8 additions & 1 deletion src/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,10 +80,17 @@ export default defineNuxtModule<ModuleOptions>({
})

nuxt.hook('modules:done', async () => {
nuxt.options.runtimeConfig.public.cookieControl = defu(
const merged = defu(
nuxt.options.runtimeConfig.public.cookieControl,
moduleOptions,
nuxt.options.cookieControl,
)
for (const type of ['necessary', 'optional'] as const) {
merged.cookies[type] = [
...new Map(merged.cookies[type].map((c) => [c.id, c])).values(),
]
}
nuxt.options.runtimeConfig.public.cookieControl = merged
})
},
})
Expand Down