npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

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:

  1. set body:
    1. setRawBody(any) - set exact body that you want to send to server;
    2. setRawBodyCallback(function) - set function that will return body to send to server
    3. setRawBodyPromise(Promise) - set promise that resolve body to send to server
    4. setJsonBody(object) - parse provided object to JSON body
    5. setFormBody(object) - parse provided object to form url encoded data
  2. setUrl(string) - set endpoint. If not starts with http or // try merge with base config url;
  3. setUrlParams(object) - receive object of url params
  4. setUrlParam(string name, string value) - receive name and value, set specific url param
  5. addUrlParams(object) - receive object, merge it with current url params
  6. setHeaders(object) - receive headers as object
  7. setHeader(string name, string value) - receive name and value, set specific header
  8. addHeaders(object) - receive object, merge it with current headers
  9. setMethod(HttpMethod) - set request method - prefer use enum HttpsMethod
  10. 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;
}