-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhttp.ts
142 lines (118 loc) · 2.98 KB
/
http.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
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
import {Observable} from 'rxjs'
import {HttpHeaders} from './headers'
import {Queries, query} from './query'
export interface RequestOptions {
url: string
method: 'GET' | 'POST' | 'PUT' | 'DELETE' | 'OPTIONS' | 'HEAD'
body?: any
headers?: Headers | {[key: string]: string}
}
function request<T>(options: RequestOptions) {
return new Observable(subscriber => {
const xhr = new XMLHttpRequest()
xhr.open(options.method, options.url, true)
// Setup request headers
const reqHeaders = new HttpHeaders(options.headers)
if (!reqHeaders.has('Content-Type')) {
reqHeaders.set('Content-Type', 'application/json; charset=utf-8')
}
if (!reqHeaders.has('Accept')) {
reqHeaders.set('Accept', 'application/json, text/plain, */*')
}
reqHeaders.forEach((v, k) => {
xhr.setRequestHeader(k, v)
})
const onLoad = () => {
let ok = xhr.status >= 200 && xhr.status < 300
let body = xhr.response || xhr.responseText
try {
body = JSON.parse(body)
} catch (error) {
ok = false
body = {error, text: body}
}
const headers = new HttpHeaders(xhr.getAllResponseHeaders())
if (ok) {
subscriber.next({
status: xhr.status,
statusText: xhr.statusText,
body: body as T,
url: options.url,
headers
})
subscriber.complete()
} else {
subscriber.error({
status: xhr.status,
statusText: xhr.statusText,
error: body,
url: options.url,
headers
})
}
}
const onError = (error: ProgressEvent) => {
const res = {
error,
status: xhr.status || 0,
statusText: xhr.statusText || 'Unknown Error',
url: options.url || undefined
}
subscriber.error(res)
}
xhr.addEventListener('load', onLoad)
xhr.addEventListener('error', onError)
xhr.send(
options.body && typeof options.body === 'string'
? options.body
: JSON.stringify(options.body)
)
return () => {
xhr.removeEventListener('load', onLoad)
xhr.removeEventListener('error', onError)
if (xhr.readyState !== xhr.DONE) {
xhr.abort()
}
}
})
}
function get<T>(url: string, queries?: Queries, options?: RequestOptions) {
const q = query(queries)
const u = url + q
return request<T>({
...options,
url: u,
method: 'GET'
})
}
function post<T>(url: string, data?: any, options?: RequestOptions) {
return request<T>({
...options,
body: data,
url,
method: 'POST'
})
}
function put<T>(url: string, data?: any, options?: RequestOptions) {
return request<T>({
...options,
body: data,
url,
method: 'PUT'
})
}
function deleteMethod<T>(url: string, data?: any, options?: RequestOptions) {
return request<T>({
...options,
body: data,
url,
method: 'DELETE'
})
}
export const http = {
request,
get,
post,
put,
delete: deleteMethod
}