Skip to content

Data Fetching

You can fetch data from other pages using snatcher().

snatcher()

'snatch' does not fetch data through http, but directly accesses the file and fetch data.

ts
import { snatcher } from 'zely';

export async function get(req, res) {
  const snatch = snatcher(req, res);
  const { body } = await snatch(/*PATH*/, /*METHOD*/);
}
import { snatcher } from 'zely';

export async function get(req, res) {
  const snatch = snatcher(req, res);
  const { body } = await snatch(/*PATH*/, /*METHOD*/);
}

TIP

You can also use it directly through req.snatch without calling snatcher.

ts
export async function get(req, res) {
  const { body } = await req.snatch('/user');

  res.send(`users: ${body}`);
}
export async function get(req, res) {
  const { body } = await req.snatch('/user');

  res.send(`users: ${body}`);
}

Example

ts
import { snatcher } from 'zely';

export async function get(req, res) {
  const snatch = snatcher(req, res);
  const { body } = await snatch('/user');

  res.send(`users: ${body}`);
}
import { snatcher } from 'zely';

export async function get(req, res) {
  const snatch = snatcher(req, res);
  const { body } = await snatch('/user');

  res.send(`users: ${body}`);
}
ts
export function get(req, res) {
  res.json({ cat: '🐱' });
}
export function get(req, res) {
  res.json({ cat: '🐱' });
}