Skip to content

Commit 39ee099

Browse files
authored
Merge pull request #110 from RafaelGSS/fix-typescript-chain
Fix typescript chain response
2 parents e8fd326 + b1bdd48 commit 39ee099

File tree

4 files changed

+32
-8
lines changed

4 files changed

+32
-8
lines changed

index.d.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ declare namespace LightMyRequest {
8686
cookies: Array<object>
8787
}
8888

89-
interface Chain {
89+
interface Chain extends Promise<Response> {
9090
delete: (url: string) => Chain
9191
get: (url: string) => Chain
9292
head: (url: string) => Chain
@@ -100,7 +100,8 @@ declare namespace LightMyRequest {
100100
payload: (payload: InjectPayload) => Chain
101101
query: (query: object) => Chain
102102
cookies: (query: object) => Chain
103-
end: (callback?: CallbackFunc) => Promise<Response>
103+
end(): Promise<Response>
104+
end(callback: CallbackFunc): void
104105
}
105106
}
106107

test/async-await.js

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
'use strict'
22

33
function asyncAwaitTest (t, inject) {
4-
t.plan(3)
4+
t.plan(4)
55

66
t.test('basic async await', async t => {
77
const dispatch = function (req, res) {
@@ -44,6 +44,20 @@ function asyncAwaitTest (t, inject) {
4444
t.fail(err)
4545
}
4646
})
47+
48+
t.test('chainable api with async await without end()', async t => {
49+
const dispatch = function (req, res) {
50+
res.writeHead(200, { 'Content-Type': 'text/plain' })
51+
res.end('hello')
52+
}
53+
54+
try {
55+
const res = await inject(dispatch).get('http://example.com:8080/hello')
56+
t.equal(res.payload, 'hello')
57+
} catch (err) {
58+
t.fail(err)
59+
}
60+
})
4761
}
4862

4963
module.exports = asyncAwaitTest

test/index.test-d.ts

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,12 +58,20 @@ inject(dispatch, { method: 'get', url: '/', query: { name1: ['value1', 'value2']
5858
expectResponse(res)
5959
})
6060

61+
expectType<void>(
62+
inject(dispatch)
63+
.get('/')
64+
.end((err, res) => {
65+
expectType<Error>(err)
66+
expectType<Response>(res)
67+
console.log(res.payload)
68+
})
69+
)
70+
6171
inject(dispatch)
6272
.get('/')
63-
.end((err, res) => {
64-
expectType<Error>(err)
65-
expectType<Response>(res)
66-
console.log(res.payload)
73+
.then((value) => {
74+
expectType<Response>(value)
6775
})
6876

6977
expectType<Chain>(inject(dispatch))

test/test.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ const zlib = require('zlib')
99
const http = require('http')
1010
const { finished } = require('stream')
1111
const eos = require('end-of-stream')
12+
const semver = require('semver')
1213

1314
const inject = require('../index')
1415
const parseURL = require('../lib/parseURL')
@@ -845,7 +846,7 @@ test('promises support', (t) => {
845846
})
846847

847848
test('async wait support', t => {
848-
if (Number(process.versions.node[0]) >= 8) {
849+
if (semver.gt(process.versions.node, '8.0.0')) {
849850
require('./async-await')(t, inject)
850851
} else {
851852
t.pass('Skip because Node version < 8')

0 commit comments

Comments
 (0)