Skip to content

Commit

Permalink
feature: ability to use underscore file for handler
Browse files Browse the repository at this point in the history
  • Loading branch information
saibotsivad committed Feb 1, 2023
1 parent 56e69b6 commit 55b7f12
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changeset/shiny-dryers-compare.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@oamerge/generator-routes": patch
---

Add ability to use underscore file for handler.
14 changes: 13 additions & 1 deletion packages/generator-routes/src/tree-to-javascript.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

Expand Down
2 changes: 1 addition & 1 deletion packages/generator-routes/tests/_runner.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
44 changes: 44 additions & 0 deletions packages/generator-routes/tests/handler-in-different-file.md
Original file line number Diff line number Diff line change
@@ -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,
},
]

```

0 comments on commit 55b7f12

Please sign in to comment.