1
0
mirror of https://github.com/uetchy/namae.git synced 2025-03-17 04:30:31 +09:00
namae/util/http.ts

31 lines
607 B
TypeScript
Raw Normal View History

2020-07-19 13:39:15 +09:00
import { NowResponse } from '@vercel/node';
2020-09-10 04:23:19 +09:00
import nodeFetch from 'isomorphic-unfetch';
2019-08-30 16:40:22 +09:00
export type HttpMethod =
| 'GET'
| 'POST'
| 'PUT'
| 'DELETE'
| 'HEAD'
| 'PATCH'
| 'CONNECT'
2019-09-17 14:30:33 +09:00
| 'TRACE';
2019-08-30 16:40:22 +09:00
2020-02-08 19:43:22 +09:00
export function fetch(
url: string,
2020-08-31 08:41:53 +09:00
method: HttpMethod = 'HEAD'
2020-02-08 19:43:22 +09:00
): Promise<Response> {
2020-08-31 09:45:25 +09:00
return nodeFetch(url, {
method: method,
});
2019-08-30 16:40:22 +09:00
}
2020-02-08 19:43:22 +09:00
export function send(res: NowResponse, data: object): void {
2019-09-17 14:30:33 +09:00
res.setHeader('Cache-Control', 's-maxage=86400');
res.json(data);
2019-08-30 16:40:22 +09:00
}
2020-02-08 19:43:22 +09:00
export function sendError(res: NowResponse, error: Error): void {
2020-07-19 13:39:15 +09:00
res.status(400).json({ error: error.message });
2019-08-30 16:40:22 +09:00
}