Skip to content

Commit

Permalink
fix Response type (#177)
Browse files Browse the repository at this point in the history
  • Loading branch information
limulus authored Jan 31, 2022
1 parent 4b28c4b commit 39415b1
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 7 deletions.
8 changes: 5 additions & 3 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,13 @@ declare namespace LightMyRequest {
callback: CallbackFunc
): void

type DispatchFunc = (req: Request, res: Response) => void
type DispatchFunc = (req: Request, res: ServerResponse) => void

type CallbackFunc = (err: Error, response: Response) => void

type InjectPayload = string | object | Buffer | NodeJS.ReadableStream

function isInjection (obj: Request | Response): boolean
function isInjection (obj: Request | ServerResponse): boolean

interface InjectOptions {
url?: string | {
Expand Down Expand Up @@ -71,7 +71,9 @@ declare namespace LightMyRequest {
connection: object
}

interface Response extends http.ServerResponse {
interface ServerResponse extends http.ServerResponse {}

interface Response {
raw: {
res: http.ServerResponse,
req: Request
Expand Down
10 changes: 6 additions & 4 deletions test/index.test-d.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import * as http from 'http'
import { inject, isInjection, Request, Response, DispatchFunc, InjectOptions, Chain } from '../index'
import { expectType, expectAssignable } from 'tsd'
import { inject, isInjection, Request, Response, DispatchFunc, InjectOptions, Chain, ServerResponse } from '../index'
import { expectType, expectAssignable, expectNotAssignable } from 'tsd'

expectAssignable<InjectOptions>({ url: '/' })

const dispatch = function (req: Request, res: Response) {
const dispatch = function (req: Request, res: ServerResponse) {
expectType<boolean>(isInjection(req))
expectType<boolean>(isInjection(res))

Expand All @@ -17,7 +17,7 @@ const expectResponse = function (res: Response) {
expectType<Response>(res)
console.log(res.payload)
expectAssignable<Function>(res.json)
expectAssignable<http.ServerResponse>(res.raw.res)
expectAssignable<ServerResponse>(res.raw.res)
expectAssignable<Request>(res.raw.req)
console.log(res.cookies)
}
Expand Down Expand Up @@ -88,3 +88,5 @@ expectType<ParsedValue>(parsedValue)

const parsedValueUsingGeneric = response.json<ParsedValue>()
expectType<ParsedValue>(parsedValueUsingGeneric)

expectNotAssignable<http.ServerResponse>(response)

0 comments on commit 39415b1

Please sign in to comment.