@okam/directus-next 0.2.0 → 0.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.
package/CHANGELOG.md ADDED
@@ -0,0 +1,75 @@
1
+ ## 0.3.0 (2024-12-03)
2
+
3
+
4
+ ### 🚀 Features
5
+
6
+ - **directus-next:** not a real change but need to trigger new version ([57f2a54](https://github.com/OKAMca/stack/commit/57f2a54))
7
+
8
+
9
+ ### ❤️ Thank You
10
+
11
+ - David Paquet Pitts
12
+
13
+ ## 0.1.2 (2024-10-04)
14
+
15
+
16
+ ### 🩹 Fixes
17
+
18
+ - **directus-next:** make package public ([#172](https://github.com/OKAMca/stack/pull/172))
19
+
20
+
21
+ ### ❤️ Thank You
22
+
23
+ - yanmorinokamca @yanmorinokamca
24
+
25
+ ## 0.2.0 (2024-09-24)
26
+
27
+
28
+ ### 🚀 Features
29
+
30
+ - **directus-next:** directus based routing ([196ab1e](https://github.com/OKAMca/stack/commit/196ab1e))
31
+
32
+
33
+ ### 🩹 Fixes
34
+
35
+ - **directus-next:** make package public ([#172](https://github.com/OKAMca/stack/pull/172))
36
+
37
+
38
+ ### ❤️ Thank You
39
+
40
+ - David Paquet Pitts
41
+ - yanmorinokamca @yanmorinokamca
42
+
43
+ ## 0.1.1 (2024-08-13)
44
+
45
+
46
+ ### 🩹 Fixes
47
+
48
+ - **directus-next:** update build dependencies, exclude next ([#179](https://github.com/OKAMca/stack/pull/179))
49
+
50
+
51
+ ### ❤️ Thank You
52
+
53
+ - mykimd
54
+ - poclerson
55
+ - yanmorinokamca @yanmorinokamca
56
+
57
+ ## 0.1.0 (2024-08-01)
58
+
59
+
60
+ ### 🚀 Features
61
+
62
+ - **directus-next:** adding draft route code ([64c665d](https://github.com/OKAMca/stack/commit/64c665d))
63
+
64
+
65
+ ### 🩹 Fixes
66
+
67
+ - **publish:** add building packages step to workflow ([c9ce442](https://github.com/OKAMca/stack/commit/c9ce442))
68
+
69
+
70
+ ### ❤️ Thank You
71
+
72
+ - Jérôme Trottier
73
+ - Marie-Maxime Tanguay @marie-maxime
74
+ - Pierre-Olivier Clerson @poclerson
75
+ - Yan Morin
package/README.md ADDED
@@ -0,0 +1,42 @@
1
+ # directus-next
2
+
3
+ This library was generated with [Nx](https://nx.dev).
4
+
5
+ ## Building
6
+
7
+ Run `nx build directus-next` to build the library.
8
+
9
+ ## Running unit tests
10
+
11
+ Run `nx test directus-next` to execute the unit tests via [Jest](https://jestjs.io).
12
+
13
+ ## Draft
14
+
15
+ ### Installation
16
+ In your next folder app/api/ directory:
17
+ * Create directory draft/
18
+ * Create file draft/route.ts
19
+ * In route.ts, import handleDraftRoute,
20
+ * Define GET function.
21
+
22
+ ### Example
23
+ ```
24
+ import { handleDraftRoute } from '@okam/directus-next'
25
+
26
+ // eslint-disable-next-line @typescript-eslint/naming-convention
27
+ export async function GET(request: Request) {
28
+ return handleDraftRoute({
29
+ url: request.url,
30
+ getDirectusLanguage: () => 'en-CA' // default language to show
31
+ })
32
+ }
33
+ ```
34
+
35
+ ### Environment
36
+ Set the secret in your .env and your directus application (preview_secret field).
37
+
38
+ .env
39
+ ```
40
+ NEXT_API_DRAFT_SECRET=123
41
+ ```
42
+
@@ -2,7 +2,7 @@
2
2
  Object.defineProperties(exports, { __esModule: { value: true }, [Symbol.toStringTag]: { value: "Module" } });
3
3
  const headers = require("next/headers");
4
4
  const navigation = require("next/navigation");
5
- const radash = require("radash");
5
+ const radashi = require("radashi");
6
6
  const response = require("../response.js");
7
7
  const env = require("./env.js");
8
8
  function parseParams(url) {
@@ -81,7 +81,7 @@ function getPathFromRoute(routeUrl, url, index = 0) {
81
81
  map[key] = param || "";
82
82
  }
83
83
  });
84
- return radash.template(routeUrl, map);
84
+ return radashi.template(routeUrl, map);
85
85
  }
86
86
  function handleDraftRoute({
87
87
  url,
@@ -1,6 +1,6 @@
1
1
  import { draftMode } from "next/headers";
2
2
  import { redirect } from "next/navigation";
3
- import { template } from "radash";
3
+ import { template } from "radashi";
4
4
  import { getJsonErrorResponse } from "../response.mjs";
5
5
  import { getDraftSecretDefault } from "./env.mjs";
6
6
  function parseParams(url) {
@@ -2,11 +2,13 @@
2
2
  Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
3
3
  const route = require("./draft/route.js");
4
4
  const env = require("./draft/env.js");
5
+ const logger = require("./logger.js");
5
6
  const directusRouteRouter = require("./lib/directusRouteRouter.js");
6
7
  const response = require("./response.js");
7
8
  exports.getPathFromRoute = route.getPathFromRoute;
8
9
  exports.handleDraftRoute = route.default;
9
10
  exports.parseParams = route.parseParams;
10
11
  exports.getDraftSecretDefault = env.getDraftSecretDefault;
12
+ exports.DirectusNextLogger = logger.logger;
11
13
  exports.directusRouteRouter = directusRouteRouter.directusRouteRouter;
12
14
  exports.getJsonErrorResponse = response.getJsonErrorResponse;
@@ -1,8 +1,10 @@
1
1
  import { getPathFromRoute, default as default2, parseParams } from "./draft/route.mjs";
2
2
  import { getDraftSecretDefault } from "./draft/env.mjs";
3
+ import { logger } from "./logger.mjs";
3
4
  import { directusRouteRouter } from "./lib/directusRouteRouter.mjs";
4
5
  import { getJsonErrorResponse } from "./response.mjs";
5
6
  export {
7
+ logger as DirectusNextLogger,
6
8
  directusRouteRouter,
7
9
  getDraftSecretDefault,
8
10
  getJsonErrorResponse,
@@ -1,12 +1,8 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
3
- function log(...messages) {
4
- if (process.env.NODE_ENV === "development") {
5
- console.log("[directusRouter]", ...messages);
6
- }
7
- }
3
+ const logger = require("../logger.js");
8
4
  async function fetchPageSettingsTranslation(path) {
9
- const graphqlEndpoint = process.env.NEXT_PUBLIC_GRAPHQL_URL;
5
+ const graphqlEndpoint = process.env.NEXT_SERVER_GRAPHQL_URL || process.env.NEXT_PUBLIC_GRAPHQL_URL;
10
6
  const graphqlApiKey = process.env.NEXT_PUBLIC_API_TOKEN;
11
7
  if (!graphqlEndpoint || !graphqlApiKey) {
12
8
  throw new Error("Missing GraphQL configuration");
@@ -35,8 +31,8 @@ async function fetchPageSettingsTranslation(path) {
35
31
  }
36
32
  };
37
33
  try {
38
- log("Executing GraphQL query:", query);
39
- log("Query variables:", variables);
34
+ logger.log("Executing GraphQL query:", query);
35
+ logger.log("Query variables:", variables);
40
36
  const response = await fetch(graphqlEndpoint, {
41
37
  method: "POST",
42
38
  headers: {
@@ -46,47 +42,47 @@ async function fetchPageSettingsTranslation(path) {
46
42
  body: JSON.stringify({ query, variables })
47
43
  });
48
44
  const { data } = await response.json();
49
- log("GraphQL response:", data);
45
+ logger.log("GraphQL response:", data);
50
46
  return data.page_settings_translations;
51
47
  } catch (error) {
52
- log("GraphQL Error:", error);
48
+ logger.log("GraphQL Error:", error);
53
49
  return null;
54
50
  }
55
51
  }
56
52
  async function directusRouteRouter(request, config, NextResponse) {
57
53
  var _a, _b;
58
54
  const { pathname } = request.nextUrl;
59
- log("Processing request for pathname:", pathname);
55
+ logger.log("Processing request for pathname:", pathname);
60
56
  const translations = await fetchPageSettingsTranslation(pathname);
61
57
  if (!translations || translations.length === 0) {
62
- log("No translation found for path:", pathname);
58
+ logger.log("No translation found for path:", pathname);
63
59
  return NextResponse.next();
64
60
  }
65
61
  const translation = translations[0];
66
- log("Using translation:", translation);
62
+ logger.log("Using translation:", translation);
67
63
  if (!translation.languages_code || !translation.page_settings_id) {
68
- console.warn(`[directusRouter] Invalid translation data for path: ${pathname}`);
64
+ logger.log(`Invalid translation data for path: ${pathname}`, { pathname }, "warn");
69
65
  return NextResponse.next();
70
66
  }
71
67
  const directusLocale = translation.languages_code.code;
72
68
  const collection = translation.page_settings_id.belongs_to_collection;
73
69
  const id = translation.page_settings_id.belongs_to_key;
74
70
  if (!collection) {
75
- console.warn(`[directusRouter] PageSettings with id ${id} was found but is not associated with any collection.`);
71
+ logger.log(`PageSettings with id ${id} was found but is not associated with any collection.`, { id }, "warn");
76
72
  return NextResponse.next();
77
73
  }
78
74
  const mappedLocale = ((_a = config.localeMap) == null ? void 0 : _a[directusLocale]) || directusLocale;
79
75
  const idField = ((_b = config.collectionSettings[collection]) == null ? void 0 : _b.idField) || config.collectionSettings.default.idField;
80
- log("Directus locale:", directusLocale);
81
- log("Mapped locale:", mappedLocale);
82
- log("Collection:", collection);
83
- log("ID Field:", idField);
84
- log("ID:", id);
76
+ logger.log("Directus locale:", directusLocale);
77
+ logger.log("Mapped locale:", mappedLocale);
78
+ logger.log("Collection:", collection);
79
+ logger.log("ID Field:", idField);
80
+ logger.log("ID:", id);
85
81
  const newPath = `/${mappedLocale}/${collection}/${id}`;
86
- log("Rewriting path:", pathname, "->", newPath);
82
+ logger.log(`Rewriting path: ${pathname} -> ${newPath}`);
87
83
  const url = request.nextUrl.clone();
88
84
  url.pathname = newPath;
89
- log("Rewriting to URL:", url.toString());
85
+ logger.log("Rewriting to URL:", url.toString());
90
86
  return NextResponse.rewrite(url);
91
87
  }
92
88
  exports.directusRouteRouter = directusRouteRouter;
@@ -1,10 +1,6 @@
1
- function log(...messages) {
2
- if (process.env.NODE_ENV === "development") {
3
- console.log("[directusRouter]", ...messages);
4
- }
5
- }
1
+ import { log } from "../logger.mjs";
6
2
  async function fetchPageSettingsTranslation(path) {
7
- const graphqlEndpoint = process.env.NEXT_PUBLIC_GRAPHQL_URL;
3
+ const graphqlEndpoint = process.env.NEXT_SERVER_GRAPHQL_URL || process.env.NEXT_PUBLIC_GRAPHQL_URL;
8
4
  const graphqlApiKey = process.env.NEXT_PUBLIC_API_TOKEN;
9
5
  if (!graphqlEndpoint || !graphqlApiKey) {
10
6
  throw new Error("Missing GraphQL configuration");
@@ -63,14 +59,14 @@ async function directusRouteRouter(request, config, NextResponse) {
63
59
  const translation = translations[0];
64
60
  log("Using translation:", translation);
65
61
  if (!translation.languages_code || !translation.page_settings_id) {
66
- console.warn(`[directusRouter] Invalid translation data for path: ${pathname}`);
62
+ log(`Invalid translation data for path: ${pathname}`, { pathname }, "warn");
67
63
  return NextResponse.next();
68
64
  }
69
65
  const directusLocale = translation.languages_code.code;
70
66
  const collection = translation.page_settings_id.belongs_to_collection;
71
67
  const id = translation.page_settings_id.belongs_to_key;
72
68
  if (!collection) {
73
- console.warn(`[directusRouter] PageSettings with id ${id} was found but is not associated with any collection.`);
69
+ log(`PageSettings with id ${id} was found but is not associated with any collection.`, { id }, "warn");
74
70
  return NextResponse.next();
75
71
  }
76
72
  const mappedLocale = ((_a = config.localeMap) == null ? void 0 : _a[directusLocale]) || directusLocale;
@@ -81,7 +77,7 @@ async function directusRouteRouter(request, config, NextResponse) {
81
77
  log("ID Field:", idField);
82
78
  log("ID:", id);
83
79
  const newPath = `/${mappedLocale}/${collection}/${id}`;
84
- log("Rewriting path:", pathname, "->", newPath);
80
+ log(`Rewriting path: ${pathname} -> ${newPath}`);
85
81
  const url = request.nextUrl.clone();
86
82
  url.pathname = newPath;
87
83
  log("Rewriting to URL:", url.toString());
@@ -0,0 +1,12 @@
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("[DirectusNext]");
6
+ function log(msg, context, severity = "log") {
7
+ if (process.env.NODE_ENV === "development") {
8
+ logger.log(msg, severity, { context });
9
+ }
10
+ }
11
+ exports.log = log;
12
+ exports.logger = logger;
@@ -0,0 +1,12 @@
1
+ import "../../../stack/logger/src/lib/logger.mjs";
2
+ import createLogger from "../../../stack/logger/src/lib/factoryLogger.mjs";
3
+ const logger = createLogger("[DirectusNext]");
4
+ function log(msg, context, severity = "log") {
5
+ if (process.env.NODE_ENV === "development") {
6
+ logger.log(msg, severity, { context });
7
+ }
8
+ }
9
+ export {
10
+ log,
11
+ logger
12
+ };
package/index.d.ts CHANGED
@@ -1,3 +1,4 @@
1
1
  export * from './draft';
2
+ export { logger as DirectusNextLogger } from './logger';
2
3
  export { directusRouteRouter } from './lib/directusRouteRouter';
3
4
  export { getJsonErrorResponse } from './response';
package/logger.d.ts ADDED
@@ -0,0 +1,3 @@
1
+ import type { LogSeverity } from '@okam/logger';
2
+ export declare const logger: import("@okam/logger").Logger;
3
+ export declare function log(msg: string, context?: unknown, severity?: LogSeverity): void;
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@okam/directus-next",
3
3
  "main": "./index.js",
4
- "version": "0.2.0",
4
+ "version": "0.4.0",
5
5
  "types": "./index.d.ts",
6
6
  "exports": {
7
7
  ".": {
@@ -15,5 +15,13 @@
15
15
  },
16
16
  "repository": {
17
17
  "url": "https://github.com/OKAMca/stack.git"
18
+ },
19
+ "publishConfig": {
20
+ "access": "public"
21
+ },
22
+ "dependencies": {
23
+ "@okam/logger": "1.1.0",
24
+ "next": "^14.1.1",
25
+ "radashi": "^12.3.0"
18
26
  }
19
27
  }
@@ -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
+ };