/
form.cr
168 lines (155 loc) Β· 4.72 KB
/
form.cr
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
165
166
167
168
require "http-params-serializable"
require "../../error"
module Onyx::REST::Endpoint
# Define form params which would be deserialzed from the request body only if
# its "Content-Type" header is "application/x-www-form-urlencoded". The serialization is powered by [`HTTP::Params::Serializable`](https://github.com/vladfaust/http-params-serializable).
#
# ## Options
#
# * `required` -- if set to `true`, will attempt to parse form params regardless
# of the `"Content-Type"` header and return a parameter error otherwise; the `params.form`
# getter becomes non-nilable
#
# ## Example
#
# ```
# struct UpdateUser
# include Onyx::REST::Action
#
# params do
# path do
# type id : Int32
# end
#
# form do
# type user do
# type email : String?
# type username : String?
# end
# end
# end
#
# def call
# if form = params.form
# pp! form.user.email
# pp! form.user.username
# end
# end
# end
# ```
#
# ```shell
# > curl -X POST -H "Content-Type: application/x-www-form-urlencoded" -d "user[email]=foo@example.com" http://localhost:5000/users/42
# form.user.email => "foo@example.com"
# form.user.username => nil
# ```
#
# If your endpoint expects form params only, then it can be simplified a bit:
#
# ```
# struct UpdateUser
# include Onyx::REST::Action
#
# params do
# path do
# type id : Int32
# end
#
# form required: true do
# type user do
# type email : String?
# type username : String?
# end
# end
# end
#
# def call
# pp! params.form.user.email
# pp! params.form.user.username
# end
# end
# ```
#
# ```shell
# > curl -X POST -d "user[email]=foo@example.com" http://localhost:5000/users/42
# ```
macro form(required = false, &block)
class FormError < Onyx::REST::Error(PARAMS_ERROR_CODE)
def initialize(message : String, @path : Array(String))
super(message)
end
def payload
{path: @path}
end
end
struct Form
include ::HTTP::Params::Serializable
{% verbatim do %}
macro type(argument, nilable = false, **options, &block)
{% if block %}
{% unless options.empty? %}
@[::HTTP::Param({{**options}})]
{% end %}
{% if argument.is_a?(Path) %}
{% raise "Cannot declare namespaced nested query parameter" if argument.names.size > 1 %}
getter {{argument.names.first.underscore}} : {{argument.names.first.camelcase.id}}{{" | Nil".id if nilable}}
{% elsif argument.is_a?(Call) %}
getter {{argument.name.underscore}} : {{argument.name.camelcase.id}}{{" | Nil".id if nilable}}
{% else %}
{% raise "BUG: Unhandled argument type #{argument.class_name}" %}
{% end %}
{% if argument.is_a?(Path) %}
struct {{argument.names.first.camelcase.id}}
{% elsif argument.is_a?(Call) %}
struct {{argument.name.camelcase.id}}
{% end %}
include ::HTTP::Params::Serializable
{% if block.body.is_a?(Expressions) %}
{% for expression in block.body.expressions %}
Form.{{expression}}
{% end %}
{% elsif block.body.is_a?(Call) %}
Form.{{yield.id}}
{% else %}
{% raise "BUG: Unhandled block body type #{block.body.class_name}" %}
{% end %}
end
{% elsif argument.is_a?(TypeDeclaration) %}
{% unless options.empty? %}
@[::HTTP::Param({{**options}})]
{% end %}
getter {{argument}}
{% else %}
{% raise "BUG: Unhandled argument type #{argument.class_name}" %}
{% end %}
end
{% end %}
{{yield.id}}
end
{% if required %}
getter! form : Form
{% else %}
getter form : Form?
{% end %}
def initialize(request : ::HTTP::Request)
previous_def
{% begin %}
begin
{% unless required %}
if request.headers["Content-Type"]?.try &.=~ /^application\/x-www-form-urlencoded/
{% end %}
if body = request.body
@form = Form.from_query(body.gets_to_end)
else
raise FormError.new("Missing request body", [] of String)
end
{% unless required %}
end
{% end %}
rescue ex : ::HTTP::Params::Serializable::Error
raise FormError.new("Form p" + ex.message.not_nil![1..-1], ex.path)
end
{% end %}
end
end
end