Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
fixtures
coverage
playground
*.d.ts
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ node_modules
coverage
playground
*.tgz
package-lock.json
40 changes: 40 additions & 0 deletions fetch.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// Type definitions for fetch.js

/**
* Options for fetchRecorder
*/
export interface FetchRecorderOptions {
/**
* Base fetch implementation to use. Defaults to globalThis.fetch
*/
fetch?: typeof fetch
}

/**
* Creates a fetch implementation that records all requests to a log array
* @param log - Array to append request/response logs to
* @param options - Optional configuration
* @returns A fetch function that records requests
*/
export function fetchRecorder(
log: any[],
options?: FetchRecorderOptions
): typeof fetch

/**
* Creates a fetch implementation that replays requests from a log array
* @param log - Array of recorded request/response logs
* @returns A fetch function that replays recorded requests
*/
export function fetchReplayer(log: any[]): typeof fetch

/**
* Creates a custom fetch implementation using a lookup function
* @param lookup - Function to lookup request/response data
* @param fallback - Optional fallback fetch function
* @returns A fetch function
*/
export function fetchApi(
lookup: (resource: RequestInfo | URL, options?: RequestInit) => Promise<any>,
fallback?: typeof fetch
): typeof fetch
16 changes: 16 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// Re-export types and functions from individual modules
export type { FetchRecorderOptions } from './fetch.js'

export { fetchRecorder, fetchReplayer, fetchApi } from './fetch.js'

export type {
WebSocketRecorderOptions,
WebSocketReplayerOptions,
} from './websocket.js'

export { WebSocketRecorder, WebSocketReplayer } from './websocket.js'

export type { PrettyJSONOptions } from './util.js'

export { prettyJSON } from './util.js'

20 changes: 17 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,30 @@
"license": "MIT",
"type": "module",
"main": "./index.js",
"types": "./index.d.ts",
"exports": {
".": "./index.js",
"./fetch": "./fetch.js",
"./websocket": "./websocket.js"
".": {
"types": "./index.d.ts",
"default": "./index.js"
},
"./fetch": {
"types": "./fetch.d.ts",
"default": "./fetch.js"
},
"./websocket": {
"types": "./websocket.d.ts",
"default": "./websocket.js"
}
},
"files": [
"fetch.js",
"fetch.d.ts",
"index.js",
"index.d.ts",
"util.js",
"util.d.ts",
"websocket.js",
"websocket.d.ts",
"CHANGELOG.md"
],
"scripts": {
Expand Down
20 changes: 20 additions & 0 deletions util.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// Type definitions for util.js

/**
* Options for prettyJSON formatting
*/
export interface PrettyJSONOptions {
/**
* Maximum width for inline formatting. Default: 120
*/
width?: number
}

/**
* Pretty-prints JSON data for human-readable logs
* @param data - Data to format
* @param options - Formatting options
* @returns Pretty-printed JSON string
*/
export function prettyJSON(data: any, options?: PrettyJSONOptions): string

46 changes: 46 additions & 0 deletions websocket.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// Type definitions for websocket.js

/**
* Options for WebSocketRecorder
*/
export interface WebSocketRecorderOptions {
/**
* Base WebSocket implementation to use. Defaults to globalThis.WebSocket
*/
WebSocket?: typeof WebSocket
}

/**
* Creates a WebSocket class that records all sessions to a log array
* @param log - Array to append session logs to
* @param options - Optional configuration
* @returns A WebSocket class that records sessions
*/
export function WebSocketRecorder(
log: any[],
options?: WebSocketRecorderOptions
): typeof WebSocket

/**
* Options for WebSocketReplayer
*/
export interface WebSocketReplayerOptions {
/**
* Delay interval for replaying events
* - 0: immediate (default)
* - Infinity: match original timing
* - number: maximum delay between events
*/
interval?: number
}

/**
* Creates a WebSocket class that replays sessions from a log array
* @param log - Array of recorded session logs
* @param options - Optional configuration
* @returns A WebSocket class that replays recorded sessions
*/
export function WebSocketReplayer(
log: any[],
options?: WebSocketReplayerOptions
): typeof WebSocket