@okam/directus-node 0.0.1

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/README.md ADDED
@@ -0,0 +1,52 @@
1
+ # directus-node
2
+
3
+ This library was generated with [Nx](https://nx.dev).
4
+
5
+ ## Building
6
+
7
+ Run `nx build directus-node` to build the library.
8
+
9
+ ## Running unit tests
10
+
11
+ Run `nx test directus-node` to execute the unit tests via [Jest](https://jestjs.io).
12
+
13
+
14
+ ## Redirection
15
+
16
+ 1. Create a fetch-redirect.js file in your next.js folder and require fetchRedirects
17
+ ```
18
+ /**
19
+ * Usage
20
+ * In apps/contest-nextjs directory
21
+ * npx env-cmd -f ../../.env.local node fetch-redirect.js
22
+ */
23
+ const { fetchRedirects, getDefaultConfig } = require('@okam/directus-node')
24
+ fetchRedirects(getDefaultConfig())
25
+ ```
26
+
27
+ 2. In project.json targets.build, add "dependsOn": ["fetch-redirect"]
28
+
29
+ 3. Create directory redirection
30
+
31
+ 4. Create file redirection/index.js
32
+ ```
33
+ const redirects = require('./redirects.json')
34
+ const rewrites = require('./rewrites.json')
35
+
36
+ module.exports = {
37
+ redirects: async () => redirects,
38
+ rewrites: async () => rewrites,
39
+ }
40
+
41
+ ```
42
+
43
+ 5. Define environments variables
44
+ ```
45
+ NEXT_PUBLIC_GRAPHQL_URL=
46
+ NEXT_API_TOKEN_ADMIN=
47
+ ```
48
+
49
+ 6. Generate redirects.json and rewrites.json using fetch-redirect (or build project with nx)
50
+ ```
51
+ npx env-cmd -f ../../.env.local node fetch-redirect.js
52
+ ```
package/package.json ADDED
@@ -0,0 +1,13 @@
1
+ {
2
+ "name": "@okam/directus-node",
3
+ "main": "./src/index.js",
4
+ "version": "0.0.1",
5
+ "types": "./index.d.ts",
6
+ "publishConfig": {
7
+ "registry": "https://registry.npmjs.org"
8
+ },
9
+ "repository": {
10
+ "url": "https://github.com/OKAMca/stack.git"
11
+ },
12
+ "type": "commonjs"
13
+ }
package/src/index.d.ts ADDED
@@ -0,0 +1 @@
1
+ export * from './lib/redirection';
package/src/index.js ADDED
@@ -0,0 +1,5 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const tslib_1 = require("tslib");
4
+ tslib_1.__exportStar(require("./lib/redirection"), exports);
5
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../../../../libs/directus/directus-node/src/index.ts"],"names":[],"mappings":";;;AAAA,4DAAiC"}
@@ -0,0 +1,13 @@
1
+ interface TFetchRedirectsConfig {
2
+ graphqlEndpoint: string;
3
+ graphqlApiKey: string;
4
+ redirectsFilename: string;
5
+ rewritesFilename: string;
6
+ }
7
+ /**
8
+ * Get Fetch Redirects Configuration
9
+ * @returns {object}
10
+ */
11
+ export declare function getDefaultConfig(): TFetchRedirectsConfig;
12
+ export declare function fetchRedirects(config: TFetchRedirectsConfig): Promise<boolean>;
13
+ export {};
@@ -0,0 +1,72 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.fetchRedirects = exports.getDefaultConfig = void 0;
4
+ const tslib_1 = require("tslib");
5
+ const promises_1 = require("node:fs/promises");
6
+ /**
7
+ * Get Fetch Redirects Configuration
8
+ * @returns {object}
9
+ */
10
+ function getDefaultConfig() {
11
+ return {
12
+ graphqlEndpoint: process.env['NEXT_PUBLIC_GRAPHQL_URL'] || '',
13
+ graphqlApiKey: process.env['NEXT_API_TOKEN_ADMIN'] || '',
14
+ redirectsFilename: './redirect/redirects.json',
15
+ rewritesFilename: './redirect/rewrites.json',
16
+ };
17
+ }
18
+ exports.getDefaultConfig = getDefaultConfig;
19
+ function fetchRedirects(config) {
20
+ return tslib_1.__awaiter(this, void 0, void 0, function* () {
21
+ const { graphqlEndpoint, graphqlApiKey, redirectsFilename, rewritesFilename } = config;
22
+ if (!graphqlEndpoint || !graphqlApiKey) {
23
+ throw new Error('Missing graphql configuration: NEXT_PUBLIC_GRAPHQL_URL or NEXT_API_TOKEN_ADMIN');
24
+ }
25
+ if (!redirectsFilename || !rewritesFilename) {
26
+ throw new Error('Missing filename');
27
+ }
28
+ const query = `query fetchRedirects {
29
+ redirects(filter: {status:{_eq:"published"},isrewrite:{_eq:false}}, sort: "sort") {
30
+ source
31
+ destination
32
+ permanent
33
+ locale
34
+ }
35
+ rewrites: redirects(filter: {status:{_eq:"published"},isrewrite:{_eq:true}}, sort: "sort") {
36
+ source
37
+ destination
38
+ permanent
39
+ locale
40
+ }
41
+ }`;
42
+ const graphqlBody = {
43
+ query,
44
+ // "operationName": "",
45
+ variables: {},
46
+ };
47
+ try {
48
+ // console.info(`Fetching redirects on ${graphqlEndpoint}`)
49
+ const response = yield fetch(graphqlEndpoint, {
50
+ method: 'POST',
51
+ headers: {
52
+ // eslint-disable-next-line @typescript-eslint/naming-convention
53
+ 'Content-Type': 'application/json',
54
+ Authorization: `Bearer ${graphqlApiKey}`,
55
+ },
56
+ body: JSON.stringify(graphqlBody),
57
+ });
58
+ const { data } = yield response.json();
59
+ const writeDataRedirects = JSON.stringify(data.redirects || []);
60
+ yield (0, promises_1.writeFile)(redirectsFilename, writeDataRedirects);
61
+ const writeDataRewrites = JSON.stringify(data.rewrites || []);
62
+ yield (0, promises_1.writeFile)(rewritesFilename, writeDataRewrites);
63
+ }
64
+ catch (e) {
65
+ // console.error('GraphQL Error', (e as Error).message)
66
+ // return false
67
+ }
68
+ return true;
69
+ });
70
+ }
71
+ exports.fetchRedirects = fetchRedirects;
72
+ //# sourceMappingURL=redirection.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"redirection.js","sourceRoot":"","sources":["../../../../../../libs/directus/directus-node/src/lib/redirection.ts"],"names":[],"mappings":";;;;AAAA,+CAA4C;AAQ5C;;;GAGG;AACH,SAAgB,gBAAgB;IAC9B,OAAO;QACL,eAAe,EAAE,OAAO,CAAC,GAAG,CAAC,yBAAyB,CAAC,IAAI,EAAE;QAC7D,aAAa,EAAE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