@schmock/core 1.0.4 → 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.
package/dist/types.d.ts CHANGED
@@ -1,215 +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?: unknown): 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?: unknown;
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: RouteConfig;
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?: unknown;
79
- /** Shared state between plugins for this request */
80
- state: Map<string, unknown>;
81
- /** Route-specific state */
82
- routeState?: Record<string, unknown>;
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?: Record<string, unknown>;
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
- * Response body type alias
116
- */
117
- export type ResponseBody = unknown;
118
- /**
119
- * Static data (non-function) that gets returned as-is
120
- */
121
- export type StaticData = string | number | boolean | null | undefined | Record<string, unknown> | unknown[];
122
- /**
123
- * Context passed to generator functions
124
- */
125
- export interface RequestContext {
126
- /** HTTP method */
127
- method: HttpMethod;
128
- /** Request path */
129
- path: string;
130
- /** Route parameters (e.g., :id) */
131
- params: Record<string, string>;
132
- /** Query string parameters */
133
- query: Record<string, string>;
134
- /** Request headers */
135
- headers: Record<string, string>;
136
- /** Request body (for POST, PUT, PATCH) */
137
- body?: unknown;
138
- /** Shared mutable state */
139
- state: Record<string, unknown>;
140
- }
141
- /**
142
- * Response result types:
143
- * - Any value: returns as 200 OK
144
- * - [status, body]: custom status with body
145
- * - [status, body, headers]: custom status, body, and headers
146
- */
147
- export type ResponseResult = ResponseBody | [number, ResponseBody] | [number, ResponseBody, Record<string, string>];
148
- /**
149
- * Response object returned by handle method
150
- */
151
- export interface Response {
152
- status: number;
153
- body: unknown;
154
- headers: Record<string, string>;
155
- }
156
- /**
157
- * Options for handle method
158
- */
159
- export interface RequestOptions {
160
- headers?: Record<string, string>;
161
- body?: unknown;
162
- query?: Record<string, string>;
163
- }
164
- /**
165
- * Main callable mock instance interface
166
- */
167
- export interface CallableMockInstance {
168
- /**
169
- * Define a route by calling the instance directly
170
- *
171
- * @param route - Route pattern in format 'METHOD /path'
172
- * @param generator - Response generator (function, static data, or schema)
173
- * @param config - Route-specific configuration
174
- * @returns The same instance for method chaining
175
- *
176
- * @example
177
- * ```typescript
178
- * const mock = schmock()
179
- * mock('GET /users', () => [...users], { contentType: 'application/json' })
180
- * mock('POST /users', userData, { contentType: 'application/json' })
181
- * ```
182
- */
183
- (route: RouteKey, generator: Generator, config?: RouteConfig): CallableMockInstance;
184
- /**
185
- * Add a plugin to the pipeline
186
- *
187
- * @param plugin - Plugin to add to the pipeline
188
- * @returns The same instance for method chaining
189
- *
190
- * @example
191
- * ```typescript
192
- * mock('GET /users', generator, config)
193
- * .pipe(authPlugin())
194
- * .pipe(corsPlugin())
195
- * ```
196
- */
197
- pipe(plugin: Plugin): CallableMockInstance;
198
- /**
199
- * Handle a request and return a response
200
- *
201
- * @param method - HTTP method
202
- * @param path - Request path
203
- * @param options - Request options (headers, body, query)
204
- * @returns Promise resolving to response object
205
- *
206
- * @example
207
- * ```typescript
208
- * const response = await mock.handle('GET', '/users', {
209
- * headers: { 'Authorization': 'Bearer token' }
210
- * })
211
- * ```
212
- */
213
- handle(method: HttpMethod, path: string, options?: RequestOptions): Promise<Response>;
214
- }
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;
215
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;IAC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AAC;AACpD,MAAM,MAAM,cAAc,GAAG,OAAO,CAAC,cAAc,CAAC;AACpD,MAAM,MAAM,QAAQ,GAAG,OAAO,CAAC,QAAQ,CAAC;AACxC,MAAM,MAAM,cAAc,GAAG,OAAO,CAAC,cAAc,CAAC;AACpD,MAAM,MAAM,YAAY,GAAG,OAAO,CAAC,YAAY,CAAC;AAChD,MAAM,MAAM,WAAW,GAAG,OAAO,CAAC,WAAW,CAAC;AAC9C,MAAM,MAAM,SAAS,GAAG,OAAO,CAAC,SAAS,CAAC;AAC1C,MAAM,MAAM,iBAAiB,GAAG,OAAO,CAAC,iBAAiB,CAAC;AAC1D,MAAM,MAAM,oBAAoB,GAAG,OAAO,CAAC,oBAAoB,CAAC;AAChE,MAAM,MAAM,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC;AACpC,MAAM,MAAM,aAAa,GAAG,OAAO,CAAC,aAAa,CAAC;AAClD,MAAM,MAAM,YAAY,GAAG,OAAO,CAAC,YAAY,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.4",
4
+ "version": "1.1.0",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",
7
7
  "types": "./dist/index.d.ts",
@@ -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