@event-driven-io/emmett-expressjs 0.20.2-alpha.1 → 0.20.2-alpha.4
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/index.cjs +492 -5
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +5 -5
- package/dist/index.js.map +1 -1
- package/package.json +2 -2
package/dist/index.cjs
CHANGED
|
@@ -1,7 +1,494 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function _nullishCoalesce(lhs, rhsFn) { if (lhs != null) { return lhs; } else { return rhsFn(); } } function _optionalChain(ops) { let lastAccessLHS = undefined; let value = ops[0]; let i = 1; while (i < ops.length) { const op = ops[i]; const fn = ops[i + 1]; i += 2; if ((op === 'optionalAccess' || op === 'optionalCall') && value == null) { return undefined; } if (op === 'access' || op === 'optionalAccess') { lastAccessLHS = value; value = fn(value); } else if (op === 'call' || op === 'optionalCall') { value = fn((...args) => value.call(lastAccessLHS, ...args)); lastAccessLHS = undefined; } } return value; } var _class; var _class2; var _class3
|
|
2
|
-
|
|
1
|
+
"use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function _nullishCoalesce(lhs, rhsFn) { if (lhs != null) { return lhs; } else { return rhsFn(); } } function _optionalChain(ops) { let lastAccessLHS = undefined; let value = ops[0]; let i = 1; while (i < ops.length) { const op = ops[i]; const fn = ops[i + 1]; i += 2; if ((op === 'optionalAccess' || op === 'optionalCall') && value == null) { return undefined; } if (op === 'access' || op === 'optionalAccess') { lastAccessLHS = value; value = fn(value); } else if (op === 'call' || op === 'optionalCall') { value = fn((...args) => value.call(lastAccessLHS, ...args)); lastAccessLHS = undefined; } } return value; } var _class; var _class2; var _class3;// src/index.ts
|
|
2
|
+
require('express-async-errors');
|
|
3
|
+
|
|
4
|
+
// src/application.ts
|
|
5
|
+
var _express = require('express'); var _express2 = _interopRequireDefault(_express);
|
|
6
|
+
|
|
7
|
+
var _http = require('http'); var _http2 = _interopRequireDefault(_http);
|
|
8
|
+
|
|
9
|
+
// ../emmett/dist/chunk-3XBWME34.js
|
|
10
|
+
var a = ((n) => (n.NOT_A_NONEMPTY_STRING = "NOT_A_NONEMPTY_STRING", n.NOT_A_POSITIVE_NUMBER = "NOT_A_POSITIVE_NUMBER", n.NOT_AN_UNSIGNED_BIGINT = "NOT_AN_UNSIGNED_BIGINT", n))(a || {});
|
|
11
|
+
var s = (t) => typeof t == "number" && t === t;
|
|
12
|
+
|
|
13
|
+
// ../emmett/dist/index.js
|
|
14
|
+
var _uuid = require('uuid');
|
|
15
|
+
var _webstreamspolyfill = require('web-streams-polyfill');
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
var _asyncretry = require('async-retry'); var _asyncretry2 = _interopRequireDefault(_asyncretry);
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
var O = (t, e = {}) => new S(t, e);
|
|
32
|
+
var S = (_class = class extends _webstreamspolyfill.TransformStream {
|
|
33
|
+
constructor(r2, n = {}) {
|
|
34
|
+
super({ cancel: (o2) => {
|
|
35
|
+
console.log("Stream was canceled. Reason:", o2), this.stopChecking();
|
|
36
|
+
} });_class.prototype.__init.call(this);_class.prototype.__init2.call(this);;
|
|
37
|
+
this.onNoActiveReaderCallback = r2;
|
|
38
|
+
this.streamId = _nullishCoalesce(_optionalChain([n, 'optionalAccess', _ => _.streamId]), () => ( _uuid.v4.call(void 0, ))), this.onNoActiveReaderCallback = r2, this.startChecking(_nullishCoalesce(_optionalChain([n, 'optionalAccess', _2 => _2.intervalCheckInMs]), () => ( 20)));
|
|
39
|
+
}
|
|
40
|
+
__init() {this.checkInterval = null}
|
|
41
|
+
|
|
42
|
+
__init2() {this._isStopped = false}
|
|
43
|
+
get hasActiveSubscribers() {
|
|
44
|
+
return !this._isStopped;
|
|
45
|
+
}
|
|
46
|
+
startChecking(r2) {
|
|
47
|
+
this.checkInterval = setInterval(() => {
|
|
48
|
+
this.checkNoActiveReader();
|
|
49
|
+
}, r2);
|
|
50
|
+
}
|
|
51
|
+
stopChecking() {
|
|
52
|
+
this.checkInterval && (clearInterval(this.checkInterval), this.checkInterval = null, this._isStopped = true, this.onNoActiveReaderCallback(this));
|
|
53
|
+
}
|
|
54
|
+
checkNoActiveReader() {
|
|
55
|
+
!this.readable.locked && !this._isStopped && this.stopChecking();
|
|
56
|
+
}
|
|
57
|
+
}, _class);
|
|
58
|
+
var w2 = async (t, e) => e === void 0 || e.retries === 0 ? t() : _asyncretry2.default.call(void 0, async (r2) => {
|
|
59
|
+
try {
|
|
60
|
+
return await t();
|
|
61
|
+
} catch (n) {
|
|
62
|
+
throw _optionalChain([e, 'optionalAccess', _3 => _3.shouldRetryError]) && !e.shouldRetryError(n) && r2(n), n;
|
|
63
|
+
}
|
|
64
|
+
}, _nullishCoalesce(e, () => ( { retries: 0 })));
|
|
65
|
+
var W = class extends Error {
|
|
66
|
+
constructor(e) {
|
|
67
|
+
super(`Cannot parse! ${e}`);
|
|
68
|
+
}
|
|
69
|
+
};
|
|
70
|
+
var c2 = { stringify: (t, e) => JSON.stringify(_optionalChain([e, 'optionalAccess', _4 => _4.map]) ? e.map(t) : t, (r2, n) => typeof n == "bigint" ? n.toString() : n), parse: (t, e) => {
|
|
71
|
+
let r2 = JSON.parse(t, _optionalChain([e, 'optionalAccess', _5 => _5.reviver]));
|
|
72
|
+
if (_optionalChain([e, 'optionalAccess', _6 => _6.typeCheck]) && !_optionalChain([e, 'optionalAccess', _7 => _7.typeCheck, 'call', _8 => _8(r2)])) throw new W(t);
|
|
73
|
+
return _optionalChain([e, 'optionalAccess', _9 => _9.map]) ? e.map(r2) : r2;
|
|
74
|
+
} };
|
|
75
|
+
var ae = (t) => new (0, _webstreamspolyfill.TransformStream)({ transform(e, r2) {
|
|
76
|
+
t(e) && r2.enqueue(e);
|
|
77
|
+
} });
|
|
78
|
+
var se = (t) => new (0, _webstreamspolyfill.TransformStream)({ transform(e, r2) {
|
|
79
|
+
r2.enqueue(t(e));
|
|
80
|
+
} });
|
|
81
|
+
var ie = (t, e) => new g2(t, e);
|
|
82
|
+
var g2 = class extends _webstreamspolyfill.TransformStream {
|
|
83
|
+
|
|
84
|
+
|
|
85
|
+
constructor(e, r2) {
|
|
86
|
+
super({ transform: (n) => {
|
|
87
|
+
this.accumulator = this.reducer(this.accumulator, n);
|
|
88
|
+
}, flush: (n) => {
|
|
89
|
+
n.enqueue(this.accumulator), n.terminate();
|
|
90
|
+
} }), this.accumulator = r2, this.reducer = e;
|
|
91
|
+
}
|
|
92
|
+
};
|
|
93
|
+
var me = (t, e, r2 = { forever: true, minTimeout: 25 }) => new (0, _webstreamspolyfill.TransformStream)({ start(n) {
|
|
94
|
+
w2(() => Ne(t, e, n), r2).catch((o2) => {
|
|
95
|
+
n.error(o2);
|
|
96
|
+
});
|
|
97
|
+
} });
|
|
98
|
+
var Ne = async (t, e, r2) => {
|
|
99
|
+
let o2 = t().getReader();
|
|
100
|
+
try {
|
|
101
|
+
let a2;
|
|
102
|
+
do {
|
|
103
|
+
let i = await o2.read();
|
|
104
|
+
a2 = i.done, await e(i, r2), a2 && r2.terminate();
|
|
105
|
+
} while (!a2);
|
|
106
|
+
} finally {
|
|
107
|
+
o2.releaseLock();
|
|
108
|
+
}
|
|
109
|
+
};
|
|
110
|
+
var de = (t) => new h(t);
|
|
111
|
+
var h = (_class2 = class extends _webstreamspolyfill.TransformStream {
|
|
112
|
+
__init3() {this.count = 0}
|
|
113
|
+
|
|
114
|
+
constructor(e) {
|
|
115
|
+
super({ transform: (r2, n) => {
|
|
116
|
+
this.count++, this.count > this.skip && n.enqueue(r2);
|
|
117
|
+
} });_class2.prototype.__init3.call(this);, this.skip = e;
|
|
118
|
+
}
|
|
119
|
+
}, _class2);
|
|
120
|
+
var pe = (t) => new (0, _webstreamspolyfill.TransformStream)({ transform(e, r2) {
|
|
121
|
+
r2.enqueue(e), t(e) && r2.terminate();
|
|
122
|
+
} });
|
|
123
|
+
var le = (t) => new (0, _webstreamspolyfill.TransformStream)({ async transform(e, r2) {
|
|
124
|
+
if (!t(e)) {
|
|
125
|
+
r2.enqueue(e);
|
|
126
|
+
return;
|
|
127
|
+
}
|
|
128
|
+
await Promise.resolve(), r2.terminate();
|
|
129
|
+
} });
|
|
130
|
+
var ce = (t) => new C(t);
|
|
131
|
+
var C = (_class3 = class extends _webstreamspolyfill.TransformStream {
|
|
132
|
+
__init4() {this.count = 0}
|
|
133
|
+
|
|
134
|
+
constructor(e) {
|
|
135
|
+
super({ transform: (r2, n) => {
|
|
136
|
+
this.count < this.limit ? (this.count++, n.enqueue(r2)) : n.terminate();
|
|
137
|
+
} });_class3.prototype.__init4.call(this);, this.limit = e;
|
|
138
|
+
}
|
|
139
|
+
}, _class3);
|
|
140
|
+
var ue = (t) => new (0, _webstreamspolyfill.TransformStream)({ start(e) {
|
|
141
|
+
let r2 = setTimeout(() => {
|
|
142
|
+
e.terminate();
|
|
143
|
+
}, t), n = e.terminate.bind(e);
|
|
144
|
+
e.terminate = () => {
|
|
145
|
+
clearTimeout(r2), n();
|
|
146
|
+
};
|
|
147
|
+
}, transform(e, r2) {
|
|
148
|
+
r2.enqueue(e);
|
|
149
|
+
} });
|
|
150
|
+
var fe = { filter: ae, take: ce, TakeTransformStream: C, skip: de, SkipTransformStream: h, map: se, notifyAboutNoActiveReadersStream: O, NotifyAboutNoActiveReadersStream: S, reduce: ie, ReduceTransformStream: g2, retry: me, stopAfter: pe, stopOn: le, waitAtMost: ue };
|
|
151
|
+
var { retry: Be } = fe;
|
|
152
|
+
var p2 = class extends Error {
|
|
153
|
+
constructor(e) {
|
|
154
|
+
super(e);
|
|
155
|
+
}
|
|
156
|
+
};
|
|
157
|
+
var ye = (t, e) => {
|
|
158
|
+
let r2 = t, n = e;
|
|
159
|
+
return U(r2), U(n), Object.keys(n).every((o2) => typeof n[o2] == "object" ? ye(r2[o2], n[o2]) : n[o2] === r2[o2]);
|
|
160
|
+
};
|
|
161
|
+
var po = (t) => {
|
|
162
|
+
throw new p2(_nullishCoalesce(t, () => ( "That should not ever happened, right?")));
|
|
163
|
+
};
|
|
164
|
+
var ve = (t, e, r2) => {
|
|
165
|
+
if (!ye(t, e)) throw new p2(_nullishCoalesce(r2, () => ( `subObj:
|
|
166
|
+
${c2.stringify(e)}
|
|
3
167
|
is not subset of
|
|
4
|
-
${
|
|
5
|
-
|
|
6
|
-
|
|
168
|
+
${c2.stringify(t)}`)));
|
|
169
|
+
};
|
|
170
|
+
function U(t, e) {
|
|
171
|
+
if (!t) throw new p2(_nullishCoalesce(e, () => ( "Condition is not truthy")));
|
|
172
|
+
}
|
|
173
|
+
function u(t, e, r2) {
|
|
174
|
+
if (t !== e) throw new p2(`${_nullishCoalesce(r2, () => ( "Objects are not equal"))}:
|
|
175
|
+
Expected: ${c2.stringify(t)}
|
|
176
|
+
Actual:${c2.stringify(e)}`);
|
|
177
|
+
}
|
|
178
|
+
var wo = (t) => {
|
|
179
|
+
let e = /* @__PURE__ */ new Map();
|
|
180
|
+
return { async aggregateStream(r2, n) {
|
|
181
|
+
return t.aggregateStream(r2, n);
|
|
182
|
+
}, readStream(r2, n) {
|
|
183
|
+
return t.readStream(r2, n);
|
|
184
|
+
}, appendToStream: async (r2, n, o2) => {
|
|
185
|
+
let a2 = await t.appendToStream(r2, n, o2), i = _nullishCoalesce(e.get(r2), () => ( [r2, []]));
|
|
186
|
+
return e.set(r2, [r2, [...i[1], ...n]]), a2;
|
|
187
|
+
}, appendedEvents: e, setup: async (r2, n) => t.appendToStream(r2, n) };
|
|
188
|
+
};
|
|
189
|
+
|
|
190
|
+
// src/middlewares/problemDetailsMiddleware.ts
|
|
191
|
+
var _httpproblemdetails = require('http-problem-details');
|
|
192
|
+
var problemDetailsMiddleware = (mapError) => (error, request, response, _next) => {
|
|
193
|
+
let problemDetails;
|
|
194
|
+
if (mapError) problemDetails = mapError(error, request);
|
|
195
|
+
problemDetails = _nullishCoalesce(problemDetails, () => ( defaulErrorToProblemDetailsMapping(error)));
|
|
196
|
+
sendProblem(response, problemDetails.status, { problem: problemDetails });
|
|
197
|
+
};
|
|
198
|
+
var defaulErrorToProblemDetailsMapping = (error) => {
|
|
199
|
+
let statusCode = 500;
|
|
200
|
+
if ("errorCode" in error && s(error.errorCode) && error.errorCode >= 100 && error.errorCode < 600) {
|
|
201
|
+
statusCode = error.errorCode;
|
|
202
|
+
}
|
|
203
|
+
return new (0, _httpproblemdetails.ProblemDocument)({
|
|
204
|
+
detail: error.message,
|
|
205
|
+
status: statusCode
|
|
206
|
+
});
|
|
207
|
+
};
|
|
208
|
+
|
|
209
|
+
// src/application.ts
|
|
210
|
+
var getApplication = (options) => {
|
|
211
|
+
const app = _express2.default.call(void 0, );
|
|
212
|
+
const {
|
|
213
|
+
apis,
|
|
214
|
+
mapError,
|
|
215
|
+
enableDefaultExpressEtag,
|
|
216
|
+
disableJsonMiddleware,
|
|
217
|
+
disableUrlEncodingMiddleware,
|
|
218
|
+
disableProblemDetailsMiddleware
|
|
219
|
+
} = options;
|
|
220
|
+
const router = _express.Router.call(void 0, );
|
|
221
|
+
app.set("etag", _nullishCoalesce(enableDefaultExpressEtag, () => ( false)));
|
|
222
|
+
if (!disableJsonMiddleware) app.use(_express2.default.json());
|
|
223
|
+
if (!disableUrlEncodingMiddleware)
|
|
224
|
+
app.use(
|
|
225
|
+
_express2.default.urlencoded({
|
|
226
|
+
extended: true
|
|
227
|
+
})
|
|
228
|
+
);
|
|
229
|
+
for (const api of apis) {
|
|
230
|
+
api(router);
|
|
231
|
+
}
|
|
232
|
+
app.use(router);
|
|
233
|
+
if (!disableProblemDetailsMiddleware)
|
|
234
|
+
app.use(problemDetailsMiddleware(mapError));
|
|
235
|
+
return app;
|
|
236
|
+
};
|
|
237
|
+
var startAPI = (app, options = { port: 3e3 }) => {
|
|
238
|
+
const { port } = options;
|
|
239
|
+
const server = _http2.default.createServer(app);
|
|
240
|
+
server.on("listening", () => {
|
|
241
|
+
console.info("server up listening");
|
|
242
|
+
});
|
|
243
|
+
return server.listen(port);
|
|
244
|
+
};
|
|
245
|
+
|
|
246
|
+
// src/etag.ts
|
|
247
|
+
var HeaderNames = {
|
|
248
|
+
IF_MATCH: "if-match",
|
|
249
|
+
IF_NOT_MATCH: "if-not-match",
|
|
250
|
+
ETag: "etag"
|
|
251
|
+
};
|
|
252
|
+
var WeakETagRegex = /W\/"(-?\d+.*)"/;
|
|
253
|
+
var ETagErrors = /* @__PURE__ */ ((ETagErrors2) => {
|
|
254
|
+
ETagErrors2["WRONG_WEAK_ETAG_FORMAT"] = "WRONG_WEAK_ETAG_FORMAT";
|
|
255
|
+
ETagErrors2["MISSING_IF_MATCH_HEADER"] = "MISSING_IF_MATCH_HEADER";
|
|
256
|
+
ETagErrors2["MISSING_IF_NOT_MATCH_HEADER"] = "MISSING_IF_NOT_MATCH_HEADER";
|
|
257
|
+
return ETagErrors2;
|
|
258
|
+
})(ETagErrors || {});
|
|
259
|
+
var isWeakETag = (etag) => {
|
|
260
|
+
return WeakETagRegex.test(etag);
|
|
261
|
+
};
|
|
262
|
+
var getWeakETagValue = (etag) => {
|
|
263
|
+
const result = WeakETagRegex.exec(etag);
|
|
264
|
+
if (result === null || result.length === 0) {
|
|
265
|
+
throw new Error("WRONG_WEAK_ETAG_FORMAT" /* WRONG_WEAK_ETAG_FORMAT */);
|
|
266
|
+
}
|
|
267
|
+
return result[1];
|
|
268
|
+
};
|
|
269
|
+
var toWeakETag = (value) => {
|
|
270
|
+
return `W/"${value}"`;
|
|
271
|
+
};
|
|
272
|
+
var getETagFromIfMatch = (request) => {
|
|
273
|
+
const etag = request.headers[HeaderNames.IF_MATCH];
|
|
274
|
+
if (etag === void 0) {
|
|
275
|
+
throw new Error("MISSING_IF_MATCH_HEADER" /* MISSING_IF_MATCH_HEADER */);
|
|
276
|
+
}
|
|
277
|
+
return etag;
|
|
278
|
+
};
|
|
279
|
+
var getETagFromIfNotMatch = (request) => {
|
|
280
|
+
const etag = request.headers[HeaderNames.IF_NOT_MATCH];
|
|
281
|
+
if (etag === void 0) {
|
|
282
|
+
throw new Error("MISSING_IF_MATCH_HEADER" /* MISSING_IF_MATCH_HEADER */);
|
|
283
|
+
}
|
|
284
|
+
return Array.isArray(etag) ? etag[0] : etag;
|
|
285
|
+
};
|
|
286
|
+
var setETag = (response, etag) => {
|
|
287
|
+
response.setHeader(HeaderNames.ETag, etag);
|
|
288
|
+
};
|
|
289
|
+
var getETagValueFromIfMatch = (request) => {
|
|
290
|
+
const eTagValue = getETagFromIfMatch(request);
|
|
291
|
+
return isWeakETag(eTagValue) ? getWeakETagValue(eTagValue) : eTagValue;
|
|
292
|
+
};
|
|
293
|
+
|
|
294
|
+
// src/handler.ts
|
|
295
|
+
|
|
296
|
+
var on = (handle) => async (request, response, _next) => {
|
|
297
|
+
const setResponse = await Promise.resolve(handle(request));
|
|
298
|
+
return setResponse(response);
|
|
299
|
+
};
|
|
300
|
+
var OK = (options) => (response) => {
|
|
301
|
+
send(response, 200, options);
|
|
302
|
+
};
|
|
303
|
+
var Created = (options) => (response) => {
|
|
304
|
+
sendCreated(response, options);
|
|
305
|
+
};
|
|
306
|
+
var Accepted = (options) => (response) => {
|
|
307
|
+
sendAccepted(response, options);
|
|
308
|
+
};
|
|
309
|
+
var NoContent = (options) => HttpResponse(204, options);
|
|
310
|
+
var HttpResponse = (statusCode, options) => (response) => {
|
|
311
|
+
send(response, statusCode, options);
|
|
312
|
+
};
|
|
313
|
+
var BadRequest = (options) => HttpProblem(400, options);
|
|
314
|
+
var Forbidden = (options) => HttpProblem(403, options);
|
|
315
|
+
var NotFound = (options) => HttpProblem(404, options);
|
|
316
|
+
var Conflict = (options) => HttpProblem(409, options);
|
|
317
|
+
var PreconditionFailed = (options) => HttpProblem(412, options);
|
|
318
|
+
var HttpProblem = (statusCode, options) => (response) => {
|
|
319
|
+
sendProblem(response, statusCode, options);
|
|
320
|
+
};
|
|
321
|
+
|
|
322
|
+
// src/responses.ts
|
|
323
|
+
|
|
324
|
+
|
|
325
|
+
var DefaultHttpResponseOptions = {};
|
|
326
|
+
var DefaultHttpProblemResponseOptions = {
|
|
327
|
+
problemDetails: "Error occured!"
|
|
328
|
+
};
|
|
329
|
+
var sendCreated = (response, { eTag, ...options }) => send(response, 201, {
|
|
330
|
+
location: "url" in options ? options.url : `${response.req.url}/${options.createdId}`,
|
|
331
|
+
body: "createdId" in options ? { id: options.createdId } : void 0,
|
|
332
|
+
eTag
|
|
333
|
+
});
|
|
334
|
+
var sendAccepted = (response, options) => send(response, 202, options);
|
|
335
|
+
var send = (response, statusCode, options) => {
|
|
336
|
+
const { location, body, eTag } = _nullishCoalesce(options, () => ( DefaultHttpResponseOptions));
|
|
337
|
+
if (eTag) setETag(response, eTag);
|
|
338
|
+
if (location) response.setHeader("Location", location);
|
|
339
|
+
if (body) {
|
|
340
|
+
response.statusCode = statusCode;
|
|
341
|
+
response.send(body);
|
|
342
|
+
} else {
|
|
343
|
+
response.sendStatus(statusCode);
|
|
344
|
+
}
|
|
345
|
+
};
|
|
346
|
+
var sendProblem = (response, statusCode, options) => {
|
|
347
|
+
options = _nullishCoalesce(options, () => ( DefaultHttpProblemResponseOptions));
|
|
348
|
+
const { location, eTag } = options;
|
|
349
|
+
const problemDetails = "problem" in options ? options.problem : new (0, _httpproblemdetails.ProblemDocument)({
|
|
350
|
+
detail: options.problemDetails,
|
|
351
|
+
status: statusCode
|
|
352
|
+
});
|
|
353
|
+
if (eTag) setETag(response, eTag);
|
|
354
|
+
if (location) response.setHeader("Location", location);
|
|
355
|
+
response.setHeader("Content-Type", "application/problem+json");
|
|
356
|
+
response.statusCode = statusCode;
|
|
357
|
+
response.json(problemDetails);
|
|
358
|
+
};
|
|
359
|
+
|
|
360
|
+
// src/testing/apiE2ESpecification.ts
|
|
361
|
+
var _supertest = require('supertest'); var _supertest2 = _interopRequireDefault(_supertest);
|
|
362
|
+
var _assert = require('assert'); var _assert2 = _interopRequireDefault(_assert);
|
|
363
|
+
var ApiE2ESpecification = {
|
|
364
|
+
for: (getEventStore, getApplication2) => {
|
|
365
|
+
{
|
|
366
|
+
return (...givenRequests) => {
|
|
367
|
+
const eventStore = wo(getEventStore());
|
|
368
|
+
const application = getApplication2(eventStore);
|
|
369
|
+
return {
|
|
370
|
+
when: (setupRequest) => {
|
|
371
|
+
const handle = async () => {
|
|
372
|
+
for (const requestFn of givenRequests) {
|
|
373
|
+
await requestFn(_supertest2.default.call(void 0, application));
|
|
374
|
+
}
|
|
375
|
+
return setupRequest(_supertest2.default.call(void 0, application));
|
|
376
|
+
};
|
|
377
|
+
return {
|
|
378
|
+
then: async (verify) => {
|
|
379
|
+
const response = await handle();
|
|
380
|
+
verify.forEach((assertion) => {
|
|
381
|
+
const succeeded = assertion(response);
|
|
382
|
+
if (succeeded === false) _assert2.default.fail();
|
|
383
|
+
});
|
|
384
|
+
}
|
|
385
|
+
};
|
|
386
|
+
}
|
|
387
|
+
};
|
|
388
|
+
};
|
|
389
|
+
}
|
|
390
|
+
}
|
|
391
|
+
};
|
|
392
|
+
|
|
393
|
+
// src/testing/apiSpecification.ts
|
|
394
|
+
|
|
395
|
+
|
|
396
|
+
var existingStream = (streamId, events) => {
|
|
397
|
+
return [streamId, events];
|
|
398
|
+
};
|
|
399
|
+
var expect = (streamId, events) => {
|
|
400
|
+
return [streamId, events];
|
|
401
|
+
};
|
|
402
|
+
var expectNewEvents = (streamId, events) => {
|
|
403
|
+
return [streamId, events];
|
|
404
|
+
};
|
|
405
|
+
var expectResponse = (statusCode, options) => (response) => {
|
|
406
|
+
const { body, headers } = _nullishCoalesce(options, () => ( {}));
|
|
407
|
+
u(statusCode, response.statusCode, "Response code doesn't match");
|
|
408
|
+
if (body) ve(response.body, body);
|
|
409
|
+
if (headers) ve(response.headers, headers);
|
|
410
|
+
};
|
|
411
|
+
var expectError = (errorCode, problemDetails) => expectResponse(
|
|
412
|
+
errorCode,
|
|
413
|
+
problemDetails ? { body: problemDetails } : void 0
|
|
414
|
+
);
|
|
415
|
+
var ApiSpecification = {
|
|
416
|
+
for: (getEventStore, getApplication2) => {
|
|
417
|
+
{
|
|
418
|
+
return (...givenStreams) => {
|
|
419
|
+
const eventStore = wo(getEventStore());
|
|
420
|
+
const application = getApplication2(eventStore);
|
|
421
|
+
return {
|
|
422
|
+
when: (setupRequest) => {
|
|
423
|
+
const handle = async () => {
|
|
424
|
+
for (const [streamName, events] of givenStreams) {
|
|
425
|
+
await eventStore.setup(streamName, events);
|
|
426
|
+
}
|
|
427
|
+
return setupRequest(_supertest2.default.call(void 0, application));
|
|
428
|
+
};
|
|
429
|
+
return {
|
|
430
|
+
then: async (verify) => {
|
|
431
|
+
const response = await handle();
|
|
432
|
+
if (typeof verify === "function") {
|
|
433
|
+
const succeeded = verify(response);
|
|
434
|
+
if (succeeded === false) po();
|
|
435
|
+
} else if (Array.isArray(verify)) {
|
|
436
|
+
const [first, ...rest] = verify;
|
|
437
|
+
if (typeof first === "function") {
|
|
438
|
+
const succeeded = first(response);
|
|
439
|
+
if (succeeded === false) po();
|
|
440
|
+
}
|
|
441
|
+
const events = typeof first === "function" ? rest : verify;
|
|
442
|
+
ve(
|
|
443
|
+
Array.from(eventStore.appendedEvents.values()),
|
|
444
|
+
events
|
|
445
|
+
);
|
|
446
|
+
}
|
|
447
|
+
}
|
|
448
|
+
};
|
|
449
|
+
}
|
|
450
|
+
};
|
|
451
|
+
};
|
|
452
|
+
}
|
|
453
|
+
}
|
|
454
|
+
};
|
|
455
|
+
|
|
456
|
+
|
|
457
|
+
|
|
458
|
+
|
|
459
|
+
|
|
460
|
+
|
|
461
|
+
|
|
462
|
+
|
|
463
|
+
|
|
464
|
+
|
|
465
|
+
|
|
466
|
+
|
|
467
|
+
|
|
468
|
+
|
|
469
|
+
|
|
470
|
+
|
|
471
|
+
|
|
472
|
+
|
|
473
|
+
|
|
474
|
+
|
|
475
|
+
|
|
476
|
+
|
|
477
|
+
|
|
478
|
+
|
|
479
|
+
|
|
480
|
+
|
|
481
|
+
|
|
482
|
+
|
|
483
|
+
|
|
484
|
+
|
|
485
|
+
|
|
486
|
+
|
|
487
|
+
|
|
488
|
+
|
|
489
|
+
|
|
490
|
+
|
|
491
|
+
|
|
492
|
+
|
|
493
|
+
exports.Accepted = Accepted; exports.ApiE2ESpecification = ApiE2ESpecification; exports.ApiSpecification = ApiSpecification; exports.BadRequest = BadRequest; exports.Conflict = Conflict; exports.Created = Created; exports.DefaultHttpProblemResponseOptions = DefaultHttpProblemResponseOptions; exports.DefaultHttpResponseOptions = DefaultHttpResponseOptions; exports.ETagErrors = ETagErrors; exports.Forbidden = Forbidden; exports.HeaderNames = HeaderNames; exports.HttpProblem = HttpProblem; exports.HttpResponse = HttpResponse; exports.NoContent = NoContent; exports.NotFound = NotFound; exports.OK = OK; exports.PreconditionFailed = PreconditionFailed; exports.WeakETagRegex = WeakETagRegex; exports.existingStream = existingStream; exports.expect = expect; exports.expectError = expectError; exports.expectNewEvents = expectNewEvents; exports.expectResponse = expectResponse; exports.getApplication = getApplication; exports.getETagFromIfMatch = getETagFromIfMatch; exports.getETagFromIfNotMatch = getETagFromIfNotMatch; exports.getETagValueFromIfMatch = getETagValueFromIfMatch; exports.getWeakETagValue = getWeakETagValue; exports.isWeakETag = isWeakETag; exports.on = on; exports.send = send; exports.sendAccepted = sendAccepted; exports.sendCreated = sendCreated; exports.sendProblem = sendProblem; exports.setETag = setETag; exports.startAPI = startAPI; exports.toWeakETag = toWeakETag;
|
|
7
494
|
//# sourceMappingURL=index.cjs.map
|