@yassidev/nuxt-directus 0.0.22 → 0.0.23

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
@@ -546,12 +546,6 @@ interface ProxyOptions {
546
546
  interface ModuleOptions {
547
547
  url?: string;
548
548
  accessToken?: string;
549
- composables?: false | {
550
- enabled?: boolean;
551
- mode?: 'graphql' | 'rest';
552
- client?: boolean;
553
- server?: boolean;
554
- };
555
549
  i18n?: false | {
556
550
  enabled?: boolean;
557
551
  sync?: boolean;
@@ -573,18 +567,7 @@ interface ModuleOptions {
573
567
  enabled?: boolean;
574
568
  alias?: string;
575
569
  };
576
- auth?: false | AuthConfig;
577
570
  }
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
- });
588
571
  declare const _default: _nuxt_schema.NuxtModule<ModuleOptions, ModuleOptions, false>;
589
572
 
590
573
  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.22",
4
+ "version": "0.0.23",
5
5
  "builder": {
6
6
  "@nuxt/module-builder": "1.0.2",
7
7
  "unbuild": "3.6.1"
package/dist/module.mjs CHANGED
@@ -1,4 +1,4 @@
1
- import { defineNuxtModule, useLogger, updateRuntimeConfig, hasNuxtModule, createResolver, addImportsDir, addImports, addServerImportsDir, addServerImports, addTemplate, addTypeTemplate } from 'nuxt/kit';
1
+ import { defineNuxtModule, useLogger, createResolver, addImports, addServerImports, updateRuntimeConfig, hasNuxtModule, addTemplate, addTypeTemplate } from 'nuxt/kit';
2
2
  import { generateDirectusTypes } from 'directus-sdk-typegen';
3
3
  import { fetchTranslations, syncTranslations } from '../dist/runtime/server.js';
4
4
  import { unwatchFile, watchFile, watch as watch$1, stat as stat$1, readFileSync, existsSync } from 'node:fs';
@@ -1807,33 +1807,40 @@ 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
+ const { resolve } = createResolver(import.meta.url);
1811
+ addImports([
1812
+ {
1813
+ name: "createBaseDirectus",
1814
+ from: resolve("./runtime/client")
1815
+ },
1816
+ {
1817
+ name: "handleDirectusError",
1818
+ from: resolve("./runtime/client")
1819
+ }
1820
+ ]);
1821
+ addServerImports([{
1822
+ name: "createBaseDirectus",
1823
+ from: resolve("./runtime/client")
1824
+ }, {
1825
+ name: "handleDirectusError",
1826
+ from: resolve("./runtime/client")
1827
+ }]);
1816
1828
  updateRuntimeConfig({
1817
1829
  [NAME]: {
1818
1830
  url: config.url,
1819
1831
  accessToken: config.accessToken,
1820
- i18nPrefix: config.i18n.prefix,
1821
- auth
1832
+ i18nPrefix: config.i18n.prefix
1822
1833
  },
1823
1834
  public: {
1824
1835
  [NAME]: {
1825
- url: config.composables.client ? config.url : void 0,
1826
- i18nPrefix: config.i18n.prefix,
1827
- auth
1836
+ url: config.url,
1837
+ i18nPrefix: config.i18n.prefix
1828
1838
  }
1829
1839
  }
1830
1840
  });
1831
1841
  if (config.types.enabled) {
1832
1842
  setupTypes(config, nuxt, logger);
1833
1843
  }
1834
- if (config.composables.enabled) {
1835
- setupComposables(config, nuxt, logger);
1836
- }
1837
1844
  if (config.i18n.enabled) {
1838
1845
  if (!hasNuxtModule("@nuxtjs/i18n")) return logger.error("@nuxtjs/i18n is not installed! Please install it to use the i18n features of this module.");
1839
1846
  setupI18n(config, nuxt, logger);
@@ -1854,39 +1861,16 @@ function normalizeConfig(options, nuxt) {
1854
1861
  i18n: options.i18n === false ? { enabled: false } : options.i18n,
1855
1862
  types: options.types === false ? { enabled: false } : options.types,
1856
1863
  proxy: options.proxy === false ? { enabled: false } : options.proxy,
1857
- image: options.image === false ? { enabled: false } : options.image,
1858
- composables: options.composables === false ? { enabled: false } : options.composables,
1859
- auth: options.auth === false ? { enabled: false } : options.auth
1864
+ image: options.image === false ? { enabled: false } : options.image
1860
1865
  }, {
1861
1866
  url: process.env.DIRECTUS_URL ?? "http://localhost:8055",
1862
1867
  accessToken: process.env.DIRECTUS_ACCESS_TOKEN || process.env.DIRECTUS_ADMIN_TOKEN || "",
1863
1868
  i18n: { enabled: hasNuxtModule("@nuxtjs/i18n"), sync: nuxt.options.dev, prefix: void 0 },
1864
1869
  types: { enabled: nuxt.options.dev, transform: [] },
1865
1870
  proxy: { enabled: true, path: "/directus", options: {} },
1866
- image: { enabled: hasNuxtModule("@nuxt/image"), alias: "directus" },
1867
- composables: { enabled: true, mode: "rest", client: true, server: false },
1868
- auth: { enabled: true, mode: "cookie", autoRefresh: true, cookieName: "auth_token" }
1871
+ image: { enabled: hasNuxtModule("@nuxt/image"), alias: "directus" }
1869
1872
  });
1870
1873
  }
1871
- function setupComposables(config, nuxt, logger) {
1872
- const { resolve } = createResolver(import.meta.url);
1873
- const path = resolve(`./runtime/composables`);
1874
- const name = config.composables.mode === "graphql" ? "useDirectusGraphql" : "useDirectusRest";
1875
- const mainImport = {
1876
- name,
1877
- as: "useDirectus",
1878
- from: join(path, "useDirectus")
1879
- };
1880
- if (config.composables.client) {
1881
- addImportsDir(path);
1882
- addImports(mainImport);
1883
- }
1884
- if (config.composables.server) {
1885
- addServerImportsDir(path);
1886
- addServerImports(mainImport);
1887
- }
1888
- logger.info(`Composables have been registered (mode: ${config.composables.mode}).`);
1889
- }
1890
1874
  function setupI18n(config, nuxt, logger) {
1891
1875
  const { resolve } = createResolver(import.meta.url);
1892
1876
  const codes = (nuxt.options.i18n?.locales || []).map((locale) => typeof locale === "string" ? locale : locale.code);
@@ -1,7 +1,11 @@
1
+ import type { FetchContext, FetchResponse } from 'ofetch';
1
2
  /**
2
3
  * Create base directus instance.
3
4
  */
4
5
  export declare function createBaseDirectus(): import("@directus/sdk").DirectusClient<import('#directus/types').Schema>;
6
+ export declare function handleDirectusError({ response }: FetchContext<unknown> & {
7
+ response: FetchResponse<any>;
8
+ }): void;
5
9
  /**
6
10
  * Get i18n translations.
7
11
  */
@@ -1,7 +1,7 @@
1
1
  import { NAME } from "./constants.js";
2
- import { authentication, createDirectus, staticToken } from "@directus/sdk";
2
+ import { createDirectus } from "@directus/sdk";
3
3
  import { parseHost, joinURL } from "ufo";
4
- import { createError, useCookie, useRuntimeConfig } from "#imports";
4
+ import { createError, useRuntimeConfig } from "#imports";
5
5
  import { fetchTranslations } from "./server.js";
6
6
  function getRuntimeConfig() {
7
7
  if (import.meta.server) return useRuntimeConfig()[NAME];
@@ -17,58 +17,16 @@ function getRuntimeClientDirectusURL(path) {
17
17
  return hasHost ? path : joinURL(window.location.origin, path);
18
18
  }
19
19
  export function createBaseDirectus() {
20
- const { url, auth } = getRuntimeConfig();
21
- const instance = createDirectus(url, {
22
- globals: {
23
- fetch: $fetch.create({
24
- async onRequest({ options }) {
25
- if (!auth?.cookieName) return;
26
- const token = useCookie(auth.cookieName).value;
27
- if (token) {
28
- options.headers.set("Authorization", `Bearer ${token}`);
29
- }
30
- },
31
- async onResponse({ response, options, request }) {
32
- try {
33
- if (!auth?.cookieName) return;
34
- if (request.toString().endsWith("/auth/login")) {
35
- const { mode } = JSON.parse(String(options.body));
36
- if (mode === "cookie") {
37
- const { access_token, expires } = response._data.data;
38
- useCookie(auth.cookieName, {
39
- expires: expires ? new Date(Date.now() + expires * 1e3) : void 0
40
- }).value = access_token;
41
- }
42
- }
43
- } catch (error) {
44
- console.error("Error handling response:", error);
45
- }
46
- },
47
- onResponseError({ response }) {
48
- const [error] = response._data.errors || [];
49
- if (!error) return;
50
- throw createError({
51
- status: response.status,
52
- message: error.message
53
- });
54
- }
55
- })
56
- }
20
+ const { url } = getRuntimeConfig();
21
+ return createDirectus(url);
22
+ }
23
+ export function handleDirectusError({ response }) {
24
+ const [error] = response._data?.errors || [];
25
+ if (!error) return;
26
+ throw createError({
27
+ status: response.status,
28
+ message: error.message
57
29
  });
58
- if (auth.mode === "static") {
59
- instance.with(staticToken(auth.token));
60
- } else if (auth.mode === "cookie") {
61
- instance.with(authentication("cookie", {
62
- autoRefresh: auth.autoRefresh,
63
- credentials: "include"
64
- }));
65
- } else if (auth.mode === "session") {
66
- instance.with(authentication("session", {
67
- autoRefresh: auth.autoRefresh,
68
- credentials: "include"
69
- }));
70
- }
71
- return instance;
72
30
  }
73
31
  export async function getI18nTranslations(locale, config = getRuntimeConfig()) {
74
32
  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.22",
3
+ "version": "0.0.23",
4
4
  "description": "A Nuxt module for better integration with Directus CMS.",
5
5
  "repository": "yassilah/nuxt-directus",
6
6
  "license": "MIT",
@@ -1,8 +0,0 @@
1
- /**
2
- * Use directus (rest)
3
- */
4
- export declare function useDirectusRest(): import("@directus/sdk").DirectusClient<import('#directus/types').Schema> & import("@directus/sdk").RestClient<import('#directus/types').Schema>;
5
- /**
6
- * Use directus (rest)
7
- */
8
- export declare function useDirectusGraphql(): import("@directus/sdk").DirectusClient<import('#directus/types').Schema> & import("@directus/sdk").GraphqlClient<import('#directus/types').Schema>;
@@ -1,8 +0,0 @@
1
- import { rest, graphql } from "@directus/sdk";
2
- import { createBaseDirectus } from "./../client.js";
3
- export function useDirectusRest() {
4
- return createBaseDirectus().with(rest());
5
- }
6
- export function useDirectusGraphql() {
7
- return createBaseDirectus().with(graphql());
8
- }