import * as http from 'http' import { inject, isInjection, Response, DispatchFunc, InjectOptions, Chain } from '..' import { expectType, expectAssignable, expectNotAssignable } from 'tsd' import { Readable } from 'stream' expectAssignable({ url: '/' }) expectAssignable({ autoStart: true }) expectAssignable({ autoStart: false }) expectAssignable({ validate: true }) expectAssignable({ validate: false }) const dispatch: http.RequestListener = function (req, res) { expectAssignable(req) expectAssignable(res) expectType(isInjection(req)) expectType(isInjection(res)) const reply = 'Hello World' res.writeHead(200, { 'Content-Type': 'text/plain', 'Content-Length': reply.length }) res.end(reply) } const expectResponse = function (res: Response | undefined) { if (!res) { return; } expectType(res) console.log(res.payload) expectAssignable(res.json) expectAssignable(res.stream) expectAssignable(res.raw.res) expectAssignable(res.raw.req) expectType(res.stream()) expectType(res.payload) expectType(res.body) expectAssignable>(res.cookies) const cookie = res.cookies[0] expectType(cookie.name) expectType(cookie.value) expectType(cookie.expires) expectType(cookie.maxAge) expectType(cookie.httpOnly) expectType(cookie.secure) expectType(cookie.sameSite) expectType(cookie.additional) } expectType(dispatch) inject(dispatch, { method: 'get', url: '/' }, (err, res) => { expectType(err) expectResponse(res) }) const url = { protocol: 'http', hostname: 'example.com', port: '8080', pathname: 'hello', query: { test: '1234' } } inject(dispatch, { method: 'get', url }, (err, res) => { expectType(err) expectResponse(res) }) inject(dispatch, { method: 'get', url: '/', cookies: { name1: 'value1', value2: 'value2' } }, (err, res) => { expectType(err) expectResponse(res) }) inject(dispatch, { method: 'get', url: '/', query: { name1: 'value1', value2: 'value2' } }, (err, res) => { expectType(err) expectResponse(res) }) inject(dispatch, { method: 'get', url: '/', query: { name1: ['value1', 'value2'] } }, (err, res) => { expectType(err) expectResponse(res) }) inject(dispatch, { method: 'get', url: '/', query: 'name1=value1' }, (err, res) => { expectType(err) expectResponse(res) }) inject(dispatch, { method: 'post', url: '/', payload: { name1: 'value1', value2: 'value2' } }, (err, res) => { expectType(err) expectResponse(res) }) inject(dispatch, { method: 'post', url: '/', body: { name1: 'value1', value2: 'value2' } }, (err, res) => { expectType(err) expectResponse(res) }) expectType( inject(dispatch) .get('/') .end((err, res) => { expectType(err) expectType(res) console.log(res?.payload) }) ) inject(dispatch) .get('/') .then((value) => { expectType(value) }) expectType(inject(dispatch)) expectType>(inject(dispatch).end()) expectType(inject(dispatch, { method: 'get', url: '/' })) // @ts-ignore tsd supports top-level await, but normal ts does not so ignore expectType(await inject(dispatch, { method: 'get', url: '/' })) type ParsedValue = { field: string } // @ts-ignore tsd supports top-level await, but normal ts does not so ignore const response: Response = await inject(dispatch) const parsedValue: ParsedValue = response.json() expectType(parsedValue) const parsedValueUsingGeneric = response.json() expectType(parsedValueUsingGeneric) expectNotAssignable(response) const httpDispatch = function (req: http.IncomingMessage, res: http.ServerResponse) { expectType(isInjection(req)) expectType(isInjection(res)) const reply = 'Hello World' res.writeHead(200, { 'Content-Type': 'text/plain', 'Content-Length': reply.length }) res.end(reply) } inject(httpDispatch, { method: 'get', url: '/' }, (err, res) => { expectType(err) expectResponse(res) })