1
1
import gleam/bit_array
2
- import gleam/bool
3
2
import gleam/bytes_builder
4
3
import gleam/dict . { type Dict }
5
4
import gleam/dynamic . { type Dynamic }
6
5
import gleam/http
7
- import gleam/http/request . { type Request , Request }
8
- import gleam/http/response . { type Response }
9
- import gleam/io
6
+ import gleam/http/request . { Request as HttpRequest }
7
+ import gleam/http/response
10
8
import gleam/javascript/promise . { type Promise }
11
9
import gleam/list
12
10
import gleam/option . { type Option , None , Some }
13
11
import gleam/regex
14
12
import gleam/result
15
13
import gleam/string
16
14
import gleam/string_builder
17
- import wisp
15
+ import wisp . { type Request , type Response }
18
16
import wisp/internal
19
17
20
18
pub type JsEvent
@@ -183,70 +181,28 @@ pub type ApiGatewayProxyResultV2 {
183
181
184
182
// --- Adapters ---------------------------------------------------------------
185
183
186
- pub fn to_handler (
187
- handler : fn ( event, Context ) -> Promise ( result) ,
188
- to_event : fn ( JsEvent ) -> event,
189
- from_result : fn ( result) -> JsResult ,
190
- ) -> fn ( JsEvent , JsContext ) -> Promise ( JsResult ) {
191
- fn ( event : JsEvent , ctx : JsContext ) -> Promise ( JsResult ) {
192
- let event = to_event ( event )
193
- let ctx = to_context ( ctx )
194
- handler ( event , ctx )
195
- |> promise . map ( from_result )
196
- }
197
- }
198
-
199
- pub fn http_handler (
200
- handler : fn ( Request ( BitArray ) , Context ) -> Promise ( Response ( BitArray ) ) ,
201
- ) -> JsHandler {
202
- api_gateway_proxy_v2_handler ( fn ( event , ctx ) {
203
- event
204
- |> create_request
205
- |> handler ( ctx )
206
- |> promise . map ( create_response )
207
- } )
208
- }
209
-
210
184
pub fn wisp_handler (
211
- handler : fn ( wisp . Request , Context ) -> Promise ( wisp . Response ) ,
185
+ handler : fn ( Request , Context ) -> Promise ( Response ) ,
212
186
) -> JsHandler {
213
187
api_gateway_proxy_v2_handler ( fn ( event , ctx ) {
214
188
event
215
- |> create_wisp_request
189
+ |> create_request
216
190
|> handler ( ctx )
217
- |> promise . map ( from_wisp_response )
191
+ |> promise . map ( from_response )
218
192
} )
219
193
}
220
194
221
- fn create_wisp_request ( event : ApiGatewayProxyEventV2 ) -> wisp . Request {
195
+ fn create_request ( event : ApiGatewayProxyEventV2 ) -> Request {
222
196
let read = case event . body {
223
197
Some ( body ) ->
224
198
internal . Chunk ( bit_array . from_string ( body ) , fn ( _size ) {
225
199
Ok ( internal . ReadingFinished )
226
200
} )
227
201
None -> internal . ReadingFinished
228
202
}
229
- create_request ( event )
230
- |> request . set_body ( internal . make_connection (
231
- fn ( _size ) { Ok ( read ) } ,
232
- wisp . random_string ( 64 ) ,
233
- ) )
234
- }
235
203
236
- fn from_wisp_response ( response : wisp . Response ) -> ApiGatewayProxyResultV2 {
237
- let body = case response . body {
238
- wisp . Empty -> << >>
239
- wisp . Bytes ( builder ) -> bytes_builder . to_bit_array ( builder )
240
- wisp . Text ( builder ) ->
241
- string_builder . to_string ( builder ) |> bit_array . from_string
242
- wisp . File ( _path ) -> panic as "not implemented"
243
- }
244
- response . set_body ( response , body )
245
- |> create_response
246
- }
247
-
248
- fn create_request ( event : ApiGatewayProxyEventV2 ) -> Request ( BitArray ) {
249
- io . debug ( event . headers )
204
+ let body =
205
+ internal . make_connection ( fn ( _size ) { Ok ( read ) } , wisp . random_string ( 64 ) )
250
206
let method =
251
207
event . request_context . http . method
252
208
|> http . parse_method
@@ -259,15 +215,11 @@ fn create_request(event: ApiGatewayProxyEventV2) -> Request(BitArray) {
259
215
}
260
216
_ -> dict . to_list ( event . headers )
261
217
}
262
- let body =
263
- event . body
264
- |> body_bit_array ( event . is_base64_encoded )
265
- |> result . unwrap ( << >> )
266
218
let host = event . request_context . domain_name
267
219
let path = event . raw_path
268
220
let query = string . to_option ( event . raw_query_string )
269
221
270
- Request (
222
+ HttpRequest (
271
223
method : ,
272
224
headers : ,
273
225
body : ,
@@ -279,17 +231,15 @@ fn create_request(event: ApiGatewayProxyEventV2) -> Request(BitArray) {
279
231
)
280
232
}
281
233
282
- fn body_bit_array (
283
- body : Option ( String ) ,
284
- is_base64_encoded : Bool ,
285
- ) -> Result ( BitArray , Nil ) {
286
- use body <- result . try ( option . to_result ( body , Nil ) )
287
- use <- bool . guard ( ! is_base64_encoded , Ok ( bit_array . from_string ( body ) ) )
288
- use body <- result . try ( bit_array . base64_decode ( body ) )
289
- Ok ( body )
290
- }
234
+ fn from_response ( response : Response ) -> ApiGatewayProxyResultV2 {
235
+ let body = case response . body {
236
+ wisp . Empty -> << >>
237
+ wisp . Bytes ( builder ) -> bytes_builder . to_bit_array ( builder )
238
+ wisp . Text ( builder ) ->
239
+ string_builder . to_string ( builder ) |> bit_array . from_string
240
+ wisp . File ( _path ) -> panic as "file body not supported yet"
241
+ }
291
242
292
- fn create_response ( response : Response ( BitArray ) ) -> ApiGatewayProxyResultV2 {
293
243
let is_content_type_binary =
294
244
response . get_header ( response , "content-type" )
295
245
|> result . map ( is_content_type_binary )
@@ -304,8 +254,8 @@ fn create_response(response: Response(BitArray)) -> ApiGatewayProxyResultV2 {
304
254
}
305
255
306
256
let body = case is_base64_encoded {
307
- True -> bit_array . base64_encode ( response . body , False )
308
- False -> bit_array . to_string ( response . body ) |> result . unwrap ( "" )
257
+ True -> bit_array . base64_encode ( body , False )
258
+ False -> bit_array . to_string ( body ) |> result . unwrap ( "" )
309
259
}
310
260
let cookies = get_cookies ( response )
311
261
@@ -318,7 +268,7 @@ fn create_response(response: Response(BitArray)) -> ApiGatewayProxyResultV2 {
318
268
)
319
269
}
320
270
321
- fn get_cookies ( response : Response ( a ) ) -> List ( String ) {
271
+ fn get_cookies ( response : Response ) -> List ( String ) {
322
272
response . get_cookies ( response )
323
273
|> list . map ( fn ( cookie ) { cookie . 0 <> "=" <> cookie . 1 } )
324
274
}
0 commit comments