@yassidev/nuxt-directus 0.0.18 → 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 +11 -0
- package/dist/module.json +1 -1
- package/dist/module.mjs +12 -4
- package/dist/runtime/client.js +24 -3
- package/package.json +1 -1
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
package/dist/module.mjs
CHANGED
|
@@ -1815,8 +1815,14 @@ const module$1 = defineNuxtModule({
|
|
|
1815
1815
|
},
|
|
1816
1816
|
public: {
|
|
1817
1817
|
[NAME]: {
|
|
1818
|
-
url: config.
|
|
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
|
});
|
|
@@ -1847,7 +1853,8 @@ function normalizeConfig(options, nuxt) {
|
|
|
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 || "",
|
|
@@ -1855,7 +1862,8 @@ function normalizeConfig(options, nuxt) {
|
|
|
1855
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) {
|
package/dist/runtime/client.js
CHANGED
|
@@ -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
|
-
|
|
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);
|