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

feat: improve cache

This commit is contained in:
2019-09-17 14:30:33 +09:00
parent 6c84493360
commit 85520b2417
2 changed files with 16 additions and 15 deletions

View File

@@ -1,4 +1,4 @@
import nodeFetch from 'isomorphic-unfetch'
import nodeFetch from 'isomorphic-unfetch';
export type HttpMethod =
| 'GET'
@@ -8,28 +8,28 @@ export type HttpMethod =
| 'HEAD'
| 'PATCH'
| 'CONNECT'
| 'TRACE'
| 'TRACE';
export interface NowRequest<T = { query: string }> {
query: T
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
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 })
return nodeFetch(url, {method: method});
}
export function send(res: NowResponse, data: object) {
res.setHeader('Cache-Control', 'maxage=0, s-maxage=43200')
res.json(data)
res.setHeader('Cache-Control', 's-maxage=86400');
res.json(data);
}
export function sendError(res: NowResponse, error: Error) {
res.status(400).json({ error: error.message })
res.status(400).json({error: error.message});
}