1.0.2 • Published 2 years ago

sioapi v1.0.2

Weekly downloads
-
License
ISC
Repository
github
Last release
2 years ago

SioApi

Navigator module for making API requests and controlling the responses.

Install

  npm install sioapi

Import

Bundle

import SioApi from "sioapi";

CDN

import SioApi from "https://unpkg.com/sioapi";

API

Creates a new SioApi Object.

This module extends SioEvents.

  • Constructor: new SioApi(url).

    NameTypeDescription
    urlStringUrl of the API, leave blank to use current host.
  • Events: .on(eventName,callback).

    Check SioEvents for more details

    EventFire TimeData
    responseWhenever a response from the server is receivedCall Object
  • Methods

    • .getService(name).

      Creates a new Service Object to the API url/name

    • .setHeader(name,value).

      Sets a header to be send to every petition on this api.

    • .removeHeader(name).

      Removes a header from the apis

    • Async .get(suburl="",options={}) or .get(options={})

      1. If subUrl is setted makes a GET fetch to API.url/subUrl with the options.
      2. If subUrl is not setted makes a GET fetch to API.url with the options. Returns a Call Object.
    • Async .post(suburl="", data={}, options={}) or .post(data={}, options={})

      1. If subUrl is setted makes a POST fetch to API.url/subUrl with the options and the body of data.
      2. If subUrl is not setted makes a POST fetch to API.url with the options and the body of data. Returns a Call Object.
    • Async .put(suburl="", data={}, options={}) or .put(data={}, options={})

      1. If subUrl is setted makes a PUT fetch to API.url/subUrl with the options and the body of data.
      2. If subUrl is not setted makes a PUT fetch to API.url with the options and the body of data. Returns a Call Object.
    • Async .delete(suburl="", data={}, options={}) or .delete(data={}, options={})

      1. If subUrl is setted makes a DELETE fetch to API.url/subUrl with the options and the body of data.
      2. If subUrl is not setted makes a DELETE fetch to API.url with the options and the body of data. Returns a Call Object.

Service

Created from the call of an API.getService(name). This is used as a middleware between the apiurl and the service you're trying to fetch. Example: If API.url="https://example.com/api" A service can be: "users"

  • Constructor: new Service(url="",service="",api=null).

    No documentation because this is not intended to be used directly. Call API.getService() instead.
  • Events: .on(eventName,callback).

    Check SioEvents for more details

    EventFire TimeData
    responseWhenever a response from the server is receivedCall Object
  • Methods

    • .getService(name).

      Creates a new Service Object to the API url/name

    • .setHeader(name,value).

      Sets a header to be send to every petition on this Service.

    • .removeHeader(name).

      Removes a header from the Service.

    • Async .get(suburl="",options={}) or .get(options={})

      This is going to use the headers from the caller API and the headers from the Service.

      1. If subUrl is setted makes a GET fetch to API.url/${this.service}/suburl with the options.
      2. If subUrl is not setted makes a GET fetch to API.url/${this.service} with the options.

        Returns a Call Object.

    • Async .post(suburl="", data={}, options={}) or .post(data={}, options={})

      This is going to use the headers from the caller API and the headers from the Service.

      1. If subUrl is setted makes a POST fetch to API.url/${this.service}/subUrl with the options and the body of data.
      2. If subUrl is not setted makes a POST fetch to API.url/${this.service} with the options and the body of data.

        Returns a Call Object.

    • Async .put(suburl="", data={}, options={}) or .put(data={}, options={})

      This is going to use the headers from the caller API and the headers from the Service.

      1. If subUrl is setted makes a PUT fetch to API.url/${this.service}/subUrl with the options and the body of data.
      2. If subUrl is not setted makes a PUT fetch to API.url/${this.service} with the options and the body of data.

        Returns a Call Object.

    • Async .delete(suburl="", data={}, options={}) or .delete(data={}, options={})

      This is going to use the headers from the caller API and the headers from the Service.

      1. If subUrl is setted makes a DELETE fetch to API.url/${this.service}/subUrl with the options and the body of data.
      2. If subUrl is not setted makes a DELETE fetch to API.url/${this.service} with the options and the body of data.

        Returns a Call Object.

Call

Properties

NameTypeDescription
urlStringThe url the request was made.
headersObjectObject with the headers of the response.
blobBlobBlob of the body of the response.
responseResponseA Response Object with a clone of the fetch response.
statusNumberNumber status of the response.
okStringresponse.ok
statusTextStringresponse.statusText.
rawAnyThe computed raw data of the response.
dataAnyThe computed prefered type of the response (In case of the response being a json this is an object representation of that JSON).

Events

This fires the events "response" in the Service and in the API listeners. The listeners can change the final properties of this call.