nuxt-directus-sdk 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 +10 -4
- package/dist/module.d.ts +10 -4
- package/dist/module.json +1 -1
- package/dist/module.mjs +31 -23
- package/dist/runtime/middleware/auth.d.ts +8 -0
- package/dist/runtime/middleware/auth.mjs +16 -0
- package/dist/runtime/plugin.mjs +3 -10
- package/dist/runtime/types/generate.d.ts +2 -8
- package/dist/runtime/types/generate.mjs +29 -22
- package/dist/runtime/types/generate.types.d.ts +5 -0
- package/package.json +1 -1
package/dist/module.d.mts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import * as _nuxt_schema from '@nuxt/schema';
|
|
2
2
|
import { Query } from '@directus/sdk';
|
|
3
|
-
import {
|
|
3
|
+
import { DirectusSchema } from '#build/types/directus';
|
|
4
4
|
|
|
5
5
|
interface ModuleOptions {
|
|
6
6
|
/**
|
|
@@ -24,9 +24,9 @@ interface ModuleOptions {
|
|
|
24
24
|
/**
|
|
25
25
|
* Directus Auth Options
|
|
26
26
|
* @default {}
|
|
27
|
-
* @type Query<
|
|
27
|
+
* @type Query<DirectusSchema, DirectusSchema['directus_users']>
|
|
28
28
|
*/
|
|
29
|
-
fetchUserParams?: Query<
|
|
29
|
+
fetchUserParams?: Query<DirectusSchema, DirectusSchema['directus_users']>;
|
|
30
30
|
/**
|
|
31
31
|
* Add Directus Admin in Nuxt Devtools
|
|
32
32
|
*
|
|
@@ -72,11 +72,17 @@ interface ModuleOptions {
|
|
|
72
72
|
*/
|
|
73
73
|
cookieSecure?: boolean;
|
|
74
74
|
/**
|
|
75
|
-
* The
|
|
75
|
+
* The prefix to your custom types
|
|
76
76
|
* @type string
|
|
77
77
|
* @default ''
|
|
78
78
|
*/
|
|
79
79
|
typePrefix?: string;
|
|
80
|
+
/**
|
|
81
|
+
* A path to redirect a user to when not logged in using auth middleware
|
|
82
|
+
* @type string
|
|
83
|
+
* @default '/login'
|
|
84
|
+
*/
|
|
85
|
+
loginPath?: string;
|
|
80
86
|
}
|
|
81
87
|
declare const _default: _nuxt_schema.NuxtModule<ModuleOptions>;
|
|
82
88
|
|
package/dist/module.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import * as _nuxt_schema from '@nuxt/schema';
|
|
2
2
|
import { Query } from '@directus/sdk';
|
|
3
|
-
import {
|
|
3
|
+
import { DirectusSchema } from '#build/types/directus';
|
|
4
4
|
|
|
5
5
|
interface ModuleOptions {
|
|
6
6
|
/**
|
|
@@ -24,9 +24,9 @@ interface ModuleOptions {
|
|
|
24
24
|
/**
|
|
25
25
|
* Directus Auth Options
|
|
26
26
|
* @default {}
|
|
27
|
-
* @type Query<
|
|
27
|
+
* @type Query<DirectusSchema, DirectusSchema['directus_users']>
|
|
28
28
|
*/
|
|
29
|
-
fetchUserParams?: Query<
|
|
29
|
+
fetchUserParams?: Query<DirectusSchema, DirectusSchema['directus_users']>;
|
|
30
30
|
/**
|
|
31
31
|
* Add Directus Admin in Nuxt Devtools
|
|
32
32
|
*
|
|
@@ -72,11 +72,17 @@ interface ModuleOptions {
|
|
|
72
72
|
*/
|
|
73
73
|
cookieSecure?: boolean;
|
|
74
74
|
/**
|
|
75
|
-
* The
|
|
75
|
+
* The prefix to your custom types
|
|
76
76
|
* @type string
|
|
77
77
|
* @default ''
|
|
78
78
|
*/
|
|
79
79
|
typePrefix?: string;
|
|
80
|
+
/**
|
|
81
|
+
* A path to redirect a user to when not logged in using auth middleware
|
|
82
|
+
* @type string
|
|
83
|
+
* @default '/login'
|
|
84
|
+
*/
|
|
85
|
+
loginPath?: string;
|
|
80
86
|
}
|
|
81
87
|
declare const _default: _nuxt_schema.NuxtModule<ModuleOptions>;
|
|
82
88
|
|
package/dist/module.json
CHANGED
package/dist/module.mjs
CHANGED
|
@@ -5,55 +5,62 @@ import { createDirectus, authentication, rest, readCollections, readFields, read
|
|
|
5
5
|
import { pascalCase } from 'change-case';
|
|
6
6
|
|
|
7
7
|
const name = "nuxt-directus-sdk";
|
|
8
|
-
const version = "0.0.
|
|
8
|
+
const version = "0.0.6";
|
|
9
9
|
|
|
10
10
|
function warn(message) {
|
|
11
11
|
useLogger("nuxt-directus-sdk").warn(message);
|
|
12
12
|
}
|
|
13
|
+
function joinTypes(types) {
|
|
14
|
+
return types.map((x) => ` ${x};`).join("\n");
|
|
15
|
+
}
|
|
13
16
|
async function generateTypes(options) {
|
|
17
|
+
let types = "";
|
|
18
|
+
const aliases = [];
|
|
14
19
|
const collections = await getCollections(options);
|
|
15
|
-
let typeValues = "";
|
|
16
|
-
const types = [];
|
|
17
20
|
Object.values(collections).forEach((collection) => {
|
|
18
|
-
const
|
|
19
|
-
const typeName =
|
|
20
|
-
types
|
|
21
|
-
typeValues += `export type ${typeName} = {
|
|
21
|
+
const name = collection.collection;
|
|
22
|
+
const typeName = name.startsWith("directus_") ? pascalCase(name) : pascalCase(`${options.prefix}${name}`);
|
|
23
|
+
types += `export type ${typeName} = {
|
|
22
24
|
`;
|
|
23
25
|
collection.fields.forEach((field) => {
|
|
24
26
|
if (field.meta?.interface?.startsWith("presentation-"))
|
|
25
27
|
return;
|
|
26
|
-
|
|
27
|
-
|
|
28
|
+
types += " ";
|
|
29
|
+
types += field.field.includes("-") ? `"${field.field}"` : field.field;
|
|
28
30
|
if (field.schema?.is_nullable)
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
31
|
+
types += "?";
|
|
32
|
+
types += ": ";
|
|
33
|
+
types += getType(field);
|
|
34
|
+
types += ";\n";
|
|
33
35
|
});
|
|
34
|
-
|
|
36
|
+
types += "};\n\n";
|
|
37
|
+
aliases.push(`${name}: ${typeName}[]`);
|
|
35
38
|
});
|
|
39
|
+
const allTypes = joinTypes(aliases);
|
|
40
|
+
const schemaTypes = joinTypes(aliases.filter((item) => {
|
|
41
|
+
return item.startsWith("directus_users") || !item.startsWith("directus_");
|
|
42
|
+
}));
|
|
36
43
|
return `
|
|
37
44
|
// This file is auto-generated by @nuxtjs/directus-sdk
|
|
38
|
-
${
|
|
45
|
+
${types}
|
|
39
46
|
|
|
40
47
|
export interface AllDirectusCollections {
|
|
41
|
-
${
|
|
48
|
+
${allTypes}
|
|
42
49
|
};
|
|
43
50
|
|
|
44
51
|
export interface DirectusSchema {
|
|
45
|
-
${
|
|
52
|
+
${schemaTypes}
|
|
46
53
|
};
|
|
47
54
|
|
|
48
55
|
declare global {
|
|
49
|
-
${
|
|
56
|
+
${types.replaceAll("export type", "type")}
|
|
50
57
|
|
|
51
58
|
interface AllDirectusCollections {
|
|
52
|
-
${
|
|
59
|
+
${allTypes}
|
|
53
60
|
};
|
|
54
61
|
|
|
55
62
|
interface DirectusSchema {
|
|
56
|
-
${
|
|
63
|
+
${schemaTypes}
|
|
57
64
|
};
|
|
58
65
|
}
|
|
59
66
|
|
|
@@ -90,8 +97,8 @@ async function getCollections(options) {
|
|
|
90
97
|
const directus = createDirectus(options.url).with(authentication("json", { autoRefresh: false })).with(rest());
|
|
91
98
|
directus.setToken(options.token);
|
|
92
99
|
const collections = {};
|
|
93
|
-
const
|
|
94
|
-
|
|
100
|
+
const directusCollections = await directus.request(readCollections());
|
|
101
|
+
directusCollections.sort((a, b) => a.collection.localeCompare(b.collection)).forEach((collection) => {
|
|
95
102
|
collections[collection.collection] = { ...collection, fields: [] };
|
|
96
103
|
});
|
|
97
104
|
const fields = await directus.request(readFields());
|
|
@@ -157,7 +164,8 @@ const module = defineNuxtModule({
|
|
|
157
164
|
cookieMaxAgeRefreshToken: 604800,
|
|
158
165
|
cookieSameSite: "lax",
|
|
159
166
|
cookieSecure: false,
|
|
160
|
-
typePrefix: ""
|
|
167
|
+
typePrefix: "",
|
|
168
|
+
loginPath: "/login"
|
|
161
169
|
},
|
|
162
170
|
async setup(options, nuxt) {
|
|
163
171
|
if (!tryResolveModule("@directus/sdk")) {
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import {
|
|
2
|
+
defineNuxtRouteMiddleware,
|
|
3
|
+
navigateTo,
|
|
4
|
+
useDirectusAuth,
|
|
5
|
+
useRuntimeConfig
|
|
6
|
+
} from "#imports";
|
|
7
|
+
export default defineNuxtRouteMiddleware((to) => {
|
|
8
|
+
const config = useRuntimeConfig();
|
|
9
|
+
const user = useDirectusAuth().user;
|
|
10
|
+
if (!user.value) {
|
|
11
|
+
return navigateTo({
|
|
12
|
+
path: config.public.directus.loginPath ?? "/login",
|
|
13
|
+
query: { redirect: to.path !== "/" ? encodeURIComponent(to.path) : void 0 }
|
|
14
|
+
});
|
|
15
|
+
}
|
|
16
|
+
});
|
package/dist/runtime/plugin.mjs
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
import { fromUrl, parseDomain } from "parse-domain";
|
|
2
|
+
import auth from "./middleware/auth.mjs";
|
|
2
3
|
import { useDomain, useSubdomain } from "./composables/domain.mjs";
|
|
3
4
|
import { useDirectusAuth } from "./composables/auth.mjs";
|
|
4
|
-
import { addRouteMiddleware, defineNuxtPlugin,
|
|
5
|
+
import { addRouteMiddleware, defineNuxtPlugin, useRuntimeConfig } from "#app";
|
|
5
6
|
export default defineNuxtPlugin(async (nuxt) => {
|
|
6
7
|
const domain = useDomain();
|
|
7
8
|
const subdomain = useSubdomain();
|
|
@@ -25,13 +26,5 @@ export default defineNuxtPlugin(async (nuxt) => {
|
|
|
25
26
|
const config = useRuntimeConfig();
|
|
26
27
|
if (config.public.directus.fetchUser)
|
|
27
28
|
await useDirectusAuth().fetchUser();
|
|
28
|
-
addRouteMiddleware("auth",
|
|
29
|
-
const user = useDirectusAuth().user;
|
|
30
|
-
if (!user.value) {
|
|
31
|
-
return navigateTo({
|
|
32
|
-
path: "/login",
|
|
33
|
-
query: { redirect: to.path !== "/" ? encodeURIComponent(to.path) : void 0 }
|
|
34
|
-
});
|
|
35
|
-
}
|
|
36
|
-
});
|
|
29
|
+
addRouteMiddleware("auth", auth);
|
|
37
30
|
});
|
|
@@ -1,8 +1,2 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
export
|
|
3
|
-
url: string;
|
|
4
|
-
token: string;
|
|
5
|
-
prefix: string;
|
|
6
|
-
}
|
|
7
|
-
export declare function generateTypes(options: OASOptions): Promise<string>;
|
|
8
|
-
export declare function getCollections(options: OASOptions): Promise<Collections>;
|
|
1
|
+
import type { GenerateOptions } from './generate.types';
|
|
2
|
+
export declare function generateTypes(options: GenerateOptions): Promise<string>;
|
|
@@ -4,50 +4,57 @@ import { pascalCase } from "change-case";
|
|
|
4
4
|
function warn(message) {
|
|
5
5
|
useLogger("nuxt-directus-sdk").warn(message);
|
|
6
6
|
}
|
|
7
|
+
function joinTypes(types) {
|
|
8
|
+
return types.map((x) => ` ${x};`).join("\n");
|
|
9
|
+
}
|
|
7
10
|
export async function generateTypes(options) {
|
|
11
|
+
let types = "";
|
|
12
|
+
const aliases = [];
|
|
8
13
|
const collections = await getCollections(options);
|
|
9
|
-
let typeValues = "";
|
|
10
|
-
const types = [];
|
|
11
14
|
Object.values(collections).forEach((collection) => {
|
|
12
|
-
const
|
|
13
|
-
const typeName =
|
|
14
|
-
types
|
|
15
|
-
typeValues += `export type ${typeName} = {
|
|
15
|
+
const name = collection.collection;
|
|
16
|
+
const typeName = name.startsWith("directus_") ? pascalCase(name) : pascalCase(`${options.prefix}${name}`);
|
|
17
|
+
types += `export type ${typeName} = {
|
|
16
18
|
`;
|
|
17
19
|
collection.fields.forEach((field) => {
|
|
18
20
|
if (field.meta?.interface?.startsWith("presentation-"))
|
|
19
21
|
return;
|
|
20
|
-
|
|
21
|
-
|
|
22
|
+
types += " ";
|
|
23
|
+
types += field.field.includes("-") ? `"${field.field}"` : field.field;
|
|
22
24
|
if (field.schema?.is_nullable)
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
25
|
+
types += "?";
|
|
26
|
+
types += ": ";
|
|
27
|
+
types += getType(field);
|
|
28
|
+
types += ";\n";
|
|
27
29
|
});
|
|
28
|
-
|
|
30
|
+
types += "};\n\n";
|
|
31
|
+
aliases.push(`${name}: ${typeName}[]`);
|
|
29
32
|
});
|
|
33
|
+
const allTypes = joinTypes(aliases);
|
|
34
|
+
const schemaTypes = joinTypes(aliases.filter((item) => {
|
|
35
|
+
return item.startsWith("directus_users") || !item.startsWith("directus_");
|
|
36
|
+
}));
|
|
30
37
|
return `
|
|
31
38
|
// This file is auto-generated by @nuxtjs/directus-sdk
|
|
32
|
-
${
|
|
39
|
+
${types}
|
|
33
40
|
|
|
34
41
|
export interface AllDirectusCollections {
|
|
35
|
-
${
|
|
42
|
+
${allTypes}
|
|
36
43
|
};
|
|
37
44
|
|
|
38
45
|
export interface DirectusSchema {
|
|
39
|
-
${
|
|
46
|
+
${schemaTypes}
|
|
40
47
|
};
|
|
41
48
|
|
|
42
49
|
declare global {
|
|
43
|
-
${
|
|
50
|
+
${types.replaceAll("export type", "type")}
|
|
44
51
|
|
|
45
52
|
interface AllDirectusCollections {
|
|
46
|
-
${
|
|
53
|
+
${allTypes}
|
|
47
54
|
};
|
|
48
55
|
|
|
49
56
|
interface DirectusSchema {
|
|
50
|
-
${
|
|
57
|
+
${schemaTypes}
|
|
51
58
|
};
|
|
52
59
|
}
|
|
53
60
|
|
|
@@ -80,12 +87,12 @@ function getType(field) {
|
|
|
80
87
|
}
|
|
81
88
|
return type;
|
|
82
89
|
}
|
|
83
|
-
|
|
90
|
+
async function getCollections(options) {
|
|
84
91
|
const directus = createDirectus(options.url).with(authentication("json", { autoRefresh: false })).with(rest());
|
|
85
92
|
directus.setToken(options.token);
|
|
86
93
|
const collections = {};
|
|
87
|
-
const
|
|
88
|
-
|
|
94
|
+
const directusCollections = await directus.request(readCollections());
|
|
95
|
+
directusCollections.sort((a, b) => a.collection.localeCompare(b.collection)).forEach((collection) => {
|
|
89
96
|
collections[collection.collection] = { ...collection, fields: [] };
|
|
90
97
|
});
|
|
91
98
|
const fields = await directus.request(readFields());
|