http-ng
Version:
a standalone angular.js $http service
228 lines (196 loc) • 8.11 kB
TypeScript
export default IHttp;
export const defaults: IHttpDefaults;
export const interceptors: IHttpInterceptor[];
export function httpParamSerializer(params: any): string;
export function httpParamSerializerJQLike(params: any): string;
interface IHttpHeadersGetter {
(): { [name: string]: string; };
(headerName: string): string;
}
// See the jsdoc for transformData() at https://github.com/angular/angular.js/blob/master/src/ng/http.js#L228
export interface IHttpRequestTransformer {
(data: any, headersGetter: IHttpHeadersGetter): any;
}
// The definition of fields are the same as IHttpPromiseCallbackArg
export interface IHttpResponseTransformer {
(data: any, headersGetter: IHttpHeadersGetter, status: number): any;
}
interface IHttpRequestConfigHeaders {
[requestType: string]: any;
common?: any;
get?: any;
post?: any;
put?: any;
patch?: any;
}
interface IHttpPromiseCallbackArg<T> {
data?: T;
status?: number;
headers?: IHttpHeadersGetter;
config?: IRequestConfig;
statusText?: string;
}
export interface IHttpInterceptor {
request?: (config: IRequestConfig) => IRequestConfig | PromiseLike<IRequestConfig>;
requestError?: (rejection: any) => any;
response?: <T>(response: IHttpPromiseCallbackArg<T>) => PromiseLike<IHttpPromiseCallbackArg<T>>|IHttpPromiseCallbackArg<T>;
responseError?: (rejection: any) => any;
}
interface IHttpDefaults {
/**
* {boolean|Cache}
* If true, a default $http cache will be used to cache the GET request, otherwise if a cache instance built with $cacheFactory, this cache will be used for caching.
*/
cache?: any;
/**
* Transform function or an array of such functions. The transform function takes the http request body and
* headers and returns its transformed (typically serialized) version.
* @see {@link https://docs.angularjs.org/api/ng/service/$http#transforming-requests-and-responses}
*/
transformRequest?: IHttpRequestTransformer |IHttpRequestTransformer[];
/**
* Transform function or an array of such functions. The transform function takes the http response body and
* headers and returns its transformed (typically deserialized) version.
*/
transformResponse?: IHttpResponseTransformer | IHttpResponseTransformer[];
/**
* Map of strings or functions which return strings representing HTTP headers to send to the server. If the
* return value of a function is null, the header will not be sent.
* The key of the map is the request verb in lower case. The "common" key applies to all requests.
* @see {@link https://docs.angularjs.org/api/ng/service/$http#setting-http-headers}
*/
headers?: IHttpRequestConfigHeaders;
/** Name of HTTP header to populate with the XSRF token. */
xsrfHeaderName?: string;
/** Name of cookie containing the XSRF token. */
xsrfCookieName?: string;
/**
* whether to to set the withCredentials flag on the XHR object. See [requests with credentials]https://developer.mozilla.org/en/http_access_control#section_5 for more information.
*/
withCredentials?: boolean;
/**
* A function used to the prepare string representation of request parameters (specified as an object). If
* specified as string, it is interpreted as a function registered with the $injector. Defaults to
* $httpParamSerializer.
*/
paramSerializer?: (obj: any) => string;
}
interface IRequestShortcutConfig {
/**
* {Object.<string|Object>}
* Map of strings or objects which will be turned to ?key1=value1&key2=value2 after the url. If the value is not a string, it will be JSONified.
*/
params?: any;
/**
* {string|Object}
* Data to be sent as the request message data.
*/
data?: any;
/**
* Timeout in milliseconds, or promise that should abort the request when resolved.
*/
timeout?: number | Promise<any>;
/**
* See [XMLHttpRequest.responseType]https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest#xmlhttprequest-responsetype
*/
responseType?: string;
/**
* cache Object
*/
cache?: any;
/**
* user defined config property
*/
[key: string]: any;
}
/**
* Object describing the request to be made and how it should be processed.
* see http://docs.angularjs.org/api/ng/service/$http#usage
*/
interface IRequestConfig extends IRequestShortcutConfig {
/**
* HTTP method (e.g. 'GET', 'POST', etc)
*/
method: string;
/**
* Absolute or relative URL of the resource that is being requested.
*/
url: string;
/**
* Event listeners to be bound to the XMLHttpRequest object.
* To bind events to the XMLHttpRequest upload object, use uploadEventHandlers. The handler will be called in the context of a $apply block.
*/
eventHandlers?: { [type: string]: EventListenerOrEventListenerObject };
/**
* Event listeners to be bound to the XMLHttpRequest upload object.
* To bind events to the XMLHttpRequest object, use eventHandlers. The handler will be called in the context of a $apply block.
*/
uploadEventHandlers?: { [type: string]: EventListenerOrEventListenerObject };
}
/**
* Object describing the request to be made and how it should be processed.
*/
declare function IHttp<T>(config: IRequestConfig): PromiseLike<IHttpPromiseCallbackArg<T>>;
declare namespace IHttp {
/**
* Shortcut method to perform GET request.
*
* @param url Relative or absolute URL specifying the destination of the request
* @param config Optional configuration object
*/
export function get<T>(url: string, config?: IRequestShortcutConfig): PromiseLike<IHttpPromiseCallbackArg<T>>;
/**
* Shortcut method to perform DELETE request.
*
* @param url Relative or absolute URL specifying the destination of the request
* @param config Optional configuration object
*/
export function del<T>(url: string, config?: IRequestShortcutConfig): PromiseLike<IHttpPromiseCallbackArg<T>>;
/**
* Shortcut method to perform HEAD request.
*
* @param url Relative or absolute URL specifying the destination of the request
* @param config Optional configuration object
*/
export function head<T>(url: string, config?: IRequestShortcutConfig): PromiseLike<IHttpPromiseCallbackArg<T>>;
/**
* Shortcut method to perform JSONP request.
*
* @param url Relative or absolute URL specifying the destination of the request
* @param config Optional configuration object
*/
export function jsonp<T>(url: string, config?: IRequestShortcutConfig): PromiseLike<IHttpPromiseCallbackArg<T>>;
/**
* Shortcut method to perform POST request.
*
* @param url Relative or absolute URL specifying the destination of the request
* @param data Request content
* @param config Optional configuration object
*/
export function post<T>(url: string, data: any, config?: IRequestShortcutConfig): PromiseLike<IHttpPromiseCallbackArg<T>>;
/**
* Shortcut method to perform PUT request.
*
* @param url Relative or absolute URL specifying the destination of the request
* @param data Request content
* @param config Optional configuration object
*/
export function put<T>(url: string, data: any, config?: IRequestShortcutConfig): PromiseLike<IHttpPromiseCallbackArg<T>>;
/**
* Shortcut method to perform PATCH request.
*
* @param url Relative or absolute URL specifying the destination of the request
* @param data Request content
* @param config Optional configuration object
*/
export function patch<T>(url: string, data: any, config?: IRequestShortcutConfig): PromiseLike<IHttpPromiseCallbackArg<T>>;
/**
* Runtime equivalent of the $httpProvider.defaults property. Allows configuration of default headers, withCredentials as well as request and response transformations.
*/
export const defaults: IHttpDefaults;
export const interceptors: IHttpInterceptor[];
/**
* Array of config objects for currently pending requests. This is primarily meant to be used for debugging purposes.
*/
export const pendingRequests: IRequestConfig[];
}