sword-framework icon indicating copy to clipboard operation
sword-framework copied to clipboard

More robust and powerful routing capabilities

Open seho-dev opened this issue 2 years ago • 3 comments

seho-dev avatar Apr 02 '23 10:04 seho-dev

The current version of sword.js does not support parameterized routing, which means that if a user requests /api/hello/foo, they will only get the API logic for /api/hello and won't be able to access the foo parameter. To address this issue, sword.js will implement this feature, but if a user has already written API logic for /api/hello/foo, the more specific route will take precedence.

Routes can be declared in the file system as follows:

api/hello/[foo].ts

If the parameter contains a /, you can use an ES6-like spread operator to achieve the same goal:

api/hello/[…foo].ts

Similarly, we have encapsulated the way to specify the GET and POST methods. We recommend that you do not use the instruct parameter to forcefully define a route in your index.ts file:

api/hello.get.ts
// or
api/hello.post.ts
// or
api/hello.get|post.ts

In addition, we also provide a global route where you can perform any callback operation and get any parameters.

seho-dev avatar Apr 09 '23 14:04 seho-dev

Referenced https://nitro.unjs.io/guide/routing Bringing inspiration

seho-dev avatar Apr 09 '23 14:04 seho-dev

You can write new API suites in sword. Before, we could only write all the business logic in index.ts, but in the new version, you can get support as long as you are in the following list:

  • get.ts (['get', 'post', 'put', 'delete', 'patch', 'head', 'options', 'trace'])
  • get|post.ts (match multiple methods)
  • [name].ts (where name can represent a route parameter slot, which can be accessed in the logic)
  • [...params].ts(if there are multiple route parameters, you can use this method to get all of them, it is an array)

seho-dev avatar May 02 '23 07:05 seho-dev