@okam/directus-next-component 1.3.1 → 1.5.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.
Files changed (38) hide show
  1. package/CHANGELOG.md +96 -0
  2. package/README.md +142 -0
  3. package/components/DirectusFile/config.d.ts +1 -1
  4. package/{hooks → directus/directus-next-component/src/hooks}/directus-file.js +2 -1
  5. package/{hooks → directus/directus-next-component/src/hooks}/directus-file.mjs +2 -1
  6. package/{index.js → directus/directus-next-component/src/index.js} +2 -0
  7. package/{index.mjs → directus/directus-next-component/src/index.mjs} +2 -0
  8. package/directus/directus-next-component/src/logger.js +6 -0
  9. package/directus/directus-next-component/src/logger.mjs +6 -0
  10. package/{server.js → directus/directus-next-component/src/server.js} +8 -10
  11. package/{server.mjs → directus/directus-next-component/src/server.mjs} +8 -10
  12. package/index.d.ts +1 -0
  13. package/logger.d.ts +1 -0
  14. package/package.json +12 -2
  15. package/server.d.ts +3 -3
  16. package/stack/logger/src/lib/factoryLogger.js +8 -0
  17. package/stack/logger/src/lib/factoryLogger.mjs +9 -0
  18. package/stack/logger/src/lib/logger.js +37 -0
  19. package/stack/logger/src/lib/logger.mjs +37 -0
  20. package/lib/directus-next-rewrite.d.ts +0 -7
  21. package/lib/directus-next-rewrite.js +0 -87
  22. package/lib/directus-next-rewrite.mjs +0 -87
  23. /package/{components → directus/directus-next-component/src/components}/DirectusFile/config.js +0 -0
  24. /package/{components → directus/directus-next-component/src/components}/DirectusFile/config.mjs +0 -0
  25. /package/{components → directus/directus-next-component/src/components}/DirectusFile/index.js +0 -0
  26. /package/{components → directus/directus-next-component/src/components}/DirectusFile/index.mjs +0 -0
  27. /package/{components → directus/directus-next-component/src/components}/DirectusImg/index.js +0 -0
  28. /package/{components → directus/directus-next-component/src/components}/DirectusImg/index.mjs +0 -0
  29. /package/{components → directus/directus-next-component/src/components}/DirectusLink/index.js +0 -0
  30. /package/{components → directus/directus-next-component/src/components}/DirectusLink/index.mjs +0 -0
  31. /package/{components → directus/directus-next-component/src/components}/DirectusVideo/index.js +0 -0
  32. /package/{components → directus/directus-next-component/src/components}/DirectusVideo/index.mjs +0 -0
  33. /package/{hooks → directus/directus-next-component/src/hooks}/directus-link.js +0 -0
  34. /package/{hooks → directus/directus-next-component/src/hooks}/directus-link.mjs +0 -0
  35. /package/{hooks → directus/directus-next-component/src/hooks}/navigation-items.js +0 -0
  36. /package/{hooks → directus/directus-next-component/src/hooks}/navigation-items.mjs +0 -0
  37. /package/{lib → directus/directus-next-component/src/lib}/withDirectus.js +0 -0
  38. /package/{lib → directus/directus-next-component/src/lib}/withDirectus.mjs +0 -0
package/CHANGELOG.md ADDED
@@ -0,0 +1,96 @@
1
+ ## 1.4.0 (2024-09-24)
2
+
3
+
4
+ ### 🚀 Features
5
+
6
+ - **directus-next:** directus based routing ([196ab1e](https://github.com/OKAMca/stack/commit/196ab1e))
7
+
8
+
9
+ ### ❤️ Thank You
10
+
11
+ - David Paquet Pitts
12
+
13
+ ## 1.3.1 (2024-08-15)
14
+
15
+
16
+ ### 🩹 Fixes
17
+
18
+ - **directus-link:** file type links render correct href ([454a847](https://github.com/OKAMca/stack/commit/454a847))
19
+
20
+
21
+ ### ❤️ Thank You
22
+
23
+ - poclerson
24
+
25
+ ## 1.3.0 (2024-08-15)
26
+
27
+
28
+ ### 🚀 Features
29
+
30
+ - directus image support for videos ([4d45c9c](https://github.com/OKAMca/stack/commit/4d45c9c))
31
+
32
+
33
+ ### 🩹 Fixes
34
+
35
+ - move tokens and variant from links to navigation item ([7fca919](https://github.com/OKAMca/stack/commit/7fca919))
36
+
37
+ - **directus-next-component:** a11y props and bugfix ([dc3dbde](https://github.com/OKAMca/stack/commit/dc3dbde))
38
+
39
+
40
+ ### ❤️ Thank You
41
+
42
+ - Marie-Maxime Tanguay @marie-maxime
43
+ - mykimd
44
+ - Pierre-Olivier Clerson @poclerson
45
+ - poclerson
46
+ - Yan Morin
47
+ - yanmorinokamca @yanmorinokamca
48
+
49
+ ## 1.2.0 (2024-07-25)
50
+
51
+
52
+ ### 🚀 Features
53
+
54
+ - directus link component + use nav items hook ([750fa5c](https://github.com/OKAMca/stack/commit/750fa5c))
55
+
56
+
57
+ ### ❤️ Thank You
58
+
59
+ - Jérôme Trottier
60
+ - Marie-Maxime Tanguay @marie-maxime
61
+ - Pierre-Olivier Clerson @poclerson
62
+
63
+ ## 1.1.1 (2024-07-16)
64
+
65
+
66
+ ### 🩹 Fixes
67
+
68
+ - **publish:** add building packages step to workflow ([c9ce442](https://github.com/OKAMca/stack/commit/c9ce442))
69
+
70
+
71
+ ### ❤️ Thank You
72
+
73
+ - Jérôme Trottier
74
+ - Pierre-Olivier Clerson @poclerson
75
+ - yanmorinokamca @yanmorinokamca
76
+
77
+ ## 1.1.0 (2024-06-28)
78
+
79
+
80
+ ### 🚀 Features
81
+
82
+ - add new libs ([#71](https://github.com/OKAMca/stack/pull/71))
83
+
84
+ - add dynamic page routing from directus page settings ([#97](https://github.com/OKAMca/stack/pull/97))
85
+
86
+
87
+ ### 🩹 Fixes
88
+
89
+ - publishing for new libs ([#125](https://github.com/OKAMca/stack/pull/125))
90
+
91
+ - publishing for new libs ([#126](https://github.com/OKAMca/stack/pull/126))
92
+
93
+
94
+ ### ❤️ Thank You
95
+
96
+ - Marie-Maxime Tanguay @marie-maxime
package/README.md ADDED
@@ -0,0 +1,142 @@
1
+ # directus-next-component
2
+
3
+ This library was generated with [Nx](https://nx.dev).
4
+
5
+ ## directusRouteMiddleware usage
6
+
7
+ Create a middleware.ts file in the project, in the `/src/app` directory of the next application, and add the following code to it.
8
+
9
+ ```jsx
10
+ // middleware.ts
11
+
12
+ import {
13
+ directusRouteMiddleware,
14
+ config as directusRouteMiddlewareConfig,
15
+ } from '@okam/directus-next-component/src/server'
16
+ import type { NextRequest } from 'next/server'
17
+
18
+ export function middleware(request: NextRequest) {
19
+ const { locale } = request.nextUrl
20
+ return directusRouteMiddleware(request, [locale])
21
+ }
22
+
23
+ export const config = {
24
+ ...directusRouteMiddlewareConfig,
25
+ }
26
+
27
+ ```
28
+
29
+ After, modify the `next.config.js` file to add `withDirectus` in the plugins.
30
+
31
+ ## useDirectusLink hook
32
+
33
+ The role of the directus link hook is to dispatch different props according to the type of the directus link. The configuration for the dispatching is overridable using the `propsConfig` prop
34
+
35
+ ## DirectusLink component
36
+
37
+ The role of the directus link component is to wrap `next/link` and dispatch the props to different components according to a configuration. The configuration for dispatching different components is overridable using the `componentsConfig` prop.
38
+
39
+ The directus link component uses the directus link hook, which means it can be passed both `componentsConfig` and `propsConfig` for both dispatching uses.
40
+
41
+ By default, the following are included:
42
+
43
+ - `collection`: Render a link using a `page_settings` relation
44
+ - `anchor`: Render a link pointing to an element's id on the page (always starts with `#`)
45
+ - `external-link`: Render a link with an external URL (e.g.: https://google.com)
46
+ - `file`: Render a link for a downloadable file
47
+
48
+ The mentionned configuration can be overwritten by passing a `componentsConfig` prop to the directus link component
49
+
50
+ ```jsx
51
+ const overrideConfig = {
52
+ 'external-link': (props) => <CustomExternalLinkComponent {...props} />
53
+ }
54
+
55
+ const BrandLink = (props) => {
56
+ return <DirectusLink config={overrideConfig} {...props} />
57
+ }
58
+ ```
59
+
60
+ ## useNavigationItems hook
61
+
62
+ The `useNavigationItems` hook allows to build recursively a navigation structure using the `DirectusLink` component.
63
+
64
+ The tokens passed to `link` and `linkProps` include also include, in order of priority:
65
+ - The tokens passed from `onNavigationItem`
66
+ - The variant of the navigation item, passed as the `type` token
67
+ - The tokens of the navigation item
68
+
69
+ ### Props
70
+
71
+
72
+
73
+ ### Returned props of each parsed navigation item in the tree
74
+
75
+ - `link`: A rendered react node of the `DirectusLink` component
76
+ - `linkProps`: The props that were passed to the link
77
+ - `children`: The navigation items containing link, linkProps, children
78
+ - `parent`: A single navigation item containing link, linkProps, parent
79
+ - `depth`: The current depth of the recursion. For each level of parent, the depth will go down by one. For each level of children, the depth will go up by one. The depth starts at zero
80
+
81
+ ### Example usage
82
+
83
+ ```tsx
84
+ // Any styling could go here
85
+ const depthMap: Record<number, object> = {
86
+ 0: { backgroundColor: 'red' },
87
+ 1: { backgroundColor: 'blue' },
88
+ 2: { backgroundColor: 'green' },
89
+ }
90
+
91
+ // Loop recursively through navigation items and assign style based on depth
92
+ function renderTree(tree: Nullable<TNavigationItemsTree>): React.ReactNode {
93
+ if (!tree) return null
94
+ const { children, link, linkProps, depth } = tree
95
+ const style = depthMap[depth]
96
+
97
+ if (!link && !children) return null
98
+ if (!children) {
99
+ return (
100
+ <li style={style} key={link.id}>
101
+ {
102
+ /*
103
+ * Here, `link` represents a rendered version of `DirectusLink` for quick use
104
+ * Use `linkProps` for custom rendering needs
105
+ */
106
+ }
107
+ {link}
108
+ </li>
109
+ )
110
+ }
111
+ return (
112
+ <ul key={link.id}>
113
+ <li style={style}>{link}</li>
114
+ {children.map((child) => renderTree(child))}
115
+ </ul>
116
+ )
117
+ }
118
+
119
+ const NavigationComponent = (props) => {
120
+ // Depth and links type cannot be inferred directly, they must be passed
121
+ const navigation = useNavigationItems<3, { link?: LinksFragment }>(
122
+ props.navigationItems,
123
+
124
+ // Use `onNavigationItem` to parse the fragments
125
+ (item) => {
126
+ const { link } = item ?? {}
127
+ const collection = getFragment(PageSettingsFragmentDoc, link?.collection)
128
+ const file = getFragment(FilesFragmentDoc, link?.file)
129
+ return {
130
+ ...link,
131
+ collection,
132
+ file,
133
+ }
134
+ })
135
+
136
+ return (
137
+ <nav>
138
+ {navigation.map((item) => renderTree(item))}
139
+ </nav>
140
+ )
141
+ }
142
+ ```
@@ -1,4 +1,4 @@
1
- import type { ComponentType } from 'react-spring';
1
+ import type { ComponentType } from '@react-spring/web';
2
2
  import type { TDirectusFileProps } from './interface';
3
3
  declare const directusFileConfig: Record<string, ComponentType<TDirectusFileProps>>;
4
4
  export default directusFileConfig;
@@ -1,4 +1,5 @@
1
1
  "use strict";
2
+ const logger = require("../logger.js");
2
3
  const IMG_DOMAIN = process.env.NEXT_PUBLIC_IMG_DOMAIN;
3
4
  const IMG_PORT = process.env.NEXT_PUBLIC_IMG_PORT;
4
5
  const IMG_PROTOCOL = process.env.NEXT_PUBLIC_IMG_PROTOCOL ?? "https";
@@ -18,7 +19,7 @@ function getDirectusUrl(file, baseUrl, searchParams) {
18
19
  if (searchParams) setSearchParams(url, searchParams);
19
20
  return url;
20
21
  } catch (error) {
21
- console.warn(error);
22
+ logger.logger.log("Couldn't create URL", "warn", { error });
22
23
  return null;
23
24
  }
24
25
  }
@@ -1,3 +1,4 @@
1
+ import { logger } from "../logger.mjs";
1
2
  const IMG_DOMAIN = process.env.NEXT_PUBLIC_IMG_DOMAIN;
2
3
  const IMG_PORT = process.env.NEXT_PUBLIC_IMG_PORT;
3
4
  const IMG_PROTOCOL = process.env.NEXT_PUBLIC_IMG_PROTOCOL ?? "https";
@@ -17,7 +18,7 @@ function getDirectusUrl(file, baseUrl, searchParams) {
17
18
  if (searchParams) setSearchParams(url, searchParams);
18
19
  return url;
19
20
  } catch (error) {
20
- console.warn(error);
21
+ logger.log("Couldn't create URL", "warn", { error });
21
22
  return null;
22
23
  }
23
24
  }
@@ -7,6 +7,7 @@ const index$3 = require("./components/DirectusFile/index.js");
7
7
  const directusFile = require("./hooks/directus-file.js");
8
8
  const directusLink = require("./hooks/directus-link.js");
9
9
  const navigationItems = require("./hooks/navigation-items.js");
10
+ const logger = require("./logger.js");
10
11
  exports.DirectusLink = index;
11
12
  exports.DirectusImg = index$1;
12
13
  exports.DirectusVideo = index$2;
@@ -14,3 +15,4 @@ exports.DirectusFile = index$3;
14
15
  exports.useDirectusFile = directusFile;
15
16
  exports.useDirectusLink = directusLink;
16
17
  exports.useNavigationItems = navigationItems;
18
+ exports.DirectusNextComponent = logger.logger;
@@ -5,10 +5,12 @@ import { default as default5 } from "./components/DirectusFile/index.mjs";
5
5
  import { default as default6 } from "./hooks/directus-file.mjs";
6
6
  import { default as default7 } from "./hooks/directus-link.mjs";
7
7
  import { default as default8 } from "./hooks/navigation-items.mjs";
8
+ import { logger } from "./logger.mjs";
8
9
  export {
9
10
  default5 as DirectusFile,
10
11
  default3 as DirectusImg,
11
12
  default2 as DirectusLink,
13
+ logger as DirectusNextComponent,
12
14
  default4 as DirectusVideo,
13
15
  default6 as useDirectusFile,
14
16
  default7 as useDirectusLink,
@@ -0,0 +1,6 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
3
+ require("../../../stack/logger/src/lib/logger.js");
4
+ const factoryLogger = require("../../../stack/logger/src/lib/factoryLogger.js");
5
+ const logger = factoryLogger("[DirectusNextComponent]");
6
+ exports.logger = logger;
@@ -0,0 +1,6 @@
1
+ import "../../../stack/logger/src/lib/logger.mjs";
2
+ import createLogger from "../../../stack/logger/src/lib/factoryLogger.mjs";
3
+ const logger = createLogger("[DirectusNextComponent]");
4
+ export {
5
+ logger
6
+ };
@@ -1,22 +1,20 @@
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");
8
+ const logger = require("./logger.js");
9
9
  const directusFile = require("./hooks/directus-file.js");
10
10
  const directusLink = require("./hooks/directus-link.js");
11
11
  const navigationItems = require("./hooks/navigation-items.js");
12
12
  exports.withDirectus = withDirectus;
13
- exports.config = directusNextRewrite.config;
14
- exports.directusRouteMiddleware = directusNextRewrite.directusRouteMiddleware;
15
- exports.fetchPageSettings = directusNextRewrite.fetchPageSettings;
16
- exports.DirectusLink = index;
13
+ exports.DirectusFile = index;
17
14
  exports.DirectusImg = index$1;
18
- exports.DirectusVideo = index$2;
19
- exports.DirectusFile = index$3;
15
+ exports.DirectusLink = index$2;
16
+ exports.DirectusVideo = index$3;
17
+ exports.DirectusNextComponent = logger.logger;
20
18
  exports.useDirectusFile = directusFile;
21
19
  exports.useDirectusLink = directusLink;
22
20
  exports.useNavigationItems = navigationItems;
@@ -1,20 +1,18 @@
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";
6
+ import { logger } from "./logger.mjs";
7
7
  import { default as default7 } from "./hooks/directus-file.mjs";
8
8
  import { default as default8 } from "./hooks/directus-link.mjs";
9
9
  import { default as default9 } from "./hooks/navigation-items.mjs";
10
10
  export {
11
- default6 as DirectusFile,
11
+ default3 as DirectusFile,
12
12
  default4 as DirectusImg,
13
- default3 as DirectusLink,
14
- default5 as DirectusVideo,
15
- config,
16
- directusRouteMiddleware,
17
- fetchPageSettings,
13
+ default5 as DirectusLink,
14
+ logger as DirectusNextComponent,
15
+ default6 as DirectusVideo,
18
16
  default7 as useDirectusFile,
19
17
  default8 as useDirectusLink,
20
18
  default9 as useNavigationItems,
package/index.d.ts CHANGED
@@ -5,6 +5,7 @@ export { default as DirectusFile } from './components/DirectusFile';
5
5
  export { default as useDirectusFile } from './hooks/directus-file';
6
6
  export { default as useDirectusLink } from './hooks/directus-link';
7
7
  export { default as useNavigationItems } from './hooks/navigation-items';
8
+ export { logger as DirectusNextComponent } from './logger';
8
9
  export type { TFiles } from './types/files';
9
10
  export type { TPageSettings } from './types/page-settings';
10
11
  export type { TNavigationItems, TNavigationItemsTree } from './types/navigation-items';
package/logger.d.ts ADDED
@@ -0,0 +1 @@
1
+ export declare const logger: import("@okam/logger").Logger;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@okam/directus-next-component",
3
- "version": "1.3.1",
3
+ "version": "1.5.0",
4
4
  "main": "./index.js",
5
5
  "types": "./index.d.ts",
6
6
  "exports": {
@@ -14,9 +14,19 @@
14
14
  }
15
15
  },
16
16
  "publishConfig": {
17
- "registry": "https://registry.npmjs.org"
17
+ "access": "public"
18
18
  },
19
19
  "repository": {
20
20
  "url": "https://github.com/OKAMca/stack.git"
21
+ },
22
+ "dependencies": {
23
+ "@okam/logger": "1.1.0",
24
+ "@okam/next-component": "0.4.0",
25
+ "@okam/stack-ui": "1.31.0",
26
+ "@react-spring/web": "^9.7.5",
27
+ "next": "^14.1.1",
28
+ "radashi": "^12.3.0",
29
+ "react": "18.3.1",
30
+ "unlazy": "^0.12.1"
21
31
  }
22
32
  }
package/server.d.ts CHANGED
@@ -1,9 +1,9 @@
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';
6
+ export { logger as DirectusNextComponent } from './logger';
7
7
  export { default as useDirectusFile } from './hooks/directus-file';
8
8
  export { default as useDirectusLink } from './hooks/directus-link';
9
9
  export { default as useNavigationItems } from './hooks/navigation-items';
@@ -0,0 +1,8 @@
1
+ "use strict";
2
+ const logger = require("./logger.js");
3
+ const createLogger = (name, logger$1, suppressConsole = false) => {
4
+ const log = new logger.Logger(name, logger$1, suppressConsole);
5
+ log.log(`Logger initialized`, "info");
6
+ return log;
7
+ };
8
+ module.exports = createLogger;
@@ -0,0 +1,9 @@
1
+ import { Logger } from "./logger.mjs";
2
+ const createLogger = (name, logger, suppressConsole = false) => {
3
+ const log = new Logger(name, logger, suppressConsole);
4
+ log.log(`Logger initialized`, "info");
5
+ return log;
6
+ };
7
+ export {
8
+ createLogger as default
9
+ };
@@ -0,0 +1,37 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
3
+ class Logger {
4
+ constructor(nameSpace, logger2, suppressConsole) {
5
+ this.nameSpace = "[STACK]";
6
+ this.suppressConsole = false;
7
+ this.env = process.env["NODE_ENV"];
8
+ this.nameSpace = nameSpace ?? this.nameSpace;
9
+ this.suppressConsole = suppressConsole ?? this.suppressConsole;
10
+ this.logger = logger2 ?? this.internalLogger;
11
+ }
12
+ internalLogger(message, severity, context) {
13
+ if (this.env === "production") {
14
+ return;
15
+ }
16
+ console[severity || "log"](`${this.nameSpace} ${message}`.trimStart(), context ?? "");
17
+ }
18
+ setLogger(logger2) {
19
+ this.logger = (message, severity, context) => {
20
+ if (this.suppressConsole) {
21
+ this.internalLogger(message, severity, context);
22
+ }
23
+ logger2(message, severity, context);
24
+ };
25
+ }
26
+ static getInstance() {
27
+ if (!Logger.instance) {
28
+ Logger.instance = new Logger();
29
+ }
30
+ return Logger.instance;
31
+ }
32
+ log(message, severity, context) {
33
+ this.logger(message, severity, context);
34
+ }
35
+ }
36
+ Logger.getInstance();
37
+ exports.Logger = Logger;
@@ -0,0 +1,37 @@
1
+ class Logger {
2
+ constructor(nameSpace, logger2, suppressConsole) {
3
+ this.nameSpace = "[STACK]";
4
+ this.suppressConsole = false;
5
+ this.env = process.env["NODE_ENV"];
6
+ this.nameSpace = nameSpace ?? this.nameSpace;
7
+ this.suppressConsole = suppressConsole ?? this.suppressConsole;
8
+ this.logger = logger2 ?? this.internalLogger;
9
+ }
10
+ internalLogger(message, severity, context) {
11
+ if (this.env === "production") {
12
+ return;
13
+ }
14
+ console[severity || "log"](`${this.nameSpace} ${message}`.trimStart(), context ?? "");
15
+ }
16
+ setLogger(logger2) {
17
+ this.logger = (message, severity, context) => {
18
+ if (this.suppressConsole) {
19
+ this.internalLogger(message, severity, context);
20
+ }
21
+ logger2(message, severity, context);
22
+ };
23
+ }
24
+ static getInstance() {
25
+ if (!Logger.instance) {
26
+ Logger.instance = new Logger();
27
+ }
28
+ return Logger.instance;
29
+ }
30
+ log(message, severity, context) {
31
+ this.logger(message, severity, context);
32
+ }
33
+ }
34
+ Logger.getInstance();
35
+ export {
36
+ Logger
37
+ };
@@ -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
- };