open-api-mocker
Advanced tools
Comparing version 1.8.0 to 1.9.0
@@ -9,2 +9,12 @@ # Changelog | ||
## [1.9.0] - 2021-12-20 | ||
### Added | ||
- Response handling for different mime types | ||
### Changed | ||
- Now `x-faker` extension takes precedence in response generation (#53) | ||
### Fixed | ||
- Faker now locale selection now works as expected (#50, #54) | ||
## [1.8.0] - 2021-10-10 | ||
@@ -11,0 +21,0 @@ ### Changed |
@@ -57,8 +57,8 @@ 'use strict'; | ||
const { statusCode, headers: responseHeaders, body } = | ||
const { statusCode, headers: responseHeaders, body, responseMimeType } = | ||
getResponseMemo(path, req.path, JSON.stringify(req.query), preferHeader, JSON.stringify(requestBody)); | ||
return responseHandler(req, res, body, statusCode, responseHeaders); | ||
return responseHandler(req, res, body, statusCode, responseHeaders, responseMimeType); | ||
}; | ||
module.exports = handleRequest; |
@@ -156,3 +156,3 @@ 'use strict'; | ||
sendResponse(req, res, body, statusCode, headers) { | ||
sendResponse(req, res, body, statusCode, headers, responseMimeType = '') { | ||
@@ -178,4 +178,10 @@ statusCode = statusCode || 200; | ||
.set(headers) | ||
.set('x-powered-by', 'jormaechea/open-api-mocker') | ||
.json(body); | ||
.set('x-powered-by', 'jormaechea/open-api-mocker'); | ||
const mimeType = responseMimeType ? responseMimeType.toLowerCase() : ''; | ||
if(mimeType) | ||
res.type(mimeType); | ||
return res.send(body); | ||
} | ||
@@ -182,0 +188,0 @@ |
@@ -205,3 +205,4 @@ 'use strict'; | ||
headers, | ||
schema | ||
schema, | ||
responseMimeType | ||
} = preferredStatusCode ? this.getResponseByStatusCode(preferredStatusCode) : this.getFirstResponse(); | ||
@@ -212,3 +213,4 @@ | ||
headers: headers && this.generateResponseHeaders(headers), | ||
body: schema ? ResponseGenerator.generate(schema, preferredExampleName) : null | ||
body: schema ? ResponseGenerator.generate(schema, preferredExampleName) : null, | ||
responseMimeType | ||
}; | ||
@@ -226,5 +228,5 @@ } | ||
const [responseContent] = Object.values(preferredResponse.content || {}); | ||
const [[responseMimeType, responseContent] = []] = Object.entries(preferredResponse.content || {}); | ||
return { statusCode, schema: responseContent, headers: preferredResponse.headers }; | ||
return { statusCode, schema: responseContent, responseMimeType, headers: preferredResponse.headers }; | ||
} | ||
@@ -235,5 +237,5 @@ | ||
const [[statusCode, firstResponse]] = Object.entries(this.responses); | ||
const [firstResponseContent] = Object.values(firstResponse.content || {}); | ||
const [[responseMimeType, firstResponseContent] = []] = Object.entries(firstResponse.content || {}); | ||
return { statusCode, schema: firstResponseContent, headers: firstResponse.headers }; | ||
return { statusCode, schema: firstResponseContent, responseMimeType, headers: firstResponse.headers }; | ||
} | ||
@@ -240,0 +242,0 @@ |
@@ -5,5 +5,5 @@ 'use strict'; | ||
const faker = require('faker'); | ||
const getFakerLocale = require('../utils/get-faker-locale'); | ||
const { locale } = Intl.DateTimeFormat().resolvedOptions(); | ||
faker.setLocale(locale.replace('-', '_')); | ||
faker.setLocale(getFakerLocale()); | ||
@@ -43,2 +43,13 @@ class ResponseGenerator { | ||
const fakerExtension = schemaResponse['x-faker']; | ||
if(fakerExtension) { | ||
try { | ||
return this.generateByFaker(fakerExtension); | ||
} catch(e) { | ||
logger.warn( | ||
`Failed to generate fake result using ${fakerExtension} schema. Falling back to primitive type.` | ||
); | ||
} | ||
} | ||
if(schemaResponse.example) | ||
@@ -58,13 +69,2 @@ return schemaResponse.example; | ||
const fakerExtension = schemaResponse['x-faker']; | ||
if(fakerExtension) { | ||
try { | ||
return this.generateByFaker(fakerExtension); | ||
} catch(e) { | ||
logger.warn( | ||
`Failed to generate fake result using ${fakerExtension} schema. Falling back to primitive type.` | ||
); | ||
} | ||
} | ||
return this.generateByType(schemaResponse); | ||
@@ -71,0 +71,0 @@ } |
{ | ||
"name": "open-api-mocker", | ||
"version": "1.8.0", | ||
"version": "1.9.0", | ||
"description": "A mock server based in Open API Specification", | ||
@@ -5,0 +5,0 @@ "main": "lib/open-api-mocker.js", |
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
60263
59
1592