1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | exports.getGot = void 0;
|
4 | const js_lib_1 = require("@naturalcycles/js-lib");
|
5 | const got_1 = require("got");
|
6 | const __1 = require("..");
|
7 | const colors_1 = require("../colors");
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 | function getGot(opt = {}) {
|
16 | return got_1.default.extend({
|
17 |
|
18 |
|
19 |
|
20 |
|
21 | timeout: 90000,
|
22 | ...opt,
|
23 | hooks: {
|
24 | ...opt.hooks,
|
25 | beforeError: [
|
26 | ...(opt.hooks?.beforeError || []),
|
27 |
|
28 | gotErrorHook(opt),
|
29 | ],
|
30 | beforeRequest: [
|
31 | gotBeforeRequestHook(opt),
|
32 |
|
33 | ...(opt.hooks?.beforeRequest || []),
|
34 | ],
|
35 | afterResponse: [
|
36 | ...(opt.hooks?.afterResponse || []),
|
37 |
|
38 | gotAfterResponseHook(opt),
|
39 | ],
|
40 | },
|
41 | });
|
42 | }
|
43 | exports.getGot = getGot;
|
44 |
|
45 |
|
46 |
|
47 |
|
48 |
|
49 |
|
50 |
|
51 |
|
52 |
|
53 |
|
54 |
|
55 |
|
56 |
|
57 |
|
58 |
|
59 |
|
60 |
|
61 |
|
62 |
|
63 |
|
64 |
|
65 |
|
66 |
|
67 |
|
68 | function gotErrorHook(opt = {}) {
|
69 | const { maxResponseLength = 10000 } = opt;
|
70 | return err => {
|
71 | if (err instanceof got_1.HTTPError) {
|
72 | const { statusCode } = err.response;
|
73 | const { method, url, prefixUrl } = err.options;
|
74 | const shortUrl = getShortUrl(opt, url, prefixUrl);
|
75 |
|
76 | const body = (0, __1.inspectAny)(err.response.body, {
|
77 | maxLen: maxResponseLength,
|
78 | colors: false,
|
79 | });
|
80 |
|
81 | err.message = [[statusCode, method, shortUrl].filter(Boolean).join(' '), body]
|
82 | .filter(Boolean)
|
83 | .join('\n');
|
84 | }
|
85 | return err;
|
86 | };
|
87 | }
|
88 | function gotBeforeRequestHook(opt) {
|
89 | return options => {
|
90 | options.context = {
|
91 | ...options.context,
|
92 | started: Date.now(),
|
93 | };
|
94 | if (opt.logStart) {
|
95 | const shortUrl = getShortUrl(opt, options.url, options.prefixUrl);
|
96 | console.log([(0, colors_1.dimGrey)(' >>'), (0, colors_1.dimGrey)(options.method), (0, colors_1.grey)(shortUrl)].join(' '));
|
97 | }
|
98 | };
|
99 | }
|
100 | function gotAfterResponseHook(opt = {}) {
|
101 | return resp => {
|
102 | const success = resp.statusCode >= 200 && resp.statusCode < 400;
|
103 | if (opt.logFinished) {
|
104 | const { started } = resp.request.options.context;
|
105 | const { url, prefixUrl, method } = resp.request.options;
|
106 | const shortUrl = getShortUrl(opt, url, prefixUrl);
|
107 | console.log([
|
108 | (0, colors_1.dimGrey)(' <<'),
|
109 | coloredHttpCode(resp.statusCode),
|
110 | (0, colors_1.dimGrey)(method),
|
111 | (0, colors_1.grey)(shortUrl),
|
112 | started && (0, colors_1.dimGrey)('in ' + (0, js_lib_1._since)(started)),
|
113 | ]
|
114 | .filter(Boolean)
|
115 | .join(' '));
|
116 |
|
117 | }
|
118 |
|
119 | if (opt.logResponse && success) {
|
120 | console.log((0, __1.inspectAny)((0, js_lib_1._jsonParseIfPossible)(resp.body), { maxLen: opt.maxResponseLength }));
|
121 | }
|
122 | return resp;
|
123 | };
|
124 | }
|
125 | function coloredHttpCode(statusCode) {
|
126 | if (statusCode < 400)
|
127 | return (0, colors_1.dimGrey)(statusCode);
|
128 | if (statusCode < 500)
|
129 | return (0, colors_1.yellow)(statusCode);
|
130 | return (0, colors_1.red)(statusCode);
|
131 | }
|
132 | function getShortUrl(opt, url, prefixUrl) {
|
133 | let shortUrl = url.toString();
|
134 | if (opt.logWithSearchParams === false) {
|
135 | shortUrl = shortUrl.split('?')[0];
|
136 | }
|
137 | if (opt.logWithPrefixUrl === false && prefixUrl && shortUrl.startsWith(prefixUrl)) {
|
138 | shortUrl = shortUrl.slice(prefixUrl.length);
|
139 | }
|
140 | return shortUrl;
|
141 | }
|