yuri-js
v0.1.3
Published
Yuri - Ajax library
Downloads
2
Readme
Yuri
Ajax Library
Installation
npm install yuri-js
Usage
Basic usage
import { Yuri } from 'yuri-js';
let yuri = new Yuri();
let getResult = await yuri.get('/get-endpoint', {paramName: 'param value'}, possibleCallback);
let postResult = await yuri.post('/post-endpoint', {dataName: 'data value'}, {paramName: 'param value'}, possibleCallback);
Advanced usage
Create instance of Yuri
with base config.
import { Yuri, YuriConfig } from 'yuri-js';
let baseConfig = new YuriConfig();
baseConfig.timeout = 5000;
baseConfig.url = 'https://exmaple.com/api'; // Base url, added to all request that not starts with http or //
baseConfig.headers['X-Requested-With'] = 'XMLHttpRequest';
let yuri = new Yuri(baseConfig);
Create pre and post execute helpers
import { AbstractYuriHelper } from 'yuri-js';
class ExampleYuriHelper extends AbstractYuriHelper
{
execute(config)
{
// some actions before request
// you have access to XMLHttpRequest
this.xhr();
let result = parent.execute(config);
// some actions after send request
return result;
}
}
Add it to yuri
yuri.registerHelper(new ExampleYuriHelper());
Create connector. You can pass another config to connector when create it
let connector = yuri.connector();
Setup request.
You have following methods:
- set body:
setRawBody(any)
- set exact body that you want to send to server;setRawBodyCallback(function)
- set function that will return body to send to serversetRawBodyPromise(Promise)
- set promise that resolve body to send to serversetJsonBody(object)
- parse provided object to JSON bodysetFormBody(object)
- parse provided object to form url encoded data
setUrl(string)
- set endpoint. If not starts withhttp
or//
try merge with base config url;setUrlParams(object)
- receive object of url paramssetUrlParam(string name, string value)
- receive name and value, set specific url paramaddUrlParams(object)
- receive object, merge it with current url paramssetHeaders(object)
- receive headers as objectsetHeader(string name, string value)
- receive name and value, set specific headeraddHeaders(object)
- receive object, merge it with current headerssetMethod(HttpMethod)
- set request method - prefer use enumHttpsMethod
setTimeout(number)
- set request timout in millis. default 0, request has no timeout
Execute request
execute()
- returnYuriRequest
request(function)
- return promise that resolveYuriResult
. Callback (if set) receiveYuriResult
paramrequestRawBody(function)
- return promise that resolve received raw body on success and rejectYuriResult
on fail. Callback receive body as first param and error as second one.requestJsonBody(function)
- return promise that resolve received parsed JSON body on success and rejectYuriResult
on fail. If parse JSON failed, resolve null. Callback receive body as first param and error as second one.requestCode(function)
- return promise that resolve code on success or server error, and rejectYuriResult
on connection error. Callback receive code as param on success or server error, and null andYuriResult
on connection error.
Objects
YuriResult
class YuriResult
{
constructor(request: YuriRequest, response: YuriResponse, error: ServerError | ConnectionError);
get request(): YuriRequest;
get error(): ServerError | ConnectionError;
get response(): YuriResponse;
getBody(): null | any;
getRawBody(): null | any;
isFailed(): boolean;
isServerError(): boolean;
isConnectionError(): boolean;
}
YuriRequest
class YuriRequest
{
constructor(xhr: XMLHttpRequest);
get xhr(): XMLHttpRequest;
abort(): void;
onComplete(callback: (result: YuriResult) => void): void;
}
YuriResponse
class YuriResponse {
constructor(body: any, code: number, headers: string, type: string, url: string, status: string);
getRawBody(): any;
getJsonBody(): object;
getRawHeaders(): string;
getAllHeaders(): object;
getCode(): number;
getResponseType(): string;
getStatus(): string;
getResponseUrl(): string;
}
YuriConfig
class YuriConfig
{
method: HttpMethod;
url: string;
timeout: number;
headers: object;
body: any;
}
HttpMethod
enum HttpMethod {
GET = 'GET',
POST = 'POST',
PUT = 'PUT',
DELETE = 'DELETE'
}
ConnectionError
class ConnectionError
{
constructor(message: string);
public get message(): string;
}
ServerError
class ServerError
{
constructor(code: number, message: string);
public get code(): number;
public get message(): string;
}