@yassidev/nuxt-directus 0.0.18 → 0.0.20

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
@@ -573,7 +573,18 @@ interface ModuleOptions {
573
573
  enabled?: boolean;
574
574
  alias?: string;
575
575
  };
576
+ auth?: false | AuthConfig;
576
577
  }
578
+ type AuthConfig = {
579
+ enabled?: boolean;
580
+ } & ({
581
+ mode: 'static';
582
+ token?: string;
583
+ } | {
584
+ mode: 'cookie' | 'session';
585
+ autoRefresh?: boolean;
586
+ cookieName?: string;
587
+ });
577
588
  declare const _default: _nuxt_schema.NuxtModule<ModuleOptions, ModuleOptions, false>;
578
589
 
579
590
  export { _default as default };
package/dist/module.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "directus",
3
3
  "configKey": "directus",
4
- "version": "0.0.18",
4
+ "version": "0.0.20",
5
5
  "builder": {
6
6
  "@nuxt/module-builder": "1.0.2",
7
7
  "unbuild": "3.6.1"
package/dist/module.mjs CHANGED
@@ -1807,16 +1807,24 @@ const module$1 = defineNuxtModule({
1807
1807
  }
1808
1808
  nuxt.options.typescript.hoist ??= [];
1809
1809
  nuxt.options.typescript.hoist.push("@directus/sdk");
1810
+ const auth = config.auth.enabled ? {
1811
+ mode: config.auth.mode,
1812
+ autoRefresh: config.auth.mode === "cookie" || config.auth.mode === "session" ? config.auth.autoRefresh : void 0,
1813
+ cookieName: config.auth.mode === "cookie" || config.auth.mode === "session" ? config.auth.cookieName : void 0,
1814
+ token: config.auth.mode === "static" ? config.auth.token ?? config.accessToken : void 0
1815
+ } : void 0;
1810
1816
  updateRuntimeConfig({
1811
1817
  [NAME]: {
1812
1818
  url: config.url,
1813
1819
  accessToken: config.accessToken,
1814
- i18nPrefix: config.i18n.prefix
1820
+ i18nPrefix: config.i18n.prefix,
1821
+ auth
1815
1822
  },
1816
1823
  public: {
1817
1824
  [NAME]: {
1818
- url: config.proxy.enabled ? config.proxy.path : config.composables.client ? config.url : void 0,
1819
- i18nPrefix: config.i18n.prefix
1825
+ url: config.composables.client ? config.url : void 0,
1826
+ i18nPrefix: config.i18n.prefix,
1827
+ auth
1820
1828
  }
1821
1829
  }
1822
1830
  });
@@ -1847,7 +1855,8 @@ function normalizeConfig(options, nuxt) {
1847
1855
  types: options.types === false ? { enabled: false } : options.types,
1848
1856
  proxy: options.proxy === false ? { enabled: false } : options.proxy,
1849
1857
  image: options.image === false ? { enabled: false } : options.image,
1850
- composables: options.composables === false ? { enabled: false } : options.composables
1858
+ composables: options.composables === false ? { enabled: false } : options.composables,
1859
+ auth: options.auth === false ? { enabled: false } : options.auth
1851
1860
  }, {
1852
1861
  url: process.env.DIRECTUS_URL ?? "http://localhost:8055",
1853
1862
  accessToken: process.env.DIRECTUS_ACCESS_TOKEN || process.env.DIRECTUS_ADMIN_TOKEN || "",
@@ -1855,7 +1864,8 @@ function normalizeConfig(options, nuxt) {
1855
1864
  types: { enabled: nuxt.options.dev, transform: [] },
1856
1865
  proxy: { enabled: true, path: "/directus", options: {} },
1857
1866
  image: { enabled: hasNuxtModule("@nuxt/image"), alias: "directus" },
1858
- composables: { enabled: true, mode: "rest", client: true, server: true }
1867
+ composables: { enabled: true, mode: "rest", client: true, server: true },
1868
+ auth: { enabled: true, mode: "session", autoRefresh: true, cookieName: "directus_session_token" }
1859
1869
  });
1860
1870
  }
1861
1871
  function setupComposables(config, nuxt, logger) {
@@ -1,5 +1,5 @@
1
1
  import { NAME } from "./constants.js";
2
- import { createDirectus } from "@directus/sdk";
2
+ import { authentication, createDirectus, staticToken } from "@directus/sdk";
3
3
  import { parseHost, joinURL } from "ufo";
4
4
  import { createError, useRuntimeConfig } from "#imports";
5
5
  import { fetchTranslations } from "./server.js";
@@ -17,11 +17,18 @@ function getRuntimeClientDirectusURL(path) {
17
17
  return hasHost ? path : joinURL(window.location.origin, path);
18
18
  }
19
19
  export function createBaseDirectus() {
20
- const { url } = getRuntimeConfig();
21
- return createDirectus(url, {
20
+ const { url, auth } = getRuntimeConfig();
21
+ const instance = createDirectus(url, {
22
22
  globals: {
23
23
  fetch: $fetch.create({
24
24
  baseURL: url,
25
+ onRequest({ options }) {
26
+ if (!auth?.cookieName) return;
27
+ const token = toValue(useCookie(auth.cookieName));
28
+ if (token) {
29
+ options.headers.set("Authorization", `Bearer ${token}`);
30
+ }
31
+ },
25
32
  onResponseError({ response }) {
26
33
  const [error] = response._data.errors || [];
27
34
  if (!error) return;
@@ -33,6 +40,20 @@ export function createBaseDirectus() {
33
40
  })
34
41
  }
35
42
  });
43
+ if (auth.mode === "static") {
44
+ instance.with(staticToken(auth.token));
45
+ } else if (auth.mode === "cookie") {
46
+ instance.with(authentication("cookie", {
47
+ autoRefresh: auth.autoRefresh,
48
+ credentials: "include"
49
+ }));
50
+ } else if (auth.mode === "session") {
51
+ instance.with(authentication("session", {
52
+ autoRefresh: auth.autoRefresh,
53
+ credentials: "include"
54
+ }));
55
+ }
56
+ return instance;
36
57
  }
37
58
  export async function getI18nTranslations(locale, config = getRuntimeConfig()) {
38
59
  const data = await fetchTranslations(locale, config);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@yassidev/nuxt-directus",
3
- "version": "0.0.18",
3
+ "version": "0.0.20",
4
4
  "description": "A Nuxt module for better integration with Directus CMS.",
5
5
  "repository": "yassilah/nuxt-directus",
6
6
  "license": "MIT",