2020-06-27 18:20:00 +03:00
|
|
|
import { promisified } from './tauri'
|
2020-07-13 01:34:44 +03:00
|
|
|
|
|
|
|
export enum ResponseType {
|
|
|
|
JSON = 1,
|
|
|
|
Text = 2,
|
|
|
|
Binary = 3
|
|
|
|
}
|
|
|
|
|
|
|
|
export enum BodyType {
|
|
|
|
Form = 1,
|
|
|
|
File = 2,
|
|
|
|
Auto = 3
|
|
|
|
}
|
|
|
|
|
|
|
|
export type Body = object | string | BinaryType
|
|
|
|
|
2020-08-19 05:36:46 +03:00
|
|
|
export type HttpVerb =
|
|
|
|
| 'GET'
|
|
|
|
| 'POST'
|
|
|
|
| 'PUT'
|
|
|
|
| 'DELETE'
|
|
|
|
| 'PATCH'
|
|
|
|
| 'HEAD'
|
|
|
|
| 'OPTIONS'
|
|
|
|
| 'CONNECT'
|
|
|
|
| 'TRACE'
|
2020-07-13 01:34:44 +03:00
|
|
|
|
|
|
|
export interface HttpOptions {
|
|
|
|
method: HttpVerb
|
|
|
|
url: string
|
|
|
|
headers?: Record<string, any>
|
|
|
|
params?: Record<string, any>
|
|
|
|
body?: Body
|
|
|
|
followRedirects: boolean
|
|
|
|
maxRedirections: boolean
|
|
|
|
connectTimeout: number
|
|
|
|
readTimeout: number
|
|
|
|
timeout: number
|
|
|
|
allowCompression: boolean
|
|
|
|
responseType?: ResponseType
|
|
|
|
bodyType: BodyType
|
|
|
|
}
|
|
|
|
|
|
|
|
export type PartialOptions = Omit<HttpOptions, 'method' | 'url'>
|
2020-06-27 18:20:00 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* makes a HTTP request
|
|
|
|
*
|
|
|
|
* @param options request options
|
|
|
|
*
|
|
|
|
* @return promise resolving to the response
|
|
|
|
*/
|
|
|
|
async function request<T>(options: HttpOptions): Promise<T> {
|
|
|
|
return await promisified({
|
|
|
|
cmd: 'httpRequest',
|
|
|
|
options: options
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* makes a GET request
|
|
|
|
*
|
|
|
|
* @param url request URL
|
|
|
|
* @param options request options
|
|
|
|
*
|
|
|
|
* @return promise resolving to the response
|
|
|
|
*/
|
|
|
|
async function get<T>(url: string, options: PartialOptions): Promise<T> {
|
|
|
|
return await request({
|
|
|
|
method: 'GET',
|
|
|
|
url,
|
|
|
|
...options
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* makes a POST request
|
|
|
|
*
|
|
|
|
* @param url request URL
|
|
|
|
* @param body request body
|
|
|
|
* @param options request options
|
|
|
|
*
|
|
|
|
* @return promise resolving to the response
|
|
|
|
*/
|
2020-08-19 05:36:46 +03:00
|
|
|
async function post<T>(
|
|
|
|
url: string,
|
|
|
|
body: Body,
|
|
|
|
options: PartialOptions
|
|
|
|
): Promise<T> {
|
2020-06-27 18:20:00 +03:00
|
|
|
return await request({
|
|
|
|
method: 'POST',
|
|
|
|
url,
|
|
|
|
body,
|
|
|
|
...options
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* makes a PUT request
|
|
|
|
*
|
|
|
|
* @param url request URL
|
|
|
|
* @param body request body
|
|
|
|
* @param options request options
|
|
|
|
*
|
|
|
|
* @return promise resolving to the response
|
|
|
|
*/
|
2020-08-19 05:36:46 +03:00
|
|
|
async function put<T>(
|
|
|
|
url: string,
|
|
|
|
body: Body,
|
|
|
|
options: PartialOptions
|
|
|
|
): Promise<T> {
|
2020-06-27 18:20:00 +03:00
|
|
|
return await request({
|
|
|
|
method: 'PUT',
|
|
|
|
url,
|
|
|
|
body,
|
|
|
|
...options
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* makes a PATCH request
|
|
|
|
*
|
|
|
|
* @param url request URL
|
|
|
|
* @param options request options
|
|
|
|
*
|
|
|
|
* @return promise resolving to the response
|
|
|
|
*/
|
|
|
|
async function patch<T>(url: string, options: PartialOptions): Promise<T> {
|
|
|
|
return await request({
|
|
|
|
method: 'PATCH',
|
|
|
|
url,
|
|
|
|
...options
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* makes a DELETE request
|
|
|
|
*
|
|
|
|
* @param url request URL
|
|
|
|
* @param options request options
|
|
|
|
*
|
|
|
|
* @return promise resolving to the response
|
|
|
|
*/
|
2020-08-19 05:36:46 +03:00
|
|
|
async function deleteRequest<T>(
|
|
|
|
url: string,
|
|
|
|
options: PartialOptions
|
|
|
|
): Promise<T> {
|
2020-06-27 18:20:00 +03:00
|
|
|
return await request({
|
|
|
|
method: 'DELETE',
|
|
|
|
url,
|
|
|
|
...options
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
export default {
|
|
|
|
request,
|
|
|
|
get,
|
|
|
|
post,
|
|
|
|
put,
|
|
|
|
patch,
|
|
|
|
delete: deleteRequest,
|
|
|
|
ResponseType,
|
|
|
|
BodyType
|
|
|
|
}
|