UNPKG

868 BJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3function nock(host, options, cb) {
4 if (typeof options === 'function') {
5 cb = options;
6 options = {};
7 }
8 if (cb === undefined)
9 throw new Error('callback is undefined');
10 let nock;
11 try {
12 nock = require('nock');
13 }
14 catch (_a) {
15 return {
16 run() {
17 require('nock');
18 },
19 };
20 }
21 const intercepter = nock(host, options);
22 return {
23 async run(ctx) {
24 ctx.nock = ctx.nock || 0;
25 await cb(intercepter);
26 ctx.nock++;
27 },
28 finally(ctx) {
29 if (!ctx.error)
30 intercepter.done();
31 ctx.nock--;
32 if (ctx.nock === 0)
33 nock.cleanAll();
34 },
35 };
36}
37exports.nock = nock;