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 pathparseContactObject.js
65 lines (54 loc) · 1.74 KB
/
parseContactObject.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
const R = require('ramda');
const {
createInvalidMemberWarning,
} = require('../annotations');
const {
hasKey, isExtension,
} = require('../../predicates');
const parseObject = require('../parseObject');
const parseString = require('../parseString');
const pipeParseResult = require('../../pipeParseResult');
const name = 'Contact Object';
const parseMember = context => R.cond([
[hasKey('name'), parseString(context, name, false)],
[hasKey('url'), parseString(context, name, false)],
[hasKey('email'), parseString(context, name, false)],
[isExtension, () => new context.namespace.elements.ParseResult()],
[R.T, createInvalidMemberWarning(context.namespace, name)],
]);
/**
* Parse the OpenAPI 'Contact Object' (`#/info/contact`)
* @see http://spec.openapis.org/oas/v3.0.2#contact-object
* @returns ParseResult<Link>
* @private
*/
const parseContactObject = context => pipeParseResult(
context.namespace,
parseObject(context, name, parseMember(context)),
(contactObject) => {
const contactName = contactObject.get('name');
const contactUrl = contactObject.get('url');
const contactEmail = contactObject.get('email');
const links = [];
if (contactUrl) {
const link = new context.namespace.elements.Link();
link.relation = 'contact';
link.href = contactUrl;
if (contactName) {
link.title = contactName.clone();
}
links.push(link);
}
if (contactEmail) {
const link = new context.namespace.elements.Link();
link.relation = 'contact';
link.href = `mailto:${contactEmail.toValue()}`;
if (!contactUrl && contactName) {
link.title = contactName.clone();
}
links.push(link);
}
return links;
}
);
module.exports = parseContactObject;