From ba80c8ad7d9f30e524ff2414853779c94a357de5 Mon Sep 17 00:00:00 2001 From: Kawika Bader Date: Thu, 1 May 2025 11:08:28 -0700 Subject: [PATCH] chore: move eslint config to rc json file --- .eslintrc.json | 17 +++ package.json | 17 --- packages/create-gasket-app/.eslintrc.json | 35 ++++++ packages/create-gasket-app/lib/index.js | 7 ++ .../lib/scaffold/create-context.js | 6 + packages/create-gasket-app/package.json | 30 ----- packages/gasket-assets/.eslintrc.json | 14 +++ packages/gasket-assets/package.json | 16 +-- packages/gasket-core/.eslintrc.json | 37 ++++++ packages/gasket-core/package.json | 40 ------- packages/gasket-data/.eslintrc.json | 35 ++++++ packages/gasket-data/package.json | 30 ----- packages/gasket-fetch/.eslintrc.json | 32 ++++++ packages/gasket-fetch/package.json | 27 ----- packages/gasket-intl/.eslintrc.json | 32 ++++++ packages/gasket-intl/package.json | 27 ----- packages/gasket-nextjs/.eslintrc.json | 32 ++++++ packages/gasket-nextjs/lib/document/index.js | 23 +++- packages/gasket-nextjs/lib/index.d.ts | 3 +- .../gasket-nextjs/lib/inject-gasket-data.js | 5 + .../lib/layout/with-gasket-data.js | 16 +-- packages/gasket-nextjs/lib/request/index.js | 2 +- .../lib/with-gasket-data-provider.js | 9 +- .../lib/with-locale-initial-props.js | 2 +- packages/gasket-nextjs/package.json | 30 ----- packages/gasket-plugin-analyze/.eslintrc.json | 32 ++++++ packages/gasket-plugin-analyze/package.json | 27 ----- packages/gasket-plugin-command/.eslintrc.json | 35 ++++++ packages/gasket-plugin-command/package.json | 30 ----- packages/gasket-plugin-cypress/.eslintrc.json | 36 ++++++ packages/gasket-plugin-cypress/package.json | 31 ------ packages/gasket-plugin-data/.eslintrc.json | 32 ++++++ packages/gasket-plugin-data/package.json | 27 ----- .../gasket-plugin-docs-graphs/.eslintrc.json | 32 ++++++ .../gasket-plugin-docs-graphs/package.json | 27 ----- packages/gasket-plugin-docs/.eslintrc.json | 32 ++++++ packages/gasket-plugin-docs/package.json | 27 ----- .../gasket-plugin-docusaurus/.eslintrc.json | 32 ++++++ .../gasket-plugin-docusaurus/package.json | 27 ----- .../.eslintrc.json | 32 ++++++ .../package.json | 27 ----- .../gasket-plugin-elastic-apm/.eslintrc.json | 32 ++++++ .../gasket-plugin-elastic-apm/package.json | 27 ----- packages/gasket-plugin-express/.eslintrc.json | 43 +++++++ packages/gasket-plugin-express/package.json | 38 ------- packages/gasket-plugin-fastify/.eslintrc.json | 43 +++++++ packages/gasket-plugin-fastify/package.json | 38 ------- packages/gasket-plugin-git/.eslintrc.json | 32 ++++++ packages/gasket-plugin-git/package.json | 27 ----- .../gasket-plugin-happyfeet/.eslintrc.json | 32 ++++++ packages/gasket-plugin-happyfeet/package.json | 27 ----- .../gasket-plugin-https-proxy/.eslintrc.json | 32 ++++++ .../gasket-plugin-https-proxy/package.json | 27 ----- packages/gasket-plugin-https/.eslintrc.json | 32 ++++++ packages/gasket-plugin-https/package.json | 27 ----- packages/gasket-plugin-intl/.eslintrc.json | 32 ++++++ packages/gasket-plugin-intl/package.json | 27 ----- packages/gasket-plugin-jest/.eslintrc.json | 32 ++++++ packages/gasket-plugin-jest/package.json | 27 ----- packages/gasket-plugin-lint/.eslintrc.json | 32 ++++++ packages/gasket-plugin-lint/package.json | 27 ----- packages/gasket-plugin-logger/.eslintrc.json | 32 ++++++ packages/gasket-plugin-logger/package.json | 27 ----- .../gasket-plugin-manifest/.eslintrc.json | 32 ++++++ packages/gasket-plugin-manifest/package.json | 27 ----- .../gasket-plugin-metadata/.eslintrc.json | 32 ++++++ packages/gasket-plugin-metadata/package.json | 27 ----- .../gasket-plugin-middleware/.eslintrc.json | 34 ++++++ .../gasket-plugin-middleware/package.json | 34 ------ packages/gasket-plugin-mocha/.eslintrc.json | 44 ++++++++ packages/gasket-plugin-mocha/package.json | 34 ------ packages/gasket-plugin-morgan/.eslintrc.json | 33 ++++++ packages/gasket-plugin-morgan/package.json | 28 ----- packages/gasket-plugin-nextjs/.eslintrc.json | 46 ++++++++ .../generator/next/typescript/next.config.js | 2 +- packages/gasket-plugin-nextjs/lib/index.d.ts | 18 +-- packages/gasket-plugin-nextjs/package.json | 42 ------- packages/gasket-plugin-redux/.eslintrc.json | 38 +++++++ packages/gasket-plugin-redux/package.json | 33 ------ .../.eslintrc.json | 36 ++++++ .../gasket-plugin-service-worker/package.json | 33 +----- packages/gasket-plugin-swagger/.eslintrc.json | 32 ++++++ packages/gasket-plugin-swagger/package.json | 27 ----- .../gasket-plugin-typescript/.eslintrc.json | 32 ++++++ .../gasket-plugin-typescript/package.json | 27 ----- packages/gasket-plugin-webpack/.eslintrc.json | 32 ++++++ packages/gasket-plugin-webpack/package.json | 27 ----- packages/gasket-plugin-winston/.eslintrc.json | 32 ++++++ packages/gasket-plugin-winston/package.json | 27 ----- packages/gasket-plugin-workbox/.eslintrc.json | 35 ++++++ packages/gasket-plugin-workbox/package.json | 34 +----- packages/gasket-preset-api/.eslintrc.json | 16 +++ packages/gasket-preset-api/package.json | 16 --- packages/gasket-preset-nextjs/.eslintrc.json | 15 +++ packages/gasket-preset-nextjs/package.json | 15 --- packages/gasket-react-intl/.eslintrc.json | 32 ++++++ packages/gasket-react-intl/package.json | 27 ----- packages/gasket-redux/.eslintrc.json | 35 ++++++ packages/gasket-redux/package.json | 30 ----- packages/gasket-request/.eslintrc.json | 45 ++++++++ packages/gasket-request/package.json | 40 ------- .../gasket-typescript-tests/.eslintrc.json | 17 +++ packages/gasket-typescript-tests/package.json | 17 --- packages/gasket-utils/.eslintrc.json | 45 ++++++++ packages/gasket-utils/package.json | 42 +------ scripts/generate-docs-index/.eslintrc.json | 11 ++ scripts/generate-docs-index/package.json | 11 -- scripts/replace-ws-aliases.js | 105 ------------------ syncpack.config.js | 9 +- 109 files changed, 1619 insertions(+), 1490 deletions(-) create mode 100644 .eslintrc.json create mode 100644 packages/create-gasket-app/.eslintrc.json create mode 100644 packages/gasket-assets/.eslintrc.json create mode 100644 packages/gasket-core/.eslintrc.json create mode 100644 packages/gasket-data/.eslintrc.json create mode 100644 packages/gasket-fetch/.eslintrc.json create mode 100644 packages/gasket-intl/.eslintrc.json create mode 100644 packages/gasket-nextjs/.eslintrc.json create mode 100644 packages/gasket-plugin-analyze/.eslintrc.json create mode 100644 packages/gasket-plugin-command/.eslintrc.json create mode 100644 packages/gasket-plugin-cypress/.eslintrc.json create mode 100644 packages/gasket-plugin-data/.eslintrc.json create mode 100644 packages/gasket-plugin-docs-graphs/.eslintrc.json create mode 100644 packages/gasket-plugin-docs/.eslintrc.json create mode 100644 packages/gasket-plugin-docusaurus/.eslintrc.json create mode 100644 packages/gasket-plugin-dynamic-plugins/.eslintrc.json create mode 100644 packages/gasket-plugin-elastic-apm/.eslintrc.json create mode 100644 packages/gasket-plugin-express/.eslintrc.json create mode 100644 packages/gasket-plugin-fastify/.eslintrc.json create mode 100644 packages/gasket-plugin-git/.eslintrc.json create mode 100644 packages/gasket-plugin-happyfeet/.eslintrc.json create mode 100644 packages/gasket-plugin-https-proxy/.eslintrc.json create mode 100644 packages/gasket-plugin-https/.eslintrc.json create mode 100644 packages/gasket-plugin-intl/.eslintrc.json create mode 100644 packages/gasket-plugin-jest/.eslintrc.json create mode 100644 packages/gasket-plugin-lint/.eslintrc.json create mode 100644 packages/gasket-plugin-logger/.eslintrc.json create mode 100644 packages/gasket-plugin-manifest/.eslintrc.json create mode 100644 packages/gasket-plugin-metadata/.eslintrc.json create mode 100644 packages/gasket-plugin-middleware/.eslintrc.json create mode 100644 packages/gasket-plugin-mocha/.eslintrc.json create mode 100644 packages/gasket-plugin-morgan/.eslintrc.json create mode 100644 packages/gasket-plugin-nextjs/.eslintrc.json create mode 100644 packages/gasket-plugin-redux/.eslintrc.json create mode 100644 packages/gasket-plugin-service-worker/.eslintrc.json create mode 100644 packages/gasket-plugin-swagger/.eslintrc.json create mode 100644 packages/gasket-plugin-typescript/.eslintrc.json create mode 100644 packages/gasket-plugin-webpack/.eslintrc.json create mode 100644 packages/gasket-plugin-winston/.eslintrc.json create mode 100644 packages/gasket-plugin-workbox/.eslintrc.json create mode 100644 packages/gasket-preset-api/.eslintrc.json create mode 100644 packages/gasket-preset-nextjs/.eslintrc.json create mode 100644 packages/gasket-react-intl/.eslintrc.json create mode 100644 packages/gasket-redux/.eslintrc.json create mode 100644 packages/gasket-request/.eslintrc.json create mode 100644 packages/gasket-typescript-tests/.eslintrc.json create mode 100644 packages/gasket-utils/.eslintrc.json create mode 100644 scripts/generate-docs-index/.eslintrc.json delete mode 100644 scripts/replace-ws-aliases.js diff --git a/.eslintrc.json b/.eslintrc.json new file mode 100644 index 000000000..49dc72188 --- /dev/null +++ b/.eslintrc.json @@ -0,0 +1,17 @@ +{ + "extends": [ + "godaddy", + "plugin:jsdoc/recommended-typescript-flavor" + ], + "ignorePatterns": [ + "node_modules/", + "coverage/" + ], + "plugins": [ + "unicorn", + "jsdoc" + ], + "rules": { + "unicorn/filename-case": "error" + } +} diff --git a/package.json b/package.json index c07d5c124..78a131bc4 100644 --- a/package.json +++ b/package.json @@ -69,23 +69,6 @@ "syncpack": "^13.0.3", "typescript": "^5.7.3" }, - "eslintConfig": { - "extends": [ - "godaddy", - "plugin:jsdoc/recommended-typescript-flavor" - ], - "ignorePatterns": [ - "node_modules/", - "coverage/" - ], - "plugins": [ - "unicorn", - "jsdoc" - ], - "rules": { - "unicorn/filename-case": "error" - } - }, "engines": { "pnpm": ">=8", "npm": "please-use-pnpm", diff --git a/packages/create-gasket-app/.eslintrc.json b/packages/create-gasket-app/.eslintrc.json new file mode 100644 index 000000000..4f7c2ac73 --- /dev/null +++ b/packages/create-gasket-app/.eslintrc.json @@ -0,0 +1,35 @@ +{ + "extends": [ + "godaddy", + "plugin:jest/recommended", + "plugin:jsdoc/recommended-typescript-flavor" + ], + "plugins": [ + "unicorn", + "jsdoc" + ], + "rules": { + "unicorn/filename-case": "error" + }, + "overrides": [ + { + "files": [ + "lib/*.ts" + ], + "extends": [ + "godaddy-typescript" + ], + "rules": { + "jsdoc/require-jsdoc": "off", + "jsdoc/require-param": "off", + "jsdoc/require-returns": "off", + "jsdoc/require-description": "off", + "jsdoc/require-param-type": "off", + "jsdoc/require-returns-description": "off" + } + } + ], + "ignorePatterns": [ + "test/fixtures" + ] +} diff --git a/packages/create-gasket-app/lib/index.js b/packages/create-gasket-app/lib/index.js index e3264cd28..861ba9436 100755 --- a/packages/create-gasket-app/lib/index.js +++ b/packages/create-gasket-app/lib/index.js @@ -18,6 +18,13 @@ const gasketBin = program .addHelpText('beforeAll', logo); +/** + * Main function to execute the Gasket CLI. + * It suppresses deprecation warnings, processes the create command, + * checks if the package is outdated, adds the command to the Gasket CLI, + * inserts 'create' into the command line arguments, and parses the command. + * @returns {Promise} A promise that resolves with the parsed command. + */ async function main() { // Suppress deprecation warning for punycode in node 22 // @ts-ignore diff --git a/packages/create-gasket-app/lib/scaffold/create-context.js b/packages/create-gasket-app/lib/scaffold/create-context.js index e53f69b2f..ffed91331 100644 --- a/packages/create-gasket-app/lib/scaffold/create-context.js +++ b/packages/create-gasket-app/lib/scaffold/create-context.js @@ -52,6 +52,12 @@ function makeCreateRuntime(context, source) { }); } +/** + * Flattens an array by concatenating the given values to the accumulator. + * @param {Array} acc - The accumulator array that values will be concatenated to. + * @param {Array} values - The array of values to be concatenated. + * @returns {Array} - The resulting array after concatenation. + */ function flatten(acc, values) { return acc.concat(values); } diff --git a/packages/create-gasket-app/package.json b/packages/create-gasket-app/package.json index 89c6243b3..c0bf8b4a6 100644 --- a/packages/create-gasket-app/package.json +++ b/packages/create-gasket-app/package.json @@ -68,36 +68,6 @@ "jest": "^29.7.0", "typescript": "^5.7.3" }, - "eslintConfig": { - "extends": [ - "godaddy", - "plugin:jest/recommended", - "plugin:jsdoc/recommended-typescript-flavor" - ], - "plugins": [ - "unicorn", - "jsdoc" - ], - "rules": { - "unicorn/filename-case": "error" - }, - "overrides": [ - { - "files": [ - "lib/*.ts" - ], - "extends": [ - "godaddy-typescript" - ], - "rules": { - "jsdoc/*": "off" - } - } - ] - }, - "eslintIgnore": [ - "test/fixtures" - ], "jest": { "transform": {} } diff --git a/packages/gasket-assets/.eslintrc.json b/packages/gasket-assets/.eslintrc.json new file mode 100644 index 000000000..b1cc42cae --- /dev/null +++ b/packages/gasket-assets/.eslintrc.json @@ -0,0 +1,14 @@ +{ + "extends": [ + "godaddy" + ], + "plugins": [ + "unicorn" + ], + "rules": { + "unicorn/filename-case": "error" + }, + "ignorePatterns": [ + "react" + ] +} diff --git a/packages/gasket-assets/package.json b/packages/gasket-assets/package.json index bf9ea657c..1d04c98e8 100644 --- a/packages/gasket-assets/package.json +++ b/packages/gasket-assets/package.json @@ -47,19 +47,5 @@ "recursive-readdir": "^2.2.3", "rimraf": "^3.0.2", "typescript": "^5.7.3" - }, - "eslintConfig": { - "extends": [ - "godaddy" - ], - "plugins": [ - "unicorn" - ], - "rules": { - "unicorn/filename-case": "error" - } - }, - "eslintIgnore": [ - "react" - ] + } } diff --git a/packages/gasket-core/.eslintrc.json b/packages/gasket-core/.eslintrc.json new file mode 100644 index 000000000..0552699b3 --- /dev/null +++ b/packages/gasket-core/.eslintrc.json @@ -0,0 +1,37 @@ +{ + "ignorePatterns": [ + "cjs/" + ], + "extends": [ + "godaddy", + "plugin:jest/recommended", + "plugin:jsdoc/recommended-typescript-flavor" + ], + "plugins": [ + "unicorn", + "jsdoc" + ], + "rules": { + "unicorn/filename-case": "error", + "no-undefined": 0, + "no-sync": 0 + }, + "overrides": [ + { + "files": [ + "test/**/*.js" + ], + "rules": { + "jsdoc/require-jsdoc": "off" + } + }, + { + "files": [ + "lib/*.ts" + ], + "extends": [ + "godaddy-typescript" + ] + } + ] +} diff --git a/packages/gasket-core/package.json b/packages/gasket-core/package.json index 015cc3765..b9d6eef96 100644 --- a/packages/gasket-core/package.json +++ b/packages/gasket-core/package.json @@ -64,46 +64,6 @@ "jest": "^29.7.0", "typescript": "^5.7.3" }, - "eslintConfig": { - "ignorePatterns": [ - "cjs/" - ], - "extends": [ - "godaddy", - "plugin:jest/recommended", - "plugin:jsdoc/recommended-typescript-flavor" - ], - "plugins": [ - "unicorn", - "jsdoc" - ], - "rules": { - "unicorn/filename-case": "error", - "no-undefined": 0, - "no-sync": 0 - }, - "overrides": [ - { - "files": [ - "test/**/*.js" - ], - "rules": { - "jsdoc/require-jsdoc": "off" - } - }, - { - "files": [ - "lib/*.ts" - ], - "extends": [ - "godaddy-typescript" - ], - "rules": { - "jsdoc/*": "off" - } - } - ] - }, "gasket": { "metadata": { "lifecycles": [ diff --git a/packages/gasket-data/.eslintrc.json b/packages/gasket-data/.eslintrc.json new file mode 100644 index 000000000..72d6e8f49 --- /dev/null +++ b/packages/gasket-data/.eslintrc.json @@ -0,0 +1,35 @@ +{ + "ignorePatterns": [ + "cjs/" + ], + "extends": [ + "godaddy", + "plugin:jest/recommended", + "plugin:jsdoc/recommended-typescript-flavor" + ], + "plugins": [ + "unicorn", + "jsdoc" + ], + "rules": { + "unicorn/filename-case": "error" + }, + "overrides": [ + { + "files": [ + "lib/*.ts" + ], + "extends": [ + "godaddy-typescript" + ], + "rules": { + "jsdoc/require-jsdoc": "off", + "jsdoc/require-param": "off", + "jsdoc/require-returns": "off", + "jsdoc/require-description": "off", + "jsdoc/require-param-type": "off", + "jsdoc/require-returns-description": "off" + } + } + ] +} diff --git a/packages/gasket-data/package.json b/packages/gasket-data/package.json index d83606b18..e5429ddca 100644 --- a/packages/gasket-data/package.json +++ b/packages/gasket-data/package.json @@ -62,36 +62,6 @@ "jsdom": "^20.0.3", "typescript": "^5.7.3" }, - "eslintConfig": { - "ignorePatterns": [ - "cjs/" - ], - "extends": [ - "godaddy", - "plugin:jest/recommended", - "plugin:jsdoc/recommended-typescript-flavor" - ], - "plugins": [ - "unicorn", - "jsdoc" - ], - "rules": { - "unicorn/filename-case": "error" - }, - "overrides": [ - { - "files": [ - "lib/*.ts" - ], - "extends": [ - "godaddy-typescript" - ], - "rules": { - "jsdoc/*": "off" - } - } - ] - }, "jest": { "transform": {} } diff --git a/packages/gasket-fetch/.eslintrc.json b/packages/gasket-fetch/.eslintrc.json new file mode 100644 index 000000000..38b1ac1c7 --- /dev/null +++ b/packages/gasket-fetch/.eslintrc.json @@ -0,0 +1,32 @@ +{ + "extends": [ + "godaddy", + "plugin:jest/recommended", + "plugin:jsdoc/recommended-typescript-flavor" + ], + "plugins": [ + "unicorn", + "jsdoc" + ], + "rules": { + "unicorn/filename-case": "error" + }, + "overrides": [ + { + "files": [ + "lib/*.ts" + ], + "extends": [ + "godaddy-typescript" + ], + "rules": { + "jsdoc/require-jsdoc": "off", + "jsdoc/require-param": "off", + "jsdoc/require-returns": "off", + "jsdoc/require-description": "off", + "jsdoc/require-param-type": "off", + "jsdoc/require-returns-description": "off" + } + } + ] +} diff --git a/packages/gasket-fetch/package.json b/packages/gasket-fetch/package.json index 79c7eccf5..c3a0e8a03 100644 --- a/packages/gasket-fetch/package.json +++ b/packages/gasket-fetch/package.json @@ -55,33 +55,6 @@ "typescript": "^5.7.3", "whatwg-fetch": "^3.6.20" }, - "eslintConfig": { - "extends": [ - "godaddy", - "plugin:jest/recommended", - "plugin:jsdoc/recommended-typescript-flavor" - ], - "plugins": [ - "unicorn", - "jsdoc" - ], - "rules": { - "unicorn/filename-case": "error" - }, - "overrides": [ - { - "files": [ - "lib/*.ts" - ], - "extends": [ - "godaddy-typescript" - ], - "rules": { - "jsdoc/*": "off" - } - } - ] - }, "babel": { "presets": [ "@babel/preset-env" diff --git a/packages/gasket-intl/.eslintrc.json b/packages/gasket-intl/.eslintrc.json new file mode 100644 index 000000000..38b1ac1c7 --- /dev/null +++ b/packages/gasket-intl/.eslintrc.json @@ -0,0 +1,32 @@ +{ + "extends": [ + "godaddy", + "plugin:jest/recommended", + "plugin:jsdoc/recommended-typescript-flavor" + ], + "plugins": [ + "unicorn", + "jsdoc" + ], + "rules": { + "unicorn/filename-case": "error" + }, + "overrides": [ + { + "files": [ + "lib/*.ts" + ], + "extends": [ + "godaddy-typescript" + ], + "rules": { + "jsdoc/require-jsdoc": "off", + "jsdoc/require-param": "off", + "jsdoc/require-returns": "off", + "jsdoc/require-description": "off", + "jsdoc/require-param-type": "off", + "jsdoc/require-returns-description": "off" + } + } + ] +} diff --git a/packages/gasket-intl/package.json b/packages/gasket-intl/package.json index a8d7e1e8f..32c931288 100644 --- a/packages/gasket-intl/package.json +++ b/packages/gasket-intl/package.json @@ -42,32 +42,5 @@ "eslint-plugin-unicorn": "^55.0.0", "jest": "^29.7.0", "typescript": "^5.7.3" - }, - "eslintConfig": { - "extends": [ - "godaddy", - "plugin:jest/recommended", - "plugin:jsdoc/recommended-typescript-flavor" - ], - "plugins": [ - "unicorn", - "jsdoc" - ], - "rules": { - "unicorn/filename-case": "error" - }, - "overrides": [ - { - "files": [ - "lib/*.ts" - ], - "extends": [ - "godaddy-typescript" - ], - "rules": { - "jsdoc/*": "off" - } - } - ] } } diff --git a/packages/gasket-nextjs/.eslintrc.json b/packages/gasket-nextjs/.eslintrc.json new file mode 100644 index 000000000..bfd5a0185 --- /dev/null +++ b/packages/gasket-nextjs/.eslintrc.json @@ -0,0 +1,32 @@ +{ + "extends": [ + "godaddy-react", + "plugin:jest/recommended", + "plugin:jsdoc/recommended-typescript-flavor" + ], + "plugins": [ + "unicorn", + "jsdoc" + ], + "rules": { + "unicorn/filename-case": "error" + }, + "overrides": [ + { + "files": [ + "lib/*.ts" + ], + "extends": [ + "godaddy-typescript" + ], + "rules": { + "jsdoc/require-jsdoc": "off", + "jsdoc/require-param": "off", + "jsdoc/require-returns": "off", + "jsdoc/require-description": "off", + "jsdoc/require-param-type": "off", + "jsdoc/require-returns-description": "off" + } + } + ] +} diff --git a/packages/gasket-nextjs/lib/document/index.js b/packages/gasket-nextjs/lib/document/index.js index 319b51bf7..7155f8713 100644 --- a/packages/gasket-nextjs/lib/document/index.js +++ b/packages/gasket-nextjs/lib/document/index.js @@ -14,10 +14,9 @@ function isDocumentClass(maybeClass) { * To avoid polluting , we want to render our JSON in the * but before our other scripts so that it is available to query. * In a basic Next.js app, this is between the Main and NextScript tags.* - * - * @param {Array} bodyChildren - Children of body element - * @returns {number} index - * @private + * @param {Array} bodyChildren - An array of React elements that are children of the body element. + * @param {number} [index] - An optional index at which to insert the script. If not provided, determine the best index. + * @returns {number} - The index at which the GasketData script should be inserted. */ function lookupIndex(bodyChildren, index = -1) { const lookups = [ @@ -34,7 +33,6 @@ function lookupIndex(bodyChildren, index = -1) { /** * Make a wrapper to extend the Next.js Document, injecting a script with the * `gasketData` from the response object. - * * @type {import('.').withGasketData} */ export function withGasketData( @@ -45,6 +43,13 @@ export function withGasketData( return Document => { + /** + * Asynchronously fetches initial properties for the document. + * If a request is present in the context, it fetches public Gasket data. + * Otherwise, it returns an empty object. + * @param {object} ctx - The context object. + * @returns {Promise} A promise that resolves with the initial properties for the document. + */ async function getInitialProps(ctx) { const gasketData = ctx.req ? await gasket.actions.getPublicGasketData?.(ctx.req) ?? {} : {}; @@ -55,7 +60,7 @@ export function withGasketData( } // if class-based Document - if(isDocumentClass(Document)) { + if (isDocumentClass(Document)) { return class GasketDocument extends Document { static async getInitialProps(ctx) { return getInitialProps(ctx); @@ -70,7 +75,13 @@ export function withGasketData( }; } + /** + * A function that wraps the Document component and injects GasketData into it. + * @param {object} props - The properties passed to the Document component. + * @returns {import('react').ReactElement} - The Document component with GasketData injected. + */ function WrappedDocument(props) { + // eslint-disable-next-line new-cap const html = Document(props); const { gasketData } = props; diff --git a/packages/gasket-nextjs/lib/index.d.ts b/packages/gasket-nextjs/lib/index.d.ts index 8d72120a8..94d18bda7 100644 --- a/packages/gasket-nextjs/lib/index.d.ts +++ b/packages/gasket-nextjs/lib/index.d.ts @@ -1,6 +1,7 @@ import type { GasketData } from '@gasket/data'; import type { Gasket } from '@gasket/core'; import type { ComponentType, FunctionComponent, PropsWithChildren, ReactElement } from 'react'; +import type { NextPageContext } from 'next'; declare module '@gasket/data' { export interface GasketData { @@ -10,7 +11,7 @@ declare module '@gasket/data' { } } -ComponentWithInitialProps = ComponentType & Partial<{ +type ComponentWithInitialProps = ComponentType & Partial<{ getInitialProps: (ctx: NextPageContext) => Promise }>; diff --git a/packages/gasket-nextjs/lib/inject-gasket-data.js b/packages/gasket-nextjs/lib/inject-gasket-data.js index ef171ad69..b3e36c41f 100644 --- a/packages/gasket-nextjs/lib/inject-gasket-data.js +++ b/packages/gasket-nextjs/lib/inject-gasket-data.js @@ -3,6 +3,11 @@ import { Children, cloneElement, createElement } from 'react'; import { GasketDataScript } from './gasket-data-script.js'; +/** + * This function finds the body element from the children array and returns it along with its index. + * @param {Array} children - The array of child elements. + * @returns {Array} - An array containing the body element and its index. + */ function selectBody(children) { const bodyIdx = children.findIndex(t => diff --git a/packages/gasket-nextjs/lib/layout/with-gasket-data.js b/packages/gasket-nextjs/lib/layout/with-gasket-data.js index 5f222ed7c..2233c8a72 100644 --- a/packages/gasket-nextjs/lib/layout/with-gasket-data.js +++ b/packages/gasket-nextjs/lib/layout/with-gasket-data.js @@ -1,13 +1,14 @@ import { request } from '../request'; -import { injectGasketData } from '../inject-gasket-data.js' +import { injectGasketData } from '../inject-gasket-data.js'; import NextScript from 'next/script'; /** - * To avoid polluting , we want to render our JSON in the - * but before our other scripts so that it is available to query. - * - * @param {Array} bodyChildren - Children of body element - * @returns {number} index + * This function is used to determine the index at which the JSON should be rendered in the . + * It tries to render the JSON before other scripts to avoid polluting . + * If it can't find a