diff --git a/types/micro/index.d.ts b/types/micro/index.d.ts new file mode 100644 index 0000000000..b2ad7c5460 --- /dev/null +++ b/types/micro/index.d.ts @@ -0,0 +1,26 @@ +// Type definitions for micro 7.3 +// Project: https://github.com/zeit/micro +// Definitions by: Kalle Ott +// 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 + +declare const serve: (fn: RequestHandler) => Server +export default serve; + +export const send: (res: ServerResponse, code: number, obj?: object) => Promise + +export const sendError: (req: IncomingMessage, res: ServerResponse, info: { statusCode?: number, status?: number, message?: string, stack?: string }) => Promise + +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 + +export const text: (req: IncomingMessage, info?: { limit?: string, encoding?: string }) => Promise + +export const json: (req: IncomingMessage, info?: { limit?: string, encoding?: string }) => Promise diff --git a/types/micro/micro-tests.ts b/types/micro/micro-tests.ts new file mode 100644 index 0000000000..d3f8743f27 --- /dev/null +++ b/types/micro/micro-tests.ts @@ -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 = '
some html stuff
' + +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) + // + 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') + } +} diff --git a/types/micro/tsconfig.json b/types/micro/tsconfig.json new file mode 100644 index 0000000000..7975056600 --- /dev/null +++ b/types/micro/tsconfig.json @@ -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" + ] +} \ No newline at end of file diff --git a/types/micro/tslint.json b/types/micro/tslint.json new file mode 100644 index 0000000000..9750934ff2 --- /dev/null +++ b/types/micro/tslint.json @@ -0,0 +1,9 @@ +{ + "extends": "dtslint/dt.json", + "rules": { + "semicolon": [ + false + ], + "prefer-declare-function": false + } +} \ No newline at end of file