Update typescripts

pull/282/head
amio 2019-05-13 20:00:56 +08:00
rodzic d6ca0afc5f
commit 6e199f72f0
10 zmienionych plików z 118 dodań i 59 usunięć

Wyświetl plik

@ -1,11 +1,10 @@
const millify = require('millify')
const got = require('../libs/got.js')
const stars = require('../libs/utils/stars.js')
const semColor = require('../libs/utils/sem-color.js')
const v = require('../libs/utils/version-formatter.js')
const badgenServe = require('../libs/badgen-serve.js')
import got from '../libs/got'
import { millify, stars, version, versionColor } from '../libs/utils'
import { badgenServe, BadgenServeHandlers, BadgenServeHandlerArgs } from '../libs/badgen-serve'
const examples = [
export const help = ``
export const examples = [
'/amo/v/markdown-viewer-chrome',
'/amo/users/markdown-viewer-chrome',
'/amo/rating/markdown-viewer-chrome',
@ -17,7 +16,7 @@ const handlers = {
'/amo/:topic/:name': handler
}
async function handler ({ topic, name }) {
async function handler ({ topic, name }: BadgenServeHandlerArgs) {
const endpoint = `https://addons.mozilla.org/api/v3/addons/addon/${name}/`
const addon = await got(endpoint).then(res => res.body)
@ -25,8 +24,8 @@ async function handler ({ topic, name }) {
case 'v':
return {
subject: 'mozilla add-on',
status: v(addon.current_version.version),
color: semColor(addon.current_version.version)
status: version(addon.current_version.version),
color: versionColor(addon.current_version.version)
}
case 'users':
return {

Wyświetl plik

@ -2,17 +2,19 @@ import fs from 'fs'
import path from 'path'
import http from 'http'
const serve404 = require('../libs/serve-404.js')
const serve404 = require('./libs/serve-404.js')
const badgeHandlers = fs.readdirSync(__dirname)
const badgeHandlers = fs.readdirSync(path.join(__dirname, 'endpoints'))
.filter(name => !name.startsWith('_'))
.map(name => name.replace(/\.js$/, ''))
.map(name => name.replace(/\.ts$/, ''))
module.exports = http.createServer((req, res) => {
module.exports = http.createServer(async (req, res) => {
const handler = badgeHandlers.find(h => req.url!.startsWith(`/${h}`))
if (handler) {
return require(path.join(__dirname, handler))(req, res)
return import(path.join(__dirname, 'endpoints', handler)).then(h => {
h.default(req, res)
})
} else {
return serve404(req, res)
}

Wyświetl plik

@ -1,36 +0,0 @@
const url = require('url')
const PathParser = require('path-parser').default
const serve404 = require('./serve-404.js')
const serveHelp = require('./serve-help.js')
const serveBadge = require('./serve-badge.js')
module.exports = function badgenServe (handlers, { help = '', examples = [] } = {}) {
return async function httpHandler (req, res) {
const { pathname, query } = url.parse(req.url, true)
// serve favicon
if (pathname === '/favicon.ico') {
return res.end()
}
// serve help message
if (pathname.startsWith('/help')) {
return serveHelp(req, res, help, examples)
}
// Lookup handler
let matchedArgs
const matchedScheme = Object.keys(handlers).find(scheme => {
matchedArgs = new PathParser(scheme).test(pathname)
return matchedArgs !== null
})
if (matchedScheme) {
const params = await handlers[matchedScheme](matchedArgs)
return serveBadge(req, res, { params, query })
} else {
return serve404(req, res)
}
}
}

Wyświetl plik

@ -0,0 +1,55 @@
import url from 'url'
import PathParser from 'path-parser'
import serve404 from './serve-404.js'
import serveHelp from './serve-help.js'
import serveBadge from './serve-badge.js'
type BadgenParams = {
subject: string
status: string
color: string
}
type BadgenServeOptions = {
help?: string
examples?: string[]
}
export type BadgenServeHandlerArgs = { [key: string]: string }
export type BadgenServeHandler = (args: BadgenServeHandlerArgs) => Promise<BadgenParams>
export type BadgenServeHandlers = { [key: string]: BadgenServeHandler }
export function badgenServe (
handlers: BadgenServeHandlers,
options: BadgenServeOptions
): Function {
const { help = '', examples = [] } = options
return async function httpHandler (req, res) {
const { pathname = '/', query } = url.parse(req.url, true)
// serve favicon
if (pathname === '/favicon.ico') {
return res.end()
}
// serve help message
if (pathname.startsWith('/help')) {
return serveHelp(req, res, help, examples)
}
// Lookup handler
let matchedArgs
const matchedScheme = Object.keys(handlers).find(scheme => {
matchedArgs = new PathParser(scheme).test(pathname)
return matchedArgs !== null
})
if (matchedScheme) {
const params = await handlers[matchedScheme](matchedArgs)
return serveBadge(req, res, { params, query })
} else {
return serve404(req, res)
}
}
}

Wyświetl plik

@ -1,7 +0,0 @@
const got = require('got')
module.exports = got.extend({
json: true,
timeout: 3200,
retry: 0
})

7
libs/got.ts 100644
Wyświetl plik

@ -0,0 +1,7 @@
import got from 'got'
export default got.extend({
json: true,
timeout: 3200,
retry: 0
})

Wyświetl plik

@ -0,0 +1,20 @@
import millify from 'millify'
import compareVersions from './compare-versions'
import coverage from './cov-format'
import coverageColor from './cov-color'
import scale from './scale'
import stars from './stars.js'
import version from './version-formatter'
import versionColor from './sem-color'
export {
millify,
compareVersions,
coverage,
coverageColor,
scale,
stars,
version,
versionColor
}

16
package-lock.json wygenerowano
Wyświetl plik

@ -2066,11 +2066,27 @@
"defer-to-connect": "^1.0.1"
}
},
"@types/got": {
"version": "9.4.4",
"resolved": "https://registry.npmjs.org/@types/got/-/got-9.4.4.tgz",
"integrity": "sha512-IGAJokJRE9zNoBdY5csIwN4U5qQn+20HxC0kM+BbUdfTKIXa7bOX/pdhy23NnLBRP8Wvyhx7X5e6EHJs+4d8HA==",
"dev": true,
"requires": {
"@types/node": "*",
"@types/tough-cookie": "*"
}
},
"@types/node": {
"version": "10.5.6",
"resolved": "https://registry.npmjs.org/@types/node/-/node-10.5.6.tgz",
"integrity": "sha512-c5Z1j1ysgo4878ptz6gxLcgMfJ6Wf908R3l5KAGabr0XJ72ZFmOCgsaodPpNYTfp4iOrSwgTDvR/BxbFfB4zPQ=="
},
"@types/tough-cookie": {
"version": "2.3.5",
"resolved": "https://registry.npmjs.org/@types/tough-cookie/-/tough-cookie-2.3.5.tgz",
"integrity": "sha512-SCcK7mvGi3+ZNz833RRjFIxrn4gI1PPR3NtuIS+6vMkvmsGjosqTJwRt5bAEFLRz+wtJMWv8+uOnZf2hi2QXTg==",
"dev": true
},
"@types/unist": {
"version": "2.0.3",
"resolved": "https://registry.npmjs.org/@types/unist/-/unist-2.0.3.tgz",

Wyświetl plik

@ -5,6 +5,7 @@
"license": "ISC",
"private": true,
"scripts": {
"dev2": "nodemon -e ts,js -w endpoints -x 'ts-node index.ts'",
"lint": "standard",
"dev": "micro-dev service.js -s -i .next",
"dev:web": "next",
@ -41,6 +42,7 @@
},
"devDependencies": {
"@mdx-js/mdx": "^0.20.3",
"@types/got": "^9.4.4",
"@zeit/next-mdx": "^1.2.0",
"babel-eslint": "^10.0.1",
"micro-dev": "^3.0.0",

Wyświetl plik

@ -1,6 +1,7 @@
{
"compilerOptions": {
"strict": true,
"noImplicitAny": false,
"target": "esnext",
"module": "commonjs",
"moduleResolution": "node",
@ -11,7 +12,7 @@
"lib": ["esnext"]
},
"include": [
"./src",
"./endpoints",
"./types"
]
}