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

Typed cookie id's #209

Closed
2 tasks done
BlueBazze opened this issue May 2, 2024 · 7 comments
Closed
2 tasks done

Typed cookie id's #209

BlueBazze opened this issue May 2, 2024 · 7 comments

Comments

@BlueBazze
Copy link
Contributor

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

  • Would you be willing to help implement this feature?

Final checks

@dargmuesli
Copy link
Owner

Looks good to me

@BlueBazze
Copy link
Contributor Author

@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
Copy link
Owner

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
Copy link
Contributor Author

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

@dargmuesli
Copy link
Owner

dargmuesli commented May 10, 2024

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
Copy link
Contributor Author

I'll take a look within the next week

@BlueBazze
Copy link
Contributor Author

BlueBazze commented May 12, 2024

@dargmuesli Done, fixed with #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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants