@cerebral/http
Advanced tools
Comparing version 4.2.0-1519234729886 to 4.2.0-1519236826541
@@ -14,62 +14,2 @@ import { Provider } from "function-tree" | ||
export default function HttpProvider(moduleOptions: HttpModuleOptions): HttpProvider; | ||
export interface HttpModuleOptions { | ||
method?: string, | ||
baseUrl?: string, | ||
headers?: { [id: string]: string }, | ||
onRequest?: (xhr: XMLHttpRequest, options: HttpResponseOptions) => void; | ||
onResponse?: (response: XMLHttpRequest, resolve: Function, reject: Function, options?: HttpResponseOptions) => void; | ||
onRequestCallback?: (xhr: XMLHttpRequest) => void; | ||
onResponseCallback?: (response: XMLHttpRequest) => void; | ||
} | ||
export interface HttpRequestOptions { | ||
query?: { [id: string]: any }, | ||
headers?: { [id: string]: string }, | ||
} | ||
export interface HttpResponseOptions extends HttpRequestOptions, HttpModuleOptions { | ||
body: string | ||
} | ||
export interface HttpRequestReponse<T> { | ||
result: T, | ||
status: string | ||
} | ||
export interface HttpProvider { | ||
request: any, | ||
get<T>(url: string, passedQuery?: { [id: string]: any }, options?: HttpRequestOptions): Promise<HttpRequestReponse<T>>, | ||
post<T>(url: string, body: any, options?: HttpRequestOptions): Promise<HttpRequestReponse<T>>, | ||
put<T>(url: string, body: any, options?: HttpRequestOptions): Promise<HttpRequestReponse<T>>, | ||
patch<T>(url: string, body: any, options?: HttpRequestOptions): Promise<HttpRequestReponse<T>>, | ||
delete<T>(url: string, passedQuery: { [id: string]: any }, options?: HttpRequestOptions): Promise<HttpRequestReponse<T>>, | ||
uploadFile<T>(url: string, files: FileList | File, options?: FileUploadOptions): FileUpload<T>, | ||
updateOptions(newOptions: HttpModuleOptions): void; | ||
abort(regexp: string): boolean; | ||
} | ||
export interface FileUploadProgress { | ||
progress: number | ||
} | ||
export interface FileUploadOptions { | ||
name?: string, | ||
data?: { [id: string]: any }, | ||
headers?: { [id: string]: string }, | ||
onProgress?: (progress: FileUploadProgress) => void | string | ||
} | ||
export interface FileUploadResult<T> { | ||
result?: T, | ||
status: string | ||
} | ||
export class FileUpload<TResponse> { | ||
constructor(options: FileUploadOptions); | ||
xhr: XMLHttpRequest; | ||
isAborted: boolean; | ||
send: (files: FileList | File[]) => Promise<FileUploadResult<TResponse>>; | ||
abort: () => void; | ||
} | ||
export default function HttpProvider(options: any): Provider |
@@ -29,2 +29,6 @@ 'use strict'; | ||
if (typeof window !== 'undefined' && window.FormData && options.body instanceof window.FormData) { | ||
delete options.headers['Content-Type']; | ||
} | ||
xhr.withCredentials = Boolean(options.withCredentials); | ||
@@ -36,9 +40,5 @@ | ||
if (options.onRequestCallback) { | ||
options.onRequestCallback(xhr); | ||
} | ||
xhr.send(options.body); | ||
}, | ||
onResponse: function onResponse(xhr, resolve, reject, options) { | ||
onResponse: function onResponse(xhr, resolve, reject) { | ||
var result = xhr.responseText; | ||
@@ -52,6 +52,2 @@ | ||
if (options && options.onRequestCallback) { | ||
options.onResponseCallback(xhr); | ||
} | ||
if (xhr.status >= 200 && xhr.status < 300) { | ||
@@ -58,0 +54,0 @@ resolve({ |
@@ -26,3 +26,3 @@ 'use strict'; | ||
case 'load': | ||
return options.onResponse(event.currentTarget, resolve, reject, options); | ||
return options.onResponse(event.currentTarget, resolve, reject); | ||
case 'progress': | ||
@@ -29,0 +29,0 @@ if (options.onProgress && event.lengthComputable) { |
{ | ||
"name": "@cerebral/http", | ||
"version": "4.2.0-1519234729886", | ||
"version": "4.2.0-1519236826541", | ||
"description": "HTTP provider for Cerebral 2", | ||
@@ -27,8 +27,8 @@ "main": "lib/index.js", | ||
"peerDependencies": { | ||
"cerebral": "^4.2.0-1519234729886" | ||
"cerebral": "^4.2.0-1519236826542" | ||
}, | ||
"devDependencies": { | ||
"cerebral": "^4.2.0-1519234729886", | ||
"cerebral": "^4.2.0-1519236826542", | ||
"xhr-mock": "^1.9.0" | ||
} | ||
} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
70634
608