Docs
Routing
Methods

Methods

#HTTP Methods

By default, exporting a handler without specifying an HTTP method allows it to respond to all methods. However, to handle specific methods like GET, you can define them explicitly.

#Multiple Handler

You can define separate handlers for different HTTP methods by organizing them into an array or exporting function.

Typescript
import { GET, POST } from 'zely';
 
export default [
  GET({
    type: 'cat',
    emoji: '🐱',
  }),
  POST({
    type: 'dog',
    emoji: '🐶',
  }),
];

#Global Import

To omit handler imports, enable the globalImport option in your configuration file:

zely.config.ts
import { defineConfig } from 'zely';
 
export default defineConfig({
  globalImport: true,
});

Then, GET, POST, and other handlers can be used directly without importing them.

#TypeScript Support

If you're using TypeScript, configure your environment to support global imports.

  1. Create an env.d.ts file.
env.d.ts
import 'zely/env';
  1. Update your tsconfig.json to include the env.d.ts file.
tsconfig.json
{
  "include": ["./env.d.ts"]
}

#Access req and res

Access the req and res objects through the context (ctx):

Typescript
import { POST } from 'zely';
 
export default [
  POST((ctx) => {
    ctx.send(ctx.req.body);
  }),
];

TIP

For more details, refer to the context documentation.