@okam/directus-next 1.2.1 → 1.2.2
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 +14 -0
- package/{directus-next/src/lib/directusRouteRouter.mjs → directusRouteRouter-Bt3bEq4S.mjs} +93 -3
- package/directusRouteRouter-CfK0xrh0.js +191 -0
- package/index.js +291 -0
- package/index.mjs +292 -0
- package/package.json +2 -2
- package/{directus-next/src/pageSettings/usePageSettings.js → server.js} +22 -9
- package/{directus-next/src/pageSettings/usePageSettings.mjs → server.mjs} +16 -2
- package/directus-next/src/draft/env.js +0 -6
- package/directus-next/src/draft/env.mjs +0 -6
- package/directus-next/src/draft/route.js +0 -141
- package/directus-next/src/draft/route.mjs +0 -141
- package/directus-next/src/index.js +0 -24
- package/directus-next/src/index.mjs +0 -24
- package/directus-next/src/lib/directusRouteRouter.js +0 -102
- package/directus-next/src/logger.js +0 -11
- package/directus-next/src/logger.mjs +0 -11
- package/directus-next/src/pageSettings/context.js +0 -14
- package/directus-next/src/pageSettings/context.mjs +0 -14
- package/directus-next/src/redirect/env.js +0 -20
- package/directus-next/src/redirect/env.mjs +0 -20
- package/directus-next/src/redirect/route.js +0 -31
- package/directus-next/src/redirect/route.mjs +0 -31
- package/directus-next/src/redirect/utils/getRedirectsRoute.js +0 -27
- package/directus-next/src/redirect/utils/getRedirectsRoute.mjs +0 -27
- package/directus-next/src/redirect/utils/handleRedirect.js +0 -48
- package/directus-next/src/redirect/utils/handleRedirect.mjs +0 -48
- package/directus-next/src/response.js +0 -14
- package/directus-next/src/response.mjs +0 -14
- package/directus-next/src/server.js +0 -9
- package/directus-next/src/server.mjs +0 -9
- package/directus-node/src/lib/redirection/fetchRedirectsData.js +0 -92
- package/directus-node/src/lib/redirection/fetchRedirectsData.mjs +0 -92
- package/directus-node/src/lib/redirection/utils/validateRedirects.js +0 -22
- package/directus-node/src/lib/redirection/utils/validateRedirects.mjs +0 -22
|
@@ -1,141 +0,0 @@
|
|
|
1
|
-
import { draftMode } from "next/headers";
|
|
2
|
-
import { redirect } from "next/navigation";
|
|
3
|
-
import { template } from "radashi";
|
|
4
|
-
import { getJsonErrorResponse } from "../response.mjs";
|
|
5
|
-
import { getDraftSecretDefault } from "./env.mjs";
|
|
6
|
-
function parseDraftParams(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 } = parseDraftParams(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
|
-
parseDraftParams
|
|
141
|
-
};
|
|
@@ -1,24 +0,0 @@
|
|
|
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 env$1 = require("./redirect/env.js");
|
|
6
|
-
const route$1 = require("./redirect/route.js");
|
|
7
|
-
const getRedirectsRoute = require("./redirect/utils/getRedirectsRoute.js");
|
|
8
|
-
const handleRedirect = require("./redirect/utils/handleRedirect.js");
|
|
9
|
-
const logger = require("./logger.js");
|
|
10
|
-
const directusRouteRouter = require("./lib/directusRouteRouter.js");
|
|
11
|
-
const response = require("./response.js");
|
|
12
|
-
exports.getPathFromRoute = route.getPathFromRoute;
|
|
13
|
-
exports.handleDraftRoute = route.default;
|
|
14
|
-
exports.parseDraftParams = route.parseDraftParams;
|
|
15
|
-
exports.getDraftSecretDefault = env.getDraftSecretDefault;
|
|
16
|
-
exports.getApiRouteUrlDefault = env$1.getApiRouteUrlDefault;
|
|
17
|
-
exports.getRedirectSecretDefault = env$1.getRedirectSecretDefault;
|
|
18
|
-
exports.handleRedirectsRoute = route$1.default;
|
|
19
|
-
exports.parseRedirectParams = route$1.parseRedirectParams;
|
|
20
|
-
exports.getRedirectsRoute = getRedirectsRoute.getRedirectsRoute;
|
|
21
|
-
exports.handleRedirect = handleRedirect.handleRedirect;
|
|
22
|
-
exports.DirectusNextLogger = logger.logger;
|
|
23
|
-
exports.directusRouteRouter = directusRouteRouter.directusRouteRouter;
|
|
24
|
-
exports.getJsonErrorResponse = response.getJsonErrorResponse;
|
|
@@ -1,24 +0,0 @@
|
|
|
1
|
-
import { getPathFromRoute, default as default2, parseDraftParams } from "./draft/route.mjs";
|
|
2
|
-
import { getDraftSecretDefault } from "./draft/env.mjs";
|
|
3
|
-
import { getApiRouteUrlDefault, getRedirectSecretDefault } from "./redirect/env.mjs";
|
|
4
|
-
import { default as default3, parseRedirectParams } from "./redirect/route.mjs";
|
|
5
|
-
import { getRedirectsRoute } from "./redirect/utils/getRedirectsRoute.mjs";
|
|
6
|
-
import { handleRedirect } from "./redirect/utils/handleRedirect.mjs";
|
|
7
|
-
import { logger } from "./logger.mjs";
|
|
8
|
-
import { directusRouteRouter } from "./lib/directusRouteRouter.mjs";
|
|
9
|
-
import { getJsonErrorResponse } from "./response.mjs";
|
|
10
|
-
export {
|
|
11
|
-
logger as DirectusNextLogger,
|
|
12
|
-
directusRouteRouter,
|
|
13
|
-
getApiRouteUrlDefault,
|
|
14
|
-
getDraftSecretDefault,
|
|
15
|
-
getJsonErrorResponse,
|
|
16
|
-
getPathFromRoute,
|
|
17
|
-
getRedirectSecretDefault,
|
|
18
|
-
getRedirectsRoute,
|
|
19
|
-
default2 as handleDraftRoute,
|
|
20
|
-
handleRedirect,
|
|
21
|
-
default3 as handleRedirectsRoute,
|
|
22
|
-
parseDraftParams,
|
|
23
|
-
parseRedirectParams
|
|
24
|
-
};
|
|
@@ -1,102 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
3
|
-
const server = require("next/server");
|
|
4
|
-
const logger = require("../logger.js");
|
|
5
|
-
const handleRedirect = require("../redirect/utils/handleRedirect.js");
|
|
6
|
-
async function fetchPageSettingsTranslation(path) {
|
|
7
|
-
const graphqlEndpoint = process.env.NEXT_SERVER_GRAPHQL_URL || process.env.NEXT_PUBLIC_GRAPHQL_URL;
|
|
8
|
-
const graphqlApiKey = process.env.NEXT_PUBLIC_API_TOKEN;
|
|
9
|
-
if (!graphqlEndpoint) {
|
|
10
|
-
throw new Error("Missing GraphQL configuration `graphqlEndpoint`");
|
|
11
|
-
}
|
|
12
|
-
if (!graphqlApiKey) {
|
|
13
|
-
throw new Error("Missing GraphQL configuration `graphqlApiKey`");
|
|
14
|
-
}
|
|
15
|
-
const query = `
|
|
16
|
-
query Languages_code($filter: page_settings_translations_filter) {
|
|
17
|
-
page_settings_translations(filter: $filter) {
|
|
18
|
-
languages_code {
|
|
19
|
-
code
|
|
20
|
-
}
|
|
21
|
-
id
|
|
22
|
-
page_settings_id {
|
|
23
|
-
belongs_to_collection
|
|
24
|
-
belongs_to_key
|
|
25
|
-
}
|
|
26
|
-
title
|
|
27
|
-
slug
|
|
28
|
-
path
|
|
29
|
-
}
|
|
30
|
-
}
|
|
31
|
-
`;
|
|
32
|
-
const variables = {
|
|
33
|
-
filter: {
|
|
34
|
-
path: { _eq: path },
|
|
35
|
-
_and: [{ page_settings_id: { belongs_to_key: { _nempty: true } } }]
|
|
36
|
-
}
|
|
37
|
-
};
|
|
38
|
-
try {
|
|
39
|
-
logger.log("Executing GraphQL query:", query);
|
|
40
|
-
logger.log("Query variables:", variables);
|
|
41
|
-
const response = await fetch(graphqlEndpoint, {
|
|
42
|
-
method: "POST",
|
|
43
|
-
headers: {
|
|
44
|
-
"Content-Type": "application/json",
|
|
45
|
-
Authorization: `Bearer ${graphqlApiKey}`
|
|
46
|
-
},
|
|
47
|
-
body: JSON.stringify({ query, variables })
|
|
48
|
-
});
|
|
49
|
-
const { data } = await response.json();
|
|
50
|
-
logger.log("GraphQL response:", data);
|
|
51
|
-
return data.page_settings_translations;
|
|
52
|
-
} catch (error) {
|
|
53
|
-
logger.log("GraphQL Error:", error);
|
|
54
|
-
return null;
|
|
55
|
-
}
|
|
56
|
-
}
|
|
57
|
-
function removeLocaleFromPathname(pathname, config) {
|
|
58
|
-
const currentLocale = Object.values(config.localeMap ?? {}).find((locale) => pathname.startsWith(`/${locale}/`));
|
|
59
|
-
return { locale: currentLocale, pathname: currentLocale ? pathname.replace(`/${currentLocale}/`, "/") : pathname };
|
|
60
|
-
}
|
|
61
|
-
async function directusRouteRouter(request, config, NextResponse = server.NextResponse) {
|
|
62
|
-
var _a, _b, _c;
|
|
63
|
-
const { pathname: localizedPathname } = request.nextUrl;
|
|
64
|
-
const { locale, pathname } = removeLocaleFromPathname(localizedPathname, config);
|
|
65
|
-
logger.log("Processing request for pathname:", { locale, pathname });
|
|
66
|
-
const redirect = await handleRedirect.handleRedirect(request, (_a = config.modules) == null ? void 0 : _a.redirects);
|
|
67
|
-
if (redirect) {
|
|
68
|
-
return redirect;
|
|
69
|
-
}
|
|
70
|
-
const translations = await fetchPageSettingsTranslation(pathname);
|
|
71
|
-
if (!translations || translations.length === 0) {
|
|
72
|
-
logger.log("No translation found for path:", pathname);
|
|
73
|
-
return NextResponse.next();
|
|
74
|
-
}
|
|
75
|
-
const translation = translations[0];
|
|
76
|
-
logger.log("Using translation:", translation);
|
|
77
|
-
if (!translation.languages_code || !translation.page_settings_id) {
|
|
78
|
-
logger.log(`Invalid translation data for path: ${pathname}`, { pathname }, "warn");
|
|
79
|
-
return NextResponse.next();
|
|
80
|
-
}
|
|
81
|
-
const directusLocale = translation.languages_code.code;
|
|
82
|
-
const collection = translation.page_settings_id.belongs_to_collection;
|
|
83
|
-
const id = translation.page_settings_id.belongs_to_key;
|
|
84
|
-
if (!collection) {
|
|
85
|
-
logger.log(`PageSettings with id ${id} was found but is not associated with any collection.`, { id }, "warn");
|
|
86
|
-
return NextResponse.next();
|
|
87
|
-
}
|
|
88
|
-
const mappedLocale = ((_b = config.localeMap) == null ? void 0 : _b[directusLocale]) || directusLocale;
|
|
89
|
-
const idField = ((_c = config.collectionSettings[collection]) == null ? void 0 : _c.idField) || config.collectionSettings.default.idField;
|
|
90
|
-
logger.log("Directus locale:", directusLocale);
|
|
91
|
-
logger.log("Mapped locale:", mappedLocale);
|
|
92
|
-
logger.log("Collection:", collection);
|
|
93
|
-
logger.log("ID Field:", idField);
|
|
94
|
-
logger.log("ID:", id);
|
|
95
|
-
const newPath = `/${mappedLocale}/${collection}/${id}`;
|
|
96
|
-
logger.log(`Rewriting path: ${pathname} -> ${newPath}`);
|
|
97
|
-
const url = request.nextUrl.clone();
|
|
98
|
-
url.pathname = newPath;
|
|
99
|
-
logger.log("Rewriting to URL:", url.toString());
|
|
100
|
-
return NextResponse.rewrite(url);
|
|
101
|
-
}
|
|
102
|
-
exports.directusRouteRouter = directusRouteRouter;
|
|
@@ -1,11 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
3
|
-
const logger$1 = require("@okam/logger");
|
|
4
|
-
const logger = logger$1.createLogger("[DirectusNext]");
|
|
5
|
-
function log(msg, context, severity = "log") {
|
|
6
|
-
if (process.env.NODE_ENV === "development") {
|
|
7
|
-
logger.log(msg, severity, { context });
|
|
8
|
-
}
|
|
9
|
-
}
|
|
10
|
-
exports.log = log;
|
|
11
|
-
exports.logger = logger;
|
|
@@ -1,11 +0,0 @@
|
|
|
1
|
-
import { createLogger } from "@okam/logger";
|
|
2
|
-
const logger = createLogger("[DirectusNext]");
|
|
3
|
-
function log(msg, context, severity = "log") {
|
|
4
|
-
if (process.env.NODE_ENV === "development") {
|
|
5
|
-
logger.log(msg, severity, { context });
|
|
6
|
-
}
|
|
7
|
-
}
|
|
8
|
-
export {
|
|
9
|
-
log,
|
|
10
|
-
logger
|
|
11
|
-
};
|
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
3
|
-
require("server-only");
|
|
4
|
-
const createServerContext = require("server-only-context");
|
|
5
|
-
function pageSettingsContext(defaultValue) {
|
|
6
|
-
const [pageSettings, setPageSettings] = createServerContext(defaultValue);
|
|
7
|
-
return [pageSettings, setPageSettings];
|
|
8
|
-
}
|
|
9
|
-
function pageSettingsVariablesContext(variables) {
|
|
10
|
-
const [pageSettingsVariables, setPageSettingsVariables] = createServerContext(variables);
|
|
11
|
-
return [pageSettingsVariables, setPageSettingsVariables];
|
|
12
|
-
}
|
|
13
|
-
exports.pageSettingsContext = pageSettingsContext;
|
|
14
|
-
exports.pageSettingsVariablesContext = pageSettingsVariablesContext;
|
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
import "server-only";
|
|
2
|
-
import createServerContext from "server-only-context";
|
|
3
|
-
function pageSettingsContext(defaultValue) {
|
|
4
|
-
const [pageSettings, setPageSettings] = createServerContext(defaultValue);
|
|
5
|
-
return [pageSettings, setPageSettings];
|
|
6
|
-
}
|
|
7
|
-
function pageSettingsVariablesContext(variables) {
|
|
8
|
-
const [pageSettingsVariables, setPageSettingsVariables] = createServerContext(variables);
|
|
9
|
-
return [pageSettingsVariables, setPageSettingsVariables];
|
|
10
|
-
}
|
|
11
|
-
export {
|
|
12
|
-
pageSettingsContext,
|
|
13
|
-
pageSettingsVariablesContext
|
|
14
|
-
};
|
|
@@ -1,20 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
3
|
-
const logger = require("../logger.js");
|
|
4
|
-
const defaultInternalUrl = "http://localhost:3000";
|
|
5
|
-
function getRedirectSecretDefault() {
|
|
6
|
-
return process.env.NEXT_API_REDIRECT_SECRET || "";
|
|
7
|
-
}
|
|
8
|
-
function getVercelUrl() {
|
|
9
|
-
const url = process.env.VERCEL_URL;
|
|
10
|
-
if (!url) return null;
|
|
11
|
-
return `https://${url}`;
|
|
12
|
-
}
|
|
13
|
-
function getApiRouteUrlDefault() {
|
|
14
|
-
const url = process.env.NEXT_MIDDLEWARE_REDIRECT_URL ?? getVercelUrl() ?? defaultInternalUrl;
|
|
15
|
-
if (URL.canParse(url)) return url;
|
|
16
|
-
logger.log(`Invalid URL ${url}. Falling back to default`, { url }, "warn");
|
|
17
|
-
return defaultInternalUrl;
|
|
18
|
-
}
|
|
19
|
-
exports.getApiRouteUrlDefault = getApiRouteUrlDefault;
|
|
20
|
-
exports.getRedirectSecretDefault = getRedirectSecretDefault;
|
|
@@ -1,20 +0,0 @@
|
|
|
1
|
-
import { log } from "../logger.mjs";
|
|
2
|
-
const defaultInternalUrl = "http://localhost:3000";
|
|
3
|
-
function getRedirectSecretDefault() {
|
|
4
|
-
return process.env.NEXT_API_REDIRECT_SECRET || "";
|
|
5
|
-
}
|
|
6
|
-
function getVercelUrl() {
|
|
7
|
-
const url = process.env.VERCEL_URL;
|
|
8
|
-
if (!url) return null;
|
|
9
|
-
return `https://${url}`;
|
|
10
|
-
}
|
|
11
|
-
function getApiRouteUrlDefault() {
|
|
12
|
-
const url = process.env.NEXT_MIDDLEWARE_REDIRECT_URL ?? getVercelUrl() ?? defaultInternalUrl;
|
|
13
|
-
if (URL.canParse(url)) return url;
|
|
14
|
-
log(`Invalid URL ${url}. Falling back to default`, { url }, "warn");
|
|
15
|
-
return defaultInternalUrl;
|
|
16
|
-
}
|
|
17
|
-
export {
|
|
18
|
-
getApiRouteUrlDefault,
|
|
19
|
-
getRedirectSecretDefault
|
|
20
|
-
};
|
|
@@ -1,31 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperties(exports, { __esModule: { value: true }, [Symbol.toStringTag]: { value: "Module" } });
|
|
3
|
-
const fetchRedirectsData = require("../../../directus-node/src/lib/redirection/fetchRedirectsData.js");
|
|
4
|
-
require("@okam/core-lib");
|
|
5
|
-
const response = require("../response.js");
|
|
6
|
-
const env = require("./env.js");
|
|
7
|
-
function parseRedirectParams(url) {
|
|
8
|
-
const { searchParams } = new URL(url);
|
|
9
|
-
const secret = searchParams.get("secret") || "";
|
|
10
|
-
return { secret };
|
|
11
|
-
}
|
|
12
|
-
async function handleRedirectsRoute({
|
|
13
|
-
url,
|
|
14
|
-
getRedirectSecret = env.getRedirectSecretDefault,
|
|
15
|
-
getJsonError = response.getJsonErrorResponse,
|
|
16
|
-
getDirectusApiToken,
|
|
17
|
-
getDirectusGraphqlUrl,
|
|
18
|
-
limit,
|
|
19
|
-
init
|
|
20
|
-
}) {
|
|
21
|
-
const { secret } = parseRedirectParams(url);
|
|
22
|
-
if (secret !== getRedirectSecret()) {
|
|
23
|
-
return getJsonError({ error: "Invalid argument" }, 401);
|
|
24
|
-
}
|
|
25
|
-
const graphqlEndpoint = getDirectusGraphqlUrl == null ? void 0 : getDirectusGraphqlUrl();
|
|
26
|
-
const graphqlApiKey = getDirectusApiToken == null ? void 0 : getDirectusApiToken();
|
|
27
|
-
const { redirects, rewrites } = await fetchRedirectsData.fetchRedirectsData({ graphqlEndpoint, graphqlApiKey, limit }, init);
|
|
28
|
-
return new Response(JSON.stringify({ redirects, rewrites }), { status: 200 });
|
|
29
|
-
}
|
|
30
|
-
exports.default = handleRedirectsRoute;
|
|
31
|
-
exports.parseRedirectParams = parseRedirectParams;
|
|
@@ -1,31 +0,0 @@
|
|
|
1
|
-
import { fetchRedirectsData } from "../../../directus-node/src/lib/redirection/fetchRedirectsData.mjs";
|
|
2
|
-
import "@okam/core-lib";
|
|
3
|
-
import { getJsonErrorResponse } from "../response.mjs";
|
|
4
|
-
import { getRedirectSecretDefault } from "./env.mjs";
|
|
5
|
-
function parseRedirectParams(url) {
|
|
6
|
-
const { searchParams } = new URL(url);
|
|
7
|
-
const secret = searchParams.get("secret") || "";
|
|
8
|
-
return { secret };
|
|
9
|
-
}
|
|
10
|
-
async function handleRedirectsRoute({
|
|
11
|
-
url,
|
|
12
|
-
getRedirectSecret = getRedirectSecretDefault,
|
|
13
|
-
getJsonError = getJsonErrorResponse,
|
|
14
|
-
getDirectusApiToken,
|
|
15
|
-
getDirectusGraphqlUrl,
|
|
16
|
-
limit,
|
|
17
|
-
init
|
|
18
|
-
}) {
|
|
19
|
-
const { secret } = parseRedirectParams(url);
|
|
20
|
-
if (secret !== getRedirectSecret()) {
|
|
21
|
-
return getJsonError({ error: "Invalid argument" }, 401);
|
|
22
|
-
}
|
|
23
|
-
const graphqlEndpoint = getDirectusGraphqlUrl == null ? void 0 : getDirectusGraphqlUrl();
|
|
24
|
-
const graphqlApiKey = getDirectusApiToken == null ? void 0 : getDirectusApiToken();
|
|
25
|
-
const { redirects, rewrites } = await fetchRedirectsData({ graphqlEndpoint, graphqlApiKey, limit }, init);
|
|
26
|
-
return new Response(JSON.stringify({ redirects, rewrites }), { status: 200 });
|
|
27
|
-
}
|
|
28
|
-
export {
|
|
29
|
-
handleRedirectsRoute as default,
|
|
30
|
-
parseRedirectParams
|
|
31
|
-
};
|
|
@@ -1,27 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
3
|
-
const logger = require("../../logger.js");
|
|
4
|
-
const env = require("../env.js");
|
|
5
|
-
const defaultApiRoute = "/api/redirect";
|
|
6
|
-
async function getRedirectsRoute({
|
|
7
|
-
apiRoute = defaultApiRoute,
|
|
8
|
-
getApiRouteUrl = env.getApiRouteUrlDefault,
|
|
9
|
-
getRedirectSecret = env.getRedirectSecretDefault
|
|
10
|
-
} = {}) {
|
|
11
|
-
const secret = getRedirectSecret();
|
|
12
|
-
try {
|
|
13
|
-
const url = new URL(apiRoute, getApiRouteUrl());
|
|
14
|
-
url.searchParams.set("secret", encodeURIComponent(secret));
|
|
15
|
-
const response = await fetch(url);
|
|
16
|
-
if (!response.ok) {
|
|
17
|
-
logger.log(`${apiRoute} not ok. Returned`, { status: response.status }, "error");
|
|
18
|
-
return { redirects: [], rewrites: [] };
|
|
19
|
-
}
|
|
20
|
-
const data = await response.json();
|
|
21
|
-
return data;
|
|
22
|
-
} catch (error) {
|
|
23
|
-
logger.log(`Error fetching redirects from ${apiRoute}.`, { error }, "error");
|
|
24
|
-
return { redirects: [], rewrites: [] };
|
|
25
|
-
}
|
|
26
|
-
}
|
|
27
|
-
exports.getRedirectsRoute = getRedirectsRoute;
|
|
@@ -1,27 +0,0 @@
|
|
|
1
|
-
import { log } from "../../logger.mjs";
|
|
2
|
-
import { getApiRouteUrlDefault, getRedirectSecretDefault } from "../env.mjs";
|
|
3
|
-
const defaultApiRoute = "/api/redirect";
|
|
4
|
-
async function getRedirectsRoute({
|
|
5
|
-
apiRoute = defaultApiRoute,
|
|
6
|
-
getApiRouteUrl = getApiRouteUrlDefault,
|
|
7
|
-
getRedirectSecret = getRedirectSecretDefault
|
|
8
|
-
} = {}) {
|
|
9
|
-
const secret = getRedirectSecret();
|
|
10
|
-
try {
|
|
11
|
-
const url = new URL(apiRoute, getApiRouteUrl());
|
|
12
|
-
url.searchParams.set("secret", encodeURIComponent(secret));
|
|
13
|
-
const response = await fetch(url);
|
|
14
|
-
if (!response.ok) {
|
|
15
|
-
log(`${apiRoute} not ok. Returned`, { status: response.status }, "error");
|
|
16
|
-
return { redirects: [], rewrites: [] };
|
|
17
|
-
}
|
|
18
|
-
const data = await response.json();
|
|
19
|
-
return data;
|
|
20
|
-
} catch (error) {
|
|
21
|
-
log(`Error fetching redirects from ${apiRoute}.`, { error }, "error");
|
|
22
|
-
return { redirects: [], rewrites: [] };
|
|
23
|
-
}
|
|
24
|
-
}
|
|
25
|
-
export {
|
|
26
|
-
getRedirectsRoute
|
|
27
|
-
};
|
|
@@ -1,48 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
3
|
-
const coreLib = require("@okam/core-lib");
|
|
4
|
-
const server = require("next/server");
|
|
5
|
-
const radashi = require("radashi");
|
|
6
|
-
const logger = require("../../logger.js");
|
|
7
|
-
const getRedirectsRoute = require("./getRedirectsRoute.js");
|
|
8
|
-
function splitDestination(destination) {
|
|
9
|
-
const [pathname, search] = destination.split("?");
|
|
10
|
-
if (!search) {
|
|
11
|
-
return [pathname];
|
|
12
|
-
}
|
|
13
|
-
return [pathname, `?${search}`];
|
|
14
|
-
}
|
|
15
|
-
function validateExternalRedirect(redirect) {
|
|
16
|
-
if (!URL.canParse(redirect.destination)) {
|
|
17
|
-
return null;
|
|
18
|
-
}
|
|
19
|
-
return redirect.destination;
|
|
20
|
-
}
|
|
21
|
-
async function handleRedirect(request, options = {}) {
|
|
22
|
-
const url = request.nextUrl.clone();
|
|
23
|
-
const { pathname } = request.nextUrl;
|
|
24
|
-
const normalizedPathname = coreLib.normalizePath(pathname);
|
|
25
|
-
const { redirects, rewrites } = await getRedirectsRoute.getRedirectsRoute(options);
|
|
26
|
-
const redirect = redirects.find(({ source }) => source === normalizedPathname);
|
|
27
|
-
const rewrite = rewrites.find(({ source }) => source === normalizedPathname);
|
|
28
|
-
const type = redirect ? "redirect" : "rewrite";
|
|
29
|
-
const reroute = redirect ?? rewrite;
|
|
30
|
-
if (!reroute) {
|
|
31
|
-
return null;
|
|
32
|
-
}
|
|
33
|
-
const { permanent } = reroute;
|
|
34
|
-
const status = permanent ? 308 : 307;
|
|
35
|
-
const externalRedirect = validateExternalRedirect(reroute);
|
|
36
|
-
if (externalRedirect) {
|
|
37
|
-
logger.log(`External ${type} found`, { [type]: externalRedirect, permanent });
|
|
38
|
-
return server.NextResponse.redirect(externalRedirect, { status });
|
|
39
|
-
}
|
|
40
|
-
logger.log(`${radashi.capitalize(type)} found`, { [type]: reroute, permanent });
|
|
41
|
-
const { destination } = reroute;
|
|
42
|
-
const [destinationPathname, search] = splitDestination(destination);
|
|
43
|
-
if (search) url.search = search;
|
|
44
|
-
url.pathname = destinationPathname;
|
|
45
|
-
logger.log(`${radashi.capitalize(type)}ing to ${url.toString()} with status ${status}`);
|
|
46
|
-
return server.NextResponse[type](url, { status });
|
|
47
|
-
}
|
|
48
|
-
exports.handleRedirect = handleRedirect;
|
|
@@ -1,48 +0,0 @@
|
|
|
1
|
-
import { normalizePath } from "@okam/core-lib";
|
|
2
|
-
import { NextResponse } from "next/server";
|
|
3
|
-
import { capitalize } from "radashi";
|
|
4
|
-
import { log } from "../../logger.mjs";
|
|
5
|
-
import { getRedirectsRoute } from "./getRedirectsRoute.mjs";
|
|
6
|
-
function splitDestination(destination) {
|
|
7
|
-
const [pathname, search] = destination.split("?");
|
|
8
|
-
if (!search) {
|
|
9
|
-
return [pathname];
|
|
10
|
-
}
|
|
11
|
-
return [pathname, `?${search}`];
|
|
12
|
-
}
|
|
13
|
-
function validateExternalRedirect(redirect) {
|
|
14
|
-
if (!URL.canParse(redirect.destination)) {
|
|
15
|
-
return null;
|
|
16
|
-
}
|
|
17
|
-
return redirect.destination;
|
|
18
|
-
}
|
|
19
|
-
async function handleRedirect(request, options = {}) {
|
|
20
|
-
const url = request.nextUrl.clone();
|
|
21
|
-
const { pathname } = request.nextUrl;
|
|
22
|
-
const normalizedPathname = normalizePath(pathname);
|
|
23
|
-
const { redirects, rewrites } = await getRedirectsRoute(options);
|
|
24
|
-
const redirect = redirects.find(({ source }) => source === normalizedPathname);
|
|
25
|
-
const rewrite = rewrites.find(({ source }) => source === normalizedPathname);
|
|
26
|
-
const type = redirect ? "redirect" : "rewrite";
|
|
27
|
-
const reroute = redirect ?? rewrite;
|
|
28
|
-
if (!reroute) {
|
|
29
|
-
return null;
|
|
30
|
-
}
|
|
31
|
-
const { permanent } = reroute;
|
|
32
|
-
const status = permanent ? 308 : 307;
|
|
33
|
-
const externalRedirect = validateExternalRedirect(reroute);
|
|
34
|
-
if (externalRedirect) {
|
|
35
|
-
log(`External ${type} found`, { [type]: externalRedirect, permanent });
|
|
36
|
-
return NextResponse.redirect(externalRedirect, { status });
|
|
37
|
-
}
|
|
38
|
-
log(`${capitalize(type)} found`, { [type]: reroute, permanent });
|
|
39
|
-
const { destination } = reroute;
|
|
40
|
-
const [destinationPathname, search] = splitDestination(destination);
|
|
41
|
-
if (search) url.search = search;
|
|
42
|
-
url.pathname = destinationPathname;
|
|
43
|
-
log(`${capitalize(type)}ing to ${url.toString()} with status ${status}`);
|
|
44
|
-
return NextResponse[type](url, { status });
|
|
45
|
-
}
|
|
46
|
-
export {
|
|
47
|
-
handleRedirect
|
|
48
|
-
};
|
|
@@ -1,14 +0,0 @@
|
|
|
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;
|
|
@@ -1,14 +0,0 @@
|
|
|
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
|
-
};
|
|
@@ -1,9 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
3
|
-
const directusRouteRouter = require("./lib/directusRouteRouter.js");
|
|
4
|
-
const context = require("./pageSettings/context.js");
|
|
5
|
-
const usePageSettings = require("./pageSettings/usePageSettings.js");
|
|
6
|
-
exports.directusRouteRouter = directusRouteRouter.directusRouteRouter;
|
|
7
|
-
exports.pageSettingsContext = context.pageSettingsContext;
|
|
8
|
-
exports.pageSettingsVariablesContext = context.pageSettingsVariablesContext;
|
|
9
|
-
exports.usePageSettings = usePageSettings.usePageSettings;
|
|
@@ -1,9 +0,0 @@
|
|
|
1
|
-
import { directusRouteRouter } from "./lib/directusRouteRouter.mjs";
|
|
2
|
-
import { pageSettingsContext, pageSettingsVariablesContext } from "./pageSettings/context.mjs";
|
|
3
|
-
import { usePageSettings } from "./pageSettings/usePageSettings.mjs";
|
|
4
|
-
export {
|
|
5
|
-
directusRouteRouter,
|
|
6
|
-
pageSettingsContext,
|
|
7
|
-
pageSettingsVariablesContext,
|
|
8
|
-
usePageSettings
|
|
9
|
-
};
|