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 CHANGED
@@ -1,6 +1,6 @@
1
1
  import * as _nuxt_schema from '@nuxt/schema';
2
2
  import { Query } from '@directus/sdk';
3
- import { AllDirectusCollections } from '#build/types/directus';
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<AllDirectusCollections, AllDirectusCollections['directus_users']>
27
+ * @type Query<DirectusSchema, DirectusSchema['directus_users']>
28
28
  */
29
- fetchUserParams?: Query<AllDirectusCollections, AllDirectusCollections['directus_users']>;
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 Secure attribute for auth cookies.
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 { AllDirectusCollections } from '#build/types/directus';
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<AllDirectusCollections, AllDirectusCollections['directus_users']>
27
+ * @type Query<DirectusSchema, DirectusSchema['directus_users']>
28
28
  */
29
- fetchUserParams?: Query<AllDirectusCollections, AllDirectusCollections['directus_users']>;
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 Secure attribute for auth cookies.
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
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "nuxt-directus-sdk",
3
- "version": "0.0.4",
3
+ "version": "0.0.6",
4
4
  "configKey": "directus",
5
5
  "compatibility": {
6
6
  "nuxt": "^3.0.0",
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.4";
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 collectionName = collection.collection;
19
- const typeName = collectionName.startsWith("directus_") ? pascalCase(collectionName) : pascalCase(`${options.prefix}${collectionName}`);
20
- types.push(`${collectionName}: ${typeName}[]`);
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
- typeValues += " ";
27
- typeValues += field.field.includes("-") ? `"${field.field}"` : field.field;
28
+ types += " ";
29
+ types += field.field.includes("-") ? `"${field.field}"` : field.field;
28
30
  if (field.schema?.is_nullable)
29
- typeValues += "?";
30
- typeValues += ": ";
31
- typeValues += getType(field);
32
- typeValues += ";\n";
31
+ types += "?";
32
+ types += ": ";
33
+ types += getType(field);
34
+ types += ";\n";
33
35
  });
34
- typeValues += "};\n\n";
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
- ${typeValues}
45
+ ${types}
39
46
 
40
47
  export interface AllDirectusCollections {
41
- ${types.map((x) => ` ${x};`).join("\n")}
48
+ ${allTypes}
42
49
  };
43
50
 
44
51
  export interface DirectusSchema {
45
- ${types.filter((item) => !item.startsWith("directus_")).map((x) => ` ${x};`).join("\n")}
52
+ ${schemaTypes}
46
53
  };
47
54
 
48
55
  declare global {
49
- ${typeValues.replaceAll("export type", "type")}
56
+ ${types.replaceAll("export type", "type")}
50
57
 
51
58
  interface AllDirectusCollections {
52
- ${types.map((x) => ` ${x};`).join("\n")}
59
+ ${allTypes}
53
60
  };
54
61
 
55
62
  interface DirectusSchema {
56
- ${types.filter((item) => !item.startsWith("directus_")).map((x) => ` ${x};`).join("\n")}
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 rawCollections = await directus.request(readCollections());
94
- rawCollections.sort((a, b) => a.collection.localeCompare(b.collection)).forEach((collection) => {
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,8 @@
1
+ type MiddlewareMeta = boolean;
2
+ declare module '#app/../pages/runtime/composables' {
3
+ interface PageMeta {
4
+ auth?: MiddlewareMeta;
5
+ }
6
+ }
7
+ declare const _default: any;
8
+ export default _default;
@@ -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
+ });
@@ -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, navigateTo, useRuntimeConfig } from "#app";
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", async (to) => {
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 { Collections } from './generate.types';
2
- export interface OASOptions {
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 collectionName = collection.collection;
13
- const typeName = collectionName.startsWith("directus_") ? pascalCase(collectionName) : pascalCase(`${options.prefix}${collectionName}`);
14
- types.push(`${collectionName}: ${typeName}[]`);
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
- typeValues += " ";
21
- typeValues += field.field.includes("-") ? `"${field.field}"` : field.field;
22
+ types += " ";
23
+ types += field.field.includes("-") ? `"${field.field}"` : field.field;
22
24
  if (field.schema?.is_nullable)
23
- typeValues += "?";
24
- typeValues += ": ";
25
- typeValues += getType(field);
26
- typeValues += ";\n";
25
+ types += "?";
26
+ types += ": ";
27
+ types += getType(field);
28
+ types += ";\n";
27
29
  });
28
- typeValues += "};\n\n";
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
- ${typeValues}
39
+ ${types}
33
40
 
34
41
  export interface AllDirectusCollections {
35
- ${types.map((x) => ` ${x};`).join("\n")}
42
+ ${allTypes}
36
43
  };
37
44
 
38
45
  export interface DirectusSchema {
39
- ${types.filter((item) => !item.startsWith("directus_")).map((x) => ` ${x};`).join("\n")}
46
+ ${schemaTypes}
40
47
  };
41
48
 
42
49
  declare global {
43
- ${typeValues.replaceAll("export type", "type")}
50
+ ${types.replaceAll("export type", "type")}
44
51
 
45
52
  interface AllDirectusCollections {
46
- ${types.map((x) => ` ${x};`).join("\n")}
53
+ ${allTypes}
47
54
  };
48
55
 
49
56
  interface DirectusSchema {
50
- ${types.filter((item) => !item.startsWith("directus_")).map((x) => ` ${x};`).join("\n")}
57
+ ${schemaTypes}
51
58
  };
52
59
  }
53
60
 
@@ -80,12 +87,12 @@ function getType(field) {
80
87
  }
81
88
  return type;
82
89
  }
83
- export async function getCollections(options) {
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 rawCollections = await directus.request(readCollections());
88
- rawCollections.sort((a, b) => a.collection.localeCompare(b.collection)).forEach((collection) => {
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());
@@ -11,3 +11,8 @@ export type Collection = DirectusCollection & {
11
11
  export interface Collections {
12
12
  [collection: string]: Collection;
13
13
  }
14
+ export interface GenerateOptions {
15
+ url: string;
16
+ token: string;
17
+ prefix: string;
18
+ }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "nuxt-directus-sdk",
3
3
  "type": "module",
4
- "version": "0.0.4",
4
+ "version": "0.0.6",
5
5
  "description": "A nuxt module that uses the directus SDK",
6
6
  "author": "Matthew Rollinson <matt@rolley.io>",
7
7
  "license": "MIT",