crow-fetch-api
v0.1.6
Published
Client side Fetch API
Readme
Crow Fetch API
Description
CrowFetchAPI is simple JS client side library. Тhis makes it easier to work with HTTP requests by implementing REST API, and automates the processing of responses from the Server.
Installation
npm
npm install --save crow-fetch-apiFor use import module(modules)
import {CrowFetchAPI} from 'crow-fetch-api';HTML
Download
git clone https://[email protected]/crow_dev/crow-fetch-api.gitMinimized script file is
dist/crow.fetch.api.last.jsInclude it in your HTML
<script src="dist/crow.fetch.api.last.js" type="text/javascript">Documentation
CrowFetchAPI
Option methods
setDataTypes(object: data_types)data_types must have this structure
{
users: function(data){
return '/users';
},
userContacts: function(data){
return '/users/' + data.userId + '/contacts';
}
}Callbacks must return strings and must not throw errors. data in arguments is data, which you send from action methods.
setIdKey(string: id_key)ID key to generate request URLs (in default 'id')
setDevMode(boolean: dev_move_flag)Turn on/off console debugging (in default 'false')
Action methods
get(string: data_type, object: data)Send request to url datatype_path/data.id
getAll(string: data_type, object: data)Send request to url datatype_path
edit(string: data_type, object: data)Send request to url datatype_path/data.id
add(string: data_type, object: data)Send request to url datatype_path
patch(string: data_type, object: data)Send request to url datatype_path/data.id
get(string: data_type, object: data)Send request to url datatype_path/data.id
Examples
So simple request looks like
// imports
import { CrowFetchAPI } from 'crow-fetch-api';
// set server host
CrowFetchAPI.setServerHost('https://www.example.com');
// set data types (types)
CrowFetchAPI.setDataTypes({
users: function(data) {
return "/users";
},
contacts: function(data) {
return "/users/" + data.userId + '/contacts';
}
});
// send request
// <data> must contain ID of datatype
// for url build
// If You want change <id_key> You can run
// default <id_key> is 'id'
CrowFetchAPI.setIdKey(new_id_key);
var data = {
id: 1
};
CrowFetchAPI.get('users', data).then(
// data is a parsed server response, // if server sent not json response, // // module throws an error
function(data){
// You can use data ...
},
function(error_message){
// pasrsing error messages
}
);