From 55b7f12a94cb890a8cb8e15583096c5fefbeefe6 Mon Sep 17 00:00:00 2001 From: Tobias Davis Date: Wed, 1 Feb 2023 12:17:24 -0600 Subject: [PATCH] feature: ability to use underscore file for handler --- .changeset/shiny-dryers-compare.md | 5 +++ .../src/tree-to-javascript.js | 14 +++++- packages/generator-routes/tests/_runner.js | 2 +- .../tests/handler-in-different-file.md | 44 +++++++++++++++++++ 4 files changed, 63 insertions(+), 2 deletions(-) create mode 100644 .changeset/shiny-dryers-compare.md create mode 100644 packages/generator-routes/tests/handler-in-different-file.md diff --git a/.changeset/shiny-dryers-compare.md b/.changeset/shiny-dryers-compare.md new file mode 100644 index 0000000..236407d --- /dev/null +++ b/.changeset/shiny-dryers-compare.md @@ -0,0 +1,5 @@ +--- +"@oamerge/generator-routes": patch +--- + +Add ability to use underscore file for handler. diff --git a/packages/generator-routes/src/tree-to-javascript.js b/packages/generator-routes/src/tree-to-javascript.js index 8d08573..3c01c9a 100644 --- a/packages/generator-routes/src/tree-to-javascript.js +++ b/packages/generator-routes/src/tree-to-javascript.js @@ -71,7 +71,7 @@ export const treeToJavascript = ({ cwd, outputDir, inputs }) => { } } } - // We loop again, to resolve references and path overwrites. + // We loop again, to resolve references, path overwrites, and underscore method handler overrides. for (const { dir, api, files } of inputs) { for (const filepath of Object.keys(files).sort()) { if (files[filepath].key[0] !== 'paths') continue @@ -87,6 +87,18 @@ export const treeToJavascript = ({ cwd, outputDir, inputs }) => { } const lastKey = files[filepath].key[files[filepath].key.length - 1] + const secondToLastKey = files[filepath].key[files[filepath].key.length - 2] + if (lastKey === '_' && METHODS_WITH_HANDLERS.includes(secondToLastKey) && files[filepath].exports?.default) { + const actualPath = path.split('/') + actualPath.pop() + pathToMethod[actualPath.join('/')][secondToLastKey] = { + dir, + filepath, + handler: true, + } + continue + } + let ref = lastKey === '_' && files[filepath].exports?.$ref if (!ref) continue diff --git a/packages/generator-routes/tests/_runner.js b/packages/generator-routes/tests/_runner.js index 6554940..8d2c565 100644 --- a/packages/generator-routes/tests/_runner.js +++ b/packages/generator-routes/tests/_runner.js @@ -17,7 +17,7 @@ const parseFile = async filename => { const string = await readFile(join(__dirname, filename), 'utf8') const config = [] const expected = [] - let currentBlock + let currentBlock = '' for (const line of string.split('\n')) { if (line === '```') { currentBlock = undefined diff --git a/packages/generator-routes/tests/handler-in-different-file.md b/packages/generator-routes/tests/handler-in-different-file.md new file mode 100644 index 0000000..fc6493c --- /dev/null +++ b/packages/generator-routes/tests/handler-in-different-file.md @@ -0,0 +1,44 @@ +A default export in an underscore file will override the method file handler. + +```js #config +return { + cwd: '/root', + outputDir: './build', + inputs: [ + { + dir: 'folder1', + ext: '@', + api: '/v1', + files: { + 'paths/hello/get.@.js': { + key: [ 'paths', 'hello', 'get' ], + exports: { + default: _ => _, + }, + }, + 'paths/hello/get/_.@.js': { + key: [ 'paths', 'hello', 'get', '_' ], + exports: { + default: _ => _, + }, + }, + }, + }, + ], +} +``` + +Because the underscore file is more specific, that handler is used instead of the first. + +```js #expected +import handler_0 from "../folder1/paths/hello/get/_.@.js" + +export const routes = [ + { + path: "/v1/hello", + method: "get", + handler: handler_0, + }, +] + +```