@okam/directus-next 0.1.1 → 0.2.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/draft/env.js ADDED
@@ -0,0 +1,6 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
3
+ function getDraftSecretDefault() {
4
+ return process.env.NEXT_API_DRAFT_SECRET || "";
5
+ }
6
+ exports.getDraftSecretDefault = getDraftSecretDefault;
package/draft/env.mjs ADDED
@@ -0,0 +1,6 @@
1
+ function getDraftSecretDefault() {
2
+ return process.env.NEXT_API_DRAFT_SECRET || "";
3
+ }
4
+ export {
5
+ getDraftSecretDefault
6
+ };
package/draft/route.js ADDED
@@ -0,0 +1,141 @@
1
+ "use strict";
2
+ Object.defineProperties(exports, { __esModule: { value: true }, [Symbol.toStringTag]: { value: "Module" } });
3
+ const headers = require("next/headers");
4
+ const navigation = require("next/navigation");
5
+ const radash = require("radash");
6
+ const response = require("../response.js");
7
+ const env = require("./env.js");
8
+ function parseParams(url) {
9
+ const { searchParams } = new URL(url);
10
+ const secret = searchParams.get("secret") || "";
11
+ const languagesParam = searchParams.get("languages");
12
+ const pathsParam = searchParams.get("urls");
13
+ const routesParam = searchParams.get("routes");
14
+ const pkParam = searchParams.get("pk");
15
+ const versionParam = searchParams.get("version");
16
+ const emptyReturn = {
17
+ secret: "",
18
+ languages: [],
19
+ paths: [],
20
+ routes: [],
21
+ type: "",
22
+ pk: "",
23
+ version: ""
24
+ };
25
+ if (!secret || !languagesParam || !(pathsParam || routesParam)) {
26
+ return emptyReturn;
27
+ }
28
+ const pk = typeof pkParam === "string" ? pkParam : "";
29
+ const version = typeof versionParam === "string" ? versionParam : "";
30
+ try {
31
+ const languages = JSON.parse(languagesParam);
32
+ if (routesParam) {
33
+ const routes = JSON.parse(routesParam);
34
+ return {
35
+ secret,
36
+ languages,
37
+ paths: [],
38
+ routes,
39
+ type: "route",
40
+ pk,
41
+ version
42
+ };
43
+ }
44
+ if (!pathsParam) {
45
+ return emptyReturn;
46
+ }
47
+ const paths = JSON.parse(pathsParam);
48
+ return {
49
+ secret,
50
+ languages,
51
+ paths,
52
+ routes: [],
53
+ type: "path",
54
+ pk,
55
+ version
56
+ };
57
+ } catch (e) {
58
+ return emptyReturn;
59
+ }
60
+ }
61
+ function getPathFromRoute(routeUrl, url, index = 0) {
62
+ const { searchParams } = new URL(url);
63
+ const matches = [...routeUrl.matchAll(/\{\{([a-z]+)\}\}/gi)];
64
+ const map = {};
65
+ matches.forEach((match) => {
66
+ const key = match[1] || "";
67
+ if (!key) {
68
+ return;
69
+ }
70
+ const listkey = `${key}s`;
71
+ const listParam = searchParams.get(listkey);
72
+ if (listParam) {
73
+ try {
74
+ const list = JSON.parse(listParam);
75
+ map[key] = list[index] || "";
76
+ } catch (e) {
77
+ map[key] = "";
78
+ }
79
+ } else {
80
+ const param = searchParams.get(key);
81
+ map[key] = param || "";
82
+ }
83
+ });
84
+ return radash.template(routeUrl, map);
85
+ }
86
+ function handleDraftRoute({
87
+ url,
88
+ getDirectusLanguage,
89
+ getDraftSecret,
90
+ getJsonError
91
+ }) {
92
+ const getSecretFunction = getDraftSecret || env.getDraftSecretDefault;
93
+ const getJsonErrorResponseFunction = getJsonError || response.getJsonErrorResponse;
94
+ const { secret, languages, paths, routes, type, version } = parseParams(url);
95
+ if (secret !== getSecretFunction()) {
96
+ return getJsonErrorResponseFunction({ error: "Invalid argument" }, 401);
97
+ }
98
+ if (type === "") {
99
+ return getJsonErrorResponseFunction({ error: "Invalid argument" }, 400);
100
+ }
101
+ if (!Array.isArray(languages) || languages.length <= 0) {
102
+ return getJsonErrorResponseFunction({ error: "Invalid languages argument" }, 400);
103
+ }
104
+ if (type === "path" && (!Array.isArray(paths) || paths.length <= 0)) {
105
+ return getJsonErrorResponseFunction({ error: "Invalid paths argument" }, 400);
106
+ }
107
+ if (type === "route" && (!Array.isArray(routes) || routes.length <= 0)) {
108
+ return getJsonErrorResponseFunction({ error: "Invalid routes argument" }, 400);
109
+ }
110
+ const directusLang = getDirectusLanguage();
111
+ const indexDefault = languages.indexOf(directusLang);
112
+ const index = indexDefault !== -1 ? indexDefault : 0;
113
+ let redirectUrl = "";
114
+ if (type === "path") {
115
+ const path = paths[index] || "";
116
+ if (!path) {
117
+ return response.getJsonErrorResponse({ error: "Invalid path" }, 400);
118
+ }
119
+ redirectUrl = path;
120
+ } else if (type === "route") {
121
+ const route = routes[index] || "";
122
+ if (!route) {
123
+ return response.getJsonErrorResponse({ error: "Invalid route" }, 400);
124
+ }
125
+ const pathFromRoute = getPathFromRoute(route, url, index);
126
+ if (!pathFromRoute) {
127
+ return response.getJsonErrorResponse({ error: "Invalid route" }, 400);
128
+ }
129
+ redirectUrl = pathFromRoute;
130
+ }
131
+ if (redirectUrl && version) {
132
+ const withParams = redirectUrl.indexOf("?") !== -1;
133
+ redirectUrl = `${redirectUrl}${withParams ? "&" : "?"}version=${encodeURIComponent(version)}`;
134
+ }
135
+ headers.draftMode().enable();
136
+ navigation.redirect(redirectUrl);
137
+ return void 0;
138
+ }
139
+ exports.default = handleDraftRoute;
140
+ exports.getPathFromRoute = getPathFromRoute;
141
+ exports.parseParams = parseParams;
@@ -0,0 +1,141 @@
1
+ import { draftMode } from "next/headers";
2
+ import { redirect } from "next/navigation";
3
+ import { template } from "radash";
4
+ import { getJsonErrorResponse } from "../response.mjs";
5
+ import { getDraftSecretDefault } from "./env.mjs";
6
+ function parseParams(url) {
7
+ const { searchParams } = new URL(url);
8
+ const secret = searchParams.get("secret") || "";
9
+ const languagesParam = searchParams.get("languages");
10
+ const pathsParam = searchParams.get("urls");
11
+ const routesParam = searchParams.get("routes");
12
+ const pkParam = searchParams.get("pk");
13
+ const versionParam = searchParams.get("version");
14
+ const emptyReturn = {
15
+ secret: "",
16
+ languages: [],
17
+ paths: [],
18
+ routes: [],
19
+ type: "",
20
+ pk: "",
21
+ version: ""
22
+ };
23
+ if (!secret || !languagesParam || !(pathsParam || routesParam)) {
24
+ return emptyReturn;
25
+ }
26
+ const pk = typeof pkParam === "string" ? pkParam : "";
27
+ const version = typeof versionParam === "string" ? versionParam : "";
28
+ try {
29
+ const languages = JSON.parse(languagesParam);
30
+ if (routesParam) {
31
+ const routes = JSON.parse(routesParam);
32
+ return {
33
+ secret,
34
+ languages,
35
+ paths: [],
36
+ routes,
37
+ type: "route",
38
+ pk,
39
+ version
40
+ };
41
+ }
42
+ if (!pathsParam) {
43
+ return emptyReturn;
44
+ }
45
+ const paths = JSON.parse(pathsParam);
46
+ return {
47
+ secret,
48
+ languages,
49
+ paths,
50
+ routes: [],
51
+ type: "path",
52
+ pk,
53
+ version
54
+ };
55
+ } catch (e) {
56
+ return emptyReturn;
57
+ }
58
+ }
59
+ function getPathFromRoute(routeUrl, url, index = 0) {
60
+ const { searchParams } = new URL(url);
61
+ const matches = [...routeUrl.matchAll(/\{\{([a-z]+)\}\}/gi)];
62
+ const map = {};
63
+ matches.forEach((match) => {
64
+ const key = match[1] || "";
65
+ if (!key) {
66
+ return;
67
+ }
68
+ const listkey = `${key}s`;
69
+ const listParam = searchParams.get(listkey);
70
+ if (listParam) {
71
+ try {
72
+ const list = JSON.parse(listParam);
73
+ map[key] = list[index] || "";
74
+ } catch (e) {
75
+ map[key] = "";
76
+ }
77
+ } else {
78
+ const param = searchParams.get(key);
79
+ map[key] = param || "";
80
+ }
81
+ });
82
+ return template(routeUrl, map);
83
+ }
84
+ function handleDraftRoute({
85
+ url,
86
+ getDirectusLanguage,
87
+ getDraftSecret,
88
+ getJsonError
89
+ }) {
90
+ const getSecretFunction = getDraftSecret || getDraftSecretDefault;
91
+ const getJsonErrorResponseFunction = getJsonError || getJsonErrorResponse;
92
+ const { secret, languages, paths, routes, type, version } = parseParams(url);
93
+ if (secret !== getSecretFunction()) {
94
+ return getJsonErrorResponseFunction({ error: "Invalid argument" }, 401);
95
+ }
96
+ if (type === "") {
97
+ return getJsonErrorResponseFunction({ error: "Invalid argument" }, 400);
98
+ }
99
+ if (!Array.isArray(languages) || languages.length <= 0) {
100
+ return getJsonErrorResponseFunction({ error: "Invalid languages argument" }, 400);
101
+ }
102
+ if (type === "path" && (!Array.isArray(paths) || paths.length <= 0)) {
103
+ return getJsonErrorResponseFunction({ error: "Invalid paths argument" }, 400);
104
+ }
105
+ if (type === "route" && (!Array.isArray(routes) || routes.length <= 0)) {
106
+ return getJsonErrorResponseFunction({ error: "Invalid routes argument" }, 400);
107
+ }
108
+ const directusLang = getDirectusLanguage();
109
+ const indexDefault = languages.indexOf(directusLang);
110
+ const index = indexDefault !== -1 ? indexDefault : 0;
111
+ let redirectUrl = "";
112
+ if (type === "path") {
113
+ const path = paths[index] || "";
114
+ if (!path) {
115
+ return getJsonErrorResponse({ error: "Invalid path" }, 400);
116
+ }
117
+ redirectUrl = path;
118
+ } else if (type === "route") {
119
+ const route = routes[index] || "";
120
+ if (!route) {
121
+ return getJsonErrorResponse({ error: "Invalid route" }, 400);
122
+ }
123
+ const pathFromRoute = getPathFromRoute(route, url, index);
124
+ if (!pathFromRoute) {
125
+ return getJsonErrorResponse({ error: "Invalid route" }, 400);
126
+ }
127
+ redirectUrl = pathFromRoute;
128
+ }
129
+ if (redirectUrl && version) {
130
+ const withParams = redirectUrl.indexOf("?") !== -1;
131
+ redirectUrl = `${redirectUrl}${withParams ? "&" : "?"}version=${encodeURIComponent(version)}`;
132
+ }
133
+ draftMode().enable();
134
+ redirect(redirectUrl);
135
+ return void 0;
136
+ }
137
+ export {
138
+ handleDraftRoute as default,
139
+ getPathFromRoute,
140
+ parseParams
141
+ };
package/index.d.ts CHANGED
@@ -1,2 +1,3 @@
1
1
  export * from './draft';
2
+ export { directusRouteRouter } from './lib/directusRouteRouter';
2
3
  export { getJsonErrorResponse } from './response';
package/index.js CHANGED
@@ -1 +1,12 @@
1
- "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const I=require("next/headers"),A=require("next/navigation"),D=require("radash");function h(s,n){const a={"Content-Type":"text/json; charset=UTF-8"},o=JSON.stringify(s);return new Response(o,{status:n,headers:a})}function P(){return process.env.NEXT_API_DRAFT_SECRET||""}function R(s){const{searchParams:n}=new URL(s),a=n.get("secret")||"",o=n.get("languages"),u=n.get("urls"),e=n.get("routes"),f=n.get("pk"),t=n.get("version"),c={secret:"",languages:[],paths:[],routes:[],type:"",pk:"",version:""};if(!a||!o||!(u||e))return c;const i=typeof f=="string"?f:"",r=typeof t=="string"?t:"";try{const l=JSON.parse(o);if(e){const d=JSON.parse(e);return{secret:a,languages:l,paths:[],routes:d,type:"route",pk:i,version:r}}if(!u)return c;const m=JSON.parse(u);return{secret:a,languages:l,paths:m,routes:[],type:"path",pk:i,version:r}}catch{return c}}function S(s,n,a=0){const{searchParams:o}=new URL(n),u=[...s.matchAll(/\{\{([a-z]+)\}\}/gi)],e={};return u.forEach(f=>{const t=f[1]||"";if(!t)return;const c=`${t}s`,i=o.get(c);if(i)try{const r=JSON.parse(i);e[t]=r[a]||""}catch{e[t]=""}else{const r=o.get(t);e[t]=r||""}}),D.template(s,e)}function J({url:s,getDirectusLanguage:n,getDraftSecret:a,getJsonError:o}){const u=a||P,e=o||h,{secret:f,languages:t,paths:c,routes:i,type:r,version:l}=R(s);if(f!==u())return e({error:"Invalid argument"},401);if(r==="")return e({error:"Invalid argument"},400);if(!Array.isArray(t)||t.length<=0)return e({error:"Invalid languages argument"},400);if(r==="path"&&(!Array.isArray(c)||c.length<=0))return e({error:"Invalid paths argument"},400);if(r==="route"&&(!Array.isArray(i)||i.length<=0))return e({error:"Invalid routes argument"},400);const m=n(),d=t.indexOf(m),y=d!==-1?d:0;let g="";if(r==="path"){const p=c[y]||"";if(!p)return h({error:"Invalid path"},400);g=p}else if(r==="route"){const p=i[y]||"";if(!p)return h({error:"Invalid route"},400);const v=S(p,s,y);if(!v)return h({error:"Invalid route"},400);g=v}if(g&&l){const p=g.indexOf("?")!==-1;g=`${g}${p?"&":"?"}version=${encodeURIComponent(l)}`}I.draftMode().enable(),A.redirect(g)}exports.getDraftSecretDefault=P;exports.getJsonErrorResponse=h;exports.getPathFromRoute=S;exports.handleDraftRoute=J;exports.parseParams=R;
1
+ "use strict";
2
+ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
3
+ const route = require("./draft/route.js");
4
+ const env = require("./draft/env.js");
5
+ const directusRouteRouter = require("./lib/directusRouteRouter.js");
6
+ const response = require("./response.js");
7
+ exports.getPathFromRoute = route.getPathFromRoute;
8
+ exports.handleDraftRoute = route.default;
9
+ exports.parseParams = route.parseParams;
10
+ exports.getDraftSecretDefault = env.getDraftSecretDefault;
11
+ exports.directusRouteRouter = directusRouteRouter.directusRouteRouter;
12
+ exports.getJsonErrorResponse = response.getJsonErrorResponse;
package/index.mjs CHANGED
@@ -1,125 +1,12 @@
1
- import { draftMode as P } from "next/headers";
2
- import { redirect as R } from "next/navigation";
3
- import { template as I } from "radash";
4
- function h(s, n) {
5
- const o = {
6
- // eslint-disable-next-line @typescript-eslint/naming-convention
7
- "Content-Type": "text/json; charset=UTF-8"
8
- }, a = JSON.stringify(s);
9
- return new Response(a, {
10
- status: n,
11
- headers: o
12
- });
13
- }
14
- function A() {
15
- return process.env.NEXT_API_DRAFT_SECRET || "";
16
- }
17
- function S(s) {
18
- const { searchParams: n } = new URL(s), o = n.get("secret") || "", a = n.get("languages"), u = n.get("urls"), t = n.get("routes"), f = n.get("pk"), r = n.get("version"), c = {
19
- secret: "",
20
- languages: [],
21
- paths: [],
22
- routes: [],
23
- type: "",
24
- pk: "",
25
- version: ""
26
- };
27
- if (!o || !a || !(u || t))
28
- return c;
29
- const i = typeof f == "string" ? f : "", e = typeof r == "string" ? r : "";
30
- try {
31
- const l = JSON.parse(a);
32
- if (t) {
33
- const m = JSON.parse(t);
34
- return {
35
- secret: o,
36
- languages: l,
37
- paths: [],
38
- routes: m,
39
- type: "route",
40
- pk: i,
41
- version: e
42
- };
43
- }
44
- if (!u)
45
- return c;
46
- const d = JSON.parse(u);
47
- return {
48
- secret: o,
49
- languages: l,
50
- paths: d,
51
- routes: [],
52
- type: "path",
53
- pk: i,
54
- version: e
55
- };
56
- } catch {
57
- return c;
58
- }
59
- }
60
- function J(s, n, o = 0) {
61
- const { searchParams: a } = new URL(n), u = [...s.matchAll(/\{\{([a-z]+)\}\}/gi)], t = {};
62
- return u.forEach((f) => {
63
- const r = f[1] || "";
64
- if (!r)
65
- return;
66
- const c = `${r}s`, i = a.get(c);
67
- if (i)
68
- try {
69
- const e = JSON.parse(i);
70
- t[r] = e[o] || "";
71
- } catch {
72
- t[r] = "";
73
- }
74
- else {
75
- const e = a.get(r);
76
- t[r] = e || "";
77
- }
78
- }), I(s, t);
79
- }
80
- function E({
81
- url: s,
82
- getDirectusLanguage: n,
83
- getDraftSecret: o,
84
- getJsonError: a
85
- }) {
86
- const u = o || A, t = a || h, { secret: f, languages: r, paths: c, routes: i, type: e, version: l } = S(s);
87
- if (f !== u())
88
- return t({ error: "Invalid argument" }, 401);
89
- if (e === "")
90
- return t({ error: "Invalid argument" }, 400);
91
- if (!Array.isArray(r) || r.length <= 0)
92
- return t({ error: "Invalid languages argument" }, 400);
93
- if (e === "path" && (!Array.isArray(c) || c.length <= 0))
94
- return t({ error: "Invalid paths argument" }, 400);
95
- if (e === "route" && (!Array.isArray(i) || i.length <= 0))
96
- return t({ error: "Invalid routes argument" }, 400);
97
- const d = n(), m = r.indexOf(d), y = m !== -1 ? m : 0;
98
- let p = "";
99
- if (e === "path") {
100
- const g = c[y] || "";
101
- if (!g)
102
- return h({ error: "Invalid path" }, 400);
103
- p = g;
104
- } else if (e === "route") {
105
- const g = i[y] || "";
106
- if (!g)
107
- return h({ error: "Invalid route" }, 400);
108
- const v = J(g, s, y);
109
- if (!v)
110
- return h({ error: "Invalid route" }, 400);
111
- p = v;
112
- }
113
- if (p && l) {
114
- const g = p.indexOf("?") !== -1;
115
- p = `${p}${g ? "&" : "?"}version=${encodeURIComponent(l)}`;
116
- }
117
- P().enable(), R(p);
118
- }
1
+ import { getPathFromRoute, default as default2, parseParams } from "./draft/route.mjs";
2
+ import { getDraftSecretDefault } from "./draft/env.mjs";
3
+ import { directusRouteRouter } from "./lib/directusRouteRouter.mjs";
4
+ import { getJsonErrorResponse } from "./response.mjs";
119
5
  export {
120
- A as getDraftSecretDefault,
121
- h as getJsonErrorResponse,
122
- J as getPathFromRoute,
123
- E as handleDraftRoute,
124
- S as parseParams
6
+ directusRouteRouter,
7
+ getDraftSecretDefault,
8
+ getJsonErrorResponse,
9
+ getPathFromRoute,
10
+ default2 as handleDraftRoute,
11
+ parseParams
125
12
  };
@@ -0,0 +1,15 @@
1
+ import type { DirectusRouteConfig } from '../types/directusRouteConfig';
2
+ interface MinimalNextUrl {
3
+ pathname: string;
4
+ clone: () => MinimalNextUrl;
5
+ }
6
+ interface MinimalNextRequest {
7
+ nextUrl: MinimalNextUrl;
8
+ }
9
+ interface MinimalNextResponse {
10
+ next: () => MinimalNextResponse;
11
+ notFound: () => MinimalNextResponse;
12
+ rewrite: (url: MinimalNextUrl | URL) => MinimalNextResponse;
13
+ }
14
+ export declare function directusRouteRouter(request: MinimalNextRequest, config: DirectusRouteConfig, NextResponse: MinimalNextResponse): Promise<MinimalNextResponse>;
15
+ export {};
@@ -0,0 +1,92 @@
1
+ "use strict";
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
+ }
8
+ async function fetchPageSettingsTranslation(path) {
9
+ const graphqlEndpoint = process.env.NEXT_PUBLIC_GRAPHQL_URL;
10
+ const graphqlApiKey = process.env.NEXT_PUBLIC_API_TOKEN;
11
+ if (!graphqlEndpoint || !graphqlApiKey) {
12
+ throw new Error("Missing GraphQL configuration");
13
+ }
14
+ const query = `
15
+ query Languages_code($filter: page_settings_translations_filter) {
16
+ page_settings_translations(filter: $filter) {
17
+ languages_code {
18
+ code
19
+ }
20
+ id
21
+ page_settings_id {
22
+ belongs_to_collection
23
+ belongs_to_key
24
+ }
25
+ title
26
+ slug
27
+ path
28
+ }
29
+ }
30
+ `;
31
+ const variables = {
32
+ filter: {
33
+ path: { _eq: path },
34
+ _and: [{ page_settings_id: { belongs_to_key: { _nempty: true } } }]
35
+ }
36
+ };
37
+ try {
38
+ log("Executing GraphQL query:", query);
39
+ log("Query variables:", variables);
40
+ const response = await fetch(graphqlEndpoint, {
41
+ method: "POST",
42
+ headers: {
43
+ "Content-Type": "application/json",
44
+ Authorization: `Bearer ${graphqlApiKey}`
45
+ },
46
+ body: JSON.stringify({ query, variables })
47
+ });
48
+ const { data } = await response.json();
49
+ log("GraphQL response:", data);
50
+ return data.page_settings_translations;
51
+ } catch (error) {
52
+ log("GraphQL Error:", error);
53
+ return null;
54
+ }
55
+ }
56
+ async function directusRouteRouter(request, config, NextResponse) {
57
+ var _a, _b;
58
+ const { pathname } = request.nextUrl;
59
+ log("Processing request for pathname:", pathname);
60
+ const translations = await fetchPageSettingsTranslation(pathname);
61
+ if (!translations || translations.length === 0) {
62
+ log("No translation found for path:", pathname);
63
+ return NextResponse.next();
64
+ }
65
+ const translation = translations[0];
66
+ log("Using translation:", translation);
67
+ if (!translation.languages_code || !translation.page_settings_id) {
68
+ console.warn(`[directusRouter] Invalid translation data for path: ${pathname}`);
69
+ return NextResponse.next();
70
+ }
71
+ const directusLocale = translation.languages_code.code;
72
+ const collection = translation.page_settings_id.belongs_to_collection;
73
+ const id = translation.page_settings_id.belongs_to_key;
74
+ if (!collection) {
75
+ console.warn(`[directusRouter] PageSettings with id ${id} was found but is not associated with any collection.`);
76
+ return NextResponse.next();
77
+ }
78
+ const mappedLocale = ((_a = config.localeMap) == null ? void 0 : _a[directusLocale]) || directusLocale;
79
+ 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);
85
+ const newPath = `/${mappedLocale}/${collection}/${id}`;
86
+ log("Rewriting path:", pathname, "->", newPath);
87
+ const url = request.nextUrl.clone();
88
+ url.pathname = newPath;
89
+ log("Rewriting to URL:", url.toString());
90
+ return NextResponse.rewrite(url);
91
+ }
92
+ exports.directusRouteRouter = directusRouteRouter;
@@ -0,0 +1,92 @@
1
+ function log(...messages) {
2
+ if (process.env.NODE_ENV === "development") {
3
+ console.log("[directusRouter]", ...messages);
4
+ }
5
+ }
6
+ async function fetchPageSettingsTranslation(path) {
7
+ const graphqlEndpoint = process.env.NEXT_PUBLIC_GRAPHQL_URL;
8
+ const graphqlApiKey = process.env.NEXT_PUBLIC_API_TOKEN;
9
+ if (!graphqlEndpoint || !graphqlApiKey) {
10
+ throw new Error("Missing GraphQL configuration");
11
+ }
12
+ const query = `
13
+ query Languages_code($filter: page_settings_translations_filter) {
14
+ page_settings_translations(filter: $filter) {
15
+ languages_code {
16
+ code
17
+ }
18
+ id
19
+ page_settings_id {
20
+ belongs_to_collection
21
+ belongs_to_key
22
+ }
23
+ title
24
+ slug
25
+ path
26
+ }
27
+ }
28
+ `;
29
+ const variables = {
30
+ filter: {
31
+ path: { _eq: path },
32
+ _and: [{ page_settings_id: { belongs_to_key: { _nempty: true } } }]
33
+ }
34
+ };
35
+ try {
36
+ log("Executing GraphQL query:", query);
37
+ log("Query variables:", variables);
38
+ const response = await fetch(graphqlEndpoint, {
39
+ method: "POST",
40
+ headers: {
41
+ "Content-Type": "application/json",
42
+ Authorization: `Bearer ${graphqlApiKey}`
43
+ },
44
+ body: JSON.stringify({ query, variables })
45
+ });
46
+ const { data } = await response.json();
47
+ log("GraphQL response:", data);
48
+ return data.page_settings_translations;
49
+ } catch (error) {
50
+ log("GraphQL Error:", error);
51
+ return null;
52
+ }
53
+ }
54
+ async function directusRouteRouter(request, config, NextResponse) {
55
+ var _a, _b;
56
+ const { pathname } = request.nextUrl;
57
+ log("Processing request for pathname:", pathname);
58
+ const translations = await fetchPageSettingsTranslation(pathname);
59
+ if (!translations || translations.length === 0) {
60
+ log("No translation found for path:", pathname);
61
+ return NextResponse.next();
62
+ }
63
+ const translation = translations[0];
64
+ log("Using translation:", translation);
65
+ if (!translation.languages_code || !translation.page_settings_id) {
66
+ console.warn(`[directusRouter] Invalid translation data for path: ${pathname}`);
67
+ return NextResponse.next();
68
+ }
69
+ const directusLocale = translation.languages_code.code;
70
+ const collection = translation.page_settings_id.belongs_to_collection;
71
+ const id = translation.page_settings_id.belongs_to_key;
72
+ if (!collection) {
73
+ console.warn(`[directusRouter] PageSettings with id ${id} was found but is not associated with any collection.`);
74
+ return NextResponse.next();
75
+ }
76
+ const mappedLocale = ((_a = config.localeMap) == null ? void 0 : _a[directusLocale]) || directusLocale;
77
+ const idField = ((_b = config.collectionSettings[collection]) == null ? void 0 : _b.idField) || config.collectionSettings.default.idField;
78
+ log("Directus locale:", directusLocale);
79
+ log("Mapped locale:", mappedLocale);
80
+ log("Collection:", collection);
81
+ log("ID Field:", idField);
82
+ log("ID:", id);
83
+ const newPath = `/${mappedLocale}/${collection}/${id}`;
84
+ log("Rewriting path:", pathname, "->", newPath);
85
+ const url = request.nextUrl.clone();
86
+ url.pathname = newPath;
87
+ log("Rewriting to URL:", url.toString());
88
+ return NextResponse.rewrite(url);
89
+ }
90
+ export {
91
+ directusRouteRouter
92
+ };
package/package.json CHANGED
@@ -1,17 +1,18 @@
1
1
  {
2
2
  "name": "@okam/directus-next",
3
3
  "main": "./index.js",
4
- "version": "0.1.1",
4
+ "version": "0.2.0",
5
5
  "types": "./index.d.ts",
6
6
  "exports": {
7
7
  ".": {
8
8
  "import": "./index.mjs",
9
9
  "require": "./index.js"
10
+ },
11
+ "./server": {
12
+ "import": "./server.mjs",
13
+ "require": "./server.js"
10
14
  }
11
15
  },
12
- "publishConfig": {
13
- "registry": "https://registry.npmjs.org"
14
- },
15
16
  "repository": {
16
17
  "url": "https://github.com/OKAMca/stack.git"
17
18
  }
package/response.js ADDED
@@ -0,0 +1,14 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
3
+ function getJsonErrorResponse(data, status) {
4
+ const headers = {
5
+ // eslint-disable-next-line @typescript-eslint/naming-convention
6
+ "Content-Type": "text/json; charset=UTF-8"
7
+ };
8
+ const body = JSON.stringify(data);
9
+ return new Response(body, {
10
+ status,
11
+ headers
12
+ });
13
+ }
14
+ exports.getJsonErrorResponse = getJsonErrorResponse;
package/response.mjs ADDED
@@ -0,0 +1,14 @@
1
+ function getJsonErrorResponse(data, status) {
2
+ const headers = {
3
+ // eslint-disable-next-line @typescript-eslint/naming-convention
4
+ "Content-Type": "text/json; charset=UTF-8"
5
+ };
6
+ const body = JSON.stringify(data);
7
+ return new Response(body, {
8
+ status,
9
+ headers
10
+ });
11
+ }
12
+ export {
13
+ getJsonErrorResponse
14
+ };
package/server.d.ts ADDED
@@ -0,0 +1,2 @@
1
+ export { directusRouteRouter } from './lib/directusRouteRouter';
2
+ export type { DirectusRouteConfig } from './types/directusRouteConfig';
package/server.js ADDED
@@ -0,0 +1,4 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
3
+ const directusRouteRouter = require("./lib/directusRouteRouter.js");
4
+ exports.directusRouteRouter = directusRouteRouter.directusRouteRouter;
package/server.mjs ADDED
@@ -0,0 +1,4 @@
1
+ import { directusRouteRouter } from "./lib/directusRouteRouter.mjs";
2
+ export {
3
+ directusRouteRouter
4
+ };
@@ -0,0 +1,12 @@
1
+ export interface DirectusRouteConfig {
2
+ localeMap?: Record<string, string>;
3
+ collectionSettings: {
4
+ [collection: string]: {
5
+ idField: string;
6
+ };
7
+ } & {
8
+ default: {
9
+ idField: string;
10
+ };
11
+ };
12
+ }