-
-
Notifications
You must be signed in to change notification settings - Fork 81
/
http.cljs
164 lines (148 loc) · 4.99 KB
/
http.cljs
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
(ns portal.ui.viewer.http
(:require [clojure.spec.alpha :as s]
[clojure.string :as str]
[portal.colors :as c]
[portal.ui.inspector :as ins]
[portal.ui.select :as select]
[portal.ui.styled :as d]
[portal.ui.theme :as theme]))
;;; :spec
(s/def ::uri string?)
(defn valid-status? [value] (<= 100 value 599))
(s/def ::status (s/and int? valid-status?))
(s/def ::name (s/or :string string? :keyword keyword?))
(s/def ::header (s/or :string string? :strings (s/coll-of string?)))
(s/def ::headers (s/map-of ::name ::header))
(s/def ::query-params (s/map-of ::name ::name))
(s/def ::doc string?)
(s/def ::request-method #{:get :head :post :put :patch :delete :options})
(s/def ::request
(s/keys :req-un [::request-method
::uri]
:opt-un [::headers
::query-params
::body
::doc]))
(s/def ::request
(s/keys :req-un [::request-method
::uri]
:opt-un [::headers
::query-params
::body
::doc]))
(s/def ::response
(s/keys :req-un [::status]
:opt-un [::headers
::body
::doc]))
;;;
(def ^:private method->color
{:get ::c/boolean
:head ::c/number
:put ::c/uri
:post ::c/string
:patch ::c/tag
:delete ::c/exception
:options ::c/package})
(defn inspect-http-request [value]
(let [theme (theme/use-theme)
opts (ins/use-options)
background (ins/get-background)
color (-> value :request-method method->color theme)
expanded? (:expanded? opts)]
[d/div
[d/div
{:style
{:display :flex
:align-items :stretch
:background background}}
[d/div
{:style
{:cursor :pointer
:color background
:padding [(:padding theme) (* 2.5 (:padding theme))]
:background color
:border [1 :solid color]
:border-top-left-radius (:border-radius theme)
:border-bottom-left-radius
(when-not expanded? (:border-radius theme))}}
(str/upper-case (name (:request-method value)))]
[d/div
{:style
{:flex "1"
:padding [(:padding theme) (* 2 (:padding theme))]
:border [1 :solid (::c/border theme)]
:border-bottom-style (when expanded? :none)
:border-top-right-radius (:border-radius theme)
:border-bottom-right-radius
(when-not expanded? (:border-radius theme))}}
[select/with-position
{:row -1 :column 0}
[ins/with-key
:uri
[ins/inspector (:uri value)]]]]]
(when (:expanded? opts)
[ins/inspect-map-k-v (dissoc value :uri :request-method)])]))
(defn- status->color [status]
(cond
(<= 100 status 199) ::c/boolean
(<= 200 status 299) ::c/string
(<= 300 status 399) ::c/tag
(<= 400 status 499) ::c/uri
(<= 500 status 599) ::c/exception))
(defn inspect-http-response [value]
(let [theme (theme/use-theme)
opts (ins/use-options)
expanded? (:expanded? opts)
background (ins/get-background)
content-type (or (get-in value [:headers "Content-Type"])
(get-in value [:headers :content-type]))
color (-> value :status status->color theme)]
[d/div
[d/div
{:style
{:display :flex
:background background}}
[d/div
{:style
{:cursor :pointer
:background color
:padding [(:padding theme) (* 2.5 (:padding theme))]
:color background
:border [1 :solid color]
:border-top-left-radius (:border-radius theme)
:border-bottom-left-radius
(when-not expanded? (:border-radius theme))}}
(:status value)]
[d/div
{:style
{:flex "1"
:padding (:padding theme)
:border [1 :solid (::c/border theme)]
:border-bottom-style (when expanded? :none)
:border-top-right-radius (:border-radius theme)
:border-bottom-right-radius
(when-not expanded? (:border-radius theme))}}
[select/with-position
{:row -1 :column 0}
[ins/with-key
:headers
[ins/with-key
"Content-Type"
[ins/inspector content-type]]]]]]
(when (:expanded? opts)
[ins/inspect-map-k-v (dissoc value :status)])]))
(defn get-component [value]
(cond
(s/valid? ::request value)
inspect-http-request
(s/valid? ::response value)
inspect-http-response))
(defn inspect-http [value]
(let [component (get-component value)]
[component value]))
(def viewer
{:predicate get-component
:component inspect-http
:name :portal.viewer/http
:doc "Highlight HTTP method and status code for http request and response."})