This repository was archived by the owner on Nov 8, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathparseResponsesObject.js
88 lines (69 loc) · 2.93 KB
/
parseResponsesObject.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
const R = require('ramda');
const pipeParseResult = require('../../pipeParseResult');
const {
isString, isExtension, hasKey, getKey, getValue,
} = require('../../predicates');
const { createWarning, createInvalidMemberWarning } = require('../annotations');
const parseObject = require('../parseObject');
const parseResponseObject = require('./parseResponseObject');
const parseReference = require('../parseReference');
const parseResponseObjectOrRef = parseReference('responses', parseResponseObject);
const name = 'Responses Object';
// Returns if member has key that is 3 digit HTTP status code
function isStatusCode(member) {
return String(member.key.toValue()).match(/^\d\d\d$/);
}
// Returns if member has key that is 3 digit HTTP status code with X to represent range
function isStatusCodeRange(member) {
return String(member.key.toValue()).match(/^[\dX]{3}$/);
}
const isResponseField = R.anyPass([isStatusCode, isStatusCodeRange, hasKey('default')]);
const isKeyString = R.compose(isString, getKey);
/**
* Parse Responses Object
*
* @param namespace {Namespace}
* @param element {Element}
* @returns ParseResult
*
* @see https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.0.md#responsesObject
* @private
*/
function parseResponsesObject(context, element) {
const { namespace } = context;
const createUnsupportedStatusCodeWarning = (member) => {
const message = `'${name}' response status code ranges are unsupported`;
return createWarning(namespace, message, member.key);
};
const attachStatusCodeNotStringWarning = member => new namespace.elements.ParseResult([
member,
createWarning(namespace, `'${name}' response status code must be a string and should be wrapped in quotes`, member.key),
]);
const isStatusCodeOrDefault = R.anyPass([isStatusCode, hasKey('default')]);
// FIXME Add support for status code ranges
// https://github.com/apiaryio/fury-adapter-oas3-parser/issues/64
const validateStatusCode = pipeParseResult(namespace,
R.unless(isStatusCodeOrDefault, createUnsupportedStatusCodeWarning),
R.unless(isKeyString, attachStatusCodeNotStringWarning));
const parseResponse = pipeParseResult(namespace,
validateStatusCode,
R.compose(parseResponseObjectOrRef(context), getValue));
const parseMember = R.cond([
[isResponseField, parseResponse],
// FIXME Support exposing extensions into parse result
[isExtension, () => new namespace.elements.ParseResult()],
// Return a warning for additional properties
[R.T, createInvalidMemberWarning(namespace, name)],
]);
const parseResponses = pipeParseResult(namespace,
parseObject(context, name, parseMember),
object => object.content.map((member) => {
const response = member.value;
if (isStatusCode(member)) {
response.statusCode = String(member.key.toValue());
}
return response;
}));
return parseResponses(element);
}
module.exports = R.curry(parseResponsesObject);