@yassidev/nuxt-directus 0.0.16 → 0.0.19

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.16",
4
+ "version": "0.0.19",
5
5
  "builder": {
6
6
  "@nuxt/module-builder": "1.0.2",
7
7
  "unbuild": "3.6.1"
package/dist/module.mjs CHANGED
@@ -1801,7 +1801,7 @@ const module$1 = defineNuxtModule({
1801
1801
  meta: { name: NAME },
1802
1802
  setup(options, nuxt) {
1803
1803
  const logger = useLogger(NAME);
1804
- const config = normalizeConfig(options);
1804
+ const config = normalizeConfig(options, nuxt);
1805
1805
  if (!config.accessToken || !config.url) {
1806
1806
  logger.error(`Please provide both 'url' and 'accessToken' options.`);
1807
1807
  }
@@ -1815,8 +1815,14 @@ const module$1 = defineNuxtModule({
1815
1815
  },
1816
1816
  public: {
1817
1817
  [NAME]: {
1818
- url: config.proxy.enabled ? config.proxy.path : config.composables.client ? config.url : void 0,
1819
- i18nPrefix: config.i18n.prefix
1818
+ url: config.composables.client ? config.url : void 0,
1819
+ i18nPrefix: config.i18n.prefix,
1820
+ auth: config.auth.enabled ? {
1821
+ mode: config.auth.mode,
1822
+ autoRefresh: config.auth.mode === "cookie" || config.auth.mode === "session" ? config.auth.autoRefresh : void 0,
1823
+ cookieName: config.auth.mode === "cookie" || config.auth.mode === "session" ? config.auth.cookieName : void 0,
1824
+ token: config.auth.mode === "static" ? config.auth.token ?? config.accessToken : void 0
1825
+ } : void 0
1820
1826
  }
1821
1827
  }
1822
1828
  });
@@ -1839,7 +1845,7 @@ const module$1 = defineNuxtModule({
1839
1845
  }
1840
1846
  }
1841
1847
  });
1842
- function normalizeConfig(options) {
1848
+ function normalizeConfig(options, nuxt) {
1843
1849
  return defu({
1844
1850
  url: options.url,
1845
1851
  accessToken: options.accessToken,
@@ -1847,15 +1853,17 @@ function normalizeConfig(options) {
1847
1853
  types: options.types === false ? { enabled: false } : options.types,
1848
1854
  proxy: options.proxy === false ? { enabled: false } : options.proxy,
1849
1855
  image: options.image === false ? { enabled: false } : options.image,
1850
- composables: options.composables === false ? { enabled: false } : options.composables
1856
+ composables: options.composables === false ? { enabled: false } : options.composables,
1857
+ auth: options.auth === false ? { enabled: false } : options.auth
1851
1858
  }, {
1852
1859
  url: process.env.DIRECTUS_URL ?? "http://localhost:8055",
1853
1860
  accessToken: process.env.DIRECTUS_ACCESS_TOKEN || process.env.DIRECTUS_ADMIN_TOKEN || "",
1854
- i18n: { enabled: hasNuxtModule("@nuxtjs/i18n"), sync: true, prefix: void 0 },
1855
- types: { enabled: true, transform: [] },
1861
+ i18n: { enabled: hasNuxtModule("@nuxtjs/i18n"), sync: nuxt.options.dev, prefix: void 0 },
1862
+ types: { enabled: nuxt.options.dev, transform: [] },
1856
1863
  proxy: { enabled: true, path: "/directus", options: {} },
1857
1864
  image: { enabled: hasNuxtModule("@nuxt/image"), alias: "directus" },
1858
- composables: { enabled: true, mode: "rest", client: true, server: true }
1865
+ composables: { enabled: true, mode: "rest", client: true, server: true },
1866
+ auth: { enabled: true, mode: "session", autoRefresh: true, cookieName: "directus_session_token" }
1859
1867
  });
1860
1868
  }
1861
1869
  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.16",
3
+ "version": "0.0.19",
4
4
  "description": "A Nuxt module for better integration with Directus CMS.",
5
5
  "repository": "yassilah/nuxt-directus",
6
6
  "license": "MIT",