This repository was archived by the owner on Oct 4, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrest-api-doc.html
143 lines (121 loc) · 3.77 KB
/
rest-api-doc.html
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
<link rel="import" href="../polymer/polymer.html">
<!--
`rest-api-doc` defines an endpoint for a `rest-api-page`.
Example:
<rest-api-page base-url="https://httpbin.org">
<rest-api-doc title="I'm a title" endpoint="/post" method="POST">
<rest-api-param key="param" type="number" values="[1, 2, 3]">Explanation for the param</rest-api-param>
<rest-api-header key="Content-Type" values='["application/json"]'>Explanation for the header</rest-api-header>
<rest-api-kv-body key="key" values='["application(json"]' optional>Explanation for the key-value body</rest-api-header>
<rest-api-explanation>Explanation for the endpoint</rest-api-explanation>
</rest-api-doc>
</rest-api-page>
@group REST Elements
@element rest-api-doc
@hero hero.svg
-->
<dom-module id="rest-api-doc">
<template>
<style>
:host {
position: relative;
display: block;
box-sizing: border-box;
font-family: 'Roboto', sans-serif;
}
paper-button {
position: absolute;
top: 8px;
right: 8px;
background: #F44336;
color: #fff;
}
</style>
<content></content>
</template>
</dom-module>
<script>
Polymer({
is: 'rest-api-doc',
hostAttributes: {
hidden: true
},
properties: {
/**
* The `title` of the request
*/
title: String,
/**
* The `endpoint` to send the request to
*/
endpoint: String,
/**
* The HTTP `method` of the request, e.g. `GET`, `POST`, etc.
*/
method: {
type: String,
value: 'GET'
},
/**
* Values the HTTP `parameters` has to or optionally can contain
* @type {Array<{key: string, type: string, optional: boolean, values: string}>}
*/
params: Array,
/**
* Values the `headers` of the HTTP Request has to or optionally can contain
* @type {Array<{key: string, type: string, optional: boolean, values: string}>}
*/
headers: Array,
/**
* Values the `body` of the HTTP Request has to or optionally can contain
* @type {Array<{key: string, type: string, optional: boolean, values: string}>}
*/
body: Array,
/**
* The explanation of the endpoint
*/
explanation: String,
/**
* The DOM observer
*/
_observer: Object
},
ready: function () {
this.set('params', []);
this.set('headers', []);
this.set('body', null);
this.set('explanation', '');
// Observe DOM mutations
this._observer = Polymer.dom(this).observeNodes(function (info) {
this._setRequestInfo();
});
},
/**
* Updates request information
* @private
*/
_setRequestInfo: function () {
var params = this.queryAllEffectiveChildren('rest-api-param');
var headers = this.queryAllEffectiveChildren('rest-api-header');
var body = this.queryAllEffectiveChildren('rest-api-body');
var kvBody = this.queryAllEffectiveChildren('rest-api-kv-body');
var explanation = this.queryAllEffectiveChildren('rest-api-explanation');
// Check for duplicate body declaration
if (body.length > 0 && kvBody > 0) {
console.warn('Only use either <rest-api-body> or <rest-api-kv-body>, ' +
'but not both for one api. Now using: <rest-api-body>');
this.set('body', body[0]);
} else if (body.length > 0) {
this.set('body', body[0]);
} else if (kvBody.length > 0) {
this.set('body', kvBody);
} else {
this.set('body', null);
}
this.set('params', params);
this.set('headers', headers);
if (explanation.length > 0)
this.set('explanation', explanation[0].innerHTML);
}
});
</script>