@schmock/core 1.0.4 → 1.2.1

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.
package/src/types.ts CHANGED
@@ -1,271 +1,20 @@
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
- /**
16
- * HTTP methods supported by Schmock
17
- */
18
- export type HttpMethod =
19
- | "GET"
20
- | "POST"
21
- | "PUT"
22
- | "DELETE"
23
- | "PATCH"
24
- | "HEAD"
25
- | "OPTIONS";
26
-
27
- /**
28
- * Route key format: 'METHOD /path'
29
- *
30
- * @example
31
- * 'GET /users'
32
- * 'POST /users/:id'
33
- * 'DELETE /api/posts/:postId/comments/:commentId'
34
- */
35
- export type RouteKey = `${HttpMethod} ${string}`;
36
-
37
- /**
38
- * Plugin interface for extending Schmock functionality
39
- */
40
- export interface Plugin {
41
- /** Unique plugin identifier */
42
- name: string;
43
- /** Plugin version (semver) */
44
- version?: string;
45
-
46
- /**
47
- * Process the request through this plugin
48
- * First plugin to set response becomes the generator, others transform
49
- * @param context - Plugin context with request details
50
- * @param response - Response from previous plugin (if any)
51
- * @returns Updated context and response
52
- */
53
- process(
54
- context: PluginContext,
55
- response?: unknown,
56
- ): PluginResult | Promise<PluginResult>;
57
-
58
- /**
59
- * Called when an error occurs
60
- * Can handle, transform, or suppress errors
61
- * @param error - The error that occurred
62
- * @param context - Plugin context
63
- * @returns Modified error, response data, or void to continue error propagation
64
- */
65
- onError?(
66
- error: Error,
67
- context: PluginContext,
68
- ):
69
- | Error
70
- | ResponseResult
71
- | undefined
72
- | Promise<Error | ResponseResult | undefined>;
73
- }
74
-
75
- /**
76
- * Result returned by plugin process method
77
- */
78
- export interface PluginResult {
79
- /** Updated context */
80
- context: PluginContext;
81
- /** Response data (if generated/modified) */
82
- response?: unknown;
83
- }
84
-
85
- /**
86
- * Context passed through plugin pipeline
87
- */
88
- export interface PluginContext {
89
- /** Request path */
90
- path: string;
91
- /** Matched route configuration */
92
- route: RouteConfig;
93
- /** HTTP method */
94
- method: HttpMethod;
95
- /** Route parameters */
96
- params: Record<string, string>;
97
- /** Query parameters */
98
- query: Record<string, string>;
99
- /** Request headers */
100
- headers: Record<string, string>;
101
- /** Request body */
102
- body?: unknown;
103
- /** Shared state between plugins for this request */
104
- state: Map<string, unknown>;
105
- /** Route-specific state */
106
- routeState?: Record<string, unknown>;
107
- }
108
-
109
- /**
110
- * Global configuration options for the mock instance
111
- */
112
- export interface GlobalConfig {
113
- /** Base path prefix for all routes */
114
- namespace?: string;
115
- /** Response delay in ms, or [min, max] for random delay */
116
- delay?: number | [number, number];
117
- /** Enable debug mode for detailed logging */
118
- debug?: boolean;
119
- /** Initial shared state object */
120
- state?: Record<string, unknown>;
121
- }
122
-
123
- /**
124
- * Route-specific configuration options
125
- */
126
- export interface RouteConfig {
127
- /** MIME type for content type validation (auto-detected if not provided) */
128
- contentType?: string;
129
- /** Additional route-specific options */
130
- [key: string]: any;
131
- }
132
-
133
- /**
134
- * Generator types that can be passed to route definitions
135
- */
136
- export type Generator = GeneratorFunction | StaticData | JSONSchema;
137
-
138
- /**
139
- * Function that generates responses
140
- */
141
- export type GeneratorFunction = (
142
- context: RequestContext,
143
- ) => ResponseResult | Promise<ResponseResult>;
144
-
145
- /**
146
- * Response body type alias
147
- */
148
- export type ResponseBody = unknown;
149
-
150
- /**
151
- * Static data (non-function) that gets returned as-is
152
- */
153
- export type StaticData =
154
- | string
155
- | number
156
- | boolean
157
- | null
158
- | undefined
159
- | Record<string, unknown>
160
- | unknown[];
161
-
162
- /**
163
- * Context passed to generator functions
164
- */
165
- export interface RequestContext {
166
- /** HTTP method */
167
- method: HttpMethod;
168
- /** Request path */
169
- path: string;
170
- /** Route parameters (e.g., :id) */
171
- params: Record<string, string>;
172
- /** Query string parameters */
173
- query: Record<string, string>;
174
- /** Request headers */
175
- headers: Record<string, string>;
176
- /** Request body (for POST, PUT, PATCH) */
177
- body?: unknown;
178
- /** Shared mutable state */
179
- state: Record<string, unknown>;
180
- }
181
-
182
- /**
183
- * Response result types:
184
- * - Any value: returns as 200 OK
185
- * - [status, body]: custom status with body
186
- * - [status, body, headers]: custom status, body, and headers
187
- */
188
- export type ResponseResult =
189
- | ResponseBody
190
- | [number, ResponseBody]
191
- | [number, ResponseBody, Record<string, string>];
192
-
193
- /**
194
- * Response object returned by handle method
195
- */
196
- export interface Response {
197
- status: number;
198
- body: unknown;
199
- headers: Record<string, string>;
200
- }
201
-
202
- /**
203
- * Options for handle method
204
- */
205
- export interface RequestOptions {
206
- headers?: Record<string, string>;
207
- body?: unknown;
208
- query?: Record<string, string>;
209
- }
210
-
211
- /**
212
- * Main callable mock instance interface
213
- */
214
- export interface CallableMockInstance {
215
- /**
216
- * Define a route by calling the instance directly
217
- *
218
- * @param route - Route pattern in format 'METHOD /path'
219
- * @param generator - Response generator (function, static data, or schema)
220
- * @param config - Route-specific configuration
221
- * @returns The same instance for method chaining
222
- *
223
- * @example
224
- * ```typescript
225
- * const mock = schmock()
226
- * mock('GET /users', () => [...users], { contentType: 'application/json' })
227
- * mock('POST /users', userData, { contentType: 'application/json' })
228
- * ```
229
- */
230
- (
231
- route: RouteKey,
232
- generator: Generator,
233
- config?: RouteConfig,
234
- ): CallableMockInstance;
235
-
236
- /**
237
- * Add a plugin to the pipeline
238
- *
239
- * @param plugin - Plugin to add to the pipeline
240
- * @returns The same instance for method chaining
241
- *
242
- * @example
243
- * ```typescript
244
- * mock('GET /users', generator, config)
245
- * .pipe(authPlugin())
246
- * .pipe(corsPlugin())
247
- * ```
248
- */
249
- pipe(plugin: Plugin): CallableMockInstance;
250
-
251
- /**
252
- * Handle a request and return a response
253
- *
254
- * @param method - HTTP method
255
- * @param path - Request path
256
- * @param options - Request options (headers, body, query)
257
- * @returns Promise resolving to response object
258
- *
259
- * @example
260
- * ```typescript
261
- * const response = await mock.handle('GET', '/users', {
262
- * headers: { 'Authorization': 'Bearer token' }
263
- * })
264
- * ```
265
- */
266
- handle(
267
- method: HttpMethod,
268
- path: string,
269
- options?: RequestOptions,
270
- ): Promise<Response>;
271
- }
1
+ /// <reference path="../../../types/schmock.d.ts" />
2
+
3
+ // Re-export types for internal use
4
+ export type HttpMethod = Schmock.HttpMethod;
5
+ export type RouteKey = Schmock.RouteKey;
6
+ export type ResponseBody = Schmock.ResponseBody;
7
+ export type ResponseResult = Schmock.ResponseResult;
8
+ export type RequestContext = Schmock.RequestContext;
9
+ export type Response = Schmock.Response;
10
+ export type RequestOptions = Schmock.RequestOptions;
11
+ export type GlobalConfig = Schmock.GlobalConfig;
12
+ export type RouteConfig = Schmock.RouteConfig;
13
+ export type Generator = Schmock.Generator;
14
+ export type GeneratorFunction = Schmock.GeneratorFunction;
15
+ export type CallableMockInstance = Schmock.CallableMockInstance;
16
+ export type Plugin = Schmock.Plugin;
17
+ export type PluginContext = Schmock.PluginContext;
18
+ export type PluginResult = Schmock.PluginResult;
19
+ export type StaticData = Schmock.StaticData;
20
+ export type RequestRecord = Schmock.RequestRecord;