0.1.3 • Published 2 years ago

yuri-js v0.1.3

Weekly downloads
-
License
MIT
Repository
-
Last release
2 years ago

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: 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

  1. setUrl(string) - set endpoint. If not starts with http or // try merge with base config url;
  2. setUrlParams(object) - receive object of url params
  3. setUrlParam(string name, string value) - receive name and value, set specific url param
  4. addUrlParams(object) - receive object, merge it with current url params
  5. setHeaders(object) - receive headers as object
  6. setHeader(string name, string value) - receive name and value, set specific header
  7. addHeaders(object) - receive object, merge it with current headers
  8. setMethod(HttpMethod) - set request method - prefer use enum HttpsMethod
  9. setTimeout(number) - set request timout in millis. default 0, request has no timeout

Execute request

  1. execute() - return YuriRequest
  2. request(function) - return promise that resolve YuriResult. Callback (if set) receive YuriResult param
  3. requestRawBody(function) - return promise that resolve received raw body on success and reject YuriResult on fail. Callback receive body as first param and error as second one.
  4. requestJsonBody(function) - return promise that resolve received parsed JSON body on success and reject YuriResult on fail. If parse JSON failed, resolve null. Callback receive body as first param and error as second one.
  5. requestCode(function) - return promise that resolve code on success or server error, and reject YuriResult on connection error. Callback receive code as param on success or server error, and null and YuriResult 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;
}
0.1.3

2 years ago

0.1.0

3 years ago

0.1.2

3 years ago

0.1.1

3 years ago

0.0.1

3 years ago