@okam/directus-node 0.5.0 → 0.6.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 +18 -0
- package/package.json +3 -2
- package/src/edge.d.ts +6 -0
- package/src/edge.js +13 -0
- package/src/edge.js.map +1 -0
- package/src/lib/redirection/fetchRedirectsData.d.ts +8 -0
- package/src/lib/redirection/fetchRedirectsData.js +85 -0
- package/src/lib/redirection/fetchRedirectsData.js.map +1 -0
- package/src/lib/redirection/index.d.ts +16 -0
- package/src/lib/redirection/index.js +22 -0
- package/src/lib/redirection/index.js.map +1 -0
- package/src/lib/redirection/interface.d.ts +18 -0
- package/src/lib/redirection/interface.js +3 -0
- package/src/lib/redirection/interface.js.map +1 -0
- package/src/lib/{redirection.d.ts → redirection/redirectsFile.d.ts} +1 -26
- package/src/lib/{redirection.js → redirection/redirectsFile.js} +6 -80
- package/src/lib/redirection/redirectsFile.js.map +1 -0
- package/src/lib/redirection/utils/validateRedirects.d.ts +3 -0
- package/src/lib/redirection/utils/validateRedirects.js +22 -0
- package/src/lib/redirection/utils/validateRedirects.js.map +1 -0
- package/src/lib/redirection.js.map +0 -1
package/CHANGELOG.md
CHANGED
|
@@ -1,3 +1,21 @@
|
|
|
1
|
+
## 0.6.0 (2025-07-22)
|
|
2
|
+
|
|
3
|
+
### 🚀 Features
|
|
4
|
+
|
|
5
|
+
- **directus-next:** /api/redirect route handler to fetch + cache directus redirects ([c21b953](https://github.com/OKAMca/stack/commit/c21b953))
|
|
6
|
+
|
|
7
|
+
### 🩹 Fixes
|
|
8
|
+
|
|
9
|
+
- **directus-node:** export edge-compatible code from separate file ([9f77744](https://github.com/OKAMca/stack/commit/9f77744))
|
|
10
|
+
- package deps error ([b665a45](https://github.com/OKAMca/stack/commit/b665a45))
|
|
11
|
+
- search field icon ([0850fde](https://github.com/OKAMca/stack/commit/0850fde))
|
|
12
|
+
|
|
13
|
+
### ❤️ Thank You
|
|
14
|
+
|
|
15
|
+
- Marie-Maxime Tanguay
|
|
16
|
+
- Pierre-Olivier Clerson @poclerson
|
|
17
|
+
- poclerson
|
|
18
|
+
|
|
1
19
|
## 0.5.0 (2025-01-30)
|
|
2
20
|
|
|
3
21
|
### 🚀 Features
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@okam/directus-node",
|
|
3
3
|
"main": "./src/index.js",
|
|
4
|
-
"version": "0.
|
|
4
|
+
"version": "0.6.0",
|
|
5
5
|
"types": "./src/index.d.ts",
|
|
6
6
|
"publishConfig": {
|
|
7
7
|
"access": "public"
|
|
@@ -12,7 +12,8 @@
|
|
|
12
12
|
"dependencies": {
|
|
13
13
|
"@graphql-codegen/cli": "^5.0.3",
|
|
14
14
|
"@okam/logger": "1.1.0",
|
|
15
|
-
"tslib": "^2.8.1"
|
|
15
|
+
"tslib": "^2.8.1",
|
|
16
|
+
"@okam/core-lib": "1.16.0"
|
|
16
17
|
},
|
|
17
18
|
"type": "commonjs"
|
|
18
19
|
}
|
package/src/edge.d.ts
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @fileoverview Export file for functions that are used in edge functions (middleware-compatible)
|
|
3
|
+
*/
|
|
4
|
+
export type { TFetchRedirectsConfig, TFetchRedirectsResponse, TRedirectData, TRedirectType, } from './lib/redirection/interface';
|
|
5
|
+
export { fetchRedirectsData, getDefaultConfig } from './lib/redirection/fetchRedirectsData';
|
|
6
|
+
export { isRedirect, normalizeRedirects } from './lib/redirection/utils/validateRedirects';
|
package/src/edge.js
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* @fileoverview Export file for functions that are used in edge functions (middleware-compatible)
|
|
4
|
+
*/
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.normalizeRedirects = exports.isRedirect = exports.getDefaultConfig = exports.fetchRedirectsData = void 0;
|
|
7
|
+
var fetchRedirectsData_1 = require("./lib/redirection/fetchRedirectsData");
|
|
8
|
+
Object.defineProperty(exports, "fetchRedirectsData", { enumerable: true, get: function () { return fetchRedirectsData_1.fetchRedirectsData; } });
|
|
9
|
+
Object.defineProperty(exports, "getDefaultConfig", { enumerable: true, get: function () { return fetchRedirectsData_1.getDefaultConfig; } });
|
|
10
|
+
var validateRedirects_1 = require("./lib/redirection/utils/validateRedirects");
|
|
11
|
+
Object.defineProperty(exports, "isRedirect", { enumerable: true, get: function () { return validateRedirects_1.isRedirect; } });
|
|
12
|
+
Object.defineProperty(exports, "normalizeRedirects", { enumerable: true, get: function () { return validateRedirects_1.normalizeRedirects; } });
|
|
13
|
+
//# sourceMappingURL=edge.js.map
|
package/src/edge.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"edge.js","sourceRoot":"","sources":["../../../../../libs/directus/directus-node/src/edge.ts"],"names":[],"mappings":";AAAA;;GAEG;;;AASH,2EAA2F;AAAlF,wHAAA,kBAAkB,OAAA;AAAE,sHAAA,gBAAgB,OAAA;AAC7C,+EAA0F;AAAjF,+GAAA,UAAU,OAAA;AAAE,uHAAA,kBAAkB,OAAA"}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import type { TFetchRedirectsConfig, TFetchRedirectsResponse } from './interface';
|
|
2
|
+
export declare const redirectDefaultLimit = 2000;
|
|
3
|
+
/**
|
|
4
|
+
* Get Fetch Redirects Configuration
|
|
5
|
+
* @returns {object}
|
|
6
|
+
*/
|
|
7
|
+
export declare function getDefaultConfig(): TFetchRedirectsConfig;
|
|
8
|
+
export declare function fetchRedirectsData(config: TFetchRedirectsConfig, init?: Omit<RequestInit, 'body' | 'method' | 'headers'>): Promise<TFetchRedirectsResponse>;
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.redirectDefaultLimit = void 0;
|
|
4
|
+
exports.getDefaultConfig = getDefaultConfig;
|
|
5
|
+
exports.fetchRedirectsData = fetchRedirectsData;
|
|
6
|
+
const tslib_1 = require("tslib");
|
|
7
|
+
const logger_1 = require("@okam/logger");
|
|
8
|
+
const validateRedirects_1 = require("./utils/validateRedirects");
|
|
9
|
+
exports.redirectDefaultLimit = 2000;
|
|
10
|
+
/**
|
|
11
|
+
* Get Fetch Redirects Configuration
|
|
12
|
+
* @returns {object}
|
|
13
|
+
*/
|
|
14
|
+
function getDefaultConfig() {
|
|
15
|
+
return {
|
|
16
|
+
graphqlEndpoint: process.env['NEXT_REDIRECT_GRAPHQL_URL'] || process.env['NEXT_PUBLIC_GRAPHQL_URL'] || '',
|
|
17
|
+
graphqlApiKey: process.env['NEXT_API_TOKEN_ADMIN'] || '',
|
|
18
|
+
redirectsFilename: './redirect/redirects.json',
|
|
19
|
+
rewritesFilename: './redirect/rewrites.json',
|
|
20
|
+
limit: exports.redirectDefaultLimit,
|
|
21
|
+
};
|
|
22
|
+
}
|
|
23
|
+
function fetchRedirectsData(config, init) {
|
|
24
|
+
return tslib_1.__awaiter(this, void 0, void 0, function* () {
|
|
25
|
+
const { graphqlApiKey: defaultGraphqlApiKey, graphqlEndpoint: defaultGraphqlEndpoint, limit: defaultLimit, } = getDefaultConfig();
|
|
26
|
+
const { graphqlEndpoint = defaultGraphqlEndpoint, graphqlApiKey = defaultGraphqlApiKey, limit = defaultLimit, } = config;
|
|
27
|
+
if (!graphqlEndpoint) {
|
|
28
|
+
throw new Error('Missing fetchRedirects configuration `graphqlEndpoint`. Check environment variables NEXT_REDIRECT_GRAPHQL_URL or NEXT_PUBLIC_GRAPHQL_URL');
|
|
29
|
+
}
|
|
30
|
+
if (!graphqlApiKey) {
|
|
31
|
+
throw new Error('Missing fetchRedirects configuration `graphqlApiKey`. Check environment variable NEXT_API_TOKEN_ADMIN');
|
|
32
|
+
}
|
|
33
|
+
const query = `query fetchRedirects($limit: Int = 2000) {
|
|
34
|
+
redirects(filter: {status:{_eq:"published"},isrewrite:{_eq:false}}, sort: "sort", limit: $limit) {
|
|
35
|
+
source
|
|
36
|
+
destination
|
|
37
|
+
permanent
|
|
38
|
+
locale
|
|
39
|
+
}
|
|
40
|
+
rewrites: redirects(filter: {status:{_eq:"published"},isrewrite:{_eq:true}}, sort: "sort", limit: $limit) {
|
|
41
|
+
source
|
|
42
|
+
destination
|
|
43
|
+
permanent
|
|
44
|
+
locale
|
|
45
|
+
}
|
|
46
|
+
}`;
|
|
47
|
+
const graphqlBody = {
|
|
48
|
+
query,
|
|
49
|
+
// "operationName": "",
|
|
50
|
+
variables: {
|
|
51
|
+
limit: Number(limit) || exports.redirectDefaultLimit,
|
|
52
|
+
},
|
|
53
|
+
};
|
|
54
|
+
try {
|
|
55
|
+
// console.info(`Fetching redirects on ${graphqlEndpoint}`)
|
|
56
|
+
const response = yield fetch(graphqlEndpoint, Object.assign(Object.assign({}, init), { method: 'POST', headers: {
|
|
57
|
+
// eslint-disable-next-line @typescript-eslint/naming-convention
|
|
58
|
+
'Content-Type': 'application/json',
|
|
59
|
+
Authorization: `Bearer ${graphqlApiKey}`,
|
|
60
|
+
}, body: JSON.stringify(graphqlBody) }));
|
|
61
|
+
const { data } = yield response.json();
|
|
62
|
+
const { redirects, rewrites } = data !== null && data !== void 0 ? data : {};
|
|
63
|
+
if (!(redirects === null || redirects === void 0 ? void 0 : redirects.length) && !(rewrites === null || rewrites === void 0 ? void 0 : rewrites.length)) {
|
|
64
|
+
logger_1.logger.log('No redirects/rewrites found', 'warn');
|
|
65
|
+
return {
|
|
66
|
+
redirects: [],
|
|
67
|
+
rewrites: [],
|
|
68
|
+
};
|
|
69
|
+
}
|
|
70
|
+
logger_1.logger.log(`Fetch redirects count: ${(redirects === null || redirects === void 0 ? void 0 : redirects.length) || 0}, rewrites count: ${(rewrites === null || rewrites === void 0 ? void 0 : rewrites.length) || 0}`);
|
|
71
|
+
return {
|
|
72
|
+
redirects: (0, validateRedirects_1.normalizeRedirects)(redirects),
|
|
73
|
+
rewrites: (0, validateRedirects_1.normalizeRedirects)(rewrites),
|
|
74
|
+
};
|
|
75
|
+
}
|
|
76
|
+
catch (e) {
|
|
77
|
+
logger_1.logger.log(`Error fetching redirects: ${e.message}`, 'error');
|
|
78
|
+
}
|
|
79
|
+
return {
|
|
80
|
+
redirects: [],
|
|
81
|
+
rewrites: [],
|
|
82
|
+
};
|
|
83
|
+
});
|
|
84
|
+
}
|
|
85
|
+
//# sourceMappingURL=fetchRedirectsData.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"fetchRedirectsData.js","sourceRoot":"","sources":["../../../../../../../libs/directus/directus-node/src/lib/redirection/fetchRedirectsData.ts"],"names":[],"mappings":";;;AAUA,4CAQC;AAED,gDAoFC;;AAxGD,yCAAqC;AAErC,iEAA8D;AAEjD,QAAA,oBAAoB,GAAG,IAAI,CAAA;AAExC;;;GAGG;AACH,SAAgB,gBAAgB;IAC9B,OAAO;QACL,eAAe,EAAE,OAAO,CAAC,GAAG,CAAC,2BAA2B,CAAC,IAAI,OAAO,CAAC,GAAG,CAAC,yBAAyB,CAAC,IAAI,EAAE;QACzG,aAAa,EAAE,OAAO,CAAC,GAAG,CAAC,sBAAsB,CAAC,IAAI,EAAE;QACxD,iBAAiB,EAAE,2BAA2B;QAC9C,gBAAgB,EAAE,0BAA0B;QAC5C,KAAK,EAAE,4BAAoB;KAC5B,CAAA;AACH,CAAC;AAED,SAAsB,kBAAkB,CACtC,MAA6B,EAC7B,IAAuD;;QAEvD,MAAM,EACJ,aAAa,EAAE,oBAAoB,EACnC,eAAe,EAAE,sBAAsB,EACvC,KAAK,EAAE,YAAY,GACpB,GAAG,gBAAgB,EAAE,CAAA;QACtB,MAAM,EACJ,eAAe,GAAG,sBAAsB,EACxC,aAAa,GAAG,oBAAoB,EACpC,KAAK,GAAG,YAAY,GACrB,GAAG,MAAM,CAAA;QAEV,IAAI,CAAC,eAAe,EAAE,CAAC;YAC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AR,QAAQ,uBAAR,QAAQ,CAAE,MAAM,KAAI,CAAC,EAAE,CAAC,CAAA;YACxG,OAAO;gBACL,SAAS,EAAE,IAAA,sCAAkB,EAAC,SAAS,CAAC;gBACxC,QAAQ,EAAE,IAAA,sCAAkB,EAAC,QAAQ,CAAC;aACvC,CAAA;QACH,CAAC;QAAC,OAAO,CAAC,EAAE,CAAC;YACX,eAAM,CAAC,GAAG,CAAC,6BAA8B,CAAW,CAAC,OAAO,EAAE,EAAE,OAAO,CAAC,CAAA;QAC1E,CAAC;QACD,OAAO;YACL,SAAS,EAAE,EAAE;YACb,QAAQ,EAAE,EAAE;SACb,CAAA;IACH,CAAC;CAAA"}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import * as fetchRedirectsDataFile from './fetchRedirectsData';
|
|
2
|
+
/**
|
|
3
|
+
* @deprecated Import from `@okam/directus-node/edge` instead
|
|
4
|
+
*/
|
|
5
|
+
export declare const fetchRedirectsData: typeof fetchRedirectsDataFile.fetchRedirectsData;
|
|
6
|
+
/**
|
|
7
|
+
* @deprecated Import from `@okam/directus-node/edge` instead
|
|
8
|
+
*/
|
|
9
|
+
export declare const getDefaultConfig: typeof fetchRedirectsDataFile.getDefaultConfig;
|
|
10
|
+
/**
|
|
11
|
+
* @deprecated Import from `@okam/directus-node/edge` instead
|
|
12
|
+
*/
|
|
13
|
+
export declare const redirectDefaultLimit = 2000;
|
|
14
|
+
export type { TFetchRedirectsConfig, TFetchRedirectsResponse, TRedirectData, TRedirectType } from './interface';
|
|
15
|
+
export * from './utils/validateRedirects';
|
|
16
|
+
export * from './redirectsFile';
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/* eslint-disable prefer-destructuring */
|
|
3
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
4
|
+
exports.redirectDefaultLimit = exports.getDefaultConfig = exports.fetchRedirectsData = void 0;
|
|
5
|
+
const tslib_1 = require("tslib");
|
|
6
|
+
const fetchRedirectsDataFile = tslib_1.__importStar(require("./fetchRedirectsData"));
|
|
7
|
+
// Re-export with deprecation warnings
|
|
8
|
+
/**
|
|
9
|
+
* @deprecated Import from `@okam/directus-node/edge` instead
|
|
10
|
+
*/
|
|
11
|
+
exports.fetchRedirectsData = fetchRedirectsDataFile.fetchRedirectsData;
|
|
12
|
+
/**
|
|
13
|
+
* @deprecated Import from `@okam/directus-node/edge` instead
|
|
14
|
+
*/
|
|
15
|
+
exports.getDefaultConfig = fetchRedirectsDataFile.getDefaultConfig;
|
|
16
|
+
/**
|
|
17
|
+
* @deprecated Import from `@okam/directus-node/edge` instead
|
|
18
|
+
*/
|
|
19
|
+
exports.redirectDefaultLimit = fetchRedirectsDataFile.redirectDefaultLimit;
|
|
20
|
+
tslib_1.__exportStar(require("./utils/validateRedirects"), exports);
|
|
21
|
+
tslib_1.__exportStar(require("./redirectsFile"), exports);
|
|
22
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../../../../../libs/directus/directus-node/src/lib/redirection/index.ts"],"names":[],"mappings":";AAAA,yCAAyC;;;;AAEzC,qFAA8D;AAC9D,sCAAsC;AACtC;;GAEG;AACU,QAAA,kBAAkB,GAAG,sBAAsB,CAAC,kBAAkB,CAAA;AAC3E;;GAEG;AACU,QAAA,gBAAgB,GAAG,sBAAsB,CAAC,gBAAgB,CAAA;AACvE;;GAEG;AACU,QAAA,oBAAoB,GAAG,sBAAsB,CAAC,oBAAoB,CAAA;AAG/E,oEAAyC;AACzC,0DAA+B"}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
export interface TFetchRedirectsConfig {
|
|
2
|
+
graphqlEndpoint: string | undefined;
|
|
3
|
+
graphqlApiKey: string | undefined;
|
|
4
|
+
redirectsFilename?: string;
|
|
5
|
+
rewritesFilename?: string;
|
|
6
|
+
limit: number | undefined;
|
|
7
|
+
}
|
|
8
|
+
export interface TFetchRedirectsResponse {
|
|
9
|
+
redirects: TRedirectData[];
|
|
10
|
+
rewrites: TRedirectData[];
|
|
11
|
+
}
|
|
12
|
+
export interface TRedirectData {
|
|
13
|
+
source: string;
|
|
14
|
+
destination: string;
|
|
15
|
+
permanent?: boolean;
|
|
16
|
+
locale?: boolean;
|
|
17
|
+
}
|
|
18
|
+
export type TRedirectType = 'redirects' | 'rewrites';
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"interface.js","sourceRoot":"","sources":["../../../../../../../libs/directus/directus-node/src/lib/redirection/interface.ts"],"names":[],"mappings":""}
|
|
@@ -1,28 +1,4 @@
|
|
|
1
|
-
|
|
2
|
-
graphqlEndpoint: string;
|
|
3
|
-
graphqlApiKey: string;
|
|
4
|
-
redirectsFilename?: string;
|
|
5
|
-
rewritesFilename?: string;
|
|
6
|
-
limit: number | undefined;
|
|
7
|
-
}
|
|
8
|
-
interface TFetchRedirectsResponse {
|
|
9
|
-
redirects: unknown;
|
|
10
|
-
rewrites: unknown;
|
|
11
|
-
}
|
|
12
|
-
interface TRedirectData {
|
|
13
|
-
source: string;
|
|
14
|
-
destination: string;
|
|
15
|
-
permanent?: boolean;
|
|
16
|
-
locale?: boolean;
|
|
17
|
-
}
|
|
18
|
-
type TRedirectType = 'redirects' | 'rewrites';
|
|
19
|
-
export declare const redirectDefaultLimit = 2000;
|
|
20
|
-
/**
|
|
21
|
-
* Get Fetch Redirects Configuration
|
|
22
|
-
* @returns {object}
|
|
23
|
-
*/
|
|
24
|
-
export declare function getDefaultConfig(): TFetchRedirectsConfig;
|
|
25
|
-
export declare function fetchRedirectsData(config: TFetchRedirectsConfig): Promise<TFetchRedirectsResponse>;
|
|
1
|
+
import type { TFetchRedirectsConfig, TRedirectType, TRedirectData } from './interface';
|
|
26
2
|
/**
|
|
27
3
|
* Write Redirect Data
|
|
28
4
|
* @param {string} filename filename
|
|
@@ -44,4 +20,3 @@ export declare function readRedirectFile(filePath: string, type?: TRedirectType)
|
|
|
44
20
|
* @throws {Error}
|
|
45
21
|
*/
|
|
46
22
|
export declare function fetchRedirects(config: TFetchRedirectsConfig): Promise<boolean>;
|
|
47
|
-
export {};
|
|
@@ -1,88 +1,14 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.redirectDefaultLimit = void 0;
|
|
4
|
-
exports.getDefaultConfig = getDefaultConfig;
|
|
5
|
-
exports.fetchRedirectsData = fetchRedirectsData;
|
|
6
3
|
exports.writeRedirectFile = writeRedirectFile;
|
|
7
4
|
exports.readRedirectFileData = readRedirectFileData;
|
|
8
5
|
exports.readRedirectFile = readRedirectFile;
|
|
9
6
|
exports.fetchRedirects = fetchRedirects;
|
|
10
7
|
const tslib_1 = require("tslib");
|
|
11
|
-
const promises_1 = require("
|
|
12
|
-
const path = tslib_1.__importStar(require("
|
|
13
|
-
const logger_1 = require("
|
|
14
|
-
|
|
15
|
-
/**
|
|
16
|
-
* Get Fetch Redirects Configuration
|
|
17
|
-
* @returns {object}
|
|
18
|
-
*/
|
|
19
|
-
function getDefaultConfig() {
|
|
20
|
-
return {
|
|
21
|
-
graphqlEndpoint: process.env['NEXT_REDIRECT_GRAPHQL_URL'] || process.env['NEXT_PUBLIC_GRAPHQL_URL'] || '',
|
|
22
|
-
graphqlApiKey: process.env['NEXT_API_TOKEN_ADMIN'] || '',
|
|
23
|
-
redirectsFilename: './redirect/redirects.json',
|
|
24
|
-
rewritesFilename: './redirect/rewrites.json',
|
|
25
|
-
limit: exports.redirectDefaultLimit,
|
|
26
|
-
};
|
|
27
|
-
}
|
|
28
|
-
function fetchRedirectsData(config) {
|
|
29
|
-
return tslib_1.__awaiter(this, void 0, void 0, function* () {
|
|
30
|
-
var _a, _b;
|
|
31
|
-
const { graphqlEndpoint, graphqlApiKey, limit } = config;
|
|
32
|
-
if (!graphqlEndpoint) {
|
|
33
|
-
throw new Error('Missing fetchRedirects configuration `graphqlEndpoint`. Check environment variables NEXT_REDIRECT_GRAPHQL_URL or NEXT_PUBLIC_GRAPHQL_URL');
|
|
34
|
-
}
|
|
35
|
-
if (!graphqlApiKey) {
|
|
36
|
-
throw new Error('Missing fetchRedirects configuration `graphqlApiKey`. Check environment variable NEXT_API_TOKEN_ADMIN');
|
|
37
|
-
}
|
|
38
|
-
const query = `query fetchRedirects($limit: Int = 2000) {
|
|
39
|
-
redirects(filter: {status:{_eq:"published"},isrewrite:{_eq:false}}, sort: "sort", limit: $limit) {
|
|
40
|
-
source
|
|
41
|
-
destination
|
|
42
|
-
permanent
|
|
43
|
-
locale
|
|
44
|
-
}
|
|
45
|
-
rewrites: redirects(filter: {status:{_eq:"published"},isrewrite:{_eq:true}}, sort: "sort", limit: $limit) {
|
|
46
|
-
source
|
|
47
|
-
destination
|
|
48
|
-
permanent
|
|
49
|
-
locale
|
|
50
|
-
}
|
|
51
|
-
}`;
|
|
52
|
-
const graphqlBody = {
|
|
53
|
-
query,
|
|
54
|
-
// "operationName": "",
|
|
55
|
-
variables: {
|
|
56
|
-
limit: Number(limit) || exports.redirectDefaultLimit,
|
|
57
|
-
},
|
|
58
|
-
};
|
|
59
|
-
try {
|
|
60
|
-
// console.info(`Fetching redirects on ${graphqlEndpoint}`)
|
|
61
|
-
const response = yield fetch(graphqlEndpoint, {
|
|
62
|
-
method: 'POST',
|
|
63
|
-
headers: {
|
|
64
|
-
// eslint-disable-next-line @typescript-eslint/naming-convention
|
|
65
|
-
'Content-Type': 'application/json',
|
|
66
|
-
Authorization: `Bearer ${graphqlApiKey}`,
|
|
67
|
-
},
|
|
68
|
-
body: JSON.stringify(graphqlBody),
|
|
69
|
-
});
|
|
70
|
-
const { data } = yield response.json();
|
|
71
|
-
logger_1.logger.log(`Fetch redirects count: ${((_a = data.redirects) === null || _a === void 0 ? void 0 : _a.length) || 0}, rewrites count: ${((_b = data.rewrites) === null || _b === void 0 ? void 0 : _b.length) || 0}`);
|
|
72
|
-
return {
|
|
73
|
-
redirects: data.redirects || [],
|
|
74
|
-
rewrites: data.rewrites || [],
|
|
75
|
-
};
|
|
76
|
-
}
|
|
77
|
-
catch (e) {
|
|
78
|
-
logger_1.logger.log(`Error fetching redirects: ${e.message}`, 'error');
|
|
79
|
-
}
|
|
80
|
-
return {
|
|
81
|
-
redirects: [],
|
|
82
|
-
rewrites: [],
|
|
83
|
-
};
|
|
84
|
-
});
|
|
85
|
-
}
|
|
8
|
+
const promises_1 = require("fs/promises");
|
|
9
|
+
const path = tslib_1.__importStar(require("path"));
|
|
10
|
+
const logger_1 = require("../../logger");
|
|
11
|
+
const fetchRedirectsData_1 = require("./fetchRedirectsData");
|
|
86
12
|
/**
|
|
87
13
|
* Write Redirect Data
|
|
88
14
|
* @param {string} filename filename
|
|
@@ -152,11 +78,11 @@ function fetchRedirects(config) {
|
|
|
152
78
|
throw new Error('Missing fetchRedirects configuration `rewritesFilename`');
|
|
153
79
|
}
|
|
154
80
|
// fetch can throw
|
|
155
|
-
const data = yield fetchRedirectsData(config);
|
|
81
|
+
const data = yield (0, fetchRedirectsData_1.fetchRedirectsData)(config);
|
|
156
82
|
// should not throw
|
|
157
83
|
yield writeRedirectFile(redirectsFilename, data.redirects);
|
|
158
84
|
yield writeRedirectFile(rewritesFilename, data.rewrites);
|
|
159
85
|
return true;
|
|
160
86
|
});
|
|
161
87
|
}
|
|
162
|
-
//# sourceMappingURL=
|
|
88
|
+
//# sourceMappingURL=redirectsFile.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"redirectsFile.js","sourceRoot":"","sources":["../../../../../../../libs/directus/directus-node/src/lib/redirection/redirectsFile.ts"],"names":[],"mappings":";;AAWA,8CASC;AAED,oDASC;AAQD,4CAaC;AAQD,wCAkBC;;AA9ED,0CAAiD;AACjD,mDAA4B;AAC5B,yCAAqC;AACrC,6DAAyD;AAGzD;;;;GAIG;AACH,SAAsB,iBAAiB,CAAC,QAAgB,EAAE,IAAa;;QACrE,IAAI,CAAC;YACH,MAAM,SAAS,GAAG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AM,CAAA,KAAK,QAAQ,IAAI,OAAO,CAAA,CAAC,aAAD,CAAC,uBAAD,CAAC,CAAE,WAAW,CAAA,KAAK,QAAQ,CAAA;YACjF,CAAC,CAAC,CAAA;YACF,eAAM,CAAC,GAAG,CAAC,WAAW,IAAI,YAAY,WAAW,CAAC,MAAM,EAAE,CAAC,CAAA;YAC3D,OAAO,WAAW,CAAA;QACpB,CAAC;QACD,eAAM,CAAC,GAAG,CAAC,kBAAkB,IAAI,qBAAqB,EAAE,OAAO,CAAC,CAAA;QAChE,OAAO,EAAqB,CAAA;IAC9B,CAAC;CAAA;AAED;;;;;GAKG;AACH,SAAsB,cAAc,CAAC,MAA6B;;QAChE,MAAM,EAAE,iBAAiB,EAAE,gBAAgB,EAAE,GAAG,MAAM,CAAA;QAEtD,IAAI,CAAC,iBAAiB,EAAE,CAAC;YACvB,MAAM,IAAI,KAAK,CAAC,0DAA0D,CAAC,CAAA;QAC7E,CAAC;QAED,IAAI,CAAC,gBAAgB,EAAE,CAAC;YACtB,MAAM,IAAI,KAAK,CAAC,yDAAyD,CAAC,CAAA;QAC5E,CAAC;QAED,kBAAkB;QAClB,MAAM,IAAI,GAAG,MAAM,IAAA,uCAAkB,EAAC,MAAM,CAAC,CAAA;QAE7C,mBAAmB;QACnB,MAAM,iBAAiB,CAAC,iBAAiB,EAAE,IAAI,CAAC,SAAS,CAAC,CAAA;QAC1D,MAAM,iBAAiB,CAAC,gBAAgB,EAAE,IAAI,CAAC,QAAQ,CAAC,CAAA;QACxD,OAAO,IAAI,CAAA;IACb,CAAC;CAAA"}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.isRedirect = isRedirect;
|
|
4
|
+
exports.normalizeRedirects = normalizeRedirects;
|
|
5
|
+
const tslib_1 = require("tslib");
|
|
6
|
+
const core_lib_1 = require("@okam/core-lib");
|
|
7
|
+
function isRedirect(redirect) {
|
|
8
|
+
return !!redirect && typeof redirect === 'object' && 'source' in redirect && 'destination' in redirect;
|
|
9
|
+
}
|
|
10
|
+
function normalizeRedirects(redirects) {
|
|
11
|
+
if (!redirects || !Array.isArray(redirects))
|
|
12
|
+
return [];
|
|
13
|
+
return redirects.flatMap((redirect) => {
|
|
14
|
+
const _a = redirect !== null && redirect !== void 0 ? redirect : {}, { source, destination } = _a, rest = tslib_1.__rest(_a, ["source", "destination"]);
|
|
15
|
+
if (!redirect || !source || !destination || !isRedirect(redirect))
|
|
16
|
+
return [];
|
|
17
|
+
return [
|
|
18
|
+
Object.assign(Object.assign({}, rest), { source: (0, core_lib_1.normalizePath)(source), destination: (0, core_lib_1.normalizePath)(destination) }),
|
|
19
|
+
];
|
|
20
|
+
});
|
|
21
|
+
}
|
|
22
|
+
//# sourceMappingURL=validateRedirects.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"validateRedirects.js","sourceRoot":"","sources":["../../../../../../../../libs/directus/directus-node/src/lib/redirection/utils/validateRedirects.ts"],"names":[],"mappings":";;AAGA,gCAEC;AAED,gDAaC;;AApBD,6CAA8C;AAG9C,SAAgB,UAAU,CAAC,QAAiB;IAC1C,OAAO,CAAC,CAAC,QAAQ,IAAI,OAAO,QAAQ,KAAK,QAAQ,IAAI,QAAQ,IAAI,QAAQ,IAAI,aAAa,IAAI,QAAQ,CAAA;AACxG,CAAC;AAED,SAAgB,kBAAkB,CAAC,SAA0C;IAC3E,IAAI,CAAC,SAAS,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,SAAS,CAAC;QAAE,OAAO,EAAE,CAAA;IACtD,OAAO,SAAS,CAAC,OAAO,CAAC,CAAC,QAAQ,EAAE,EAAE;QACpC,MAAM,KAAmC,QAAQ,aAAR,QAAQ,cAAR,QAAQ,GAAI,EAAE,EAAjD,EAAE,MAAM,EAAE,WAAW,OAA4B,EAAvB,IAAI,sBAA9B,yBAAgC,CAAiB,CAAA;QACvD,IAAI,CAAC,QAAQ,IAAI,CAAC,MAAM,IAAI,CAAC,WAAW,IAAI,CAAC,UAAU,CAAC,QAAQ,CAAC;YAAE,OAAO,EAAE,CAAA;QAC5E,OAAO;4CAEA,IAAI,KACP,MAAM,EAAE,IAAA,wBAAa,EAAC,MAAM,CAAC,EAC7B,WAAW,EAAE,IAAA,wBAAa,EAAC,WAAW,CAAC;SAE1C,CAAA;IACH,CAAC,CAAC,CAAA;AACJ,CAAC"}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"redirection.js","sourceRoot":"","sources":["../../../../../../libs/directus/directus-node/src/lib/redirection.ts"],"names":[],"mappings":";;;AAgCA,4CAQC;AAED,gDA8DC;AAOD,8CASC;AAED,oDASC;AAQD,4CAaC;AAQD,wCAkBC;;AAlLD,+CAAsD;AACtD,wDAAiC;AACjC,sCAAkC;AAwBrB,QAAA,oBAAoB,GAAG,IAAI,CAAA;AAExC;;;GAGG;AACH,SAAgB,gBAAgB;IAC9B,OAAO;QACL,eAAe,EAAE,OAAO,CAAC,GAAG,CAAC,2BAA2B,CAAC,IAAI,OAAO,CAAC,GAAG,CAAC,yBAAyB,CAAC,IAAI,EAAE;QACzG,aAAa,EAAE,OAAO,CAAC,GAAG,CAAC,sBAAsB,CAAC,IAAI,EAAE;QACxD,iBAAiB,EAAE,2BAA2B;QAC9C,gBAAgB,EAAE,0BAA0B;QAC5C,KAAK,EAAE,4BAAoB;KAC5B,CAAA;AACH,CAAC;AAED,SAAsB,kBAAkB,CAAC,MAA6B;;;QACpE,MAAM,EAAE,eAAe,EAAE,aAAa,EAAE,KAAK,EAAE,GAAG,MAAM,CAAA;QAExD,IAAI,CAAC,eAAe,EAAE,CAAC;YACrB,MAAM,IAAI,KAAK,CACb,0IAA0I,CAC3I,CAAA;QACH,CAAC;QACD,IAAI,CAAC,aAAa,EAAE,CAAC;YACnB,MAAM,IAAI,KAAK,CACb,uGAAuG,CACxG,CAAA;QACH,CAAC;QAED,MAAM,KAAK,GAAG;;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