@lokalise/node-core
Advanced tools
Comparing version 2.3.0 to 2.4.0
@@ -15,2 +15,3 @@ /// <reference types="node" /> | ||
reqContext?: HttpRequestContext; | ||
safeParseJson?: boolean; | ||
}; | ||
@@ -24,2 +25,3 @@ export type DeleteRequestOptions = GetRequestOptions; | ||
reqContext?: HttpRequestContext; | ||
safeParseJson?: boolean; | ||
}; | ||
@@ -26,0 +28,0 @@ export type Response<T> = { |
@@ -5,2 +5,3 @@ "use strict"; | ||
const undici_1 = require("undici"); | ||
const InternalError_1 = require("../errors/InternalError"); | ||
const defaultOptions = { | ||
@@ -23,3 +24,3 @@ throwOnError: true, | ||
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment | ||
const resolvedBody = await resolveBody(response); | ||
const resolvedBody = await resolveBody(response, options.safeParseJson); | ||
return { | ||
@@ -46,3 +47,3 @@ // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment | ||
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment | ||
const resolvedBody = await resolveBody(response); | ||
const resolvedBody = await resolveBody(response, options.safeParseJson); | ||
return { | ||
@@ -70,3 +71,3 @@ // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment | ||
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment | ||
const resolvedBody = await resolveBody(response); | ||
const resolvedBody = await resolveBody(response, options.safeParseJson); | ||
return { | ||
@@ -94,3 +95,3 @@ // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment | ||
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment | ||
const resolvedBody = await resolveBody(response); | ||
const resolvedBody = await resolveBody(response, options.safeParseJson); | ||
return { | ||
@@ -118,3 +119,3 @@ // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment | ||
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment | ||
const resolvedBody = await resolveBody(response); | ||
const resolvedBody = await resolveBody(response, options.safeParseJson); | ||
return { | ||
@@ -142,3 +143,3 @@ // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment | ||
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment | ||
const resolvedBody = await resolveBody(response); | ||
const resolvedBody = await resolveBody(response, options.safeParseJson); | ||
return { | ||
@@ -152,8 +153,25 @@ // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment | ||
exports.sendPatch = sendPatch; | ||
async function resolveBody(response) { | ||
async function resolveBody(response, safeParseJson = false) { | ||
const contentType = response.headers['content-type']; | ||
// eslint-disable-next-line @typescript-eslint/no-unsafe-return | ||
return !contentType || contentType?.startsWith('application/json') | ||
? await response.body.json() | ||
: await response.body.text(); | ||
if (contentType?.startsWith('application/json')) { | ||
if (!safeParseJson) { | ||
// eslint-disable-next-line @typescript-eslint/no-unsafe-return | ||
return await response.body.json(); | ||
} | ||
const rawBody = await response.body.text(); | ||
try { | ||
// eslint-disable-next-line @typescript-eslint/no-unsafe-return | ||
return JSON.parse(rawBody); | ||
} | ||
catch (err) { | ||
throw new InternalError_1.InternalError({ | ||
message: 'Error while parsing HTTP JSON response', | ||
errorCode: 'INVALID_HTTP_RESPONSE_JSON', | ||
details: { | ||
rawBody, | ||
}, | ||
}); | ||
} | ||
} | ||
return await response.body.text(); | ||
} | ||
@@ -160,0 +178,0 @@ function resolveUrl(baseUrl, path) { |
@@ -22,2 +22,26 @@ "use strict"; | ||
describe('GET', () => { | ||
it('returns original payload when breaking during parsing', async () => { | ||
expect.assertions(1); | ||
const client = mockAgent.get('https://fakestoreapi.com'); | ||
client | ||
.intercept({ | ||
path: '/products/1', | ||
method: 'GET', | ||
}) | ||
.reply(200, 'this is not a real json', { headers: JSON_HEADERS }); | ||
try { | ||
await (0, httpClient_1.sendGet)('https://fakestoreapi.com', 'products/1', { safeParseJson: true }); | ||
} | ||
catch (err) { | ||
// This is needed, because built-in error assertions do not assert nested fields | ||
// eslint-disable-next-line jest/no-conditional-expect | ||
expect(err).toMatchObject({ | ||
message: 'Error while parsing HTTP JSON response', | ||
errorCode: 'INVALID_HTTP_RESPONSE_JSON', | ||
details: { | ||
rawBody: 'this is not a real json', | ||
}, | ||
}); | ||
} | ||
}); | ||
it('GET without queryParams', async () => { | ||
@@ -47,2 +71,13 @@ const client = mockAgent.get('https://fakestoreapi.com'); | ||
}); | ||
it('GET returning text without content type', async () => { | ||
const client = mockAgent.get('https://fakestoreapi.com'); | ||
client | ||
.intercept({ | ||
path: '/products/1', | ||
method: 'GET', | ||
}) | ||
.reply(200, 'just text', {}); | ||
const result = await (0, httpClient_1.sendGet)('https://fakestoreapi.com', 'products/1'); | ||
expect(result.body).toBe('just text'); | ||
}); | ||
it('GET with queryParams', async () => { | ||
@@ -49,0 +84,0 @@ const client = mockAgent.get('https://fakestoreapi.com'); |
{ | ||
"name": "@lokalise/node-core", | ||
"version": "2.3.0", | ||
"version": "2.4.0", | ||
"author": { | ||
@@ -5,0 +5,0 @@ "name": "Lokalise", |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
98660
1184