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