@jaredwray/mockhttp 1.8.1 → 2.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (64) hide show
  1. package/README.md +29 -7
  2. package/dist/index.d.mts +135 -33
  3. package/dist/index.mjs +360 -342
  4. package/package.json +22 -18
  5. package/site/dist/api/index.html +17983 -0
  6. package/site/dist/api/swagger.json +5487 -0
  7. package/site/dist/changelog/index.html +767 -0
  8. package/site/dist/changelog/page/2/index.html +578 -0
  9. package/site/dist/changelog/v0-1-0/index.html +453 -0
  10. package/site/dist/changelog/v0-2-0/index.html +437 -0
  11. package/site/dist/changelog/v0-2-1/index.html +438 -0
  12. package/site/dist/changelog/v0-2-2/index.html +436 -0
  13. package/site/dist/changelog/v0-2-3/index.html +440 -0
  14. package/site/dist/changelog/v0-3-0/index.html +443 -0
  15. package/site/dist/changelog/v0-4-0/index.html +437 -0
  16. package/site/dist/changelog/v0-4-1/index.html +441 -0
  17. package/site/dist/changelog/v0-4-2/index.html +446 -0
  18. package/site/dist/changelog/v0-4-3/index.html +437 -0
  19. package/site/dist/changelog/v0-5-0/index.html +443 -0
  20. package/site/dist/changelog/v0-6-0/index.html +444 -0
  21. package/site/dist/changelog/v0-6-1/index.html +441 -0
  22. package/site/dist/changelog/v0-7-0/index.html +445 -0
  23. package/site/dist/changelog/v0-8-0/index.html +440 -0
  24. package/site/dist/changelog/v0-9-0/index.html +437 -0
  25. package/site/dist/changelog/v1-0-0/index.html +446 -0
  26. package/site/dist/changelog/v1-0-1/index.html +437 -0
  27. package/site/dist/changelog/v1-1-0/index.html +445 -0
  28. package/site/dist/changelog/v1-2-0/index.html +443 -0
  29. package/site/dist/changelog/v1-3-0/index.html +448 -0
  30. package/site/dist/changelog/v1-4-0/index.html +445 -0
  31. package/site/dist/changelog/v1-5-0/index.html +439 -0
  32. package/site/dist/changelog/v1-5-1/index.html +443 -0
  33. package/site/dist/changelog/v1-6-0/index.html +467 -0
  34. package/site/dist/changelog/v1-7-0/index.html +462 -0
  35. package/site/dist/changelog/v1-8-0/index.html +481 -0
  36. package/site/dist/changelog/v1-8-1/index.html +457 -0
  37. package/site/dist/changelog/v1-9-0/index.html +495 -0
  38. package/site/dist/changelog/v2-0-0/index.html +500 -0
  39. package/site/dist/changelog-latest.json +269 -0
  40. package/site/dist/changelog.json +399 -0
  41. package/site/dist/css/api.css +1113 -0
  42. package/site/dist/css/highlight/highlight.min.js +1433 -0
  43. package/site/dist/css/highlight/styles/base16/docula.css +123 -0
  44. package/site/dist/css/home.css +133 -0
  45. package/site/dist/css/styles.css +1227 -0
  46. package/site/dist/css/variables.css +32 -0
  47. package/site/dist/docs/bins/index.html +942 -0
  48. package/site/dist/docs/configuration/index.html +771 -0
  49. package/site/dist/docs/hosted/index.html +544 -0
  50. package/site/dist/docs/https/index.html +758 -0
  51. package/site/dist/docs/index.html +601 -0
  52. package/site/dist/docs/library/index.html +780 -0
  53. package/site/dist/docs/taps/index.html +708 -0
  54. package/site/dist/favicon.svg +14 -0
  55. package/site/dist/feed.xml +1 -0
  56. package/site/dist/index.html +601 -0
  57. package/site/dist/js/api.js +363 -0
  58. package/site/dist/js/search.js +436 -0
  59. package/site/dist/llms-full.txt +7415 -0
  60. package/site/dist/llms.txt +40 -0
  61. package/site/dist/logo.svg +21 -0
  62. package/site/dist/robots.txt +2 -0
  63. package/site/dist/search-index.json +1 -0
  64. package/site/dist/sitemap.xml +1 -0
package/README.md CHANGED
@@ -17,8 +17,8 @@ A simple HTTP server that can be used to mock HTTP responses for testing purpose
17
17
  * `@fastify/helmet` built in by default
18
18
  * Built with `nodejs`, `typescript`, and `fastify`
19
19
  * Deploy via `docker` or `nodejs`
20
- * Global deployment via [mockhttp.org](https://mockhttp.org) (free service)
21
- * Better API documentation and examples
20
+ * Free hosted service at [mockhttp.org](https://mockhttp.org), running on a Cloudflare Worker
21
+ * Documentation site and interactive OpenAPI reference (Docula)
22
22
  * Auto detect the port that is not in use
23
23
  * Maintained and updated regularly!
24
24
 
@@ -998,9 +998,13 @@ new MockHttp(options?)
998
998
  - `host?`: string - The host to listen on (default: '0.0.0.0')
999
999
  - `autoDetectPort?`: boolean - Auto-detect next available port if in use (default: true)
1000
1000
  - `helmet?`: boolean - Use Helmet for security headers (default: true)
1001
- - `apiDocs?`: boolean - Enable Swagger API documentation (default: true)
1001
+ - `apiDocs?`: boolean - Serve the Docula documentation site and OpenAPI spec (default: true)
1002
+ - `siteDistPath?`: string - Path to the built Docula site (default: the package `site/dist` directory)
1002
1003
  - `rateLimit?`: RateLimitPluginOptions - Configure rate limiting (default: 1000 req/min, localhost excluded)
1003
1004
  - `logging?`: boolean - Enable logging (default: true)
1005
+ - `staticFiles?`: boolean - Serve files from the package `public/` directory (default: true)
1006
+ - `pluginTimeout?`: number - Fastify plugin boot timeout in milliseconds; `0` disables it (default: Fastify's timeout)
1007
+ - `startBins?`: boolean - Start the request-bin cleanup timer during `initialize()` (default: true)
1004
1008
  - `httpBin?`: HttpBinOptions - Configure which httpbin routes to enable
1005
1009
  - `httpMethods?`: boolean - Enable HTTP method routes (default: true)
1006
1010
  - `redirects?`: boolean - Enable redirect routes (default: true)
@@ -1024,8 +1028,12 @@ new MockHttp(options?)
1024
1028
  - `host`: string - Get/set the server host
1025
1029
  - `autoDetectPort`: boolean - Get/set auto-detect port behavior
1026
1030
  - `helmet`: boolean - Get/set Helmet security headers
1027
- - `apiDocs`: boolean - Get/set API documentation
1031
+ - `apiDocs`: boolean - Get/set whether the documentation site and OpenAPI spec are served
1032
+ - `siteDistPath`: string - Get/set the path to the built Docula site
1028
1033
  - `logging`: boolean - Get/set logging enabled state
1034
+ - `staticFiles`: boolean - Get/set whether files from `public/` are served
1035
+ - `pluginTimeout`: number | undefined - Get/set Fastify plugin boot timeout
1036
+ - `startBins`: boolean - Get/set whether bin cleanup starts during `initialize()`
1029
1037
  - `rateLimit`: RateLimitPluginOptions | undefined - Get/set rate limiting options
1030
1038
  - `httpBin`: HttpBinOptions - Get/set httpbin route options
1031
1039
  - `https`: HttpsOptions | undefined - Get/set HTTPS configuration
@@ -1038,6 +1046,10 @@ new MockHttp(options?)
1038
1046
 
1039
1047
  ### Methods
1040
1048
 
1049
+ #### `async initialize()`
1050
+
1051
+ Register plugins and routes without listening. Use `server.inject()` afterwards, or call `start()` to bind a port.
1052
+
1041
1053
  #### `async start()`
1042
1054
 
1043
1055
  Start the Fastify server. If already running, it will be closed and restarted.
@@ -1054,7 +1066,15 @@ Detect the next available port.
1054
1066
 
1055
1067
  #### `async registerApiDocs(fastifyInstance?)`
1056
1068
 
1057
- Register Swagger API documentation routes.
1069
+ Register OpenAPI generation and the Docula documentation site.
1070
+
1071
+ #### `async registerSwagger(fastifyInstance?)`
1072
+
1073
+ Register `@fastify/swagger` and the live `/openapi.json` spec route.
1074
+
1075
+ #### `async registerSite(fastifyInstance?)`
1076
+
1077
+ Serve the built Docula site from `siteDistPath` when the directory exists.
1058
1078
 
1059
1079
  #### `async registerHttpMethods(fastifyInstance?)`
1060
1080
 
@@ -1210,9 +1230,11 @@ A getter that returns whether there are any active injections.
1210
1230
 
1211
1231
  **Returns:** boolean - `true` if there are active injections, `false` otherwise
1212
1232
 
1213
- # About mockhttp.org
1233
+ # About mockhttp.org
1234
+
1235
+ [mockhttp.org](https://mockhttp.org) is a free hosted instance of this codebase for testing. It runs on a [Cloudflare Worker](https://developers.cloudflare.com/workers/) (no containers): documentation is served from Workers Assets, and mock APIs such as `/get` and `/post` run in the Worker. The service is globally available and rate-limited (1000 requests per minute per IP) to prevent abuse.
1214
1236
 
1215
- [mockhttp.org](https://mockhttp.org) is a free service that runs this codebase and allows you to use it for testing purposes. It is a simple way to mock HTTP responses for testing purposes. It is globally available has some limitations on it to prevent abuse such as requests per second. It is ran via [Cloudflare](https://cloudflare.com) and [Google Cloud Run](https://cloud.google.com/run/) across 7 regions globally and can do millions of requests per second.
1237
+ Getting Started is served at `/` and `/docs`, other guides at `/docs/...`, and the interactive HTTP API reference at `/api`. Mock endpoints such as `/get` and `/post` are unchanged.
1216
1238
 
1217
1239
  # Contributing
1218
1240
 
package/dist/index.d.mts CHANGED
@@ -1,34 +1,51 @@
1
1
  import { RateLimitPluginOptions } from "@fastify/rate-limit";
2
2
  import { FastifyInstance, FastifyRequest } from "fastify";
3
3
  import { Hookified, HookifiedOptions } from "hookified";
4
-
5
4
  //#region src/bin-store.d.ts
6
5
  /**
7
6
  * A request captured by a bin, including method, headers, body and metadata.
8
7
  */
9
8
  type CapturedRequest = {
10
- /** Unique identifier for this captured request */id: string; /** Identifier of the bin that captured this request */
11
- binId: string; /** HTTP method (GET, POST, etc.) */
12
- method: string; /** Request URL path + query, relative to the bin (e.g. "/foo?x=1") */
13
- url: string; /** Sub-path after the bin id ("/" if the request hit the bin root) */
14
- path: string; /** Parsed query string parameters */
15
- query: Record<string, string | string[]>; /** Request headers */
16
- headers: Record<string, string | string[]>; /** Client IP address as reported by Fastify */
17
- remoteAddress: string; /** Content-Type header value, if any */
18
- contentType?: string; /** Original body length in bytes */
19
- bodySize: number; /** Body payload as utf8 text, base64-encoded bytes, or null when empty */
20
- body: string | null; /** Encoding used for the `body` field */
21
- bodyEncoding: "utf8" | "base64" | "none"; /** True if the body was longer than the configured `maxBodySize` */
22
- truncated: boolean; /** ISO timestamp of when the request was captured */
9
+ /** Unique identifier for this captured request */
10
+ id: string;
11
+ /** Identifier of the bin that captured this request */
12
+ binId: string;
13
+ /** HTTP method (GET, POST, etc.) */
14
+ method: string;
15
+ /** Request URL path + query, relative to the bin (e.g. "/foo?x=1") */
16
+ url: string;
17
+ /** Sub-path after the bin id ("/" if the request hit the bin root) */
18
+ path: string;
19
+ /** Parsed query string parameters */
20
+ query: Record<string, string | string[]>;
21
+ /** Request headers */
22
+ headers: Record<string, string | string[]>;
23
+ /** Client IP address as reported by Fastify */
24
+ remoteAddress: string;
25
+ /** Content-Type header value, if any */
26
+ contentType?: string;
27
+ /** Original body length in bytes */
28
+ bodySize: number;
29
+ /** Body payload as utf8 text, base64-encoded bytes, or null when empty */
30
+ body: string | null;
31
+ /** Encoding used for the `body` field */
32
+ bodyEncoding: "utf8" | "base64" | "none";
33
+ /** True if the body was longer than the configured `maxBodySize` */
34
+ truncated: boolean;
35
+ /** ISO timestamp of when the request was captured */
23
36
  capturedAt: string;
24
37
  };
25
38
  /**
26
39
  * Metadata for a bin (without its captured requests).
27
40
  */
28
41
  type Bin = {
29
- /** Unique short identifier for the bin */id: string; /** ISO timestamp of bin creation */
30
- createdAt: string; /** ISO timestamp at which the bin expires */
31
- expiresAt: string; /** Number of requests currently stored in the bin */
42
+ /** Unique short identifier for the bin */
43
+ id: string;
44
+ /** ISO timestamp of bin creation */
45
+ createdAt: string;
46
+ /** ISO timestamp at which the bin expires */
47
+ expiresAt: string;
48
+ /** Number of requests currently stored in the bin */
32
49
  requestCount: number;
33
50
  };
34
51
  /**
@@ -66,7 +83,7 @@ interface BinStore {
66
83
  * In-memory implementation of {@link BinStore} backed by two Maps. State is
67
84
  * lost when the process exits.
68
85
  */
69
- declare class InMemoryBinStore implements BinStore {
86
+ export declare class InMemoryBinStore implements BinStore {
70
87
  private readonly _bins;
71
88
  private readonly _requests;
72
89
  createBin(bin: Bin): void;
@@ -85,18 +102,24 @@ declare class InMemoryBinStore implements BinStore {
85
102
  * Configuration options for {@link BinManager}.
86
103
  */
87
104
  type BinManagerOptions = {
88
- /** Storage backend. Defaults to a new {@link InMemoryBinStore}. */store?: BinStore; /** Bin lifetime in milliseconds. Defaults to 24 hours. */
89
- defaultTtlMs?: number; /** Maximum number of requests retained per bin (FIFO). Defaults to 100. */
90
- maxRequestsPerBin?: number; /** Maximum captured body size in bytes. Larger bodies are truncated. Defaults to 1 MiB. */
91
- maxBodySize?: number; /** Length of generated bin and request ids. Defaults to 12. */
92
- idLength?: number; /** Interval between cleanup sweeps in milliseconds. Defaults to 1 minute. */
105
+ /** Storage backend. Defaults to a new {@link InMemoryBinStore}. */
106
+ store?: BinStore;
107
+ /** Bin lifetime in milliseconds. Defaults to 24 hours. */
108
+ defaultTtlMs?: number;
109
+ /** Maximum number of requests retained per bin (FIFO). Defaults to 100. */
110
+ maxRequestsPerBin?: number;
111
+ /** Maximum captured body size in bytes. Larger bodies are truncated. Defaults to 1 MiB. */
112
+ maxBodySize?: number;
113
+ /** Length of generated bin and request ids. Defaults to 12. */
114
+ idLength?: number;
115
+ /** Interval between cleanup sweeps in milliseconds. Defaults to 1 minute. */
93
116
  cleanupIntervalMs?: number;
94
117
  };
95
118
  /**
96
119
  * Manages request bins: ephemeral URL endpoints that capture incoming HTTP
97
120
  * requests so they can be inspected later.
98
121
  */
99
- declare class BinManager {
122
+ export declare class BinManager {
100
123
  private readonly _store;
101
124
  private readonly _defaultTtlMs;
102
125
  private readonly _maxRequestsPerBin;
@@ -192,12 +215,12 @@ type CertificateFileOptions = CertificateOptions & {
192
215
  * Generate a self-signed certificate using only Node.js built-in crypto.
193
216
  * Returns PEM-encoded certificate and private key strings.
194
217
  */
195
- declare function generateCertificate(options?: CertificateOptions): CertificateResult;
218
+ export declare function generateCertificate(options?: CertificateOptions): CertificateResult;
196
219
  /**
197
220
  * Generate a self-signed certificate and write PEM files to disk.
198
221
  * Returns the same CertificateResult as generateCertificate().
199
222
  */
200
- declare function generateCertificateFiles(options: CertificateFileOptions): Promise<CertificateResult>;
223
+ export declare function generateCertificateFiles(options: CertificateFileOptions): Promise<CertificateResult>;
201
224
  //#endregion
202
225
  //#region src/tap-manager.d.ts
203
226
  /**
@@ -371,11 +394,22 @@ type MockHttpOptions = {
371
394
  */
372
395
  helmet?: boolean;
373
396
  /**
374
- * Whether to use Swagger for API documentation. Defaults to true.
397
+ * Whether to serve files from the package `public/` directory (favicon, logos).
398
+ * Defaults to true. Disable this when those files are served by another layer
399
+ * such as Cloudflare Workers Assets.
400
+ */
401
+ staticFiles?: boolean;
402
+ /**
403
+ * Whether to serve the Docula documentation site and OpenAPI spec. Defaults to true.
375
404
  */
376
405
  apiDocs?: boolean;
377
406
  /**
378
- * Whether to use Swagger UI. Defaults to true.
407
+ * Path to the built Docula site (`site/dist`). Defaults to the package's
408
+ * `site/dist` directory. Explicit paths are resolved from the current working directory.
409
+ */
410
+ siteDistPath?: string;
411
+ /**
412
+ * Configure which httpbin-style mock routes to enable. Defaults to all enabled.
379
413
  */
380
414
  httpBin?: HttpBinOptions;
381
415
  /**
@@ -391,6 +425,17 @@ type MockHttpOptions = {
391
425
  * Whether to enable logging. Defaults to true.
392
426
  */
393
427
  logging?: boolean;
428
+ /**
429
+ * Fastify plugin boot timeout in milliseconds. Set to `0` to disable the
430
+ * timeout (needed on Cloudflare Workers, where avvio timers are unreliable).
431
+ */
432
+ pluginTimeout?: number;
433
+ /**
434
+ * Whether to start the request-bin cleanup timer during `initialize()`.
435
+ * Defaults to true. Disable this on Cloudflare Workers, where `setInterval`
436
+ * during startup can hang the isolate.
437
+ */
438
+ startBins?: boolean;
394
439
  /**
395
440
  * HTTPS configuration. Set to true to auto-generate a self-signed certificate,
396
441
  * or provide an object with cert/key strings/paths or auto-generation options.
@@ -414,8 +459,12 @@ declare class MockHttp extends Hookified {
414
459
  private _host;
415
460
  private _autoDetectPort;
416
461
  private _helmet;
462
+ private _staticFiles;
417
463
  private _apiDocs;
464
+ private _siteDistPath;
418
465
  private _logging;
466
+ private _pluginTimeout?;
467
+ private _startBins;
419
468
  private _httpBin;
420
469
  private _rateLimit?;
421
470
  private _http2;
@@ -467,15 +516,35 @@ declare class MockHttp extends Hookified {
467
516
  */
468
517
  set helmet(helmet: boolean);
469
518
  /**
470
- * Whether to use Swagger for API documentation. Defaults to true.
519
+ * Whether to serve files from the package `public/` directory. Defaults to true.
520
+ * @default true
521
+ */
522
+ get staticFiles(): boolean;
523
+ /**
524
+ * Whether to serve files from the package `public/` directory. Defaults to true.
525
+ * @default true
526
+ */
527
+ set staticFiles(staticFiles: boolean);
528
+ /**
529
+ * Whether to serve the Docula documentation site and OpenAPI spec. Defaults to true.
471
530
  * @default true
472
531
  */
473
532
  get apiDocs(): boolean;
474
533
  /**
475
- * Whether to use Swagger for API documentation. Defaults to true.
534
+ * Whether to serve the Docula documentation site and OpenAPI spec. Defaults to true.
476
535
  * @default true
477
536
  */
478
537
  set apiDocs(apiDocs: boolean);
538
+ /**
539
+ * Path to the built Docula site served when `apiDocs` is enabled.
540
+ * @default the package `site/dist` directory
541
+ */
542
+ get siteDistPath(): string;
543
+ /**
544
+ * Path to the built Docula site served when `apiDocs` is enabled.
545
+ * @default the package `site/dist` directory
546
+ */
547
+ set siteDistPath(siteDistPath: string);
479
548
  /**
480
549
  * Whether to enable logging. Defaults to true.
481
550
  * @default true
@@ -486,6 +555,24 @@ declare class MockHttp extends Hookified {
486
555
  * @default true
487
556
  */
488
557
  set logging(logging: boolean);
558
+ /**
559
+ * Fastify plugin boot timeout in milliseconds. `undefined` uses Fastify's default.
560
+ */
561
+ get pluginTimeout(): number | undefined;
562
+ /**
563
+ * Fastify plugin boot timeout in milliseconds. `undefined` uses Fastify's default.
564
+ */
565
+ set pluginTimeout(pluginTimeout: number | undefined);
566
+ /**
567
+ * Whether to start the request-bin cleanup timer during `initialize()`.
568
+ * @default true
569
+ */
570
+ get startBins(): boolean;
571
+ /**
572
+ * Whether to start the request-bin cleanup timer during `initialize()`.
573
+ * @default true
574
+ */
575
+ set startBins(startBins: boolean);
489
576
  /**
490
577
  * HTTPS configuration. Set to true to auto-generate a self-signed certificate,
491
578
  * or provide an HttpsOptions object with cert/key or auto-generation options.
@@ -566,6 +653,11 @@ declare class MockHttp extends Hookified {
566
653
  * The BinManager instance for managing request bins.
567
654
  */
568
655
  set bins(bins: BinManager);
656
+ /**
657
+ * Register plugins and routes without listening. Use `inject()` against
658
+ * `server`, or call `start()` to bind a port.
659
+ */
660
+ initialize(): Promise<void>;
569
661
  /**
570
662
  * Start the Fastify server. If the server is already running, it will be closed and restarted.
571
663
  */
@@ -580,10 +672,20 @@ declare class MockHttp extends Hookified {
580
672
  */
581
673
  detectPort(): Promise<number>;
582
674
  /**
583
- * This will register the API documentation routes including openapi and swagger ui.
675
+ * Register OpenAPI generation and the Docula documentation site.
584
676
  * @param fastifyInstance - the server instance to register the routes on.
585
677
  */
586
678
  registerApiDocs(fastifyInstance?: FastifyInstance): Promise<void>;
679
+ /**
680
+ * Register `@fastify/swagger` and the live `/openapi.json` spec route.
681
+ * @param fastifyInstance - the server instance to register the routes on.
682
+ */
683
+ registerSwagger(fastifyInstance?: FastifyInstance): Promise<void>;
684
+ /**
685
+ * Serve the built Docula site from `siteDistPath` when the directory exists.
686
+ * @param fastifyInstance - the server instance to register the routes on.
687
+ */
688
+ registerSite(fastifyInstance?: FastifyInstance): Promise<void>;
587
689
  /**
588
690
  * Register the HTTP methods routes.
589
691
  * @param fastifyInstance - the server instance to register the routes on.
@@ -645,6 +747,6 @@ declare class MockHttp extends Hookified {
645
747
  }
646
748
  //#endregion
647
749
  //#region src/index.d.ts
648
- declare const start: () => Promise<MockHttp>;
750
+ export declare const start: () => Promise<MockHttp>;
649
751
  //#endregion
650
- export { type Bin, BinManager, type BinManagerOptions, type BinStore, type CapturedRequest, type CertificateFileOptions, type CertificateOptions, type CertificateResult, type HttpsOptions, InMemoryBinStore, type InjectionMatcher, type InjectionResponse, type InjectionTap, MockHttp as default, MockHttp as mockhttp, generateCertificate, generateCertificateFiles, start };
752
+ export { type Bin, type BinManagerOptions, type BinStore, type CapturedRequest, type CertificateFileOptions, type CertificateOptions, type CertificateResult, type HttpsOptions, type InjectionMatcher, type InjectionResponse, type InjectionTap, MockHttp as default, MockHttp as mockhttp };