Skip to content

Commit

Permalink
Add TypeScript definition (#16)
Browse files Browse the repository at this point in the history
Co-authored-by: Sindre Sorhus <sindresorhus@gmail.com>
  • Loading branch information
BendingBender and sindresorhus committed Mar 11, 2019
1 parent 0d2da7f commit 8b36877
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 5 deletions.
10 changes: 10 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/// <reference types="node"/>
import {IncomingMessage} from 'http';

/**
* Decompress a HTTP response if needed.
*
* @param response - The HTTP incoming stream with compressed data.
* @returns The decompressed HTTP response stream.
*/
export default function decompressResponse(response: IncomingMessage): IncomingMessage;
5 changes: 4 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ const {PassThrough: PassThroughStream} = require('stream');
const zlib = require('zlib');
const mimicResponse = require('mimic-response');

module.exports = response => {
const decompressResponse = response => {
const contentEncoding = response.headers['content-encoding'];

if (!['gzip', 'deflate', 'br'].includes(contentEncoding)) {
Expand Down Expand Up @@ -34,3 +34,6 @@ module.exports = response => {

return stream;
};

module.exports = decompressResponse;
module.exports.default = decompressResponse;
7 changes: 7 additions & 0 deletions index.test-d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import * as http from 'http';
import {expectType} from 'tsd-check';
import decompressResponse from '.';

http.get('localhost', response => {
expectType<http.IncomingMessage>(decompressResponse(response));
});
11 changes: 7 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,11 @@
"node": ">=8"
},
"scripts": {
"test": "xo && ava"
"test": "xo && ava && tsd-check"
},
"files": [
"index.js"
"index.js",
"index.d.ts"
],
"keywords": [
"decompress",
Expand All @@ -39,9 +40,11 @@
"mimic-response": "^2.0.0"
},
"devDependencies": {
"ava": "^1.1.0",
"get-stream": "^4.1.0",
"@types/node": "^11.10.5",
"ava": "^1.3.1",
"get-stream": "^5.0.0",
"pify": "^4.0.1",
"tsd-check": "^0.3.0",
"xo": "^0.24.0"
}
}
13 changes: 13 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,19 @@ http.get('https://sindresorhus.com', response => {
```


## API

### decompressResponse(response)

Returns the decompressed HTTP response stream.

#### response

Type: [`http.IncomingMessage`](https://nodejs.org/api/http.html#http_class_http_incomingmessage)

The HTTP incoming stream with compressed data.


## License

MIT © [Sindre Sorhus](https://sindresorhus.com)

0 comments on commit 8b36877

Please sign in to comment.