1 | import { _isHttpErrorResponse, _jsonParseIfPossible, _since } from '@naturalcycles/js-lib'
|
2 | import got, { AfterResponseHook, BeforeErrorHook, BeforeRequestHook, Got, HTTPError } from 'got'
|
3 | import { URL } from 'url'
|
4 | import { inspectAny } from '..'
|
5 | import { dimGrey, grey, red, yellow } from '../colors'
|
6 | import { GetGotOptions, GotRequestContext } from './got.model'
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 | export function getGot(opt: GetGotOptions = {}): Got {
|
16 | return got.extend({
|
17 |
|
18 |
|
19 |
|
20 |
|
21 | timeout: 90_000,
|
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 |
|
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 |
|
69 |
|
70 | function gotErrorHook(opt: GetGotOptions = {}): BeforeErrorHook {
|
71 | const { maxResponseLength = 10000 } = opt
|
72 |
|
73 | return err => {
|
74 | if (err instanceof HTTPError) {
|
75 | const { statusCode } = err.response
|
76 | const { method, url, prefixUrl } = err.options
|
77 | const shortUrl = getShortUrl(opt, url, prefixUrl)
|
78 |
|
79 |
|
80 |
|
81 | let body = _jsonParseIfPossible(err.response.body)
|
82 |
|
83 |
|
84 | if (_isHttpErrorResponse(body)) {
|
85 | body = body.error
|
86 | }
|
87 |
|
88 | body = inspectAny(body, {
|
89 | maxLen: maxResponseLength,
|
90 | colors: false,
|
91 | })
|
92 |
|
93 |
|
94 | err.message = [[statusCode, method, shortUrl].filter(Boolean).join(' '), body]
|
95 | .filter(Boolean)
|
96 | .join('\n')
|
97 | }
|
98 |
|
99 | return err
|
100 | }
|
101 | }
|
102 |
|
103 | function gotBeforeRequestHook(opt: GetGotOptions): BeforeRequestHook {
|
104 | return options => {
|
105 | options.context = {
|
106 | ...options.context,
|
107 | started: Date.now(),
|
108 | } as GotRequestContext
|
109 |
|
110 | if (opt.logStart) {
|
111 | const shortUrl = getShortUrl(opt, options.url, options.prefixUrl)
|
112 | console.log([dimGrey(' >>'), dimGrey(options.method), grey(shortUrl)].join(' '))
|
113 | }
|
114 | }
|
115 | }
|
116 |
|
117 | function gotAfterResponseHook(opt: GetGotOptions = {}): AfterResponseHook {
|
118 | return resp => {
|
119 | const success = resp.statusCode >= 200 && resp.statusCode < 400
|
120 |
|
121 | if (opt.logFinished) {
|
122 | const { started } = resp.request.options.context as GotRequestContext
|
123 | const { url, prefixUrl, method } = resp.request.options
|
124 | const shortUrl = getShortUrl(opt, url, prefixUrl)
|
125 |
|
126 | console.log(
|
127 | [
|
128 | dimGrey(' <<'),
|
129 | coloredHttpCode(resp.statusCode),
|
130 | dimGrey(method),
|
131 | grey(shortUrl),
|
132 | started && dimGrey('in ' + _since(started)),
|
133 | ]
|
134 | .filter(Boolean)
|
135 | .join(' '),
|
136 | )
|
137 |
|
138 | }
|
139 |
|
140 |
|
141 | if (opt.logResponse && success) {
|
142 | console.log(inspectAny(_jsonParseIfPossible(resp.body), { maxLen: opt.maxResponseLength }))
|
143 | }
|
144 |
|
145 | return resp
|
146 | }
|
147 | }
|
148 |
|
149 | function coloredHttpCode(statusCode: number): string {
|
150 | if (statusCode < 400) return dimGrey(statusCode)
|
151 | if (statusCode < 500) return yellow(statusCode)
|
152 | return red(statusCode)
|
153 | }
|
154 |
|
155 | function getShortUrl(opt: GetGotOptions, url: URL, prefixUrl?: string): string {
|
156 | let shortUrl = url.toString()
|
157 |
|
158 | if (opt.logWithSearchParams === false) {
|
159 | shortUrl = shortUrl.split('?')[0]!
|
160 | }
|
161 |
|
162 | if (opt.logWithPrefixUrl === false && prefixUrl && shortUrl.startsWith(prefixUrl)) {
|
163 | shortUrl = shortUrl.slice(prefixUrl.length)
|
164 | }
|
165 |
|
166 | return shortUrl
|
167 | }
|