tauri/cli/tauri.js/api-src/http.ts
Jacob Bolda 6a21965ff3
chore: add prettier for js formatting (#937)
* chore: add prettier for js/ts formatting

* fix lint-staged to object

* test commit

* format all

* lock file bump

* eslint extends prettier

This will let us skip rules in eslint that prettier can control. Prettier for styles, eslint for code errors.

* add prettier config

* roll back to what we had with eslint settings

* skip mutation observer

* add prettier typescript eslint

* run prettier in lint workflow

* format:check script

* turn off space before function in eslint

it is fighting with prettier

* fix dir in workflow

* remove semis

* add api to eslint

* shift eslint ignore comment after prettier format

* ignore errors that currently exist

* build:typevalidators

* replace was broken on typevalidator build

* try pushing up error

* format

* try removing working dir from eslint workflow

* try node 12

* fix indent in action

* bump eslint

* fix supposeded error and try another

* try breaking eslint

* try building in action

* adjust action paths again

* need dot

* remove build

* fix(tauri.js/eslint): escape glob *

* fix(tauri.js): ignore lint error

* Create prettier-taurijs.md

Co-authored-by: Noah Klayman <noahklayman@gmail.com>
2020-08-18 21:36:46 -05:00

165 lines
2.7 KiB
TypeScript

import { promisified } from './tauri'
export enum ResponseType {
JSON = 1,
Text = 2,
Binary = 3
}
export enum BodyType {
Form = 1,
File = 2,
Auto = 3
}
export type Body = object | string | BinaryType
export type HttpVerb =
| 'GET'
| 'POST'
| 'PUT'
| 'DELETE'
| 'PATCH'
| 'HEAD'
| 'OPTIONS'
| 'CONNECT'
| 'TRACE'
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'>
/**
* 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
*/
async function post<T>(
url: string,
body: Body,
options: PartialOptions
): Promise<T> {
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
*/
async function put<T>(
url: string,
body: Body,
options: PartialOptions
): Promise<T> {
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
*/
async function deleteRequest<T>(
url: string,
options: PartialOptions
): Promise<T> {
return await request({
method: 'DELETE',
url,
...options
})
}
export default {
request,
get,
post,
put,
patch,
delete: deleteRequest,
ResponseType,
BodyType
}