@schmock/express 1.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.
@@ -0,0 +1,70 @@
1
+ import type { CallableMockInstance } from "@schmock/core";
2
+ import type { Request, RequestHandler, Response } from "express";
3
+ /**
4
+ * Configuration options for Express adapter
5
+ */
6
+ export interface ExpressAdapterOptions {
7
+ /**
8
+ * Custom error formatter
9
+ * @param error - The error that occurred
10
+ * @param req - Express request
11
+ * @returns Custom error response
12
+ */
13
+ errorFormatter?: (error: Error, req: Request) => any;
14
+ /**
15
+ * Whether to pass non-Schmock errors to Express error handler
16
+ * @default true
17
+ */
18
+ passErrorsToNext?: boolean;
19
+ /**
20
+ * Custom header transformation
21
+ * @param headers - Express headers
22
+ * @returns Transformed headers for Schmock
23
+ */
24
+ transformHeaders?: (headers: Request["headers"]) => Record<string, string>;
25
+ /**
26
+ * Custom query transformation
27
+ * @param query - Express query
28
+ * @returns Transformed query for Schmock
29
+ */
30
+ transformQuery?: (query: Request["query"]) => Record<string, string>;
31
+ /**
32
+ * Request interceptor - called before handling request
33
+ * @param req - Express request
34
+ * @param res - Express response
35
+ * @returns Modified request data or void
36
+ */
37
+ beforeRequest?: (req: Request, res: Response) => {
38
+ method?: string;
39
+ path?: string;
40
+ headers?: Record<string, string>;
41
+ body?: any;
42
+ query?: Record<string, string>;
43
+ } | undefined | Promise<any>;
44
+ /**
45
+ * Response interceptor - called before sending response
46
+ * @param schmockResponse - Response from Schmock
47
+ * @param req - Express request
48
+ * @param res - Express response
49
+ * @returns Modified response or void
50
+ */
51
+ beforeResponse?: (schmockResponse: {
52
+ status: number;
53
+ body: any;
54
+ headers: Record<string, string>;
55
+ }, req: Request, res: Response) => {
56
+ status: number;
57
+ body: any;
58
+ headers: Record<string, string>;
59
+ } | undefined | Promise<{
60
+ status: number;
61
+ body: any;
62
+ headers: Record<string, string>;
63
+ } | undefined>;
64
+ }
65
+ /**
66
+ * Convert a Schmock mock instance to Express middleware
67
+ */
68
+ export declare function toExpress(mock: CallableMockInstance, options?: ExpressAdapterOptions): RequestHandler;
69
+ export default toExpress;
70
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,oBAAoB,EAAE,MAAM,eAAe,CAAC;AAE1D,OAAO,KAAK,EAAgB,OAAO,EAAE,cAAc,EAAE,QAAQ,EAAE,MAAM,SAAS,CAAC;AAE/E;;GAEG;AACH,MAAM,WAAW,qBAAqB;IACpC;;;;;OAKG;IACH,cAAc,CAAC,EAAE,CAAC,KAAK,EAAE,KAAK,EAAE,GAAG,EAAE,OAAO,KAAK,GAAG,CAAC;IAErD;;;OAGG;IACH,gBAAgB,CAAC,EAAE,OAAO,CAAC;IAE3B;;;;OAIG;IACH,gBAAgB,CAAC,EAAE,CAAC,OAAO,EAAE,OAAO,CAAC,SAAS,CAAC,KAAK,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;IAE3E;;;;OAIG;IACH,cAAc,CAAC,EAAE,CAAC,KAAK,EAAE,OAAO,CAAC,OAAO,CAAC,KAAK,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;IAErE;;;;;OAKG;IACH,aAAa,CAAC,EAAE,CACd,GAAG,EAAE,OAAO,EACZ,GAAG,EAAE,QAAQ,KAEX;QACE,MAAM,CAAC,EAAE,MAAM,CAAC;QAChB,IAAI,CAAC,EAAE,MAAM,CAAC;QACd,OAAO,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;QACjC,IAAI,CAAC,EAAE,GAAG,CAAC;QACX,KAAK,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;KAChC,GACD,SAAS,GACT,OAAO,CAAC,GAAG,CAAC,CAAC;IAEjB;;;;;;OAMG;IACH,cAAc,CAAC,EAAE,CACf,eAAe,EAAE;QACf,MAAM,EAAE,MAAM,CAAC;QACf,IAAI,EAAE,GAAG,CAAC;QACV,OAAO,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;KACjC,EACD,GAAG,EAAE,OAAO,EACZ,GAAG,EAAE,QAAQ,KAEX;QAAE,MAAM,EAAE,MAAM,CAAC;QAAC,IAAI,EAAE,GAAG,CAAC;QAAC,OAAO,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAA;KAAE,GAC9D,SAAS,GACT,OAAO,CACH;QAAE,MAAM,EAAE,MAAM,CAAC;QAAC,IAAI,EAAE,GAAG,CAAC;QAAC,OAAO,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAA;KAAE,GAC9D,SAAS,CACZ,CAAC;CACP;AAwED;;GAEG;AACH,wBAAgB,SAAS,CACvB,IAAI,EAAE,oBAAoB,EAC1B,OAAO,GAAE,qBAA0B,GAClC,cAAc,CA+EhB;AAED,eAAe,SAAS,CAAC"}
package/dist/index.js ADDED
@@ -0,0 +1 @@
1
+ class X extends Error{code;context;constructor(A,B,J){super(A);this.code=B;this.context=J;this.name="SchmockError",Error.captureStackTrace(this,this.constructor)}}function z(A,B){if(A.status)B.status(A.status);if(A.headers)Object.entries(A.headers).forEach(([J,U])=>{if(typeof U==="string")B.set(J,U)});if(A.body!==void 0)if(typeof A.body==="string")B.send(A.body);else B.json(A.body);else B.end()}function M(A){return Object.fromEntries(Object.entries(A).map(([B,J])=>[B,Array.isArray(J)?J[0]:J||""]))}function F(A){let B={};for(let[J,U]of Object.entries(A))if(typeof U==="string")B[J]=U;else if(Array.isArray(U))B[J]=U[0]?String(U[0]):"";else if(U!=null)B[J]=String(U);return B}function D(A,B={}){let{errorFormatter:J,passErrorsToNext:U=!0,transformHeaders:O=M,transformQuery:L=F,beforeRequest:_,beforeResponse:$}=B;return async(V,Y,j)=>{try{let G={method:V.method,path:V.path,headers:O(V.headers),body:V.body,query:L(V.query)};if(_){let Z=await _(V,Y);if(Z)G={...G,...Z}}let W=await A.handle(G.method,G.path,{headers:G.headers,body:G.body,query:G.query});if(W){if($){let Z=await $(W,V,Y);if(Z)W=Z}z(W,Y)}else j()}catch(G){if(G instanceof X&&J){let W=J(G,V);Y.status(500).json(W)}else if(U)j(G);else Y.status(500).json({error:G instanceof Error?G.message:"Internal Server Error",code:G instanceof X?G.code:"INTERNAL_ERROR"})}}}var h=D;export{D as toExpress,h as default};
package/package.json ADDED
@@ -0,0 +1,46 @@
1
+ {
2
+ "name": "@schmock/express",
3
+ "version": "1.0.0",
4
+ "description": "Express adapter for Schmock mock API generator",
5
+ "type": "module",
6
+ "main": "dist/index.js",
7
+ "types": "dist/index.d.ts",
8
+ "exports": {
9
+ ".": {
10
+ "import": "./dist/index.js",
11
+ "types": "./dist/index.d.ts"
12
+ }
13
+ },
14
+ "files": [
15
+ "dist"
16
+ ],
17
+ "scripts": {
18
+ "build": "bun build:lib && bun build:types",
19
+ "build:lib": "bun build --minify --outdir=dist src/index.ts",
20
+ "build:types": "tsc -p tsconfig.json",
21
+ "test": "vitest",
22
+ "test:watch": "vitest --watch",
23
+ "lint": "biome check src/",
24
+ "lint:fix": "biome check --write --unsafe src/"
25
+ },
26
+ "dependencies": {},
27
+ "devDependencies": {
28
+ "@types/express": "^5.0.6",
29
+ "express": "^5.2.1",
30
+ "typescript": "^5.9.3",
31
+ "vitest": "^4.0.15"
32
+ },
33
+ "peerDependencies": {
34
+ "@schmock/core": "^1.0.0",
35
+ "express": "^4.18.0"
36
+ },
37
+ "keywords": [
38
+ "mock",
39
+ "api",
40
+ "express",
41
+ "testing",
42
+ "development"
43
+ ],
44
+ "author": "Khalic Lab",
45
+ "license": "MIT"
46
+ }