dargmuesli / nuxt-cookie-control

A highly configurable cookie banner for Nuxt.
MIT License
229 stars 43 forks source link

Typed cookie id's #209

Closed BlueBazze closed 4 months ago

BlueBazze commented 4 months ago

Describe the feature

Would be nice with having cookies typed.

I wanna know if this is wanted, before making any pr.

const cookies = [
  {
    id: "auth-token",
    name: "Auth token",
    isPreselected: true,
    description: "This cookie is used to authenticate the user.",
  },
  {
    id: "XSRF-TOKEN",
    name: "CSRF token",
    isPreselected: true,
    description: "This cookie is used to prevent CSRF attacks.",
  },
] as const;

// This is a type that represents the id's of the `cookies` array
type CookieId =  typeof cookies[number]["id"];

const rr: CookieId = "auth-token"

I am working on a composable for my project, i'd like to have some kind of selector for which cookie i am targeting with the composable. Having the cookie id's as string literals instead of being typed as strings would catch misspelling.

It would probably not be anything major, just a new types file, a generator for this typing would be fairly small probably 10 lines.

Additional information

Final checks

dargmuesli commented 4 months ago

Looks good to me

BlueBazze commented 4 months ago

@dargmuesli Im working on it. And was wondering why the moduleOptions is being printed to a .ts file in the build directory. https://github.com/dargmuesli/nuxt-cookie-control/blob/master/src/module.ts#L59-L68

The only place it is being used is in the plugin. https://github.com/dargmuesli/nuxt-cookie-control/blob/master/src/runtime/plugin.ts#L9


Another workaround: https://nuxt.com/docs/guide/going-further/modules#exposing-options-to-runtime

module.ts

nuxt.options.runtimeConfig.cookieControl = moduleOptions

plugin.ts

const plugin: Plugin<{ cookies: State }> = defineNuxtPlugin((_nuxtApp) => {
  console.log('moduleOptions', _nuxtApp.$config.cookieControl)
}
dargmuesli commented 4 months ago

Migrating to runtimeConfig looks good to me, the file output was implemented because this module was one of the first to migrate to Nuxt 3, at a time at which recipes or other documentations were not available yet. Do you want to create a separate PR for that?

BlueBazze commented 4 months ago

Sorry, was on another task. I see that you already did it - Well done.

dargmuesli commented 4 months ago

I didn't manage to get runtimeConfig running though due to typing issues. The linter sourced from .nuxt (string properties only) and types (proper types) which led to type incompatibilities. Maybe you can figure out a way to have correct types in all places.

BlueBazze commented 4 months ago

I'll take a look within the next week

BlueBazze commented 4 months ago

@dargmuesli Done, fixed with https://github.com/dargmuesli/nuxt-cookie-control/pull/213

declare module '@nuxt/schema' {
  interface PublicRuntimeConfig {
    cookieControl: ModuleOptions
  }
}

I also moved it to public runtime config. Since the plugin is being run on the client side aswell. In case you want to change it to the non-public runtime config later, just simply remove the "Public" part of "PublicRuntimeConfig"

And the line

nuxt.options.runtimeConfig.public.cookieControl = moduleOptions

I've placed at the bottom of the module setup, since the moduleOptions are being changed during setup.