Skip to content

Commit

Permalink
fix: ts type definitions and specs
Browse files Browse the repository at this point in the history
  • Loading branch information
blephy committed Jan 31, 2025
1 parent 7d9c679 commit 78bb6b2
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 29 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@
"test:node-test": "borp -p \"test/node-test/**/*.js\"",
"test:tdd": "borp --expose-gc -p \"test/*.js\"",
"test:tdd:node-test": "borp -p \"test/node-test/**/*.js\" -w",
"test:typescript": "tsd && tsc test/imports/undici-import.ts --downlevelIteration --typeRoots ./types --noEmit && tsc ./types/*.d.ts --noEmit --typeRoots ./types",
"test:typescript": "tsd && tsc test/imports/undici-import.ts --typeRoots ./types --noEmit && tsc ./types/*.d.ts --noEmit --typeRoots ./types",
"test:webidl": "borp -p \"test/webidl/*.js\"",
"test:websocket": "borp -p \"test/websocket/*.js\"",
"test:websocket:autobahn": "node test/autobahn/client.js",
Expand Down
21 changes: 1 addition & 20 deletions test/imports/undici-import.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { expectType } from 'tsd'
import { Dispatcher, interceptors, MockCallHistory, MockCallHistoryLog, request } from '../../'
import { kMockCallHistoryAddLog, kMockCallHistoryDeleteAll } from '../../lib/mock/mock-symbols'
import { Dispatcher, interceptors, request } from '../../'

async function exampleCode () {
const retry = interceptors.retry()
Expand All @@ -14,22 +13,4 @@ async function exampleCode () {
await request('http://localhost:3000/foo')
}

function checkMockCallHistoryIterator () {
const mockCallHistory = new MockCallHistory('hello')
// @ts-ignore -- not relevant here
mockCallHistory[kMockCallHistoryAddLog]({ path: '/', origin: 'http://localhost:4000', method: 'GET' })
// @ts-ignore -- not relevant here
mockCallHistory[kMockCallHistoryAddLog]({ path: '/endpoint', origin: 'http://localhost:4000', method: 'GET' })

expectType<Array<MockCallHistoryLog>>([...mockCallHistory])

for (const log of mockCallHistory) {
expectType<MockCallHistoryLog>(log)
}

// @ts-ignore -- not relevant here
MockCallHistory[kMockCallHistoryDeleteAll]()
}

exampleCode()
checkMockCallHistoryIterator()
58 changes: 58 additions & 0 deletions test/types/mock-call-history.test-d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import { expectType } from 'tsd'
import { MockAgent, MockCallHistory, MockCallHistoryLog } from '../..'
import { MockScope } from '../../types/mock-interceptor'

{
const mockAgent = new MockAgent()
expectType<MockCallHistory | undefined>(mockAgent.getCallHistory())
expectType<MockCallHistory | undefined>(mockAgent.getCallHistory('hello'))
expectType<void>(mockAgent.clearAllCallHistory())
expectType<MockAgent>(mockAgent.enableCallHistory())
expectType<MockAgent>(mockAgent.disableCallHistory())
expectType<MockScope<object>>(mockAgent.get('http://localhost:3000').intercept({ path: '/' }).reply(200, 'hello').registerCallHistory('local-history'))
}

{
const mockAgent = new MockAgent()
expectType<MockCallHistoryLog | undefined>(mockAgent.getCallHistory()?.firstCall())
expectType<MockCallHistoryLog | undefined>(mockAgent.getCallHistory()?.lastCall())
expectType<MockCallHistoryLog | undefined>(mockAgent.getCallHistory()?.nthCall(1))
expectType<Array<MockCallHistoryLog> | undefined>(mockAgent.getCallHistory()?.calls())
expectType<Array<MockCallHistoryLog> | undefined>(mockAgent.getCallHistory()?.filterCallsByFullUrl(''))
expectType<Array<MockCallHistoryLog> | undefined>(mockAgent.getCallHistory()?.filterCallsByHash(''))
expectType<Array<MockCallHistoryLog> | undefined>(mockAgent.getCallHistory()?.filterCallsByHost(''))
expectType<Array<MockCallHistoryLog> | undefined>(mockAgent.getCallHistory()?.filterCallsByMethod(''))
expectType<Array<MockCallHistoryLog> | undefined>(mockAgent.getCallHistory()?.filterCallsByOrigin(''))
expectType<Array<MockCallHistoryLog> | undefined>(mockAgent.getCallHistory()?.filterCallsByPath(''))
expectType<Array<MockCallHistoryLog> | undefined>(mockAgent.getCallHistory()?.filterCallsByPort(''))
expectType<Array<MockCallHistoryLog> | undefined>(mockAgent.getCallHistory()?.filterCallsByProtocol(''))
expectType<Array<MockCallHistoryLog> | undefined>(mockAgent.getCallHistory()?.filterCalls((log) => log.path === '/'))
expectType<Array<MockCallHistoryLog> | undefined>(mockAgent.getCallHistory()?.filterCalls(/path->\//))
expectType<Array<MockCallHistoryLog> | undefined>(mockAgent.getCallHistory()?.filterCalls({ method: 'POST' }))
expectType<Array<MockCallHistoryLog> | undefined>(mockAgent.getCallHistory()?.filterCalls({ method: 'POST' }, { operator: 'AND' }))

const callHistory = mockAgent.getCallHistory()

if (callHistory !== undefined) {
expectType<Array<MockCallHistoryLog>>([...callHistory])
expectType<Set<MockCallHistoryLog>>(new Set(callHistory))

for (const log of callHistory) {
expectType<MockCallHistoryLog>(log)
expectType<string | null | undefined>(log.body)
expectType<string>(log.fullUrl)
expectType<string>(log.hash)
expectType<Record<string, string | Array<string>> | null | undefined>(log.headers)
expectType<string>(log.host)
expectType<string>(log.method)
expectType<string>(log.origin)
expectType<string>(log.path)
expectType<string>(log.port)
expectType<string>(log.protocol)
expectType<Record<string, string>>(log.searchParams)
expectType<Map<MockCallHistoryLog.MockCallHistoryLogProperties, string | Record<string, string | string[]> | null | undefined>>(log.toMap())
expectType<string>(log.toString())
}
}
expectType<void>(mockAgent.getCallHistory()?.clear())
}
31 changes: 23 additions & 8 deletions types/mock-call-history.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ declare namespace MockCallHistoryLog {
/** a log reflecting request configuration */
declare class MockCallHistoryLog {
constructor (requestInit: Dispatcher.DispatchOptions)
/** protocol used. */
/** protocol used. ie. 'https:' or 'http:' etc... */
protocol: string
/** request's host. ie. 'https:' or 'http:' etc... */
/** request's host. */
host: string
/** request's port. */
port: string
Expand All @@ -23,22 +23,29 @@ declare class MockCallHistoryLog {
/** the full url requested. */
fullUrl: string
/** request's method. */
method: Dispatcher.DispatchOptions['method']
method: string
/** search params. */
searchParams: Record<string, string>
/** request's body */
body: Dispatcher.DispatchOptions['body']
body: string | null | undefined
/** request's headers */
headers: Dispatcher.DispatchOptions['headers']
headers: Record<string, string | string[]> | null | undefined

/** returns an Map of property / value pair */
toMap (): Map<MockCallHistoryLog.MockCallHistoryLogProperties, string | Dispatcher.DispatchOptions['headers'] | Dispatcher.DispatchOptions['body'] | Dispatcher.DispatchOptions['method']>
toMap (): Map<MockCallHistoryLog.MockCallHistoryLogProperties, string | Record<string, string | string[]> | null | undefined>

/** returns a string computed with all key value pair */
toString (): string
}

declare namespace MockCallHistory {
export type FilterCallsOperator = 'AND' | 'OR'

/** modify the filtering behavior */
export interface FilterCallsOptions {
/** the operator to apply when filtering. 'OR' will adds any MockCallHistoryLog matching any criteria given. 'AND' will adds only MockCallHistoryLog matching every criteria given. (default 'OR') */
operator?: FilterCallsOperator | Lowercase<FilterCallsOperator>
}
/** a function to be executed for filtering MockCallHistoryLog */
export type FilterCallsFunctionCriteria = (log: MockCallHistoryLog) => boolean

Expand All @@ -47,13 +54,21 @@ declare namespace MockCallHistory {

/** an object to execute multiple filtering at once */
export interface FilterCallsObjectCriteria extends Record<string, FilterCallsParameter> {
/** filter by request protocol. ie https: */
protocol?: FilterCallsParameter;
/** filter by request host. */
host?: FilterCallsParameter;
/** filter by request port. */
port?: FilterCallsParameter;
/** filter by request origin. */
origin?: FilterCallsParameter;
/** filter by request path. */
path?: FilterCallsParameter;
/** filter by request hash. */
hash?: FilterCallsParameter;
/** filter by request fullUrl. */
fullUrl?: FilterCallsParameter;
/** filter by request method. */
method?: FilterCallsParameter;
}
}
Expand All @@ -69,8 +84,8 @@ declare class MockCallHistory {
lastCall (): MockCallHistoryLog | undefined
/** returns the nth MockCallHistoryLog. */
nthCall (position: number): MockCallHistoryLog | undefined
/** return all MockCallHistoryLog matching any of criteria given. */
filterCalls (criteria: MockCallHistory.FilterCallsObjectCriteria | MockCallHistory.FilterCallsFunctionCriteria | RegExp): Array<MockCallHistoryLog>
/** return all MockCallHistoryLog matching any of criteria given. if an object is used with multiple properties, you can change the operator to apply during filtering on options */
filterCalls (criteria: MockCallHistory.FilterCallsFunctionCriteria | MockCallHistory.FilterCallsObjectCriteria | RegExp, options?: MockCallHistory.FilterCallsOptions): Array<MockCallHistoryLog>
/** return all MockCallHistoryLog matching the given protocol. if a string is given, it is matched with includes */
filterCallsByProtocol (protocol: MockCallHistory.FilterCallsParameter): Array<MockCallHistoryLog>
/** return all MockCallHistoryLog matching the given host. if a string is given, it is matched with includes */
Expand Down

0 comments on commit 78bb6b2

Please sign in to comment.