-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
http.view.ts
87 lines (72 loc) · 1.98 KB
/
http.view.ts
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
namespace $.$$ {
export class $hyoo_http extends $.$hyoo_http {
uri( next? : string ) {
return this.$.$mol_state_arg.value( 'uri' , next ) || super.uri()
}
method( next? : string ) {
return this.$.$mol_state_arg.value( 'method' , next ) || super.method()
}
request_headers( next? : string ) : string{
return this.$.$mol_state_arg.value( 'headers' , next ) || ''
}
request_headers_dict() {
const dict = {} as Record< string , string >
const lines = this.request_headers().split( /[\r\n]/g ).filter( line => line.trim() )
for( const line of lines ) {
const pair = line.split( ': ' )
dict[ pair[0] ] = pair[1]
}
return dict
}
request_body( next? : string ) {
return this.$.$mol_state_arg.value( 'body' , next ) || ''
}
@ $mol_mem
request_params( next = {
uri: $mol_wire_sync( ()=> this.uri() )(),
method: $mol_wire_sync( ()=> this.method() )(),
headers: $mol_wire_sync( ()=> this.request_headers_dict() )(),
body: $mol_wire_sync( ()=> this.request_body() || undefined )(),
} ) {
return next
}
submit() {
this.request_params({
uri: this.uri(),
method: this.method() ,
headers: this.request_headers_dict() ,
body: this.request_body() || undefined ,
} )
}
@ $mol_mem
response() {
this.$.$mol_wait_timeout( 1000 )
const { uri, ... params } = this.request_params()
return this.$.$mol_fetch.response( uri, params )
}
response_output() {
try {
this.response()
return super.response_output()
} catch( error: any ) {
if( error instanceof Promise ) $mol_fail_hidden( error )
return [ this.Response_error( error ) ]
}
}
response_error( error: Error ) {
return error.message ?? error
}
@ $mol_mem
response_headers() {
let lines = [] as string[]
for( const [ name , value ] of this.response().headers() ) {
lines.push( `${name}: ${value}` )
}
return lines.join('\n')
}
@ $mol_mem
response_body() {
return this.response().text()
}
}
}