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

29 lines
599 B
TypeScript
Raw Normal View History

2020-02-08 19:43:22 +09:00
import nodeFetch, {Response} from 'node-fetch';
2020-06-19 16:15:53 +09:00
import {NowResponse} from '@vercel/node';
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,
method: HttpMethod = 'HEAD',
): Promise<Response> {
2019-09-17 14:30:33 +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 {
2019-09-17 14:30:33 +09:00
res.status(400).json({error: error.message});
2019-08-30 16:40:22 +09:00
}