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

feat(api): typescript

This commit is contained in:
2019-08-30 16:40:22 +09:00
parent 6fbd0346d0
commit f2e45bac4b
24 changed files with 340 additions and 878 deletions

View File

@@ -1,14 +0,0 @@
const fetch = require('isomorphic-unfetch')
exports.fetch = (url, method = 'HEAD') => {
return fetch(url, { method })
}
exports.send = (res, data) => {
res.setHeader('Cache-Control', 'maxage=0, s-maxage=43200')
res.json(data)
}
exports.sendError = (res, error) => {
res.status(400).json({ error: error.message })
}

35
api/util/http.ts Normal file
View File

@@ -0,0 +1,35 @@
import nodeFetch from 'isomorphic-unfetch'
export type HttpMethod =
| 'GET'
| 'POST'
| 'PUT'
| 'DELETE'
| 'HEAD'
| 'PATCH'
| 'CONNECT'
| 'TRACE'
export interface NowRequest<T = { query: string }> {
query: T
}
export interface NowResponse {
setHeader: (label: string, body: string) => void
json: (obj: object) => void
status: (code: number) => NowResponse
length: number
}
export function fetch(url: string, method: HttpMethod = 'HEAD') {
return nodeFetch(url, { method: method })
}
export function send(res: NowResponse, data: object) {
res.setHeader('Cache-Control', 'maxage=0, s-maxage=43200')
res.json(data)
}
export function sendError(res: NowResponse, error: Error) {
res.status(400).json({ error: error.message })
}

13
api/util/testHelpers.ts Normal file
View File

@@ -0,0 +1,13 @@
export async function mockProvider(provider: any, query: any) {
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]
}