@okam/directus-next 1.2.2 → 1.2.4

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 CHANGED
@@ -1,3 +1,21 @@
1
+ ## 1.2.4 (2025-08-05)
2
+
3
+ ### 🩹 Fixes
4
+
5
+ - **directus-next:** bump version ([e9441ad](https://github.com/OKAMca/stack/commit/e9441ad))
6
+
7
+ ### ❤️ Thank You
8
+
9
+ - poclerson
10
+
11
+ ## 1.2.3 (2025-07-24)
12
+
13
+ ### 🧱 Updated Dependencies
14
+
15
+ - Updated core-lib to 1.17.0
16
+ - Updated directus-node to 0.6.2
17
+ - Updated directus-query to 1.4.2
18
+
1
19
  ## 1.2.2 (2025-07-23)
2
20
 
3
21
  ### 🩹 Fixes
package/index.d.ts CHANGED
@@ -1,7 +1,7 @@
1
1
  export * from './draft';
2
2
  export * from './redirect';
3
3
  export { logger as DirectusNextLogger } from './logger';
4
- export { directusRouteRouter } from './lib/directusRouteRouter';
4
+ export { directusRouteRouter } from './router/router';
5
5
  export { getJsonErrorResponse } from './response';
6
6
  export * from './types';
7
7
  export type { TPageSettings, TPageSettingsTranslation, TPageSettingsItemQuery, TPageSettingsItemDocument, TUsePageSettingsProps, } from './pageSettings/interface';
package/index.js CHANGED
@@ -3,7 +3,7 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
3
3
  const headers = require("next/headers");
4
4
  const navigation = require("next/navigation");
5
5
  const radashi = require("radashi");
6
- const directusRouteRouter = require("./directusRouteRouter-CfK0xrh0.js");
6
+ const router = require("./router-XhYR_hlg.js");
7
7
  const logger = require("@okam/logger");
8
8
  const coreLib = require("@okam/core-lib");
9
9
  function getJsonErrorResponse(data, status) {
@@ -260,7 +260,7 @@ function parseRedirectParams(url) {
260
260
  }
261
261
  async function handleRedirectsRoute({
262
262
  url,
263
- getRedirectSecret = directusRouteRouter.getRedirectSecretDefault,
263
+ getRedirectSecret = router.getRedirectSecretDefault,
264
264
  getJsonError = getJsonErrorResponse,
265
265
  getDirectusApiToken,
266
266
  getDirectusGraphqlUrl,
@@ -276,12 +276,12 @@ async function handleRedirectsRoute({
276
276
  const { redirects, rewrites } = await fetchRedirectsData({ graphqlEndpoint, graphqlApiKey, limit }, init);
277
277
  return new Response(JSON.stringify({ redirects, rewrites }), { status: 200 });
278
278
  }
279
- exports.DirectusNextLogger = directusRouteRouter.logger;
280
- exports.directusRouteRouter = directusRouteRouter.directusRouteRouter;
281
- exports.getApiRouteUrlDefault = directusRouteRouter.getApiRouteUrlDefault;
282
- exports.getRedirectSecretDefault = directusRouteRouter.getRedirectSecretDefault;
283
- exports.getRedirectsRoute = directusRouteRouter.getRedirectsRoute;
284
- exports.handleRedirect = directusRouteRouter.handleRedirect;
279
+ exports.DirectusNextLogger = router.logger;
280
+ exports.directusRouteRouter = router.directusRouteRouter;
281
+ exports.getApiRouteUrlDefault = router.getApiRouteUrlDefault;
282
+ exports.getRedirectSecretDefault = router.getRedirectSecretDefault;
283
+ exports.getRedirectsRoute = router.getRedirectsRoute;
284
+ exports.handleRedirect = router.handleRedirect;
285
285
  exports.getDraftSecretDefault = getDraftSecretDefault;
286
286
  exports.getJsonErrorResponse = getJsonErrorResponse;
287
287
  exports.getPathFromRoute = getPathFromRoute;
package/index.mjs CHANGED
@@ -1,8 +1,8 @@
1
1
  import { draftMode } from "next/headers";
2
2
  import { redirect } from "next/navigation";
3
3
  import { template } from "radashi";
4
- import { g as getRedirectSecretDefault } from "./directusRouteRouter-Bt3bEq4S.mjs";
5
- import { l, d, b, a, h } from "./directusRouteRouter-Bt3bEq4S.mjs";
4
+ import { g as getRedirectSecretDefault } from "./router-BdkmiPvg.mjs";
5
+ import { l, d, b, a, h } from "./router-BdkmiPvg.mjs";
6
6
  import { logger } from "@okam/logger";
7
7
  import { normalizePath } from "@okam/core-lib";
8
8
  function getJsonErrorResponse(data, status) {
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@okam/directus-next",
3
3
  "main": "./index.js",
4
- "version": "1.2.2",
4
+ "version": "1.2.4",
5
5
  "types": "./index.d.ts",
6
6
  "exports": {
7
7
  ".": {
@@ -32,12 +32,12 @@
32
32
  "access": "public"
33
33
  },
34
34
  "dependencies": {
35
- "@okam/core-lib": "1.16.0",
36
- "@okam/directus-node": "0.6.1",
35
+ "@okam/core-lib": "1.17.0",
36
+ "@okam/directus-node": "0.6.2",
37
37
  "@okam/logger": "1.1.0",
38
38
  "next": "^14.1.1",
39
39
  "radashi": "^12.3.0",
40
- "@okam/directus-query": "1.4.1",
40
+ "@okam/directus-query": "1.4.2",
41
41
  "server-only": "0.0.1",
42
42
  "graphql-request": "7.1.2",
43
43
  "server-only-context": "0.1.0",
@@ -0,0 +1,7 @@
1
+ import type { NextRequest, NextResponse as NextResponseType } from 'next/server';
2
+ import type { DirectusRouteConfig } from '../types/directusRouteConfig';
3
+ export declare function directusRouteRouter(request: NextRequest, config: DirectusRouteConfig,
4
+ /**
5
+ * @deprecated Use `directusRouteRouter(request, config)` instead. NextResponse is now directly imported in this file.
6
+ */
7
+ NextResponse?: typeof NextResponseType): Promise<NextResponseType>;
@@ -0,0 +1,2 @@
1
+ import type { PageSettingsTranslation } from '../../types';
2
+ export declare function fetchPageSettingsTranslation(path: string): Promise<PageSettingsTranslation[] | null>;
@@ -1,7 +1,7 @@
1
1
  import { NextResponse } from "next/server";
2
+ import { capitalize, isEmpty } from "radashi";
2
3
  import { createLogger } from "@okam/logger";
3
4
  import { normalizePath } from "@okam/core-lib";
4
- import { capitalize } from "radashi";
5
5
  const logger = createLogger("[DirectusNext]");
6
6
  function log(msg, context, severity = "log") {
7
7
  if (process.env.NODE_ENV === "development") {
@@ -85,6 +85,23 @@ async function handleRedirect(request, options = {}) {
85
85
  log(`${capitalize(type)}ing to ${url.toString()} with status ${status}`);
86
86
  return NextResponse[type](url, { status });
87
87
  }
88
+ const query = `
89
+ query Languages_code($filter: page_settings_translations_filter) {
90
+ page_settings_translations(filter: $filter) {
91
+ languages_code {
92
+ code
93
+ }
94
+ id
95
+ page_settings_id {
96
+ belongs_to_collection
97
+ belongs_to_key
98
+ }
99
+ title
100
+ slug
101
+ path
102
+ }
103
+ }
104
+ `;
88
105
  async function fetchPageSettingsTranslation(path) {
89
106
  const graphqlEndpoint = process.env.NEXT_SERVER_GRAPHQL_URL || process.env.NEXT_PUBLIC_GRAPHQL_URL;
90
107
  const graphqlApiKey = process.env.NEXT_PUBLIC_API_TOKEN;
@@ -94,23 +111,6 @@ async function fetchPageSettingsTranslation(path) {
94
111
  if (!graphqlApiKey) {
95
112
  throw new Error("Missing GraphQL configuration `graphqlApiKey`");
96
113
  }
97
- const query = `
98
- query Languages_code($filter: page_settings_translations_filter) {
99
- page_settings_translations(filter: $filter) {
100
- languages_code {
101
- code
102
- }
103
- id
104
- page_settings_id {
105
- belongs_to_collection
106
- belongs_to_key
107
- }
108
- title
109
- slug
110
- path
111
- }
112
- }
113
- `;
114
114
  const variables = {
115
115
  filter: {
116
116
  path: { _eq: path },
@@ -123,6 +123,7 @@ async function fetchPageSettingsTranslation(path) {
123
123
  const response = await fetch(graphqlEndpoint, {
124
124
  method: "POST",
125
125
  headers: {
126
+ // eslint-disable-next-line @typescript-eslint/naming-convention
126
127
  "Content-Type": "application/json",
127
128
  Authorization: `Bearer ${graphqlApiKey}`
128
129
  },
@@ -140,6 +141,17 @@ function removeLocaleFromPathname(pathname, config) {
140
141
  const currentLocale = Object.values(config.localeMap ?? {}).find((locale) => pathname.startsWith(`/${locale}/`));
141
142
  return { locale: currentLocale, pathname: currentLocale ? pathname.replace(`/${currentLocale}/`, "/") : pathname };
142
143
  }
144
+ function getValidTranslation(translations, locale) {
145
+ const translation = translations[0];
146
+ if (!locale) {
147
+ log("No locale provided, using first translation as default:", { translation });
148
+ return translation;
149
+ }
150
+ return (translations == null ? void 0 : translations.find((t) => {
151
+ var _a, _b;
152
+ return (_b = (_a = t.languages_code) == null ? void 0 : _a.code) == null ? void 0 : _b.startsWith(locale);
153
+ })) ?? translation;
154
+ }
143
155
  async function directusRouteRouter(request, config, NextResponse$1 = NextResponse) {
144
156
  var _a, _b, _c;
145
157
  const { pathname: localizedPathname } = request.nextUrl;
@@ -150,13 +162,13 @@ async function directusRouteRouter(request, config, NextResponse$1 = NextRespons
150
162
  return redirect;
151
163
  }
152
164
  const translations = await fetchPageSettingsTranslation(pathname);
153
- if (!translations || translations.length === 0) {
165
+ if (isEmpty(translations)) {
154
166
  log("No translation found for path:", pathname);
155
167
  return NextResponse$1.next();
156
168
  }
157
- const translation = translations[0];
169
+ const translation = getValidTranslation(translations, locale);
158
170
  log("Using translation:", translation);
159
- if (!translation.languages_code || !translation.page_settings_id) {
171
+ if (!translation || !translation.languages_code || !translation.page_settings_id) {
160
172
  log(`Invalid translation data for path: ${pathname}`, { pathname }, "warn");
161
173
  return NextResponse$1.next();
162
174
  }
@@ -167,6 +179,10 @@ async function directusRouteRouter(request, config, NextResponse$1 = NextRespons
167
179
  log(`PageSettings with id ${id} was found but is not associated with any collection.`, { id }, "warn");
168
180
  return NextResponse$1.next();
169
181
  }
182
+ if (!directusLocale) {
183
+ log(`PageSettings with id ${id} was found but is not associated with any locale.`, { id }, "warn");
184
+ return NextResponse$1.next();
185
+ }
170
186
  const mappedLocale = ((_b = config.localeMap) == null ? void 0 : _b[directusLocale]) || directusLocale;
171
187
  const idField = ((_c = config.collectionSettings[collection]) == null ? void 0 : _c.idField) || config.collectionSettings.default.idField;
172
188
  log("Directus locale:", directusLocale);
@@ -1,8 +1,8 @@
1
1
  "use strict";
2
2
  const server = require("next/server");
3
+ const radashi = require("radashi");
3
4
  const logger$1 = require("@okam/logger");
4
5
  const coreLib = require("@okam/core-lib");
5
- const radashi = require("radashi");
6
6
  const logger = logger$1.createLogger("[DirectusNext]");
7
7
  function log(msg, context, severity = "log") {
8
8
  if (process.env.NODE_ENV === "development") {
@@ -86,6 +86,23 @@ async function handleRedirect(request, options = {}) {
86
86
  log(`${radashi.capitalize(type)}ing to ${url.toString()} with status ${status}`);
87
87
  return server.NextResponse[type](url, { status });
88
88
  }
89
+ const query = `
90
+ query Languages_code($filter: page_settings_translations_filter) {
91
+ page_settings_translations(filter: $filter) {
92
+ languages_code {
93
+ code
94
+ }
95
+ id
96
+ page_settings_id {
97
+ belongs_to_collection
98
+ belongs_to_key
99
+ }
100
+ title
101
+ slug
102
+ path
103
+ }
104
+ }
105
+ `;
89
106
  async function fetchPageSettingsTranslation(path) {
90
107
  const graphqlEndpoint = process.env.NEXT_SERVER_GRAPHQL_URL || process.env.NEXT_PUBLIC_GRAPHQL_URL;
91
108
  const graphqlApiKey = process.env.NEXT_PUBLIC_API_TOKEN;
@@ -95,23 +112,6 @@ async function fetchPageSettingsTranslation(path) {
95
112
  if (!graphqlApiKey) {
96
113
  throw new Error("Missing GraphQL configuration `graphqlApiKey`");
97
114
  }
98
- const query = `
99
- query Languages_code($filter: page_settings_translations_filter) {
100
- page_settings_translations(filter: $filter) {
101
- languages_code {
102
- code
103
- }
104
- id
105
- page_settings_id {
106
- belongs_to_collection
107
- belongs_to_key
108
- }
109
- title
110
- slug
111
- path
112
- }
113
- }
114
- `;
115
115
  const variables = {
116
116
  filter: {
117
117
  path: { _eq: path },
@@ -124,6 +124,7 @@ async function fetchPageSettingsTranslation(path) {
124
124
  const response = await fetch(graphqlEndpoint, {
125
125
  method: "POST",
126
126
  headers: {
127
+ // eslint-disable-next-line @typescript-eslint/naming-convention
127
128
  "Content-Type": "application/json",
128
129
  Authorization: `Bearer ${graphqlApiKey}`
129
130
  },
@@ -141,6 +142,17 @@ function removeLocaleFromPathname(pathname, config) {
141
142
  const currentLocale = Object.values(config.localeMap ?? {}).find((locale) => pathname.startsWith(`/${locale}/`));
142
143
  return { locale: currentLocale, pathname: currentLocale ? pathname.replace(`/${currentLocale}/`, "/") : pathname };
143
144
  }
145
+ function getValidTranslation(translations, locale) {
146
+ const translation = translations[0];
147
+ if (!locale) {
148
+ log("No locale provided, using first translation as default:", { translation });
149
+ return translation;
150
+ }
151
+ return (translations == null ? void 0 : translations.find((t) => {
152
+ var _a, _b;
153
+ return (_b = (_a = t.languages_code) == null ? void 0 : _a.code) == null ? void 0 : _b.startsWith(locale);
154
+ })) ?? translation;
155
+ }
144
156
  async function directusRouteRouter(request, config, NextResponse = server.NextResponse) {
145
157
  var _a, _b, _c;
146
158
  const { pathname: localizedPathname } = request.nextUrl;
@@ -151,13 +163,13 @@ async function directusRouteRouter(request, config, NextResponse = server.NextRe
151
163
  return redirect;
152
164
  }
153
165
  const translations = await fetchPageSettingsTranslation(pathname);
154
- if (!translations || translations.length === 0) {
166
+ if (radashi.isEmpty(translations)) {
155
167
  log("No translation found for path:", pathname);
156
168
  return NextResponse.next();
157
169
  }
158
- const translation = translations[0];
170
+ const translation = getValidTranslation(translations, locale);
159
171
  log("Using translation:", translation);
160
- if (!translation.languages_code || !translation.page_settings_id) {
172
+ if (!translation || !translation.languages_code || !translation.page_settings_id) {
161
173
  log(`Invalid translation data for path: ${pathname}`, { pathname }, "warn");
162
174
  return NextResponse.next();
163
175
  }
@@ -168,6 +180,10 @@ async function directusRouteRouter(request, config, NextResponse = server.NextRe
168
180
  log(`PageSettings with id ${id} was found but is not associated with any collection.`, { id }, "warn");
169
181
  return NextResponse.next();
170
182
  }
183
+ if (!directusLocale) {
184
+ log(`PageSettings with id ${id} was found but is not associated with any locale.`, { id }, "warn");
185
+ return NextResponse.next();
186
+ }
171
187
  const mappedLocale = ((_b = config.localeMap) == null ? void 0 : _b[directusLocale]) || directusLocale;
172
188
  const idField = ((_c = config.collectionSettings[collection]) == null ? void 0 : _c.idField) || config.collectionSettings.default.idField;
173
189
  log("Directus locale:", directusLocale);
package/server.d.ts CHANGED
@@ -1,3 +1,3 @@
1
- export { directusRouteRouter } from './lib/directusRouteRouter';
1
+ export { directusRouteRouter } from './router/router';
2
2
  export type { DirectusRouteConfig } from './types/directusRouteConfig';
3
3
  export * from './pageSettings';
package/server.js CHANGED
@@ -1,7 +1,7 @@
1
1
  "use server";
2
2
  "use strict";
3
3
  Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
4
- const directusRouteRouter = require("./directusRouteRouter-CfK0xrh0.js");
4
+ const router = require("./router-XhYR_hlg.js");
5
5
  require("server-only");
6
6
  const createServerContext = require("server-only-context");
7
7
  const directusQuery = require("@okam/directus-query");
@@ -34,31 +34,31 @@ async function usePageSettings(props, itemKey) {
34
34
  const directusVariables = getDirectusVariables(variables, config);
35
35
  const defaultReturn = getPageSettings() ?? {};
36
36
  if (!props || radashi.isEqual(getVariables(), directusVariables)) {
37
- directusRouteRouter.log("Using cached page settings", { path: (_c = (_b = (_a = defaultReturn.page_settings) == null ? void 0 : _a.translations) == null ? void 0 : _b[0]) == null ? void 0 : _c.path });
37
+ router.log("Using cached page settings", { path: (_c = (_b = (_a = defaultReturn.page_settings) == null ? void 0 : _a.translations) == null ? void 0 : _b[0]) == null ? void 0 : _c.path });
38
38
  return defaultReturn;
39
39
  }
40
40
  const { document } = props;
41
41
  const key = itemKey ?? radashi.get(document, "definitions[0].selectionSet.selections[0].name.value");
42
- directusRouteRouter.log("Querying new page settings", directusVariables);
42
+ router.log("Querying new page settings", directusVariables);
43
43
  const result = await directusQuery.queryGql(document, directusVariables);
44
44
  const items = result == null ? void 0 : result[key];
45
45
  const currentItem = Array.isArray(items) ? items == null ? void 0 : items[0] : items;
46
46
  const currentPageSettings = currentItem == null ? void 0 : currentItem.page_settings;
47
47
  const currentPath = (_e = (_d = currentPageSettings == null ? void 0 : currentPageSettings.translations) == null ? void 0 : _d[0]) == null ? void 0 : _e.path;
48
48
  if (!currentItem) {
49
- directusRouteRouter.log("No item found. Falling back to cached page settings", { path: currentPath }, "warn");
49
+ router.log("No item found. Falling back to cached page settings", { path: currentPath }, "warn");
50
50
  return defaultReturn;
51
51
  }
52
52
  if (!currentPageSettings) {
53
- directusRouteRouter.log("No page settings found. Falling back to cached page settings", { path: currentPath }, "warn");
53
+ router.log("No page settings found. Falling back to cached page settings", { path: currentPath }, "warn");
54
54
  return defaultReturn;
55
55
  }
56
- directusRouteRouter.log("Caching new page settings", { path: currentPath });
56
+ router.log("Caching new page settings", { path: currentPath });
57
57
  setPageSettings(currentItem);
58
58
  setVariables(variables);
59
59
  return currentItem;
60
60
  }
61
- exports.directusRouteRouter = directusRouteRouter.directusRouteRouter;
61
+ exports.directusRouteRouter = router.directusRouteRouter;
62
62
  exports.pageSettingsContext = pageSettingsContext;
63
63
  exports.pageSettingsVariablesContext = pageSettingsVariablesContext;
64
64
  exports.usePageSettings = usePageSettings;
package/server.mjs CHANGED
@@ -1,6 +1,6 @@
1
1
  "use server";
2
- import { c as log } from "./directusRouteRouter-Bt3bEq4S.mjs";
3
- import { d } from "./directusRouteRouter-Bt3bEq4S.mjs";
2
+ import { c as log } from "./router-BdkmiPvg.mjs";
3
+ import { d } from "./router-BdkmiPvg.mjs";
4
4
  import "server-only";
5
5
  import createServerContext from "server-only-context";
6
6
  import { queryGql } from "@okam/directus-query";
@@ -1,13 +1,13 @@
1
1
  export interface PageSettingsTranslation {
2
- languages_code: {
3
- code: string;
4
- };
2
+ languages_code?: {
3
+ code?: string | null | undefined;
4
+ } | null | undefined;
5
5
  id: string;
6
- page_settings_id: {
7
- belongs_to_collection: string;
8
- belongs_to_key: string;
9
- };
10
- title: string;
11
- slug: string;
12
- path: string;
6
+ page_settings_id?: {
7
+ belongs_to_collection?: string | null | undefined;
8
+ belongs_to_key?: string | null | undefined;
9
+ } | null | undefined;
10
+ title?: string | null | undefined;
11
+ slug?: string | null | undefined;
12
+ path?: string | null | undefined;
13
13
  }
@@ -1,10 +0,0 @@
1
- import type { NextRequest, NextResponse as NextResponseType } from 'next/server';
2
- import type { DirectusRouteConfig } from '../types/directusRouteConfig';
3
- /**
4
- * Handles routing for Directus.
5
- *
6
- * @param {NextRequest} request - The incoming request object.
7
- * @param {DirectusRouteConfig} config - Configuration for routing.
8
- * @returns {Promise<NextResponse>} The response object.
9
- */
10
- export declare function directusRouteRouter(request: NextRequest, config: DirectusRouteConfig): Promise<NextResponseType>;