@schmock/core 1.0.3 → 1.1.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 (39) hide show
  1. package/dist/builder.d.ts +13 -5
  2. package/dist/builder.d.ts.map +1 -1
  3. package/dist/builder.js +147 -60
  4. package/dist/constants.d.ts +6 -0
  5. package/dist/constants.d.ts.map +1 -0
  6. package/dist/constants.js +20 -0
  7. package/dist/errors.d.ts.map +1 -1
  8. package/dist/errors.js +3 -1
  9. package/dist/index.d.ts +3 -3
  10. package/dist/index.d.ts.map +1 -1
  11. package/dist/index.js +20 -11
  12. package/dist/parser.d.ts.map +1 -1
  13. package/dist/parser.js +2 -17
  14. package/dist/types.d.ts +17 -210
  15. package/dist/types.d.ts.map +1 -1
  16. package/dist/types.js +1 -0
  17. package/package.json +4 -4
  18. package/src/builder.test.ts +2 -2
  19. package/src/builder.ts +232 -108
  20. package/src/constants.test.ts +59 -0
  21. package/src/constants.ts +25 -0
  22. package/src/errors.ts +3 -1
  23. package/src/index.ts +41 -29
  24. package/src/namespace.test.ts +3 -2
  25. package/src/parser.property.test.ts +495 -0
  26. package/src/parser.ts +2 -20
  27. package/src/route-matching.test.ts +1 -1
  28. package/src/steps/async-support.steps.ts +101 -91
  29. package/src/steps/basic-usage.steps.ts +49 -36
  30. package/src/steps/developer-experience.steps.ts +110 -94
  31. package/src/steps/error-handling.steps.ts +90 -66
  32. package/src/steps/fluent-api.steps.ts +75 -72
  33. package/src/steps/http-methods.steps.ts +33 -33
  34. package/src/steps/performance-reliability.steps.ts +52 -88
  35. package/src/steps/plugin-integration.steps.ts +176 -176
  36. package/src/steps/request-history.steps.ts +333 -0
  37. package/src/steps/state-concurrency.steps.ts +418 -316
  38. package/src/steps/stateful-workflows.steps.ts +138 -136
  39. package/src/types.ts +20 -259
package/dist/types.d.ts CHANGED
@@ -1,211 +1,18 @@
1
- /**
2
- * JSON Schema type (simplified for core package)
3
- * Full schema support available via @schmock/schema
4
- */
5
- export interface JSONSchema {
6
- type?: string | string[];
7
- properties?: Record<string, JSONSchema>;
8
- items?: JSONSchema | JSONSchema[];
9
- required?: string[];
10
- enum?: any[];
11
- const?: any;
12
- [key: string]: any;
13
- }
14
- /**
15
- * HTTP methods supported by Schmock
16
- */
17
- export type HttpMethod = "GET" | "POST" | "PUT" | "DELETE" | "PATCH" | "HEAD" | "OPTIONS";
18
- /**
19
- * Route key format: 'METHOD /path'
20
- *
21
- * @example
22
- * 'GET /users'
23
- * 'POST /users/:id'
24
- * 'DELETE /api/posts/:postId/comments/:commentId'
25
- */
26
- export type RouteKey = `${HttpMethod} ${string}`;
27
- /**
28
- * Plugin interface for extending Schmock functionality
29
- */
30
- export interface Plugin {
31
- /** Unique plugin identifier */
32
- name: string;
33
- /** Plugin version (semver) */
34
- version?: string;
35
- /**
36
- * Process the request through this plugin
37
- * First plugin to set response becomes the generator, others transform
38
- * @param context - Plugin context with request details
39
- * @param response - Response from previous plugin (if any)
40
- * @returns Updated context and response
41
- */
42
- process(context: PluginContext, response?: any): PluginResult | Promise<PluginResult>;
43
- /**
44
- * Called when an error occurs
45
- * Can handle, transform, or suppress errors
46
- * @param error - The error that occurred
47
- * @param context - Plugin context
48
- * @returns Modified error, response data, or void to continue error propagation
49
- */
50
- onError?(error: Error, context: PluginContext): Error | ResponseResult | undefined | Promise<Error | ResponseResult | undefined>;
51
- }
52
- /**
53
- * Result returned by plugin process method
54
- */
55
- export interface PluginResult {
56
- /** Updated context */
57
- context: PluginContext;
58
- /** Response data (if generated/modified) */
59
- response?: any;
60
- }
61
- /**
62
- * Context passed through plugin pipeline
63
- */
64
- export interface PluginContext {
65
- /** Request path */
66
- path: string;
67
- /** Matched route configuration */
68
- route: any;
69
- /** HTTP method */
70
- method: HttpMethod;
71
- /** Route parameters */
72
- params: Record<string, string>;
73
- /** Query parameters */
74
- query: Record<string, string>;
75
- /** Request headers */
76
- headers: Record<string, string>;
77
- /** Request body */
78
- body?: any;
79
- /** Shared state between plugins for this request */
80
- state: Map<string, any>;
81
- /** Route-specific state */
82
- routeState?: any;
83
- }
84
- /**
85
- * Global configuration options for the mock instance
86
- */
87
- export interface GlobalConfig {
88
- /** Base path prefix for all routes */
89
- namespace?: string;
90
- /** Response delay in ms, or [min, max] for random delay */
91
- delay?: number | [number, number];
92
- /** Enable debug mode for detailed logging */
93
- debug?: boolean;
94
- /** Initial shared state object */
95
- state?: any;
96
- }
97
- /**
98
- * Route-specific configuration options
99
- */
100
- export interface RouteConfig {
101
- /** MIME type for content type validation (auto-detected if not provided) */
102
- contentType?: string;
103
- /** Additional route-specific options */
104
- [key: string]: any;
105
- }
106
- /**
107
- * Generator types that can be passed to route definitions
108
- */
109
- export type Generator = GeneratorFunction | StaticData | JSONSchema;
110
- /**
111
- * Function that generates responses
112
- */
113
- export type GeneratorFunction = (context: RequestContext) => ResponseResult | Promise<ResponseResult>;
114
- /**
115
- * Static data (non-function) that gets returned as-is
116
- */
117
- export type StaticData = any;
118
- /**
119
- * Context passed to generator functions
120
- */
121
- export interface RequestContext {
122
- /** HTTP method */
123
- method: HttpMethod;
124
- /** Request path */
125
- path: string;
126
- /** Route parameters (e.g., :id) */
127
- params: Record<string, string>;
128
- /** Query string parameters */
129
- query: Record<string, string>;
130
- /** Request headers */
131
- headers: Record<string, string>;
132
- /** Request body (for POST, PUT, PATCH) */
133
- body?: any;
134
- /** Shared mutable state */
135
- state: any;
136
- }
137
- /**
138
- * Response result types:
139
- * - Any value: returns as 200 OK
140
- * - [status, body]: custom status with body
141
- * - [status, body, headers]: custom status, body, and headers
142
- */
143
- export type ResponseResult = any | [number, any] | [number, any, Record<string, string>];
144
- /**
145
- * Response object returned by handle method
146
- */
147
- export interface Response {
148
- status: number;
149
- body: any;
150
- headers: Record<string, string>;
151
- }
152
- /**
153
- * Options for handle method
154
- */
155
- export interface RequestOptions {
156
- headers?: Record<string, string>;
157
- body?: any;
158
- query?: Record<string, string>;
159
- }
160
- /**
161
- * Main callable mock instance interface
162
- */
163
- export interface CallableMockInstance {
164
- /**
165
- * Define a route by calling the instance directly
166
- *
167
- * @param route - Route pattern in format 'METHOD /path'
168
- * @param generator - Response generator (function, static data, or schema)
169
- * @param config - Route-specific configuration
170
- * @returns The same instance for method chaining
171
- *
172
- * @example
173
- * ```typescript
174
- * const mock = schmock()
175
- * mock('GET /users', () => [...users], { contentType: 'application/json' })
176
- * mock('POST /users', userData, { contentType: 'application/json' })
177
- * ```
178
- */
179
- (route: RouteKey, generator: Generator, config?: RouteConfig): CallableMockInstance;
180
- /**
181
- * Add a plugin to the pipeline
182
- *
183
- * @param plugin - Plugin to add to the pipeline
184
- * @returns The same instance for method chaining
185
- *
186
- * @example
187
- * ```typescript
188
- * mock('GET /users', generator, config)
189
- * .pipe(authPlugin())
190
- * .pipe(corsPlugin())
191
- * ```
192
- */
193
- pipe(plugin: Plugin): CallableMockInstance;
194
- /**
195
- * Handle a request and return a response
196
- *
197
- * @param method - HTTP method
198
- * @param path - Request path
199
- * @param options - Request options (headers, body, query)
200
- * @returns Promise resolving to response object
201
- *
202
- * @example
203
- * ```typescript
204
- * const response = await mock.handle('GET', '/users', {
205
- * headers: { 'Authorization': 'Bearer token' }
206
- * })
207
- * ```
208
- */
209
- handle(method: HttpMethod, path: string, options?: RequestOptions): Promise<Response>;
210
- }
1
+ export type HttpMethod = Schmock.HttpMethod;
2
+ export type RouteKey = Schmock.RouteKey;
3
+ export type ResponseBody = Schmock.ResponseBody;
4
+ export type ResponseResult = Schmock.ResponseResult;
5
+ export type RequestContext = Schmock.RequestContext;
6
+ export type Response = Schmock.Response;
7
+ export type RequestOptions = Schmock.RequestOptions;
8
+ export type GlobalConfig = Schmock.GlobalConfig;
9
+ export type RouteConfig = Schmock.RouteConfig;
10
+ export type Generator = Schmock.Generator;
11
+ export type GeneratorFunction = Schmock.GeneratorFunction;
12
+ export type CallableMockInstance = Schmock.CallableMockInstance;
13
+ export type Plugin = Schmock.Plugin;
14
+ export type PluginContext = Schmock.PluginContext;
15
+ export type PluginResult = Schmock.PluginResult;
16
+ export type StaticData = Schmock.StaticData;
17
+ export type RequestRecord = Schmock.RequestRecord;
211
18
  //# sourceMappingURL=types.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA;;;GAGG;AACH,MAAM,WAAW,UAAU;IACzB,IAAI,CAAC,EAAE,MAAM,GAAG,MAAM,EAAE,CAAC;IACzB,UAAU,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,UAAU,CAAC,CAAC;IACxC,KAAK,CAAC,EAAE,UAAU,GAAG,UAAU,EAAE,CAAC;IAClC,QAAQ,CAAC,EAAE,MAAM,EAAE,CAAC;IACpB,IAAI,CAAC,EAAE,GAAG,EAAE,CAAC;IACb,KAAK,CAAC,EAAE,GAAG,CAAC;IACZ,CAAC,GAAG,EAAE,MAAM,GAAG,GAAG,CAAC;CACpB;AAED;;GAEG;AACH,MAAM,MAAM,UAAU,GAClB,KAAK,GACL,MAAM,GACN,KAAK,GACL,QAAQ,GACR,OAAO,GACP,MAAM,GACN,SAAS,CAAC;AAEd;;;;;;;GAOG;AACH,MAAM,MAAM,QAAQ,GAAG,GAAG,UAAU,IAAI,MAAM,EAAE,CAAC;AAEjD;;GAEG;AACH,MAAM,WAAW,MAAM;IACrB,+BAA+B;IAC/B,IAAI,EAAE,MAAM,CAAC;IACb,8BAA8B;IAC9B,OAAO,CAAC,EAAE,MAAM,CAAC;IAEjB;;;;;;OAMG;IACH,OAAO,CACL,OAAO,EAAE,aAAa,EACtB,QAAQ,CAAC,EAAE,GAAG,GACb,YAAY,GAAG,OAAO,CAAC,YAAY,CAAC,CAAC;IAExC;;;;;;OAMG;IACH,OAAO,CAAC,CACN,KAAK,EAAE,KAAK,EACZ,OAAO,EAAE,aAAa,GAEpB,KAAK,GACL,cAAc,GACd,SAAS,GACT,OAAO,CAAC,KAAK,GAAG,cAAc,GAAG,SAAS,CAAC,CAAC;CACjD;AAED;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AY,CAAC;AAChD,MAAM,MAAM,UAAU,GAAG,OAAO,CAAC,UAAU,CAAC;AAC5C,MAAM,MAAM,aAAa,GAAG,OAAO,CAAC,aAAa,CAAC"}
package/dist/types.js CHANGED
@@ -1 +1,2 @@
1
+ /// <reference path="../../../types/schmock.d.ts" />
1
2
  export {};
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@schmock/core",
3
3
  "description": "Core functionality for Schmock",
4
- "version": "1.0.3",
4
+ "version": "1.1.0",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",
7
7
  "types": "./dist/index.d.ts",
@@ -31,9 +31,9 @@
31
31
  },
32
32
  "license": "MIT",
33
33
  "devDependencies": {
34
- "@amiceli/vitest-cucumber": "^6.1.0",
35
- "@types/node": "^25.0.0",
36
- "@vitest/ui": "^4.0.15",
34
+ "@amiceli/vitest-cucumber": "^6.2.0",
35
+ "@types/node": "^25.1.0",
36
+ "@vitest/ui": "^4.0.16",
37
37
  "vitest": "^4.0.15"
38
38
  }
39
39
  }
@@ -271,9 +271,9 @@ describe("schmock callable API", () => {
271
271
  const mock = schmock();
272
272
  mock("GET /users", [{ id: 1, name: "John" }], {}).pipe({
273
273
  name: "test-plugin",
274
- process: (ctx, response) => ({
274
+ process: (ctx, pluginResponse) => ({
275
275
  context: ctx,
276
- response: { data: response, processed: true },
276
+ response: { data: pluginResponse, processed: true },
277
277
  }),
278
278
  });
279
279