Skip to content

Commit

Permalink
feat: add typings to org repo (#56)
Browse files Browse the repository at this point in the history
* feat: add typings to org repo

* fix: ci & tests
  • Loading branch information
jannikkeye authored and mcollina committed Feb 10, 2019
1 parent 1caff82 commit 43de985
Show file tree
Hide file tree
Showing 7 changed files with 124 additions and 5 deletions.
8 changes: 8 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
root = true

[*]
indent_style = space
indent_size = 2
charset = utf-8
trim_trailing_whitespace = false
insert_final_newline = false
10 changes: 10 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,18 @@
language: node_js

node_js:
- "8"
- "6"
- "10"
- "11"

os:
- linux
- windows

script:
- npm run lint && npm run typescript

notifications:
email:
on_success: never
Expand Down
57 changes: 57 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
// Definitions by: Jannik Keye <https://github.com/jannikkeye>

import busboy = require("busboy");
import fastify = require("fastify");

import { Server, IncomingMessage, ServerResponse } from 'http';

type MultipartHandler = (
field: string,
file: any,
filename: string,
encoding: string,
mimetype: string,
) => void;

declare module "fastify" {
interface FastifyRequest<HttpRequest> {
isMultipart: () => boolean;
multipart: (handler: MultipartHandler, next: (err: Error) => void) => busboy.Busboy;
}
}

declare const fastifyMultipart: fastify.Plugin<Server, IncomingMessage, ServerResponse, {
limits?: {
/**
* Max field name size in bytes
*/
fieldNameSize?: number;

/**
* Max field value size in bytes
*/
fieldSize?: number;

/**
* Max number of non-file fields
*/
fields?: number;

/**
* For multipart forms, the max file size
*/
fileSize?: number;

/**
* Max number of file fields
*/
files?: number;

/**
* Max number of header key=>value pairs
*/
headerPairs?: number;
}
}>;

export = fastifyMultipart;
15 changes: 12 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,27 @@
"fastify-plugin": "^1.5.0"
},
"devDependencies": {
"@types/busboy": "^0.2.3",
"climem": "^1.0.3",
"concat-stream": "^2.0.0",
"fastify": "^1.11.2",
"eslint-plugin-typescript": "^0.14.0",
"fastify": "^1.13.4",
"form-data": "^2.3.2",
"pre-commit": "^1.2.2",
"pump": "^3.0.0",
"snazzy": "^8.0.0",
"standard": "^12.0.1",
"tap": "^12.0.0"
"tap": "^12.0.0",
"typescript": "^3.3.1",
"typescript-eslint-parser": "^22.0.0"
},
"scripts": {
"test": "standard | snazzy && tap test.js",
"lint": "npm run lint:standard && npm run lint:typescript",
"lint:standard": "standard | snazzy",
"lint:typescript": "standard --fix --parser typescript-eslint-parser --plugin typescript test/types/*.ts",
"unit": "tap test/*.test.js",
"typescript": "tsc --project ./test/types/tsconfig.json",
"test": "npm run lint && npm run unit && npm run typescript",
"start": "CLIMEM=8999 node -r climem example",
"climem": "climem 8999 localhost"
},
Expand Down
4 changes: 2 additions & 2 deletions test.js → test/multipart.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ const os = require('os')
const test = require('tap').test
const FormData = require('form-data')
const Fastify = require('fastify')
const multipart = require('.')
const multipart = require('..')
const http = require('http')
const path = require('path')
const fs = require('fs')
Expand All @@ -13,7 +13,7 @@ const Readable = require('stream').Readable
const Writable = require('stream').Writable
const crypto = require('crypto')

const filePath = path.join(__dirname, 'README.md')
const filePath = path.join(__dirname, '../README.md')

test('should parse forms', function (t) {
t.plan(14)
Expand Down
24 changes: 24 additions & 0 deletions test/types/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import * as fastify from 'fastify'
import * as fastifyMultipart from '../..'

const app = fastify()

app.register(fastifyMultipart, {
limits: {
fieldNameSize: 200,
fieldSize: 200,
fields: 200,
fileSize: 200,
files: 2,
headerPairs: 200
}
})

app.get('/path', (request) => {
const isMultiPart = request.isMultipart()
request.multipart((field, file, filename, encoding, mimetype) => {
console.log(field, file, filename, encoding, mimetype, isMultiPart)
}, (err) => {
throw err
})
})
11 changes: 11 additions & 0 deletions test/types/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"compilerOptions": {
"target": "es6",
"module": "commonjs",
"noEmit": true,
"strict": true,
},
"files": [
"./index.ts"
]
}

0 comments on commit 43de985

Please sign in to comment.