-
Notifications
You must be signed in to change notification settings - Fork 1
/
hello.ts
52 lines (41 loc) · 1.28 KB
/
hello.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
// Next.js API route support: https://nextjs.org/docs/api-routes/introduction
import type { NextApiRequest, NextApiResponse } from 'next';
import { handler, Middleware } from '../middleware/handler';
import { allowMethods } from '../middleware/method';
const middleware_1: Middleware = async (_req, _res, next) => {
console.log('Running middleware 1');
next();
};
const middleware_2: Middleware = async (_req, _res, next) => {
console.log('Running middleware 2');
// sleep for 2.5 seconds
console.log('Sleeping for 2.5 seconds');
await new Promise((resolve) => setTimeout(resolve, 2500));
next();
};
const middleware_3: Middleware = async (_req, _res, next) => {
console.log('Running middleware 3');
// Fetch data from json api
const res = await fetch('https://jsonplaceholder.typicode.com/todos/1');
const json = await res.json();
console.log(json);
next();
};
const middleware_4: Middleware = async (_req, _res, next) => {
console.log('Running middleware 4');
next();
};
type Data = {
message: string;
};
const hello = async (req: NextApiRequest, res: NextApiResponse<Data>) => {
res.status(200).json({ message: 'Hello World.' });
};
export default handler(
allowMethods(['GET']),
middleware_1,
middleware_2,
middleware_3,
middleware_4,
hello,
);