diff --git a/mod.ts b/mod.ts index d2a7250..fa28cd6 100644 --- a/mod.ts +++ b/mod.ts @@ -1,5 +1,10 @@ import { assertEquals, assertMatch, parseMediaType } from './deps.ts' -import { Handler, HandlerOrListener } from './types.ts' +import type { + FetchFunction, + Handler, + HandlerOrListener, + MakeFetchResponse, +} from './types.ts' // credit - 'https://deno.land/x/free_port@v1.2.0/mod.ts' function random(min: number, max: number): number { @@ -22,15 +27,6 @@ const getFreeListener = ( throw new Error('Unable to get free port') } -type Expect = { - expectStatus: (a: number, b?: string) => Expect - expectHeader: (a: string, b: string | RegExp | null | string[]) => Expect - expectBody: (a: unknown) => void - expect: (a: unknown, b?: unknown) => Expect -} - -type MakeFetchResponse = { port: number } & Response & Expect - const fetchEndpoint = async ( port: number, url: string | URL, @@ -97,7 +93,7 @@ const makeFetchPromise = (handlerOrListener: HandlerOrListener) => { } } -export const makeFetch = (h: HandlerOrListener) => { +export const makeFetch = (h: HandlerOrListener): FetchFunction => { const { resp, port } = makeFetchPromise(h) async function fetch(url: string | URL, options?: RequestInit) { const { res, data } = await resp(url, options) diff --git a/types.ts b/types.ts index d067f07..a792f09 100644 --- a/types.ts +++ b/types.ts @@ -4,3 +4,17 @@ export type Handler = ( ) => Response | Promise export type HandlerOrListener = Handler | Deno.Listener + +export type MakeFetchResponse = { port: number } & Response & Expect + +export type FetchFunction = ( + url: string | URL, + options?: RequestInit, +) => Promise + +export type Expect = { + expectStatus: (a: number, b?: string) => Expect + expectHeader: (a: string, b: string | RegExp | null | string[]) => Expect + expectBody: (a: unknown) => void + expect: (a: unknown, b?: unknown) => Expect +}