From 68370478ce6f6648d026b854c72cd78fd18c4b02 Mon Sep 17 00:00:00 2001 From: Jared Wray Date: Tue, 7 Oct 2025 12:10:36 -0700 Subject: [PATCH 1/2] feat: updating documentation for taps --- README.md | 72 +++++++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 68 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index f943c78..1f3befa 100644 --- a/README.md +++ b/README.md @@ -188,6 +188,65 @@ const tap = mock.taps.inject({ }); ``` +### Dynamic Function Response + +You can provide a function that dynamically generates the response based on the incoming request: + +```javascript +// Function response with access to the request object +const tap = mock.taps.inject( + (request) => { + return { + response: { + message: `You requested ${request.url}`, + method: request.method, + timestamp: new Date().toISOString() + }, + statusCode: 200, + headers: { + "X-Request-Path": request.url + } + }; + }, + { url: "/api/*" } +); +``` + +```javascript +// Conditional responses based on request +const tap = mock.taps.inject((request) => { + // Return error for URLs containing 'error' + if (request.url.includes('error')) { + return { + response: { error: "Something went wrong" }, + statusCode: 500 + }; + } + + // Return success for everything else + return { + response: { status: "success" }, + statusCode: 200 + }; +}); +``` + +```javascript +// Dynamic headers based on request +const tap = mock.taps.inject( + (request) => ({ + response: "OK", + statusCode: 200, + headers: { + "X-Original-Method": request.method, + "X-Original-URL": request.url, + "X-Original-Host": request.hostname + } + }), + { url: "/api/mirror" } +); +``` + # API Reference ## MockHttp Class @@ -284,10 +343,15 @@ Access the TapManager via `mockHttp.taps` to inject custom responses. Injects a custom response for requests matching the criteria. **Parameters:** -- `response` (InjectionResponse): - - `response`: string | object | Buffer - The response body - - `statusCode?`: number - HTTP status code (default: 200) - - `headers?`: object - Response headers +- `response` (InjectionResponse | InjectionResponseFunction): + - **Static Response** (InjectionResponse): + - `response`: string | object | Buffer - The response body + - `statusCode?`: number - HTTP status code (default: 200) + - `headers?`: object - Response headers + - **Function Response** (InjectionResponseFunction): + - A function that receives the Fastify request object and returns an InjectionResponse + - `(request: FastifyRequest) => InjectionResponse` + - Allows dynamic response generation based on request properties (url, method, headers, etc.) - `matcher?` (InjectionMatcher) - Optional matching criteria: - `url?`: string - URL path (supports wildcards with `*`) From f5f0e99aa86c515daf1322538a9ec4d72027c638 Mon Sep 17 00:00:00 2001 From: Jared Wray Date: Tue, 7 Oct 2025 12:11:53 -0700 Subject: [PATCH 2/2] Update package.json --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index cbb8fc7..1c8fc78 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@jaredwray/mockhttp", - "version": "0.9.0", + "version": "1.0.0", "description": "Mock Http - Easy way to mock http with httpbin replacement", "type": "module", "main": "dist/index.js",