@@ -2,6 +2,7 @@ import { faker } from '@faker-js/faker'
22import cors from 'cors'
33import express , { Request , Response } from 'express'
44import { validate } from 'jsonschema'
5+ import { decompressFromEncodedURIComponent } from 'lz-string'
56import morgan from 'morgan'
67import pkg from './package.json'
78
@@ -57,6 +58,7 @@ app.all('*', (req: Request, res: Response) => {
5758 let status = mock ?. response ?. status
5859 let delay = mock ?. response ?. delay
5960 let bodySchema = mock ?. request ?. body ?. schema
61+ const isCompressed = mock ?. compressed === 'true'
6062
6163 if ( secret ) {
6264 if ( clientSecret !== secret ) {
@@ -82,7 +84,19 @@ app.all('*', (req: Request, res: Response) => {
8284 return
8385 }
8486
87+ // Decompress JSON params if mock[compressed]=true is present (default: false)
88+ const decompressIfNeeded = ( value : string ) : string => {
89+ if ( ! isCompressed || ! value ) return value
90+ try {
91+ const decompressed = decompressFromEncodedURIComponent ( value )
92+ return decompressed || value
93+ } catch {
94+ return value
95+ }
96+ }
97+
8598 if ( body ) {
99+ body = decompressIfNeeded ( body )
86100 try {
87101 body = faker . helpers . fake ( body )
88102 } catch ( error : any ) {
@@ -96,14 +110,24 @@ app.all('*', (req: Request, res: Response) => {
96110 }
97111 }
98112
113+ if ( headers ) {
114+ headers = decompressIfNeeded ( headers )
115+ }
116+
117+ if ( bodySchema ) {
118+ bodySchema = decompressIfNeeded ( bodySchema )
119+ }
120+
99121 if ( headers ) {
100122 try {
123+ const customHeaders = JSON . parse ( headers )
101124 headers = {
102125 'Content-Type' : 'application/json' ,
103- ...JSON . parse ( headers ) ,
126+ ...customHeaders ,
104127 'Access-Control-Allow-Origin' : allowOrigin ,
105128 'Access-Control-Allow-Methods' : allowMethods ,
106129 'Access-Control-Allow-Headers' : allowHeaders ,
130+ 'Access-Control-Expose-Headers' : Object . keys ( customHeaders ) . join ( ', ' ) ,
107131 'X-Powered-By' : 'Smockr' ,
108132 }
109133 } catch ( _error : any ) {
0 commit comments