@okam/directus-next-component 1.3.0 → 1.4.0

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.
@@ -4,9 +4,9 @@ const directusFile = require("./directus-file.js");
4
4
  function useFile(props) {
5
5
  const { file } = props;
6
6
  const { filename_download: filenameDownload } = file ?? {};
7
- const href = directusFile(file);
7
+ const { src } = directusFile(file) ?? {};
8
8
  return {
9
- href: (href == null ? void 0 : href.toString()) ?? void 0,
9
+ href: src,
10
10
  download: filenameDownload ?? true
11
11
  };
12
12
  }
@@ -3,9 +3,9 @@ import useDirectusFile from "./directus-file.mjs";
3
3
  function useFile(props) {
4
4
  const { file } = props;
5
5
  const { filename_download: filenameDownload } = file ?? {};
6
- const href = useDirectusFile(file);
6
+ const { src } = useDirectusFile(file) ?? {};
7
7
  return {
8
- href: (href == null ? void 0 : href.toString()) ?? void 0,
8
+ href: src,
9
9
  download: filenameDownload ?? true
10
10
  };
11
11
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@okam/directus-next-component",
3
- "version": "1.3.0",
3
+ "version": "1.4.0",
4
4
  "main": "./index.js",
5
5
  "types": "./index.d.ts",
6
6
  "exports": {
package/server.d.ts CHANGED
@@ -1,9 +1,8 @@
1
1
  export { default as withDirectus } from './lib/withDirectus';
2
- export * from './lib/directus-next-rewrite';
3
- export { default as DirectusLink } from './components/DirectusLink';
2
+ export { default as DirectusFile } from './components/DirectusFile';
4
3
  export { default as DirectusImg } from './components/DirectusImg';
4
+ export { default as DirectusLink } from './components/DirectusLink';
5
5
  export { default as DirectusVideo } from './components/DirectusVideo';
6
- export { default as DirectusFile } from './components/DirectusFile';
7
6
  export { default as useDirectusFile } from './hooks/directus-file';
8
7
  export { default as useDirectusLink } from './hooks/directus-link';
9
8
  export { default as useNavigationItems } from './hooks/navigation-items';
package/server.js CHANGED
@@ -1,22 +1,18 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
3
3
  const withDirectus = require("./lib/withDirectus.js");
4
- const directusNextRewrite = require("./lib/directus-next-rewrite.js");
5
- const index = require("./components/DirectusLink/index.js");
4
+ const index = require("./components/DirectusFile/index.js");
6
5
  const index$1 = require("./components/DirectusImg/index.js");
7
- const index$2 = require("./components/DirectusVideo/index.js");
8
- const index$3 = require("./components/DirectusFile/index.js");
6
+ const index$2 = require("./components/DirectusLink/index.js");
7
+ const index$3 = require("./components/DirectusVideo/index.js");
9
8
  const directusFile = require("./hooks/directus-file.js");
10
9
  const directusLink = require("./hooks/directus-link.js");
11
10
  const navigationItems = require("./hooks/navigation-items.js");
12
11
  exports.withDirectus = withDirectus;
13
- exports.config = directusNextRewrite.config;
14
- exports.directusRouteMiddleware = directusNextRewrite.directusRouteMiddleware;
15
- exports.fetchPageSettings = directusNextRewrite.fetchPageSettings;
16
- exports.DirectusLink = index;
12
+ exports.DirectusFile = index;
17
13
  exports.DirectusImg = index$1;
18
- exports.DirectusVideo = index$2;
19
- exports.DirectusFile = index$3;
14
+ exports.DirectusLink = index$2;
15
+ exports.DirectusVideo = index$3;
20
16
  exports.useDirectusFile = directusFile;
21
17
  exports.useDirectusLink = directusLink;
22
18
  exports.useNavigationItems = navigationItems;
package/server.mjs CHANGED
@@ -1,20 +1,16 @@
1
1
  import { default as default2 } from "./lib/withDirectus.mjs";
2
- import { config, directusRouteMiddleware, fetchPageSettings } from "./lib/directus-next-rewrite.mjs";
3
- import { default as default3 } from "./components/DirectusLink/index.mjs";
2
+ import { default as default3 } from "./components/DirectusFile/index.mjs";
4
3
  import { default as default4 } from "./components/DirectusImg/index.mjs";
5
- import { default as default5 } from "./components/DirectusVideo/index.mjs";
6
- import { default as default6 } from "./components/DirectusFile/index.mjs";
4
+ import { default as default5 } from "./components/DirectusLink/index.mjs";
5
+ import { default as default6 } from "./components/DirectusVideo/index.mjs";
7
6
  import { default as default7 } from "./hooks/directus-file.mjs";
8
7
  import { default as default8 } from "./hooks/directus-link.mjs";
9
8
  import { default as default9 } from "./hooks/navigation-items.mjs";
10
9
  export {
11
- default6 as DirectusFile,
10
+ default3 as DirectusFile,
12
11
  default4 as DirectusImg,
13
- default3 as DirectusLink,
14
- default5 as DirectusVideo,
15
- config,
16
- directusRouteMiddleware,
17
- fetchPageSettings,
12
+ default5 as DirectusLink,
13
+ default6 as DirectusVideo,
18
14
  default7 as useDirectusFile,
19
15
  default8 as useDirectusLink,
20
16
  default9 as useNavigationItems,
@@ -1,7 +0,0 @@
1
- import type { NextRequest } from 'next/server';
2
- import { NextResponse } from 'next/server';
3
- export declare function fetchPageSettings(pathName: string, lang?: string): Promise<string | null>;
4
- export declare function directusRouteMiddleware(request: NextRequest, locales: string[]): Promise<NextResponse<unknown>>;
5
- export declare const config: {
6
- matcher: string[];
7
- };
@@ -1,87 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
3
- const server_js = require("next/server.js");
4
- const radash = require("radash");
5
- async function fetchPageSettings(pathName, lang = "fr-CA") {
6
- const graphqlEndpoint = process.env.NEXT_PUBLIC_GRAPHQL_URL;
7
- const graphqlApiKey = process.env.NEXT_PUBLIC_API_TOKEN;
8
- if (!graphqlEndpoint || !graphqlApiKey) {
9
- throw new Error("Missing graphql configuration: NEXT_PUBLIC_GRAPHQL_URL or NEXT_API_TOKEN_ADMIN");
10
- }
11
- let langCodeFilter = "_eq";
12
- if (lang.length === 2) {
13
- langCodeFilter = "_contains";
14
- }
15
- const query = `
16
- query PageSettings($path: String = null, $lang: String = "fr-CA") {
17
- page_settings(
18
- filter: { translations: { path: { _eq: $path }, languages_code: {code: {${langCodeFilter}: $lang}} }},
19
- limit: 1
20
- ) {
21
- belongs_to_key
22
- belongs_to_collection
23
- }
24
- }
25
- `;
26
- const graphqlBody = {
27
- query,
28
- variables: {
29
- lang,
30
- path: pathName
31
- }
32
- };
33
- try {
34
- const response = await fetch(graphqlEndpoint, {
35
- method: "POST",
36
- headers: {
37
- // eslint-disable-next-line @typescript-eslint/naming-convention
38
- "Content-Type": "application/json",
39
- Authorization: `Bearer ${graphqlApiKey}`
40
- },
41
- body: JSON.stringify(graphqlBody)
42
- });
43
- const { data } = await response.json();
44
- const key = radash.get(data, "page_settings.0.belongs_to_key");
45
- const collection = radash.get(data, "page_settings.0.belongs_to_collection");
46
- if (key == null || collection == null) {
47
- throw new Error("No page settings found");
48
- }
49
- return `/${collection}/${key}`;
50
- } catch (e) {
51
- console.error("GraphQL Error", e.message);
52
- return null;
53
- }
54
- }
55
- function removeLocaleFromPathName(pathName, locale) {
56
- if (pathName.includes(locale)) {
57
- return pathName.replace(`/${locale}`, "");
58
- }
59
- return pathName;
60
- }
61
- async function directusRouteMiddleware(request, locales) {
62
- const { pathname } = request.nextUrl;
63
- const locale = locales.find((l) => pathname.startsWith(`/${l}`));
64
- const pathNameWithoutLocale = locale ? removeLocaleFromPathName(pathname, locale) : pathname;
65
- const route = await fetchPageSettings(pathNameWithoutLocale, locale);
66
- if (route === null) {
67
- return server_js.NextResponse.next();
68
- }
69
- const url = request.nextUrl.clone();
70
- url.pathname = route;
71
- return server_js.NextResponse.rewrite(url);
72
- }
73
- const config = {
74
- matcher: [
75
- /*
76
- * Match all request paths except for the ones starting with:
77
- * - api (API routes)
78
- * - _next/static (static files)
79
- * - _next/image (image optimization files)
80
- * - favicon.ico (favicon file)
81
- */
82
- "/((?!api|_next/static|_next/image|favicon.ico).*)"
83
- ]
84
- };
85
- exports.config = config;
86
- exports.directusRouteMiddleware = directusRouteMiddleware;
87
- exports.fetchPageSettings = fetchPageSettings;
@@ -1,87 +0,0 @@
1
- import { NextResponse } from "next/server.js";
2
- import { get } from "radash";
3
- async function fetchPageSettings(pathName, lang = "fr-CA") {
4
- const graphqlEndpoint = process.env.NEXT_PUBLIC_GRAPHQL_URL;
5
- const graphqlApiKey = process.env.NEXT_PUBLIC_API_TOKEN;
6
- if (!graphqlEndpoint || !graphqlApiKey) {
7
- throw new Error("Missing graphql configuration: NEXT_PUBLIC_GRAPHQL_URL or NEXT_API_TOKEN_ADMIN");
8
- }
9
- let langCodeFilter = "_eq";
10
- if (lang.length === 2) {
11
- langCodeFilter = "_contains";
12
- }
13
- const query = `
14
- query PageSettings($path: String = null, $lang: String = "fr-CA") {
15
- page_settings(
16
- filter: { translations: { path: { _eq: $path }, languages_code: {code: {${langCodeFilter}: $lang}} }},
17
- limit: 1
18
- ) {
19
- belongs_to_key
20
- belongs_to_collection
21
- }
22
- }
23
- `;
24
- const graphqlBody = {
25
- query,
26
- variables: {
27
- lang,
28
- path: pathName
29
- }
30
- };
31
- try {
32
- const response = await fetch(graphqlEndpoint, {
33
- method: "POST",
34
- headers: {
35
- // eslint-disable-next-line @typescript-eslint/naming-convention
36
- "Content-Type": "application/json",
37
- Authorization: `Bearer ${graphqlApiKey}`
38
- },
39
- body: JSON.stringify(graphqlBody)
40
- });
41
- const { data } = await response.json();
42
- const key = get(data, "page_settings.0.belongs_to_key");
43
- const collection = get(data, "page_settings.0.belongs_to_collection");
44
- if (key == null || collection == null) {
45
- throw new Error("No page settings found");
46
- }
47
- return `/${collection}/${key}`;
48
- } catch (e) {
49
- console.error("GraphQL Error", e.message);
50
- return null;
51
- }
52
- }
53
- function removeLocaleFromPathName(pathName, locale) {
54
- if (pathName.includes(locale)) {
55
- return pathName.replace(`/${locale}`, "");
56
- }
57
- return pathName;
58
- }
59
- async function directusRouteMiddleware(request, locales) {
60
- const { pathname } = request.nextUrl;
61
- const locale = locales.find((l) => pathname.startsWith(`/${l}`));
62
- const pathNameWithoutLocale = locale ? removeLocaleFromPathName(pathname, locale) : pathname;
63
- const route = await fetchPageSettings(pathNameWithoutLocale, locale);
64
- if (route === null) {
65
- return NextResponse.next();
66
- }
67
- const url = request.nextUrl.clone();
68
- url.pathname = route;
69
- return NextResponse.rewrite(url);
70
- }
71
- const config = {
72
- matcher: [
73
- /*
74
- * Match all request paths except for the ones starting with:
75
- * - api (API routes)
76
- * - _next/static (static files)
77
- * - _next/image (image optimization files)
78
- * - favicon.ico (favicon file)
79
- */
80
- "/((?!api|_next/static|_next/image|favicon.ico).*)"
81
- ]
82
- };
83
- export {
84
- config,
85
- directusRouteMiddleware,
86
- fetchPageSettings
87
- };