@stuntman/client 0.1.1 → 0.1.2

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/README.md CHANGED
@@ -29,3 +29,5 @@ const rule = ruleBuilder()
29
29
 
30
30
  client.addRule(rule).then((x) => console.log(x));
31
31
  ```
32
+
33
+ Check [example app](https://github.com/andrzej-woof/stuntman/tree/master/packages/example#readme) for more samples
package/dist/index.d.ts CHANGED
@@ -1 +1,2 @@
1
1
  export { Client as StuntmanClient } from './apiClient';
2
+ export { ruleBuilder } from './ruleBuilder';
package/dist/index.js CHANGED
@@ -1,5 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.StuntmanClient = void 0;
3
+ exports.ruleBuilder = exports.StuntmanClient = void 0;
4
4
  var apiClient_1 = require("./apiClient");
5
5
  Object.defineProperty(exports, "StuntmanClient", { enumerable: true, get: function () { return apiClient_1.Client; } });
6
+ var ruleBuilder_1 = require("./ruleBuilder");
7
+ Object.defineProperty(exports, "ruleBuilder", { enumerable: true, get: function () { return ruleBuilder_1.ruleBuilder; } });
@@ -13,43 +13,52 @@ class RuleBuilderBaseBase {
13
13
  priority: shared_1.DEFAULT_RULE_PRIORITY,
14
14
  matches: {
15
15
  localFn: (req) => {
16
+ var _a, _b, _c;
16
17
  const ___url = new URL(req.url);
17
18
  const ___headers = req.rawHeaders;
18
19
  const arrayIndexerRegex = /\[(?<arrayIndex>[0-9]*)\]/i;
19
- const matchObject = (obj, path, value) => {
20
+ const matchObject = (obj, path, value, parentPath) => {
20
21
  var _a, _b;
21
22
  if (!obj) {
22
- return false;
23
+ return { result: false, description: `${parentPath} is falsey` };
23
24
  }
24
25
  const [rawKey, ...rest] = path.split('.');
25
26
  const key = rawKey.replace(arrayIndexerRegex, '');
26
27
  const shouldBeArray = arrayIndexerRegex.test(rawKey);
27
28
  const arrayIndex = Number((_b = (_a = arrayIndexerRegex.exec(rawKey)) === null || _a === void 0 ? void 0 : _a.groups) === null || _b === void 0 ? void 0 : _b.arrayIndex);
28
29
  const actualValue = key ? obj[key] : obj;
30
+ const currentPath = `${parentPath ? `${parentPath}.` : ''}${rawKey}`;
29
31
  if (value === undefined && actualValue === undefined) {
30
- return false;
32
+ return { result: false, description: `${currentPath}=undefined` };
31
33
  }
32
34
  if (rest.length === 0) {
33
35
  if (shouldBeArray &&
34
36
  (!Array.isArray(actualValue) ||
35
37
  !(!Number.isInteger(arrayIndex) && actualValue.length > Number(arrayIndex)))) {
36
- return false;
38
+ return { result: false, description: `${currentPath} empty array` };
37
39
  }
38
40
  if (value === undefined) {
39
- return shouldBeArray
41
+ const result = shouldBeArray
40
42
  ? !Number.isInteger(arrayIndex) || actualValue.length >= Number(arrayIndex)
41
43
  : actualValue !== undefined;
44
+ return { result, description: `${currentPath}` };
42
45
  }
43
46
  if (!shouldBeArray) {
44
- return value instanceof RegExp ? value.test(actualValue) : value === actualValue;
47
+ const result = value instanceof RegExp ? value.test(actualValue) : value === actualValue;
48
+ return { result, description: `${currentPath}` };
45
49
  }
46
50
  }
47
51
  if (shouldBeArray) {
48
- return Number.isInteger(arrayIndex)
49
- ? matchObject(actualValue[Number(arrayIndex)], rest.join('.'), value)
50
- : actualValue.some((arrayValue) => matchObject(arrayValue, rest.join('.'), value));
52
+ if (Number.isInteger(arrayIndex)) {
53
+ return matchObject(actualValue[Number(arrayIndex)], rest.join('.'), value, currentPath);
54
+ }
55
+ const hasArrayMatch = actualValue.some((arrayValue) => matchObject(arrayValue, rest.join('.'), value, currentPath));
56
+ return { result: hasArrayMatch, description: `array match ${currentPath}` };
51
57
  }
52
- return typeof actualValue !== 'object' ? false : matchObject(actualValue, rest.join('.'), value);
58
+ if (typeof actualValue !== 'object') {
59
+ return { result: false, description: `${currentPath} not an object` };
60
+ }
61
+ return matchObject(actualValue, rest.join('.'), value, currentPath);
53
62
  };
54
63
  const ___matchesValue = (matcher, value) => {
55
64
  if (matcher === undefined) {
@@ -70,32 +79,49 @@ class RuleBuilderBaseBase {
70
79
  return true;
71
80
  };
72
81
  if (!___matchesValue(matchBuilderVariables.filter, req.url)) {
73
- return false;
82
+ return {
83
+ result: false,
84
+ description: `url ${req.url} doesn't match ${(_a = matchBuilderVariables.filter) === null || _a === void 0 ? void 0 : _a.toString()}`,
85
+ };
74
86
  }
75
87
  if (!___matchesValue(matchBuilderVariables.hostname, ___url.hostname)) {
76
- return false;
88
+ return {
89
+ result: false,
90
+ description: `hostname ${___url.hostname} doesn't match ${(_b = matchBuilderVariables.hostname) === null || _b === void 0 ? void 0 : _b.toString()}`,
91
+ };
77
92
  }
78
93
  if (!___matchesValue(matchBuilderVariables.pathname, ___url.pathname)) {
79
- return false;
94
+ return {
95
+ result: false,
96
+ description: `pathname ${___url.pathname} doesn't match ${(_c = matchBuilderVariables.pathname) === null || _c === void 0 ? void 0 : _c.toString()}`,
97
+ };
80
98
  }
81
99
  if (matchBuilderVariables.searchParams) {
82
100
  for (const searchParamMatcher of matchBuilderVariables.searchParams) {
83
101
  if (typeof searchParamMatcher === 'string') {
84
- return ___url.searchParams.has(searchParamMatcher);
102
+ const result = ___url.searchParams.has(searchParamMatcher);
103
+ return { result, description: `searchParams.has("${searchParamMatcher}")` };
85
104
  }
86
105
  if (searchParamMatcher instanceof RegExp) {
87
- return Array.from(___url.searchParams.keys()).some((key) => searchParamMatcher.test(key));
106
+ const result = Array.from(___url.searchParams.keys()).some((key) => searchParamMatcher.test(key));
107
+ return { result, description: `searchParams.keys() matches ${searchParamMatcher.toString()}` };
88
108
  }
89
109
  if (!___url.searchParams.has(searchParamMatcher.key)) {
90
- return false;
110
+ return { result: false, description: `searchParams.has("${searchParamMatcher.key}")` };
91
111
  }
92
112
  if (searchParamMatcher.value) {
93
113
  const value = ___url.searchParams.get(searchParamMatcher.key);
94
114
  if (value === null) {
95
- return false;
115
+ return {
116
+ result: false,
117
+ description: `searchParams.get("${searchParamMatcher.key}") === null`,
118
+ };
96
119
  }
97
120
  if (!___matchesValue(searchParamMatcher.value, value)) {
98
- return false;
121
+ return {
122
+ result: false,
123
+ description: `searchParams.get("${searchParamMatcher.key}") = "${searchParamMatcher.value}"`,
124
+ };
99
125
  }
100
126
  }
101
127
  }
@@ -103,31 +129,50 @@ class RuleBuilderBaseBase {
103
129
  if (matchBuilderVariables.headers) {
104
130
  for (const headerMatcher of matchBuilderVariables.headers) {
105
131
  if (typeof headerMatcher === 'string') {
106
- return ___headers.has(headerMatcher);
132
+ const result = ___headers.has(headerMatcher);
133
+ return { result, description: `headers.has("${headerMatcher}")` };
107
134
  }
108
135
  if (headerMatcher instanceof RegExp) {
109
- return ___headers.toHeaderPairs().some(([key]) => headerMatcher.test(key));
136
+ const result = ___headers.toHeaderPairs().some(([key]) => headerMatcher.test(key));
137
+ return { result, description: `headers.keys matches ${headerMatcher.toString()}` };
110
138
  }
111
139
  if (!___headers.has(headerMatcher.key)) {
112
- return false;
140
+ return { result: false, description: `headers.has("${headerMatcher.key}")` };
113
141
  }
114
142
  if (headerMatcher.value) {
115
143
  const value = ___headers.get(headerMatcher.key);
116
144
  if (value === null) {
117
- return false;
145
+ return { result: false, description: `headers.get("${headerMatcher.key}") === null` };
118
146
  }
119
147
  if (!___matchesValue(headerMatcher.value, value)) {
120
- return false;
148
+ return {
149
+ result: false,
150
+ description: `headerMatcher.get("${headerMatcher.key}") = "${headerMatcher.value}"`,
151
+ };
121
152
  }
122
153
  }
123
154
  }
124
155
  }
125
156
  if (matchBuilderVariables.bodyText === null && !!req.body) {
126
- return false;
157
+ return { result: false, description: `empty body` };
127
158
  }
128
159
  if (matchBuilderVariables.bodyText) {
129
- if (!___matchesValue(matchBuilderVariables.bodyText, req.body)) {
130
- return false;
160
+ if (!req.body) {
161
+ return { result: false, description: `empty body` };
162
+ }
163
+ if (matchBuilderVariables.bodyText instanceof RegExp) {
164
+ if (!___matchesValue(matchBuilderVariables.bodyText, req.body)) {
165
+ return {
166
+ result: false,
167
+ description: `body text doesn't match ${matchBuilderVariables.bodyText.toString()}`,
168
+ };
169
+ }
170
+ }
171
+ else if (!req.body.includes(matchBuilderVariables.bodyText)) {
172
+ return {
173
+ result: false,
174
+ description: `body text doesn't include "${matchBuilderVariables.bodyText}"`,
175
+ };
131
176
  }
132
177
  }
133
178
  if (matchBuilderVariables.bodyJson) {
@@ -136,47 +181,63 @@ class RuleBuilderBaseBase {
136
181
  json = JSON.parse(req.body);
137
182
  }
138
183
  catch (kiss) {
139
- return false;
184
+ return { result: false, description: `unparseable json` };
140
185
  }
141
186
  if (!json) {
142
- return false;
187
+ return { result: false, description: `empty json object` };
143
188
  }
144
189
  for (const jsonMatcher of Array.isArray(matchBuilderVariables.bodyJson)
145
190
  ? matchBuilderVariables.bodyJson
146
191
  : [matchBuilderVariables.bodyJson]) {
147
192
  if (!matchObject(json, jsonMatcher.key, jsonMatcher.value)) {
148
- return false;
193
+ return { result: false, description: `$.${jsonMatcher.key} != "${jsonMatcher.value}"` };
149
194
  }
150
195
  }
151
196
  }
152
197
  if (matchBuilderVariables.bodyGql) {
153
198
  if (!req.gqlBody) {
154
- return false;
199
+ return { result: false, description: `not a gql body` };
155
200
  }
156
201
  if (!___matchesValue(matchBuilderVariables.bodyGql.methodName, req.gqlBody.methodName)) {
157
- return false;
202
+ return {
203
+ result: false,
204
+ description: `methodName "${matchBuilderVariables.bodyGql.methodName}" !== "${req.gqlBody.methodName}"`,
205
+ };
158
206
  }
159
207
  if (!___matchesValue(matchBuilderVariables.bodyGql.operationName, req.gqlBody.operationName)) {
160
- return false;
208
+ return {
209
+ result: false,
210
+ description: `operationName "${matchBuilderVariables.bodyGql.operationName}" !== "${req.gqlBody.operationName}"`,
211
+ };
161
212
  }
162
213
  if (!___matchesValue(matchBuilderVariables.bodyGql.query, req.gqlBody.query)) {
163
- return false;
214
+ return {
215
+ result: false,
216
+ description: `query "${matchBuilderVariables.bodyGql.query}" !== "${req.gqlBody.query}"`,
217
+ };
164
218
  }
165
219
  if (!___matchesValue(matchBuilderVariables.bodyGql.type, req.gqlBody.type)) {
166
- return false;
220
+ return {
221
+ result: false,
222
+ description: `type "${matchBuilderVariables.bodyGql.type}" !== "${req.gqlBody.type}"`,
223
+ };
167
224
  }
168
225
  if (!matchBuilderVariables.bodyGql.variables) {
169
- return true;
226
+ return { result: true, description: `no variables to match` };
170
227
  }
171
228
  for (const jsonMatcher of Array.isArray(matchBuilderVariables.bodyGql.variables)
172
229
  ? matchBuilderVariables.bodyGql.variables
173
230
  : [matchBuilderVariables.bodyGql.variables]) {
174
- if (!matchObject(req.gqlBody.variables, jsonMatcher.key, jsonMatcher.value)) {
175
- return false;
231
+ const matchObjectResult = matchObject(req.gqlBody.variables, jsonMatcher.key, jsonMatcher.value);
232
+ if (!matchObjectResult.result) {
233
+ return {
234
+ result: false,
235
+ description: `GQL variable ${jsonMatcher.key} != "${jsonMatcher.value}". Detail: ${matchObjectResult.description}`,
236
+ };
176
237
  }
177
238
  }
178
239
  }
179
- return true;
240
+ return { result: true, description: 'match' };
180
241
  },
181
242
  localVariables: { matchBuilderVariables: this._matchBuilderVariables },
182
243
  },
@@ -243,8 +304,7 @@ class RuleBuilder extends RuleBuilderBase {
243
304
  return new RuleBuilderInitialized(this.rule, this._matchBuilderVariables);
244
305
  }
245
306
  onAnyRequest() {
246
- this.rule.matches = { localFn: () => true };
247
- return new RuleBuilderInitialized(this.rule, this._matchBuilderVariables);
307
+ return this.onRequestTo(/.*/);
248
308
  }
249
309
  }
250
310
  class RuleBuilderInitialized extends RuleBuilderBase {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@stuntman/client",
3
- "version": "0.1.1",
3
+ "version": "0.1.2",
4
4
  "description": "Stuntman - HTTP proxy / mock API client",
5
5
  "main": "dist/index.js",
6
6
  "repository": {
@@ -32,7 +32,7 @@
32
32
  "author": "Andrzej Pasterczyk",
33
33
  "license": "MIT",
34
34
  "dependencies": {
35
- "@stuntman/shared": "^0.1.1",
35
+ "@stuntman/shared": "^0.1.2",
36
36
  "serialize-javascript": "6.0.1",
37
37
  "uuid": "9.0.0"
38
38
  },
@@ -1 +0,0 @@
1
- export {};
@@ -1,22 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- const apiClient_1 = require("./apiClient");
4
- const ruleBuilder_1 = require("./ruleBuilder");
5
- const client = new apiClient_1.Client();
6
- const uniqueQaUserEmail = 'unique_qa_email@example.com';
7
- const rule = (0, ruleBuilder_1.ruleBuilder)()
8
- .limitedUse(2)
9
- .onAnyRequest()
10
- .withBodyJson('test', 'value')
11
- .mockResponse({
12
- localFn: (req) => {
13
- if (JSON.parse(req.body).email !== uniqueQaUserEmail) {
14
- return {
15
- status: 500,
16
- };
17
- }
18
- return { status: 201 };
19
- },
20
- localVariables: { uniqueQaUserEmail },
21
- });
22
- client.addRule(rule).then((x) => console.log(x));