Skip to content

Commit

Permalink
move stuff to types
Browse files Browse the repository at this point in the history
  • Loading branch information
talentlessguy committed Jul 17, 2024
1 parent d3ff204 commit c4fd429
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 11 deletions.
18 changes: 7 additions & 11 deletions mod.ts
Original file line number Diff line number Diff line change
@@ -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/[email protected]/mod.ts'
function random(min: number, max: number): number {
Expand All @@ -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,
Expand Down Expand Up @@ -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)
Expand Down
14 changes: 14 additions & 0 deletions types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,17 @@ export type Handler = (
) => Response | Promise<Response>

export type HandlerOrListener = Handler | Deno.Listener

export type MakeFetchResponse = { port: number } & Response & Expect

export type FetchFunction = (
url: string | URL,
options?: RequestInit,
) => Promise<MakeFetchResponse>

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
}

0 comments on commit c4fd429

Please sign in to comment.