2020-07-19 13:39:15 +09:00
|
|
|
import { NowResponse } from '@vercel/node';
|
2021-02-25 15:13:15 +09:00
|
|
|
import nodeFetch from 'cross-fetch';
|
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
|
|
|
}
|