@frigear-nu/cookie-consent 0.1.2 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/module.d.mts CHANGED
@@ -87,6 +87,11 @@ interface ConsentState {
87
87
  ts: number;
88
88
  categories: Record<string, boolean>;
89
89
  }
90
+ /** The response of `GET` and `POST /api/cookie-consent`: the signed-in user's stored choice. */
91
+ interface ConsentDto {
92
+ /** `null` while the user has not answered. */
93
+ consent: ConsentState | null;
94
+ }
90
95
 
91
96
  declare module '@nuxt/schema' {
92
97
  interface NuxtConfig {
@@ -137,4 +142,4 @@ declare module 'nitropack' {
137
142
  declare const _default: _nuxt_schema.NuxtModule<ModuleOptions, ModuleOptions, false>;
138
143
 
139
144
  export { _default as default };
140
- export type { ConsentCategory, ConsentCookie, ConsentMessages, ConsentState, ModuleOptions, PublicModuleOptions };
145
+ export type { ConsentCategory, ConsentCookie, ConsentDto, ConsentMessages, ConsentState, ModuleOptions, PublicModuleOptions };
package/dist/module.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@frigear-nu/cookie-consent",
3
3
  "configKey": "cookieConsent",
4
- "version": "0.1.2",
4
+ "version": "0.2.0",
5
5
  "builder": {
6
6
  "@nuxt/module-builder": "1.0.3",
7
7
  "unbuild": "unknown"
package/dist/module.mjs CHANGED
@@ -1,5 +1,5 @@
1
- import { defineNuxtModule, createResolver, hasNuxtModule, addImports, addServerImports, addComponentsDir, addServerHandler } from '@nuxt/kit';
2
- import { registerModule, registerTailwindSource, registerLocales, extendDatabaseTable } from '@frigear-nu/module-kit/utils';
1
+ import { defineNuxtModule, createResolver, hasNuxtModule, addImports, addServerImports, addComponentsDir } from '@nuxt/kit';
2
+ import { registerModule, registerTailwindSource, registerLocales, addOptionalImports, inlineModuleRuntime, addModuleRoutes, extendDatabaseTable } from '@frigear-nu/module-kit/utils';
3
3
  import { mergeCategories, DEFAULT_CATEGORIES, computeVersion } from '../dist/runtime/utils/consent.js';
4
4
 
5
5
  const DEFAULT_MAX_AGE = 60 * 60 * 24 * 180;
@@ -26,6 +26,12 @@ const module$1 = defineNuxtModule({
26
26
  registerTailwindSource(nuxt, resolver.resolve("./runtime/app"));
27
27
  registerLocales(nuxt, resolver.resolve("./runtime/i18n/lang"));
28
28
  const persistToUser = (options.persistToUser ?? true) && hasNuxtModule("@frigear-nu/users");
29
+ addOptionalImports(nuxt, "#cookie-consent/optional", [{
30
+ name: "useUserSession",
31
+ available: () => hasNuxtModule("@frigear-nu/auth", nuxt),
32
+ fallback: `() => { throw new Error('[@frigear-nu/cookie-consent] \`persistToUser\` needs \`@frigear-nu/users\`') }`,
33
+ fallbackType: "() => { loggedIn: import('vue').Ref<boolean> }"
34
+ }]);
29
35
  addImports([
30
36
  { name: "useCookieConsent", from: resolver.resolve("./runtime/app/composables/useCookieConsent") },
31
37
  { name: "onCookieConsent", from: resolver.resolve("./runtime/app/composables/useCookieConsent") },
@@ -35,10 +41,13 @@ const module$1 = defineNuxtModule({
35
41
  { name: "getCookieConsent", from: resolver.resolve("./runtime/server/utils/consent") },
36
42
  { name: "hasCookieConsent", from: resolver.resolve("./runtime/server/utils/consent") }
37
43
  ]);
44
+ inlineModuleRuntime(nuxt, resolver);
38
45
  addComponentsDir({ path: resolver.resolve("./runtime/app/components"), priority: -1 });
39
46
  if (persistToUser) {
40
- addServerHandler({ route: "/api/cookie-consent", method: "get", handler: resolver.resolve("./runtime/server/routes/cookie-consent/index.get") });
41
- addServerHandler({ route: "/api/cookie-consent", method: "post", handler: resolver.resolve("./runtime/server/routes/cookie-consent/index.post") });
47
+ addModuleRoutes(resolver, [
48
+ { method: "get", path: "", file: "cookie-consent/index.get" },
49
+ { method: "post", path: "", file: "cookie-consent/index.post" }
50
+ ], { base: "/api/cookie-consent" });
42
51
  extendDatabaseTable(nuxt, {
43
52
  resolver,
44
53
  file: (dialect) => `./runtime/server/db/schema.${dialect}`
@@ -1,5 +1,7 @@
1
1
  <script setup>
2
2
  import { useWindowScroll } from "@vueuse/core";
3
+ import { computed, onMounted, ref, useConsentT, useCookieConsent, useNuxtApp, useRoute, watch } from "#imports";
4
+ import { useUserSession } from "#cookie-consent/optional";
3
5
  const { config, hasResponded, hydrateFromUser } = useCookieConsent();
4
6
  const { t, policyUrl } = useConsentT();
5
7
  const route = useRoute();
@@ -1,4 +1,5 @@
1
1
  <script setup>
2
+ import { useConsentT, useCookieConsent } from "#imports";
2
3
  const { acceptAll, rejectAll, openPreferences } = useCookieConsent();
3
4
  const { t } = useConsentT();
4
5
  </script>
@@ -1,4 +1,5 @@
1
1
  <script setup>
2
+ import { useConsentT, useCookieConsent } from "#imports";
2
3
  const { openPreferences } = useCookieConsent();
3
4
  const { t } = useConsentT();
4
5
  </script>
@@ -1,4 +1,5 @@
1
1
  <script setup>
2
+ import { ref, useConsentT, useCookieConsent, watch } from "#imports";
2
3
  const { categories, consent, isPreferencesOpen, save, acceptAll, rejectAll } = useCookieConsent();
3
4
  const { t, label, description, localize } = useConsentT();
4
5
  const choices = ref({});
@@ -1,8 +1,8 @@
1
1
  import type { ConsentCategory, ConsentMessages, Translated } from '../../../types.js';
2
2
  export declare function useConsentT(): {
3
- locale: any;
3
+ locale: import("vue").ComputedRef<string>;
4
4
  /** The policy link target for the current locale, if `policyUrl` is set. */
5
- policyUrl: any;
5
+ policyUrl: import("vue").ComputedRef<string | undefined>;
6
6
  /** The `policyLabel` option wins over the translated `policy` text. */
7
7
  t: (key: keyof ConsentMessages) => any;
8
8
  label: (category: ConsentCategory) => string;
@@ -1,4 +1,5 @@
1
1
  import { localize } from "../../utils/consent.js";
2
+ import { computed, useModuleT, useNuxtApp, useRuntimeConfig } from "#imports";
2
3
  const EN = {
3
4
  description: "We use cookies to improve your experience. Choose which categories you allow.",
4
5
  accept: "Accept all",
@@ -1,22 +1,22 @@
1
1
  import type { ConsentState } from '../../../types.js';
2
2
  export declare function useCookieConsent(): {
3
- config: any;
3
+ config: {};
4
4
  categories: any;
5
- consent: any;
6
- hasResponded: any;
5
+ consent: import("vue").ComputedRef<ConsentState | null>;
6
+ hasResponded: import("vue").ComputedRef<boolean>;
7
7
  isAllowed: (key: string) => boolean;
8
8
  acceptAll: () => Promise<ConsentState>;
9
9
  rejectAll: () => Promise<ConsentState>;
10
10
  save: (choices: Record<string, boolean>) => Promise<ConsentState>;
11
11
  reset: () => Promise<void>;
12
12
  hydrateFromUser: () => Promise<void>;
13
- isPreferencesOpen: any;
13
+ isPreferencesOpen: import("vue").Ref<boolean, boolean>;
14
14
  openPreferences: () => void;
15
- } | undefined;
15
+ };
16
16
  /**
17
17
  * Runs `callback` once the visitor has granted `category` - immediately if they already did, or as
18
18
  * soon as they do. The optional return value is called when consent is withdrawn.
19
19
  *
20
20
  * onCookieConsent("analytics", () => { startTracking(); return () => stopTracking() })
21
21
  */
22
- export declare function onCookieConsent(category: string, callback: () => (() => void) | undefined): any;
22
+ export declare function onCookieConsent(category: string, callback: () => (() => void) | undefined): import("vue").WatchHandle;
@@ -1,4 +1,7 @@
1
+ import { cookieConsentApi } from "../../utils/api.js";
1
2
  import { acceptAllChoices, buildConsent, normalizeConsent, rejectAllChoices } from "../../utils/consent.js";
3
+ import { $fetch, computed, effectScope, getCurrentScope, onScopeDispose, useCookie, useNuxtApp, useRuntimeConfig, useState, watch } from "#imports";
4
+ import { useUserSession } from "#cookie-consent/optional";
2
5
  const createCookieConsent = (nuxtApp) => {
3
6
  const config = useRuntimeConfig().public.cookieConsent;
4
7
  const cookie = useCookie(config.cookieName, {
@@ -18,7 +21,7 @@ const createCookieConsent = (nuxtApp) => {
18
21
  const { loggedIn } = useUserSession();
19
22
  if (!loggedIn.value) return;
20
23
  try {
21
- await $fetch("/api/cookie-consent", { method: "POST", body: { categories: state.categories, version: state.v } });
24
+ await $fetch(cookieConsentApi.root, { method: "POST", body: { categories: state.categories, version: state.v } });
22
25
  } catch (error) {
23
26
  console.warn("[cookie-consent] Failed to store consent on the user.", error);
24
27
  }
@@ -46,7 +49,7 @@ const createCookieConsent = (nuxtApp) => {
46
49
  const { loggedIn } = useUserSession();
47
50
  if (!loggedIn.value) return;
48
51
  try {
49
- const { consent: stored } = await $fetch("/api/cookie-consent");
52
+ const { consent: stored } = await $fetch(cookieConsentApi.root);
50
53
  const state = normalizeConsent(stored, config.categories, config.version, stored?.ts);
51
54
  if (state) {
52
55
  cookie.value = state;
@@ -65,7 +68,11 @@ export function useCookieConsent() {
65
68
  const nuxtApp = useNuxtApp();
66
69
  let instance = instances.get(nuxtApp);
67
70
  if (!instance) {
68
- instance = nuxtApp.runWithContext(() => effectScope(true).run(() => createCookieConsent(nuxtApp)));
71
+ let created;
72
+ nuxtApp.runWithContext(() => {
73
+ created = effectScope(true).run(() => createCookieConsent(nuxtApp));
74
+ });
75
+ instance = created;
69
76
  instances.set(nuxtApp, instance);
70
77
  }
71
78
  return instance;
@@ -1,6 +1,7 @@
1
1
  import { db, schema } from "@nuxthub/db";
2
2
  import { eq } from "drizzle-orm";
3
3
  import { z } from "zod";
4
+ import { apiError } from "@frigear-nu/module-kit/server";
4
5
  import { buildConsent } from "../../../utils/consent.js";
5
6
  import { getCookieConsentConfig } from "../../utils/consent.js";
6
7
  const bodySchema = z.object({
@@ -12,12 +13,12 @@ export default defineEventHandler(async (event) => {
12
13
  const config = getCookieConsentConfig(event);
13
14
  const body = await readValidatedBody(event, bodySchema.parse);
14
15
  if (body.version !== config.version) {
15
- throw createError({ statusCode: 409, statusMessage: "Outdated cookie consent version." });
16
+ throw apiError(409, "outdated_version", "Outdated cookie consent version.");
16
17
  }
17
18
  const known = new Set(config.categories.map((category) => category.key));
18
19
  const unknown = Object.keys(body.categories).filter((key) => !known.has(key));
19
20
  if (unknown.length) {
20
- throw createError({ statusCode: 400, statusMessage: `Unknown categories: ${unknown.join(", ")}` });
21
+ throw apiError(400, "unknown_categories", `Unknown categories: ${unknown.join(", ")}`, { categories: unknown });
21
22
  }
22
23
  const state = buildConsent(config.categories, config.version, body.categories);
23
24
  await db.update(schema.users).set({
@@ -26,5 +27,5 @@ export default defineEventHandler(async (event) => {
26
27
  cookieConsentCategories: JSON.stringify(state.categories)
27
28
  }).where(eq(schema.users.id, user.id));
28
29
  await useNitroApp().hooks.callHook("cookie-consent:saved", { userId: user.id, state, acceptedAt: state.ts });
29
- return state;
30
+ return { consent: state };
30
31
  });
@@ -0,0 +1,8 @@
1
+ /** The cookie-consent API (registered only while consent is persisted on the user). */
2
+ export declare const cookieConsentApi: {
3
+ /**
4
+ * Session only. `GET`: `{ consent }`, the signed-in user's stored choice (`null` before they answered).
5
+ * `POST { categories, version }`: stores the choice and answers the same `{ consent }` shape (200).
6
+ */
7
+ readonly root: "/api/cookie-consent";
8
+ };
@@ -0,0 +1,7 @@
1
+ export const cookieConsentApi = {
2
+ /**
3
+ * Session only. `GET`: `{ consent }`, the signed-in user's stored choice (`null` before they answered).
4
+ * `POST { categories, version }`: stores the choice and answers the same `{ consent }` shape (200).
5
+ */
6
+ root: "/api/cookie-consent"
7
+ };
@@ -1,3 +1,4 @@
1
1
  export { extendCookieConsent, defineConsentCategory } from './extend.js';
2
2
  export { DEFAULT_CATEGORIES, mergeCategories, computeVersion, buildConsent, normalizeConsent, acceptAllChoices, rejectAllChoices, localize, } from './consent.js';
3
- export type { ConsentCategory, ConsentCookie, ConsentState, Translated } from '../../types.js';
3
+ export { cookieConsentApi } from './api.js';
4
+ export type { ConsentCategory, ConsentCookie, ConsentDto, ConsentState, Translated } from '../../types.js';
@@ -9,3 +9,4 @@ export {
9
9
  rejectAllChoices,
10
10
  localize
11
11
  } from "./consent.js";
12
+ export { cookieConsentApi } from "./api.js";
package/dist/types.d.mts CHANGED
@@ -2,4 +2,4 @@ export { type Translated } from '@frigear-nu/ui/translated'
2
2
 
3
3
  export { default } from './module.mjs'
4
4
 
5
- export { type ConsentCategory, type ConsentCookie, type ConsentMessages, type ConsentState, type ModuleOptions, type PublicModuleOptions } from './module.mjs'
5
+ export { type ConsentCategory, type ConsentCookie, type ConsentDto, type ConsentMessages, type ConsentState, type ModuleOptions, type PublicModuleOptions } from './module.mjs'
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@frigear-nu/cookie-consent",
3
- "version": "0.1.2",
3
+ "version": "0.2.0",
4
4
  "description": "GDPR cookie consent banner with categories - modules contribute categories (extendCookieConsent), apps gate scripts on consent, and the choice is stored as proof on the user.",
5
5
  "homepage": "https://docs.frigear.dev/modules/cookie-consent",
6
6
  "repository": {
@@ -21,6 +21,11 @@
21
21
  "types": "./dist/runtime/utils/index.d.ts",
22
22
  "import": "./dist/runtime/utils/index.js",
23
23
  "default": "./dist/runtime/utils/index.js"
24
+ },
25
+ "./api": {
26
+ "types": "./dist/runtime/utils/api.d.ts",
27
+ "import": "./dist/runtime/utils/api.js",
28
+ "default": "./dist/runtime/utils/api.js"
24
29
  }
25
30
  },
26
31
  "files": [
@@ -30,8 +35,8 @@
30
35
  "playground"
31
36
  ],
32
37
  "dependencies": {
33
- "@frigear-nu/module-kit": "1.1.2",
34
- "@frigear-nu/ui": "0.1.2",
38
+ "@frigear-nu/module-kit": "1.2.0",
39
+ "@frigear-nu/ui": "0.2.0",
35
40
  "@libsql/client": "^0.18.0",
36
41
  "@nuxt/kit": "^4.6.1",
37
42
  "@vueuse/core": "^15.0.0",
@@ -60,8 +65,8 @@
60
65
  }
61
66
  },
62
67
  "devDependencies": {
63
- "@frigear-nu/auth": "0.1.3",
64
- "@frigear-nu/users": "1.1.3",
68
+ "@frigear-nu/auth": "0.2.0",
69
+ "@frigear-nu/users": "1.2.0",
65
70
  "@nuxt/devtools": "^3.4.2",
66
71
  "@nuxt/eslint-config": "^1.17.0",
67
72
  "@nuxt/module-builder": "^1.0.3",