Skip to content

Commit

Permalink
Merge pull request #3 from leboncoin/fix-default-success
Browse files Browse the repository at this point in the history
Fix default success
  • Loading branch information
vroudge authored Nov 7, 2017
2 parents 97530ed + 74ea3fa commit 8451f13
Showing 1 changed file with 19 additions and 10 deletions.
29 changes: 19 additions & 10 deletions src/utils/createRouterFromSwagger.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,10 +79,10 @@ const addMethodsToRouter = (db, router, { route, routerMethods, pathOptions }) =
*/
const responseGetter = (db, responses) => {
let body;
const defaultSuccess = responses[200] || responses[204];
const defaultSuccess = responses[200] || responses[201] || responses[204];

return ((ctx) => {
if (defaultSuccess) {
if (defaultSuccess && defaultSuccess.schema) {
const objectRef = getReferencesAndCount(defaultSuccess.schema);
const collection = findCollection(db, objectRef.object);

Expand All @@ -94,15 +94,24 @@ const responseGetter = (db, responses) => {
)
)
, objectRef.fixtureCount);
if (defaultSuccess.schema.type === 'array') {
ctx.body = body;
}
else if (defaultSuccess.schema['$ref']) {
ctx.body = body[0];
}
return;
} else if (defaultSuccess) {
const possibleCodes = [200, 201, 204];
for (const code of possibleCodes){
if(responses[code]){
ctx.status = code;
break;
}
}
return;
}
if (defaultSuccess.schema.type === 'array') {
ctx.body = body;
}
else if (defaultSuccess.schema['$ref']) {
ctx.body = body[0];
} else {
ctx.throw('Unsupported format! Can you create an issue in Github about that?')
}
ctx.throw('Unsupported format! Can you create an issue in Github about that?')

});

Expand Down

0 comments on commit 8451f13

Please sign in to comment.