@yassidev/nuxt-directus 0.0.5 → 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.json +1 -1
- package/dist/module.mjs +1 -44
- package/dist/runtime/{helpers.js → client.js} +1 -1
- 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/package.json +1 -1
- /package/dist/runtime/{helpers.d.ts → client.d.ts} +0 -0
package/dist/module.json
CHANGED
package/dist/module.mjs
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
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
6
|
const JOIN_LEADING_SLASH_RE = /^\.?\//;
|
|
@@ -91,49 +91,6 @@ function createDefu(merger) {
|
|
|
91
91
|
}
|
|
92
92
|
const defu = createDefu();
|
|
93
93
|
|
|
94
|
-
function useDirectus(config) {
|
|
95
|
-
return createDirectus(config.url).with(rest()).with(staticToken(config.accessToken || ""));
|
|
96
|
-
}
|
|
97
|
-
async function fetchTranslations(locale, config) {
|
|
98
|
-
const { i18nPrefix } = config;
|
|
99
|
-
const directus = useDirectus(config);
|
|
100
|
-
return await directus.request(readTranslations({
|
|
101
|
-
fields: ["key", "value", "id"],
|
|
102
|
-
limit: -1,
|
|
103
|
-
filter: { language: { _eq: locale }, ...i18nPrefix ? { key: { _startsWith: i18nPrefix } } : {} }
|
|
104
|
-
}));
|
|
105
|
-
}
|
|
106
|
-
async function syncTranslations(locale, translations, config) {
|
|
107
|
-
const directus = useDirectus(config);
|
|
108
|
-
const current = await fetchTranslations(locale, config);
|
|
109
|
-
const payload = {
|
|
110
|
-
create: [],
|
|
111
|
-
update: [],
|
|
112
|
-
remove: []
|
|
113
|
-
};
|
|
114
|
-
for (const [key, value] of Object.entries(translations)) {
|
|
115
|
-
const translation = current.find((t) => t.key === key);
|
|
116
|
-
if (!translation) {
|
|
117
|
-
payload.create.push({ key, value, language: locale });
|
|
118
|
-
} else if (translation.value !== value) {
|
|
119
|
-
payload.update.push({ id: translation.id, value });
|
|
120
|
-
}
|
|
121
|
-
}
|
|
122
|
-
for (const translation of current) {
|
|
123
|
-
if (!translations[translation.key]) {
|
|
124
|
-
payload.remove.push(translation.id);
|
|
125
|
-
}
|
|
126
|
-
}
|
|
127
|
-
if (!payload.create.length && !payload.update.length && !payload.remove.length) {
|
|
128
|
-
return false;
|
|
129
|
-
}
|
|
130
|
-
return Promise.all([
|
|
131
|
-
payload.update.length > 0 ? directus.request(updateTranslationsBatch(payload.update)) : null,
|
|
132
|
-
payload.create.length > 0 ? directus.request(createTranslations(payload.create)) : null,
|
|
133
|
-
payload.remove.length > 0 ? directus.request(deleteTranslations(payload.remove)) : null
|
|
134
|
-
]);
|
|
135
|
-
}
|
|
136
|
-
|
|
137
94
|
const NAME = "directus";
|
|
138
95
|
const module = defineNuxtModule({
|
|
139
96
|
meta: { name: NAME },
|
|
@@ -2,7 +2,7 @@ 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/package.json
CHANGED
|
File without changes
|