This repository has been archived by the owner on Mar 12, 2020. It is now read-only.
/
api.ts
200 lines (183 loc) · 6.18 KB
/
api.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
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
import '@ef-carbon/fetch/install'
import URL from 'url-parse'
import { buildAbsoluteURL } from 'url-toolkit'
import { KeyValue, ApiOptions } from '../models'
export const DEFAULT_API_OPTIONS = {
url: 'http://127.0.0.1',
port: 40600,
version: 0
}
/**
* Create 'args' like a CLI command would take
*
* @param {string[]} argsAr An array of arguments
* @private
*/
export const getArgs = (argsAr?: string[]) => {
if (!argsAr || !argsAr.length) {
return ''
}
return argsAr.map((ar) => encodeValue(ar)).join(',')
}
/**
* Create 'options' like a CLI command would take.
*
* @param {Object.<string, string>} opts A map of option keys and values
* @private
*/
export const getOpts = (opts?: KeyValue) => {
if (!opts) {
return ''
}
return Object.keys(opts)
.map((key) => `${key}=${encodeValue(opts[key])}`)
.join(',')
}
const encodeValue = (val: string | number | boolean) => {
return encodeURIComponent(val.toString())
}
export const createHeaders = (args?: string[], opts?: KeyValue, headers?: KeyValue): Record<string, string> => {
const h = headers || {}
return {
...h,
'X-Textile-Args': getArgs(args),
'X-Textile-Opts': getOpts(opts)
}
}
const handleErrors = (response: Response) => {
if (!response.ok) {
throw Error(response.statusText)
}
return response
}
/**
* API is the base class for all SDK modules.
*
* @params {ApiOptions] opts API options object
*/
class API {
private opts: ApiOptions
private baseURL: string
private gatewayURL: string
constructor(opts: ApiOptions = DEFAULT_API_OPTIONS) {
this.opts = opts
const url = new URL(opts.url)
if (opts.port) {
url.set('port', opts.port)
}
url.set('pathname', `/api/v${opts.version || 0}/`)
this.baseURL = url.toString()
const gateway = new URL(this.opts.url)
gateway.set('port', 5050)
gateway.set('pathname', `/ipfs/`)
this.gatewayURL = gateway.toString()
}
/**
* Make a get request to the Textile node
*
* @param url The relative URL of the API endpoint
* @param args An array of arguments to pass as Textile args headers
* @param opts An object of options to pass as Textile options headers
*/
protected async sendGatewayGet(path: string, headers?: KeyValue) {
return fetch(buildAbsoluteURL(this.gatewayURL, path), {
method: 'GET',
headers: createHeaders([], {}, headers)
})
}
/**
* Make a post request to the Textile node
*
* @param url The relative URL of the API endpoint
* @param args An array of arguments to pass as Textile args headers
* @param opts An object of options to pass as Textile options headers
* @param data An object of data to post
*/
protected async sendPost(url: string, args?: string[], opts?: KeyValue, data?: any, headers?: KeyValue, raw?: boolean) {
const h = createHeaders(args, opts, headers)
const response = await fetch(buildAbsoluteURL(this.baseURL, url), {
method: 'POST',
headers: new Headers(h),
body: raw ? data : JSON.stringify(data)
})
return handleErrors(response)
}
/**
* Make a get request to the Textile node
*
* @param url The relative URL of the API endpoint
* @param args An array of arguments to pass as Textile args headers
* @param opts An object of options to pass as Textile options headers
*/
protected async sendGet(url: string, args?: string[], opts?: KeyValue, headers?: KeyValue) {
const response = await fetch(buildAbsoluteURL(this.baseURL, url), {
method: 'GET',
headers: createHeaders(args, opts, headers)
})
return handleErrors(response)
}
/**
* Make a delete request to the Textile node
*
* @param url The relative URL of the API endpoint
* @param args An array of arguments to pass as Textile args headers
* @param opts An object of options to pass as Textile options headers
*/
protected async sendDelete(url: string, args?: string[], opts?: KeyValue, headers?: KeyValue) {
const response = await fetch(buildAbsoluteURL(this.baseURL, url), {
method: 'DELETE',
headers: createHeaders(args, opts, headers)
})
return handleErrors(response)
}
/**
* Make a put request to the Textile node
*
* @param url The relative URL of the API endpoint
* @param args An array of arguments to pass as Textile args headers
* @param opts An object of options to pass as Textile options headers
* @param data An object of data to put
*/
protected async sendPut(url: string, args?: string[], opts?: KeyValue, data?: any, headers?: KeyValue) {
const response = await fetch(buildAbsoluteURL(this.baseURL, url), {
method: 'PUT',
headers: createHeaders(args, opts, headers),
body: JSON.stringify(data)
})
return handleErrors(response)
}
/**
* Make a patch request to the Textile node
*
* @param url The relative URL of the API endpoint
* @param args An array of arguments to pass as Textile args headers
* @param opts An object of options to pass as Textile options headers
* @param data An object of data to put
*/
protected async sendPatch(url: string, args?: string[], opts?: KeyValue, data?: any, headers?: KeyValue) {
const response = await fetch(buildAbsoluteURL(this.baseURL, url), {
method: 'patch',
headers: createHeaders(args, opts, headers),
body: JSON.stringify(data)
})
return handleErrors(response)
}
/**
* Make an EventSource request to the Textile node
*
* @param url The relative URL of the API endpoint
* @param args An array of arguments to pass as query in native EventSource or Textile args headers in EventSourcePolyfill
* @param opts An object of options to pass as Textile options headers
*/
protected sendEventSource(url: string, args?: string[], opts?: KeyValue, headers?: KeyValue) {
// native EventSource can't set header, but can CORS
return new EventSource(buildAbsoluteURL(this.baseURL, `${url}${opts ? `?${URL.qs.stringify(opts)}` : ''}`))
// EventSourcePolyfill of eventsource@1.0.7 can set header, but can't CORS
// return new EventSourcePolyfill(buildAbsoluteURL(this.baseURL, url), {
// headers: {
// 'X-Textile-Opts': getOpts(opts),
// }
// });
}
}
export { API }