1
0
mirror of https://github.com/uetchy/namae.git synced 2025-08-20 01:48:12 +09:00

fix: support new vercel style

This commit is contained in:
2020-06-19 16:15:53 +09:00
parent a5f9074303
commit f95f8ef551
106 changed files with 4523 additions and 1390 deletions

28
util/http.ts Normal file
View File

@@ -0,0 +1,28 @@
import nodeFetch, {Response} from 'node-fetch';
import {NowResponse} from '@vercel/node';
export type HttpMethod =
| 'GET'
| 'POST'
| 'PUT'
| 'DELETE'
| 'HEAD'
| 'PATCH'
| 'CONNECT'
| 'TRACE';
export function fetch(
url: string,
method: HttpMethod = 'HEAD',
): Promise<Response> {
return nodeFetch(url, {method: method});
}
export function send(res: NowResponse, data: object): void {
res.setHeader('Cache-Control', 's-maxage=86400');
res.json(data);
}
export function sendError(res: NowResponse, error: Error): void {
res.status(400).json({error: error.message});
}

16
util/testHelpers.ts Normal file
View File

@@ -0,0 +1,16 @@
export async function mockProvider(
provider: any,
query: unknown,
): Promise<string> {
const req = {
query,
};
const res = {
status: jest.fn().mockReturnThis(),
send: jest.fn().mockReturnThis(),
json: jest.fn().mockReturnThis(),
setHeader: jest.fn(),
};
await provider(req, res);
return res.json.mock.calls[0][0];
}