restful-dummy-server 1.0.3 → 1.0.5

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/.npmrc_x ADDED
@@ -0,0 +1 @@
1
+ //registry.npmjs.org/:_authToken=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
package/README.md CHANGED
@@ -8,32 +8,40 @@ npm install -g restful-dummy-server
8
8
  ### CLI options
9
9
 
10
10
  #### 1. port
11
+ ```js
11
12
  "scripts": {
12
13
  "start": "restful-dummy-server -port 4000"
13
14
  }
15
+ ```
14
16
 
15
17
  This will start restful dummy server on port 4000
16
18
 
17
19
 
18
20
  #### 2. host
19
21
  You can also mention host.
22
+ ```js
20
23
  "scripts": {
21
24
  "start": "restful-dummy-server -port 4000 -host xx.xx.xx.xx"
22
25
  }
26
+ ```
23
27
 
24
28
  #### 3. static
29
+ ```js
25
30
  "scripts": {
26
31
  "start": "restful-dummy-server -port 4000 -static ./data"
27
32
  }
33
+ ```
28
34
 
29
35
  **static** is used to know the relative path of a folder where all statics files are hosted. Which then would be used by Dummy server to download files against the rest api dowbloaded request.
30
36
  Here this folder is **data**.
31
37
 
32
38
 
33
39
  #### 4. config
40
+ ```js
34
41
  "scripts": {
35
42
  "start": "restful-dummy-server -port 4000 -static ./data -config ./test"
36
43
  }
44
+ ```
37
45
 
38
46
  **config** is used to know the relative path of a folder which are having dummy request response instruction files in **.json** extensions only.
39
47
 
@@ -44,6 +52,7 @@ Note -
44
52
  Example 1-
45
53
  **test/config.json**
46
54
 
55
+ ```js
47
56
  [{
48
57
  "request": {
49
58
  "url": "/get/message",
@@ -54,6 +63,7 @@ Example 1-
54
63
  "body": "Hello Dummy Server"
55
64
  }
56
65
  }]
66
+ ```
57
67
 
58
68
  In this case if you hit http://localhost:5000/get/message then you will get **Hello Dummy Server** as response.
59
69
 
@@ -61,6 +71,7 @@ In this case if you hit http://localhost:5000/get/message then you will get **He
61
71
  Example 2-
62
72
  **test/config.json**
63
73
 
74
+ ```js
64
75
  [{
65
76
  "request": {
66
77
  "url": "/get/:message",
@@ -71,6 +82,7 @@ Example 2-
71
82
  "body": { "message": "Hello Dummy Server" }
72
83
  }
73
84
  }]
85
+ ```
74
86
 
75
87
  In this case if you hit http://localhost:5000/get/test or http://localhost:5000/get/msg etc. then you will get **{ "message": "Hello Dummy Server" }** as response.
76
88
 
@@ -78,6 +90,7 @@ In this case if you hit http://localhost:5000/get/test or http://localhost:5000/
78
90
  Example 3-
79
91
  **test/config.json**
80
92
 
93
+ ```js
81
94
  [{
82
95
  "request": {
83
96
  "url": "/get/png/*.*",
@@ -90,6 +103,7 @@ Example 3-
90
103
  "body": "./test.png"
91
104
  }
92
105
  }]
106
+ ```
93
107
 
94
108
  In this case if you hit http://localhost:5000/get/png/abc.png or http://localhost:5000/get/png/msg.png etc. then **test.png** will be downloaded as response.
95
109
 
@@ -97,6 +111,7 @@ In this case if you hit http://localhost:5000/get/png/abc.png or http://localhos
97
111
  Example 4-
98
112
  **test/config.json**
99
113
 
114
+ ```js
100
115
  [{
101
116
  "request": {
102
117
  "url": "/get/**/*.*",
@@ -109,6 +124,7 @@ Example 4-
109
124
  "body": "./test.png"
110
125
  }
111
126
  }]
127
+ ```
112
128
 
113
129
  In this case if you hit http://localhost:5000/get/png/abc.png or http://localhost:5000/get/png/png2/msg.jpeg etc. then **test.png** will be downloaded as response.
114
130
 
@@ -116,6 +132,7 @@ In this case if you hit http://localhost:5000/get/png/abc.png or http://localhos
116
132
  Example 5-
117
133
  **test/config.json**
118
134
 
135
+ ```js
119
136
  [{
120
137
  "request": {
121
138
  "url": "/get/**/*.png",
@@ -128,6 +145,7 @@ Example 5-
128
145
  "body": "./test.png"
129
146
  }
130
147
  }]
148
+ ```
131
149
 
132
150
  In this case if you hit http://localhost:5000/get/png/abc.png or http://localhost:5000/get/png/png2/msg.png etc. then **test.png** will be downloaded as response.
133
151
 
@@ -135,6 +153,7 @@ In this case if you hit http://localhost:5000/get/png/abc.png or http://localhos
135
153
  Example 6-
136
154
  **test/config.json**
137
155
 
156
+ ```js
138
157
  [{
139
158
  "request": {
140
159
  "url": "/get/**/*.png",
@@ -148,14 +167,17 @@ Example 6-
148
167
  "delay": 2000
149
168
  }
150
169
  }]
170
+ ```
151
171
 
152
172
  Above **delay** is the special attribute which would cause Dummy server to respond in 2000 milliseconds.
153
173
 
154
174
 
155
175
  #### 5. proxy
176
+ ```js
156
177
  "scripts": {
157
178
  "start": "restful-dummy-server -port 4000 -static ./data -config ./test -proxy localhost:2000"
158
179
  }
180
+ ```
159
181
 
160
182
  If **proxy** is provided then Dummy server will redirect the request to the proxy server if there is no matching record found.
161
183
 
package/dist/utils.js CHANGED
@@ -53,6 +53,40 @@ function getItem(req, filePath) {
53
53
  return mockData?.find((item) => (isMatchingPathExists(req, item)));
54
54
  }
55
55
 
56
+ // Below cases supporting -
57
+ // 1. url: /test/:input will match with /test/abc or /test/xyz etc.
58
+ // 2. url: /test/*.* will match with /test/abc.jpg or /test/xyz.png etc.
59
+ // 3. url: /test/**/*.jpg will match with /test/abc.jpg or /test/xyz/abc.jpg or /test/test2/xyg.jpg etc.
60
+ // 4. url: /test/**/*.* will match with /test/test2/xyz.jpg or /test/test2/test3/xyz.png etc.
61
+ function buildMockPathRegex(mockPath) {
62
+ if (!mockPath || typeof mockPath !== 'string') {
63
+ throw new Error('mockPath must be a non-empty string');
64
+ }
65
+
66
+ // 1) Escape literal '.' and '/'
67
+ let pattern = mockPath
68
+ .replace(/\//g, '\\/')
69
+ .replace(/\./g, '\\.');
70
+
71
+ // 2) Handle double-star first (deep wildcard)
72
+ // - matches across '/' as well
73
+ pattern = pattern.replace(/\*\*(?!\*)/g, '[\\w./-]+');
74
+
75
+ // 3) Handle single star (single segment wildcard)
76
+ pattern = pattern.replace(/\*(?!\*)/g, '[\\w.-]+');
77
+
78
+ // 4) Special :list placeholder -> comma-separated list of tokens
79
+ // Example: :list -> SRDSS,ERTT,SAA,SSSS
80
+ pattern = pattern.replace(/:list\b/g, '[\\w.-]+(?:,[\\w.-]+)*');
81
+
82
+ // 5) Other named params like :id, :type etc. -> single token
83
+ pattern = pattern.replace(/:[\w.-]+\b/g, '[\\w.-]+');
84
+
85
+ // 6) Anchor the pattern
86
+ return new RegExp('^' + pattern + '$');
87
+ }
88
+
89
+
56
90
  /**
57
91
  * To check if req path exists on mock data
58
92
  */
@@ -63,18 +97,7 @@ function isMatchingPathExists(req, mockData) {
63
97
  // removing starting and trailing /
64
98
  path = path.replace(/^\//, '').replace(/\/$/, '');
65
99
  mockPath = mockPath.replace(/^\//, '').replace(/\/$/, '');
66
-
67
- // Below cases supporting -
68
- // 1. url: /test/:input will match with /test/abc or /test/xyz etc.
69
- // 2. url: /test/*.* will match with /test/abc.jpg or /test/xyz.png etc.
70
- // 3. url: /test/**/*.jpg will match with /test/abc.jpg or /test/xyz/abc.jpg or /test/test2/xyg.jpg etc.
71
- // 4. url: /test/**/*.* will match with /test/test2/xyz.jpg or /test/test2/test3/xyz.png etc.
72
- const regexForMockPath = mockPath?.replace(/\//g, '\\/')
73
- .replace(/\./g, '\\.')
74
- .replace(/\*{2,}/g, '[\\w-\.\/]\+')
75
- .replace(/\*/g, '[\\w-]\+')
76
- .replace(/:[\w-\.]+/g, '[\\w-\.]\+');
77
- const regexEval = eval('/^' + regexForMockPath + '$/'); ;
100
+ const regexEval = buildMockPathRegex(mockPath);
78
101
 
79
102
  if (reqMethod?.toLowerCase() !== mockMethod?.toLowerCase()) {
80
103
  return false;
@@ -88,4 +111,4 @@ function isMatchingPathExists(req, mockData) {
88
111
 
89
112
  module.exports = {
90
113
  getMatchingDummyData
91
- };
114
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "restful-dummy-server",
3
- "version": "1.0.3",
3
+ "version": "1.0.5",
4
4
  "description": "This can be used to mock restful api responses.",
5
5
  "main": "dist/index.js",
6
6
  "scripts": {
package/.idea/modules.xml DELETED
@@ -1,8 +0,0 @@
1
- <?xml version="1.0" encoding="UTF-8"?>
2
- <project version="4">
3
- <component name="ProjectModuleManager">
4
- <modules>
5
- <module fileurl="file://$PROJECT_DIR$/.idea/restful-dummy-server.iml" filepath="$PROJECT_DIR$/.idea/restful-dummy-server.iml" />
6
- </modules>
7
- </component>
8
- </project>
@@ -1,9 +0,0 @@
1
- <?xml version="1.0" encoding="UTF-8"?>
2
- <module type="JAVA_MODULE" version="4">
3
- <component name="NewModuleRootManager" inherit-compiler-output="true">
4
- <exclude-output />
5
- <content url="file://$MODULE_DIR$" />
6
- <orderEntry type="inheritedJdk" />
7
- <orderEntry type="sourceFolder" forTests="false" />
8
- </component>
9
- </module>
package/.idea/vcs.xml DELETED
@@ -1,7 +0,0 @@
1
- <?xml version="1.0" encoding="UTF-8"?>
2
- <project version="4">
3
- <component name="VcsDirectoryMappings">
4
- <mapping directory="" vcs="Git" />
5
- <mapping directory="$PROJECT_DIR$/node_modules/restful-dummy-server" vcs="Git" />
6
- </component>
7
- </project>