@okam/directus-node 0.0.1 → 0.1.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/.eslintrc.js ADDED
@@ -0,0 +1,21 @@
1
+ const { getDefaultIgnorePatterns } = require("../../../linter/helpers");
2
+
3
+ module.exports = {
4
+ extends: ['plugin:@nx/react', '../../../.eslintrc.js', '../../../linter/bases/typescript', '../../../linter/bases/regexp', '../../../linter/bases/jest', '../../../linter/bases/rtl', '../../../linter/bases/storybook', '../../../linter/bases/react', '../../../linter/bases/prettier', 'plugin:storybook/recommended'],
5
+ ignorePatterns: ['!**/*', ...getDefaultIgnorePatterns()],
6
+ overrides: [
7
+ {
8
+ files: ['*.ts', '*.tsx', '*.js', '*.jsx'],
9
+ // We set parserOptions.project for the project to allow TypeScript to create the type-checker behind the scenes when we run linting
10
+ parserOptions: {
11
+ project: `${__dirname}/tsconfig.*?.json`,
12
+ },
13
+ rules: {
14
+ "@nx/dependency-checks": "error"
15
+ }
16
+ },
17
+ ],
18
+ rules: {
19
+ 'react/react-in-jsx-scope': 'off',
20
+ },
21
+ }
package/CHANGELOG.md ADDED
@@ -0,0 +1,11 @@
1
+ ## 0.1.0 (2024-06-28)
2
+
3
+
4
+ ### 🚀 Features
5
+
6
+ - add gql query ([#73](https://github.com/OKAMca/stack/pull/73))
7
+
8
+
9
+ ### ❤️ Thank You
10
+
11
+ - Marie-Maxime Tanguay @marie-maxime
package/README.md CHANGED
@@ -21,14 +21,28 @@ Run `nx test directus-node` to execute the unit tests via [Jest](https://jestjs.
21
21
  * npx env-cmd -f ../../.env.local node fetch-redirect.js
22
22
  */
23
23
  const { fetchRedirects, getDefaultConfig } = require('@okam/directus-node')
24
+
24
25
  fetchRedirects(getDefaultConfig())
25
26
  ```
26
27
 
27
- 2. In project.json targets.build, add "dependsOn": ["fetch-redirect"]
28
+ 2. In project.json,
29
+ 1. In targets.build, add `"dependsOn": ["fetch-redirect"],`
30
+ 2. In targets:
31
+ ```
32
+ "fetch-redirect": {
33
+ "executor": "nx:run-commands",
34
+ "options": {
35
+ "cwd": "{projectRoot}",
36
+ "command": "node fetch-redirect"
37
+ }
38
+ },
39
+ ```
40
+ 3. Don't forget to change the cwd path
28
41
 
29
- 3. Create directory redirection
30
42
 
31
- 4. Create file redirection/index.js
43
+ 3. Create directory redirect
44
+
45
+ 4. Create file redirect/index.js
32
46
  ```
33
47
  const redirects = require('./redirects.json')
34
48
  const rewrites = require('./rewrites.json')
@@ -46,7 +60,38 @@ NEXT_PUBLIC_GRAPHQL_URL=
46
60
  NEXT_API_TOKEN_ADMIN=
47
61
  ```
48
62
 
49
- 6. Generate redirects.json and rewrites.json using fetch-redirect (or build project with nx)
63
+ 6. Generate redirect/redirects.json and redirect/rewrites.json using fetch-redirect (or build project with nx)
50
64
  ```
51
65
  npx env-cmd -f ../../.env.local node fetch-redirect.js
52
66
  ```
67
+
68
+ 7. Update nextConfig in next.config.js with
69
+ ```
70
+ const { rewrites, redirects } = require('./redirect/index')
71
+ ```
72
+ ```
73
+ const nextConfig = {
74
+ ...
75
+ async redirects() {
76
+ const rest = await redirects()
77
+ return [
78
+ ...i18nReRouter({locale: false, permanent: true}, 'redirect'),
79
+ ...rest,
80
+ ]
81
+ return rest
82
+ },
83
+ async rewrites() {
84
+ const fallback = await rewrites()
85
+ return {
86
+ beforeFiles: [
87
+ ...i18nReRouter({locale: false}, 'rewrite'),
88
+ ],
89
+ afterFiles: [
90
+ ...i18nRewriter({...i18nConfigWithoutLocaleDetector, localeDetector: false}),
91
+ ],
92
+ fallback,
93
+ }
94
+ },
95
+ ...
96
+ }
97
+ ```
package/jest.config.ts ADDED
@@ -0,0 +1,11 @@
1
+ /* eslint-disable */
2
+ export default {
3
+ displayName: 'directus-node',
4
+ preset: '../../../jest.preset.js',
5
+ testEnvironment: 'node',
6
+ transform: {
7
+ '^.+\\.[tj]s$': ['ts-jest', { tsconfig: '<rootDir>/tsconfig.spec.json' }],
8
+ },
9
+ moduleFileExtensions: ['ts', 'js', 'html'],
10
+ coverageDirectory: '../../../coverage/libs/directus/directus-node',
11
+ }
package/package.json CHANGED
@@ -1,13 +1,12 @@
1
1
  {
2
2
  "name": "@okam/directus-node",
3
- "main": "./src/index.js",
4
- "version": "0.0.1",
3
+ "main": "./index.js",
4
+ "version": "0.1.0",
5
5
  "types": "./index.d.ts",
6
6
  "publishConfig": {
7
7
  "registry": "https://registry.npmjs.org"
8
8
  },
9
9
  "repository": {
10
10
  "url": "https://github.com/OKAMca/stack.git"
11
- },
12
- "type": "commonjs"
11
+ }
13
12
  }
package/project.json ADDED
@@ -0,0 +1,34 @@
1
+ {
2
+ "name": "directus-node",
3
+ "$schema": "../../../node_modules/nx/schemas/project-schema.json",
4
+ "sourceRoot": "libs/directus/directus-node/src",
5
+ "projectType": "library",
6
+ "tags": [],
7
+ "targets": {
8
+ "build": {
9
+ "executor": "@nx/js:tsc",
10
+ "outputs": ["{options.outputPath}"],
11
+ "options": {
12
+ "outputPath": "dist/libs/directus/directus-node",
13
+ "tsConfig": "libs/directus/directus-node/tsconfig.lib.json",
14
+ "packageJson": "libs/directus/directus-node/package.json",
15
+ "main": "libs/directus/directus-node/src/index.ts",
16
+ "assets": ["libs/directus/directus-node/*.md"]
17
+ }
18
+ },
19
+ "publish": {
20
+ "command": "node tools/scripts/publish.mjs directus-node {args.ver} {args.tag}",
21
+ "dependsOn": ["build"]
22
+ },
23
+ "lint": {
24
+ "executor": "@nx/eslint:lint"
25
+ },
26
+ "test": {
27
+ "executor": "@nx/jest:jest",
28
+ "outputs": ["{workspaceRoot}/coverage/{projectRoot}"],
29
+ "options": {
30
+ "jestConfig": "libs/directus/directus-node/jest.config.ts"
31
+ }
32
+ }
33
+ }
34
+ }
package/src/index.ts ADDED
@@ -0,0 +1,2 @@
1
+ export * from './lib/redirection'
2
+ export { default as graphqlCodegenConfig } from './lib/codegen'
@@ -0,0 +1,25 @@
1
+ import type { CodegenConfig } from '@graphql-codegen/cli'
2
+
3
+ const graphqlCodegenConfig = (schemaUrl: string, generatePath: string, headers: Record<string, string>) => {
4
+ const config: CodegenConfig = {
5
+ overwrite: true,
6
+ schema: [
7
+ {
8
+ [schemaUrl]: {
9
+ headers,
10
+ },
11
+ },
12
+ ],
13
+ documents: [`${__dirname}/apps/demo/**/*.graphql`],
14
+ ignoreNoDocuments: true, // for better experience with the watcher
15
+ generates: {
16
+ [generatePath]: {
17
+ preset: 'client',
18
+ },
19
+ },
20
+ }
21
+
22
+ return config
23
+ }
24
+
25
+ export default graphqlCodegenConfig
@@ -0,0 +1,78 @@
1
+ import { writeFile } from 'node:fs/promises'
2
+
3
+ interface TFetchRedirectsConfig {
4
+ graphqlEndpoint: string
5
+ graphqlApiKey: string
6
+ redirectsFilename: string
7
+ rewritesFilename: string
8
+ }
9
+ /**
10
+ * Get Fetch Redirects Configuration
11
+ * @returns {object}
12
+ */
13
+ export function getDefaultConfig(): TFetchRedirectsConfig {
14
+ return {
15
+ graphqlEndpoint: process.env['NEXT_PUBLIC_GRAPHQL_URL'] || '',
16
+ graphqlApiKey: process.env['NEXT_API_TOKEN_ADMIN'] || '',
17
+ redirectsFilename: './redirect/redirects.json',
18
+ rewritesFilename: './redirect/rewrites.json',
19
+ }
20
+ }
21
+
22
+ export async function fetchRedirects(config: TFetchRedirectsConfig): Promise<boolean> {
23
+ const { graphqlEndpoint, graphqlApiKey, redirectsFilename, rewritesFilename } = config
24
+
25
+ if (!graphqlEndpoint || !graphqlApiKey) {
26
+ throw new Error('Missing graphql configuration: NEXT_PUBLIC_GRAPHQL_URL or NEXT_API_TOKEN_ADMIN')
27
+ }
28
+
29
+ if (!redirectsFilename || !rewritesFilename) {
30
+ throw new Error('Missing filename')
31
+ }
32
+
33
+ const query = `query fetchRedirects {
34
+ redirects(filter: {status:{_eq:"published"},isrewrite:{_eq:false}}, sort: "sort") {
35
+ source
36
+ destination
37
+ permanent
38
+ locale
39
+ }
40
+ rewrites: redirects(filter: {status:{_eq:"published"},isrewrite:{_eq:true}}, sort: "sort") {
41
+ source
42
+ destination
43
+ permanent
44
+ locale
45
+ }
46
+ }`
47
+
48
+ const graphqlBody = {
49
+ query,
50
+ // "operationName": "",
51
+ variables: {},
52
+ }
53
+
54
+ try {
55
+ // console.info(`Fetching redirects on ${graphqlEndpoint}`)
56
+ const response = await fetch(graphqlEndpoint, {
57
+ method: 'POST',
58
+ headers: {
59
+ // eslint-disable-next-line @typescript-eslint/naming-convention
60
+ 'Content-Type': 'application/json',
61
+ Authorization: `Bearer ${graphqlApiKey}`,
62
+ },
63
+ body: JSON.stringify(graphqlBody),
64
+ })
65
+ const { data } = await response.json()
66
+
67
+ const writeDataRedirects = JSON.stringify(data.redirects || [])
68
+ await writeFile(redirectsFilename, writeDataRedirects)
69
+
70
+ const writeDataRewrites = JSON.stringify(data.rewrites || [])
71
+ await writeFile(rewritesFilename, writeDataRewrites)
72
+ } catch (e) {
73
+ // console.error('GraphQL Error', (e as Error).message)
74
+ // return false
75
+ }
76
+
77
+ return true
78
+ }
package/tsconfig.json ADDED
@@ -0,0 +1,22 @@
1
+ {
2
+ "extends": "../../../tsconfig.base.json",
3
+ "compilerOptions": {
4
+ "module": "commonjs",
5
+ "forceConsistentCasingInFileNames": true,
6
+ "strict": true,
7
+ "noImplicitOverride": true,
8
+ "noPropertyAccessFromIndexSignature": true,
9
+ "noImplicitReturns": true,
10
+ "noFallthroughCasesInSwitch": true
11
+ },
12
+ "files": [],
13
+ "include": [],
14
+ "references": [
15
+ {
16
+ "path": "./tsconfig.lib.json"
17
+ },
18
+ {
19
+ "path": "./tsconfig.spec.json"
20
+ }
21
+ ]
22
+ }
@@ -0,0 +1,11 @@
1
+ {
2
+ "extends": "./tsconfig.json",
3
+ "compilerOptions": {
4
+ "module": "commonjs",
5
+ "outDir": "../../../dist/out-tsc",
6
+ "declaration": true,
7
+ "types": ["node"]
8
+ },
9
+ "exclude": ["jest.config.ts", "src/**/*.spec.ts", "src/**/*.test.ts"],
10
+ "include": ["src/**/*.ts"]
11
+ }
@@ -0,0 +1,9 @@
1
+ {
2
+ "extends": "./tsconfig.json",
3
+ "compilerOptions": {
4
+ "outDir": "../../../dist/out-tsc",
5
+ "module": "commonjs",
6
+ "types": ["jest", "node"]
7
+ },
8
+ "include": ["jest.config.ts", "src/**/*.test.ts", "src/**/*.spec.ts", "src/**/*.d.ts"]
9
+ }
package/src/index.d.ts DELETED
@@ -1 +0,0 @@
1
- export * from './lib/redirection';
package/src/index.js DELETED
@@ -1,5 +0,0 @@
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
package/src/index.js.map DELETED
@@ -1 +0,0 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../../../../../libs/directus/directus-node/src/index.ts"],"names":[],"mappings":";;;AAAA,4DAAiC"}
@@ -1,13 +0,0 @@
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 {};
@@ -1,72 +0,0 @@
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
@@ -1 +0,0 @@
1
- {"version":3,"file":"redirection.js","sourceRoot":"","sources":["../../../../../../libs/directus/directus-node/src/lib/redirection.ts"],"names":[],"mappings":";;;;AAAA,+CAA4C;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