1
1
import './key-val.js' ;
2
+ import { w3CodeColor } from './w3CodeColor.js' ;
2
3
import { API , APIProxy } from '../API.js' ;
3
- import { w3CodeColor } from '../w3CodeColor.js' ;
4
4
5
5
class APIFetch extends HTMLElement {
6
6
static __tag = 'api-fetch' ;
7
7
static __template = document . createElement ( 'template' ) ;
8
+ static #funcpattern = / ^ [ _ a - z A - Z ] [ _ a - z A - Z 0 - 9 - .] * $ / ;
8
9
#root;
9
10
#pcntr;
10
11
#ccntr;
11
12
#rcntr;
13
+ #ocntr;
12
14
#fname;
13
15
#api;
14
16
constructor ( ) {
@@ -24,13 +26,17 @@ class APIFetch extends HTMLElement {
24
26
25
27
this . #fname = this . #root. querySelector ( '#func' ) ;
26
28
this . #pcntr = this . #root. querySelector ( '#para_cntr' ) ;
27
- this . #ccntr = this . #root. querySelector ( '#code_cntr' ) ;
28
- this . #rcntr = this . #root. querySelector ( '#resp_cntr' ) ;
29
+ this . #ccntr = this . #root. querySelector ( '#code_cntr pre' ) ;
30
+ this . #rcntr = this . #root. querySelector ( '#resp_cntr pre' ) ;
31
+ this . #ocntr = this . #root. querySelector ( '#out_cntr' ) ;
32
+
33
+ this . #ocntr. style . display = 'none' ;
29
34
30
- this . #ccntr. style . display = 'none' ;
31
- this . #rcntr. style . display = 'none' ;
32
35
this . #root. querySelector ( '#fetch' ) . onclick = async ( ) => {
33
- if ( ! this . #fname. reportValidity ( ) ) {
36
+ this . #ocntr. style . display = 'none' ;
37
+ if ( ! APIFetch . #funcpattern. test ( this . #fname. value ) ) {
38
+ this . #fname. setCustomValidity ( `Required 'function-name' pattern: ${ APIFetch . #funcpattern} ` ) ;
39
+ this . #fname. reportValidity ( ) ;
34
40
return ;
35
41
}
36
42
let fname = this . #fname. value ;
@@ -42,15 +48,15 @@ class APIFetch extends HTMLElement {
42
48
}
43
49
param = { ...param , ...d } ;
44
50
}
45
- this . #rcntr. querySelector ( 'pre' ) . innerHTML = JSON . stringify ( await this . #api. fetchRaw ( fname , param ) , null , 4 ) ;
51
+ this . #rcntr. innerHTML = JSON . stringify ( await this . #api. fetchRaw ( fname , param ) , null , 4 ) ;
46
52
47
53
let paramjson = JSON . stringify ( param ) ;
48
54
let hasparam = Object . entries ( param ) . length === 0 ;
49
55
let paramstr1 = hasparam ? '' : `, ${ paramjson } ` ;
50
56
let paramstr2 = hasparam ? '' : paramjson ;
51
57
let jsonfname = JSON . stringify ( fname ) ;
52
58
53
- this . #ccntr. querySelector ( 'pre' ) . innerText = `<script type="module">
59
+ this . #ccntr. innerText = `<script type="module">
54
60
import {API, APIProxy} from './API.js';
55
61
const url = '../server/public'; /* The API URL eg: '', 'api' */
56
62
const base = window.location.href /* The API base path eg: 'https://api.example.com' */
@@ -86,9 +92,8 @@ const base = window.location.href /* The API base path eg: 'https://api.example.
86
92
}
87
93
}
88
94
</script>` ;
89
- w3CodeColor ( this . #ccntr. querySelector ( 'pre' , 'html' ) ) ;
90
- this . #ccntr. style . display = 'block' ;
91
- this . #rcntr. style . display = 'block' ;
95
+ w3CodeColor ( this . #ccntr, 'html' ) ;
96
+ this . #ocntr. style . display = 'grid' ;
92
97
} ;
93
98
}
94
99
}
@@ -114,36 +119,47 @@ header {
114
119
}
115
120
pre {
116
121
font-family: Consolas,'Courier New', monospace;
122
+ white-space: pre-wrap;
123
+ padding: 0;
124
+ margin: 0;
125
+ margin-top: 0.5em;
117
126
}
118
127
#cntr {
119
128
display: grid;
120
129
gap: 0.5em;
121
- grid-template-areas:
122
- "heading heading"
123
- "func func"
124
- "pcntr pcntr"
125
- "add fetch"
126
- "ccntr rcntr";
130
+ grid-template-columns: 1fr;
131
+ background: #1a237e;
132
+ color: white;
127
133
}
128
134
#para_cntr {
129
135
display: grid;
130
136
gap: 0.5em;
131
137
}
138
+ #out_cntr {
139
+ display: grid;
140
+ grid-template-columns: 1fr 1fr;
141
+ padding: 0.5em 1em;
142
+ gap: 0.5em
143
+ }
144
+ #out_cntr > section {
145
+ box-shadow: 0 0 0.5em black;
146
+ padding: 0.5em 1em;
147
+ }
132
148
</style><div id="cntr">
133
- <header style="grid-area: heading;">API Caller </header>
134
- <input pattern="[_a-zA-Z][_a-zA-Z0-9-.]*" style="grid-area: func;" type="text" id="func" placeholder="Function Name" required />
135
- <section style="grid-area: pcntr;" id="para_cntr">
136
- <header>Parameters:</header>
137
- </section>
138
- <button style="grid-area: add;" id="add_para">Add parameter</button >
139
- <button style="grid-area: fetch;" id="fetch">Fetch Result</button >
140
- <section style="grid-area: ccntr;" id="code_cntr">
141
- <header>Code Example:</header>
142
- <pre></pre>
143
- </section>
144
- <section style="grid-area: rcntr;" id="resp_cntr">
145
- <header>Raw Response:</header>
146
- <pre></pre>
147
- </section>
149
+ <header>API Function: <button id="fetch">Execute 🗘</button> </header>
150
+ <input type="text" id="func" placeholder="Function Name" required />
151
+ <section id="para_cntr">
152
+ <header>Parameters: <button id="add_para"><strong>+</strong></button> </header>
153
+ </section>
154
+ </div >
155
+ <div id="out_cntr" >
156
+ <section id="code_cntr">
157
+ <header>Code Example:</header>
158
+ <pre></pre>
159
+ </section>
160
+ <section id="resp_cntr">
161
+ <header>Raw Response:</header>
162
+ <pre></pre>
163
+ </section>
148
164
</div>` ;
149
165
customElements . define ( APIFetch . __tag , APIFetch ) ;
0 commit comments