sioapi
v1.0.2
Published
Module for making API requests
Downloads
10
Readme
SioApi
Navigator module for making API requests and controlling the responses.
Install
npm install sioapiImport
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).
|Name|Type|Description| |---|---|---| |url|String|Url of the API, leave blank to use current host.
Events: .on(eventName,callback).
Check SioEvents for more details
|Event|Fire Time|Data| |---|---|---| |response|Whenever a response from the server is received|Call 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={})
- If subUrl is setted makes a GET fetch to API.url/subUrl with the options.
- 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={})
- If subUrl is setted makes a POST fetch to API.url/subUrl with the options and the body of data.
- 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={})
- If subUrl is setted makes a PUT fetch to API.url/subUrl with the options and the body of data.
- 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={})
- If subUrl is setted makes a DELETE fetch to API.url/subUrl with the options and the body of data.
- 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
|Event|Fire Time|Data| |---|---|---| |response|Whenever a response from the server is received|Call 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.
- If subUrl is setted makes a GET fetch to API.url/${this.service}/suburl with the options.
- 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.
- If subUrl is setted makes a POST fetch to API.url/${this.service}/subUrl with the options and the body of data.
- 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.
- If subUrl is setted makes a PUT fetch to API.url/${this.service}/subUrl with the options and the body of data.
- 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.
- If subUrl is setted makes a DELETE fetch to API.url/${this.service}/subUrl with the options and the body of data.
- 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
|Name|Type|Description| |---|---|---| |url|String|The url the request was made. |headers|Object|Object with the headers of the response. |blob|Blob|Blob of the body of the response. |response|Response|A Response Object with a clone of the fetch response. |status|Number|Number status of the response. |ok|String|response.ok |statusText|String|response.statusText. |raw|Any|The computed raw data of the response. |data|Any|The 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.
