added type definitions for the micro library

This commit is contained in:
Kalle Ott
2017-05-10 10:09:11 +02:00
parent 8868f90acf
commit de2bdd9e66
4 changed files with 119 additions and 0 deletions

26
types/micro/index.d.ts vendored Normal file
View File

@@ -0,0 +1,26 @@
// Type definitions for micro 7.3
// Project: https://github.com/zeit/micro
// Definitions by: Kalle Ott <https://github.com/kaoDev>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
// TypeScript Version: 2.2
import { IncomingMessage, ServerResponse, Server } from 'http';
export type RequestHandler = (req: IncomingMessage, res: ServerResponse) => any
export const run: (req: IncomingMessage, res: ServerResponse, fn: RequestHandler) => Promise<void>
declare const serve: (fn: RequestHandler) => Server
export default serve;
export const send: (res: ServerResponse, code: number, obj?: object) => Promise<void>
export const sendError: (req: IncomingMessage, res: ServerResponse, info: { statusCode?: number, status?: number, message?: string, stack?: string }) => Promise<void>
export function createError(code: number, msg: string, orig?: Error): Error & { statusCode: number, originalError?: Error }
export const buffer: (req: IncomingMessage, info?: { limit?: string, encoding?: string }) => Promise<Buffer | string>
export const text: (req: IncomingMessage, info?: { limit?: string, encoding?: string }) => Promise<string>
export const json: (req: IncomingMessage, info?: { limit?: string, encoding?: string }) => Promise<object>

View File

@@ -0,0 +1,62 @@
import { json, RequestHandler, buffer, text, send, createError } from 'micro'
import micro from 'micro'
// Json sample
export const jsonHandler: RequestHandler = async (req, res) => {
const data = await json(req)
console.log(data)
return 'Data logged to your console'
}
// socket.io chat app sample
const html = '<div>some html stuff</div>'
const server = micro(async (req, res) => {
console.log('Serving index.html');
res.end(html);
});
import * as socketIO from 'socket.io'
const io = socketIO(server);
server.listen(4000);
// Micro expects a function to be exported
module.exports = () => console.log('YOLO');
// body parsing sample
const bodyParsingHandler: RequestHandler = async (req, res) => {
const buf = await buffer(req)
console.log(buf)
// <Buffer 7b 22 70 72 69 63 65 22 3a 20 39 2e 39 39 7d>
const txt = await text(req)
// '{"price": 9.99}'
const js: any = await json(req)
// { price: 9.99 }
console.log(js.price)
return ''
}
// send different status code sample
const statusHandler: RequestHandler = async (req, res) => {
const statusCode = 400
const data = { error: 'Custom error message' }
send(res, statusCode, data)
}
// createError sample
const errorHandler: RequestHandler = async (req, res) => {
const data = { error: 'Custom error message' }
if (data.error) {
throw createError(429, 'Rate limit exceeded')
}
}

22
types/micro/tsconfig.json Normal file
View File

@@ -0,0 +1,22 @@
{
"compilerOptions": {
"module": "commonjs",
"lib": [
"es2017"
],
"noImplicitAny": true,
"noImplicitThis": true,
"strictNullChecks": true,
"baseUrl": "../",
"typeRoots": [
"../"
],
"types": [],
"noEmit": true,
"forceConsistentCasingInFileNames": true
},
"files": [
"index.d.ts",
"micro-tests.ts"
]
}

9
types/micro/tslint.json Normal file
View File

@@ -0,0 +1,9 @@
{
"extends": "dtslint/dt.json",
"rules": {
"semicolon": [
false
],
"prefer-declare-function": false
}
}