0.1.3 • Published 3 years ago
yuri-js v0.1.3
Yuri
Ajax Library
Installation
npm install yuri-jsUsage
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:
1. set body:
1. setRawBody(any) - set exact body that you want to send to server;
1. setRawBodyCallback(function) - set function that will return body to send to server
1. setRawBodyPromise(Promise) - set promise that resolve body to send to server
1. setJsonBody(object) - parse provided object to JSON body
1. setFormBody(object) - parse provided object to form url encoded data
setUrl(string)- set endpoint. If not starts withhttpor//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 enumHttpsMethodsetTimeout(number)- set request timout in millis. default 0, request has no timeout
Execute request
execute()- returnYuriRequestrequest(function)- return promise that resolveYuriResult. Callback (if set) receiveYuriResultparamrequestRawBody(function)- return promise that resolve received raw body on success and rejectYuriResulton 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 rejectYuriResulton 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 rejectYuriResulton connection error. Callback receive code as param on success or server error, and null andYuriResulton 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;
}