@yassidev/nuxt-directus 0.0.4 → 0.0.6
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 +2 -1
- package/dist/module.json +1 -1
- package/dist/module.mjs +3 -47
- package/dist/runtime/{helpers.js → client.js} +2 -2
- package/dist/runtime/composables/graphql.js +1 -1
- package/dist/runtime/composables/rest.js +1 -1
- package/dist/runtime/lang/index.js +1 -1
- package/dist/runtime/server.d.ts +19 -0
- package/dist/runtime/server.js +43 -0
- package/dist/types.d.mts +1 -1
- package/package.json +1 -1
- /package/dist/runtime/{helpers.d.ts → client.d.ts} +0 -0
package/dist/module.d.mts
CHANGED
|
@@ -570,6 +570,7 @@ interface ModuleOptions {
|
|
|
570
570
|
alias?: string;
|
|
571
571
|
};
|
|
572
572
|
}
|
|
573
|
+
declare const NAME = "directus";
|
|
573
574
|
declare const _default: _nuxt_schema.NuxtModule<ModuleOptions, ModuleOptions, false>;
|
|
574
575
|
|
|
575
|
-
export { _default as default };
|
|
576
|
+
export { NAME, _default as default };
|
package/dist/module.json
CHANGED
package/dist/module.mjs
CHANGED
|
@@ -1,10 +1,8 @@
|
|
|
1
1
|
import { defineNuxtModule, useLogger, updateRuntimeConfig, hasNuxtModule, createResolver, addImports, addServerImports, addTemplate, addTypeTemplate } from 'nuxt/kit';
|
|
2
2
|
import { generateDirectusTypes } from 'directus-sdk-typegen';
|
|
3
|
-
import {
|
|
3
|
+
import { fetchTranslations, syncTranslations } from '../dist/runtime/server.js';
|
|
4
4
|
import { readFileSync } from 'node:fs';
|
|
5
5
|
|
|
6
|
-
const NAME = "directus";
|
|
7
|
-
|
|
8
6
|
const JOIN_LEADING_SLASH_RE = /^\.?\//;
|
|
9
7
|
function hasTrailingSlash(input = "", respectQueryAndFragment) {
|
|
10
8
|
{
|
|
@@ -93,49 +91,7 @@ function createDefu(merger) {
|
|
|
93
91
|
}
|
|
94
92
|
const defu = createDefu();
|
|
95
93
|
|
|
96
|
-
|
|
97
|
-
return createDirectus(config.url).with(rest()).with(staticToken(config.accessToken || ""));
|
|
98
|
-
}
|
|
99
|
-
async function fetchTranslations(locale, config) {
|
|
100
|
-
const { i18nPrefix } = config;
|
|
101
|
-
const directus = useDirectus(config);
|
|
102
|
-
return await directus.request(readTranslations({
|
|
103
|
-
fields: ["key", "value", "id"],
|
|
104
|
-
limit: -1,
|
|
105
|
-
filter: { language: { _eq: locale }, ...i18nPrefix ? { key: { _startsWith: i18nPrefix } } : {} }
|
|
106
|
-
}));
|
|
107
|
-
}
|
|
108
|
-
async function syncTranslations(locale, translations, config) {
|
|
109
|
-
const directus = useDirectus(config);
|
|
110
|
-
const current = await fetchTranslations(locale, config);
|
|
111
|
-
const payload = {
|
|
112
|
-
create: [],
|
|
113
|
-
update: [],
|
|
114
|
-
remove: []
|
|
115
|
-
};
|
|
116
|
-
for (const [key, value] of Object.entries(translations)) {
|
|
117
|
-
const translation = current.find((t) => t.key === key);
|
|
118
|
-
if (!translation) {
|
|
119
|
-
payload.create.push({ key, value, language: locale });
|
|
120
|
-
} else if (translation.value !== value) {
|
|
121
|
-
payload.update.push({ id: translation.id, value });
|
|
122
|
-
}
|
|
123
|
-
}
|
|
124
|
-
for (const translation of current) {
|
|
125
|
-
if (!translations[translation.key]) {
|
|
126
|
-
payload.remove.push(translation.id);
|
|
127
|
-
}
|
|
128
|
-
}
|
|
129
|
-
if (!payload.create.length && !payload.update.length && !payload.remove.length) {
|
|
130
|
-
return false;
|
|
131
|
-
}
|
|
132
|
-
return Promise.all([
|
|
133
|
-
payload.update.length > 0 ? directus.request(updateTranslationsBatch(payload.update)) : null,
|
|
134
|
-
payload.create.length > 0 ? directus.request(createTranslations(payload.create)) : null,
|
|
135
|
-
payload.remove.length > 0 ? directus.request(deleteTranslations(payload.remove)) : null
|
|
136
|
-
]);
|
|
137
|
-
}
|
|
138
|
-
|
|
94
|
+
const NAME = "directus";
|
|
139
95
|
const module = defineNuxtModule({
|
|
140
96
|
meta: { name: NAME },
|
|
141
97
|
setup(options, nuxt) {
|
|
@@ -287,4 +243,4 @@ function setupImage(config, nuxt, logger) {
|
|
|
287
243
|
logger.info(`Image alias has been registered: ${config.image.alias} -> ${config.url}`);
|
|
288
244
|
}
|
|
289
245
|
|
|
290
|
-
export { module as default };
|
|
246
|
+
export { NAME, module as default };
|
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
import { NAME } from "
|
|
1
|
+
import { NAME } from "../module";
|
|
2
2
|
import { createDirectus } from "@directus/sdk";
|
|
3
3
|
import { parseHost, joinURL } from "ufo";
|
|
4
4
|
import { createError, useRuntimeConfig } from "#imports";
|
|
5
|
-
import { fetchTranslations } from "
|
|
5
|
+
import { fetchTranslations } from "./server.js";
|
|
6
6
|
function getRuntimeConfig() {
|
|
7
7
|
if (import.meta.server) return useRuntimeConfig()[NAME];
|
|
8
8
|
const base = useRuntimeConfig().public[NAME];
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
type Config = {
|
|
2
|
+
url: string;
|
|
3
|
+
accessToken?: string;
|
|
4
|
+
i18nPrefix?: string;
|
|
5
|
+
};
|
|
6
|
+
/**
|
|
7
|
+
* Fetch translations.
|
|
8
|
+
*/
|
|
9
|
+
export declare function fetchTranslations(locale: string, config: Config): Promise<{
|
|
10
|
+
id: string;
|
|
11
|
+
language: string;
|
|
12
|
+
key: string;
|
|
13
|
+
value: string;
|
|
14
|
+
}[]>;
|
|
15
|
+
/**
|
|
16
|
+
* Update translations.
|
|
17
|
+
*/
|
|
18
|
+
export declare function syncTranslations(locale: string, translations: Record<string, string>, config: Config): Promise<false | [Record<string, any>[] | null, Record<string, any>[] | null, void | null]>;
|
|
19
|
+
export {};
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import { createDirectus, createTranslations, deleteTranslations, readTranslations, rest, staticToken, updateTranslationsBatch } from "@directus/sdk";
|
|
2
|
+
function useDirectus(config) {
|
|
3
|
+
return createDirectus(config.url).with(rest()).with(staticToken(config.accessToken || ""));
|
|
4
|
+
}
|
|
5
|
+
export async function fetchTranslations(locale, config) {
|
|
6
|
+
const { i18nPrefix } = config;
|
|
7
|
+
const directus = useDirectus(config);
|
|
8
|
+
return await directus.request(readTranslations({
|
|
9
|
+
fields: ["key", "value", "id"],
|
|
10
|
+
limit: -1,
|
|
11
|
+
filter: { language: { _eq: locale }, ...i18nPrefix ? { key: { _startsWith: i18nPrefix } } : {} }
|
|
12
|
+
}));
|
|
13
|
+
}
|
|
14
|
+
export async function syncTranslations(locale, translations, config) {
|
|
15
|
+
const directus = useDirectus(config);
|
|
16
|
+
const current = await fetchTranslations(locale, config);
|
|
17
|
+
const payload = {
|
|
18
|
+
create: [],
|
|
19
|
+
update: [],
|
|
20
|
+
remove: []
|
|
21
|
+
};
|
|
22
|
+
for (const [key, value] of Object.entries(translations)) {
|
|
23
|
+
const translation = current.find((t) => t.key === key);
|
|
24
|
+
if (!translation) {
|
|
25
|
+
payload.create.push({ key, value, language: locale });
|
|
26
|
+
} else if (translation.value !== value) {
|
|
27
|
+
payload.update.push({ id: translation.id, value });
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
for (const translation of current) {
|
|
31
|
+
if (!translations[translation.key]) {
|
|
32
|
+
payload.remove.push(translation.id);
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
if (!payload.create.length && !payload.update.length && !payload.remove.length) {
|
|
36
|
+
return false;
|
|
37
|
+
}
|
|
38
|
+
return Promise.all([
|
|
39
|
+
payload.update.length > 0 ? directus.request(updateTranslationsBatch(payload.update)) : null,
|
|
40
|
+
payload.create.length > 0 ? directus.request(createTranslations(payload.create)) : null,
|
|
41
|
+
payload.remove.length > 0 ? directus.request(deleteTranslations(payload.remove)) : null
|
|
42
|
+
]);
|
|
43
|
+
}
|
package/dist/types.d.mts
CHANGED
package/package.json
CHANGED
|
File without changes
|