JS => TS (serve-badge)

pull/282/head
amio 2019-05-14 09:36:25 +08:00
rodzic c2a9448674
commit 948b201f4b
4 zmienionych plików z 22 dodań i 13 usunięć

Wyświetl plik

@ -2,7 +2,7 @@ import fs from 'fs'
import path from 'path'
import http from 'http'
const serve404 = require('./libs/serve-404.js')
import serve404 from './libs/serve-404'
const badgeHandlers = fs.readdirSync(path.join(__dirname, 'endpoints'))
.filter(name => !name.startsWith('_'))

Wyświetl plik

@ -4,11 +4,7 @@ import PathParser from 'path-parser'
import serve404 from './serve-404.js'
import serveBadge from './serve-badge.js'
export type BadgenParams = {
subject: string
status: string
color: string
}
import { BadgenParams } from './types'
export type BadgenServeHandlerArgs = { [key: string]: string }
export type BadgenServeHandler = (args: BadgenServeHandlerArgs) => Promise<BadgenParams>

Wyświetl plik

@ -1,10 +1,16 @@
const badgen = require('badgen')
const { send } = require('micro')
const icons = require('badgen-icons')
import badgen from 'badgen'
import icons from 'badgen-icons'
const CACHE_CONTROL = `public, max-age=10, stale-while-revalidate=604800, stale-if-error=604800`
import { BadgenParams } from './types'
module.exports = (req, res, options = {}) => {
type ServeBadgeOptions = {
code?: number
sMaxAge?: number,
query?: { [key: string]: any },
params?: BadgenParams
}
export default function (req, res, options: ServeBadgeOptions) {
const { code = 200, sMaxAge = '604800', query = {}, params } = options
const hostStyle = req.headers.host === 'flat.badgen.net' ? 'flat' : undefined
@ -24,7 +30,9 @@ module.exports = (req, res, options = {}) => {
iconWidth: _icon.width
})
const staleControl = `stale-while-revalidate=604800, stale-if-error=604800`
res.setHeader('Cache-Control', `public, max-age=10, s-maxage=${sMaxAge}, ${staleControl}`)
res.setHeader('Content-Type', 'image/svg+xml;charset=utf-8')
res.setHeader('Cache-Control', `${CACHE_CONTROL}, s-maxage=${sMaxAge}`)
send(res, code, badge)
res.statusCode = code
res.end(badge)
}

5
libs/types.ts 100644
Wyświetl plik

@ -0,0 +1,5 @@
export type BadgenParams = {
subject: string
status: string
color: string
}