Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feature Request]: Automatic method #37

Open
rtritto opened this issue Dec 1, 2024 · 1 comment
Open

[Feature Request]: Automatic method #37

rtritto opened this issue Dec 1, 2024 · 1 comment

Comments

@rtritto
Copy link
Contributor

rtritto commented Dec 1, 2024

Get method from filename (eg (post).ts is a POST method), otherwise use GET method.

Example of implementation:

// src/index.ts

// file = 'user/(post).ts'
const matchedFile = file.match(/\/?\((.*?)\)/)
console.log(matchedFile)
// [
//   '(post)',
//   'post',
//   index: 4,
//   input: 'user/(post).ts',
//   groups: undefined
// ]

const method = matchedFile ? matchedFile[1] : 'get'
console.log(method)
// post

// DRAFT
if (typeof importedValue === "function")
  if (importedValue.length > 0)
    plugin.group(url, groupOptions, app[method]('', importedValue));
  else
    plugin.group(url, groupOptions, (app) => app[method]('', importedValue()));
else if (importedValue instanceof Elysia)
  plugin.group(url, groupOptions, (app) => app.use(app[method]('', importedValue))));

The route file:

// user/(post).ts

import type { ElysiaApp } from "app"

export default (() => ({ hello: "world" })) satisfies Parameters<ElysiaApp["post"]>[1]

Examples

  • GET with parameters
    // routes/getBody.ts
    
    import type { ElysiaApp } from "app"
    
    export default (({ body }) => {
      return { body }
    }) satisfies Parameters<ElysiaApp["get"]>[1]
@kravetsone
Copy link
Owner

Good idea but with this we will forced to introduce new way of autorouting

Because for now we use group(app => ...)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants