MCPcopy
hub / github.com/sindresorhus/got / parseBody

Function parseBody

source/core/response.ts:158–191  ·  view source on GitHub ↗
(response: Response, responseType: ResponseType, parseJson: ParseJsonFunction, encoding?: BufferEncoding)

Source from the content-addressed store, hash-verified

156};
157
158export const parseBody = (response: Response, responseType: ResponseType, parseJson: ParseJsonFunction, encoding?: BufferEncoding): unknown => {
159 const {rawBody} = response;
160 const cachedDecodedBody = decodedBodyCache.get(response);
161
162 try {
163 if (responseType === 'text') {
164 if (cachedDecodedBody !== undefined) {
165 return cachedDecodedBody;
166 }
167
168 return decodeUint8Array(rawBody, encoding);
169 }
170
171 if (responseType === 'json') {
172 if (rawBody.length === 0) {
173 return '';
174 }
175
176 const text = cachedDecodedBody ?? decodeUint8Array(rawBody, encoding);
177 return parseJson(text);
178 }
179
180 if (responseType === 'buffer') {
181 return rawBody;
182 }
183 } catch (error) {
184 throw new ParseError(error as Error, response);
185 }
186
187 throw new ParseError({
188 message: `Unknown body type '${responseType as string}'`,
189 name: 'Error',
190 }, response);
191};

Callers 2

makeRequestFunction · 0.85
shortcutFunction · 0.85

Calls 2

decodeUint8ArrayFunction · 0.85
getMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…