The basis for the construction of JavaScript SDK.
Cloning of repository
git clone https://github.com/shcoder-ru/sdk.js.git
Include sdk.js in HTML code
<script src="/path/to/sdk.js"></script>
Include middleware files on the server side.
var method = require('/path/to/middleware/method.js');
var cors = require('/path/to/middleware/cors.js');
app.use(method());
app.use(cors(['http://origin', 'http://anotherorigin']));
.setup({
contentType: '<string>', // Example application/json
baseUrl: <string>, // Example http(s)://servername/path/to/api
httpMethodOverride: <boolean>, // If set to true, will be replaced all methods except GET and POST to POST (For CORS)
forcedResponseStatus: <integer>, // HTTP status code (exmple 200 - For CORS)
withCredentials: <boolean>, // XHR withCredentials field (For CORS)
timeout: <integer> // Request timeout
});
.exec(<function>) // Callback function
.exec(function(error, result){
// error - Response error (null|object)
// result - Response result
})
.then(<function>) // Callback function
.then(function(result){
// result - Response result
})
.catch(<function>) // Callback function
.catch(function(error){
// error - Response error (null|object)
})
.data(<object>) // Body of HTTP request
.query(<object>) // URI query parameters
.args(<object>) // Pathname parameters (For "/entitys/{entityId}" {entityId: 'value'})
SDK.setup({
contentType: 'application/json',
baseUrl: http://servername/api/v1,
httpMethodOverride: true,
forcedResponseStatus: 200,
withCredentials: true,
timeout: 30000
});
angular
.module('services')
.service('$sdk', function($window){
$window.SDK.setup({
contentType: 'application/json',
baseUrl: http://servername/api/v1,
httpMethodOverride: true,
forcedResponseStatus: 200,
withCredentials: true,
timeout: 30000
});
return $window.SDK;
});
SDK.addMethod('Entity.create', '/entitys', 'POST');
SDK.addMethod('Entity.list', '/entitys', 'GET');
SDK.addMethod('Entity.get', '/entitys/{entityId}', 'GET');
SDK.addMethod('Entity.update', '/entitys/{entityId}', 'PUT');
SDK.addMethod('Entity.delete', '/entitys/{entityId}', 'DELETE');
SDK.callMethod('Entity.create')
.data({
key: 'value'
})
.exec(function(err, result){
// use err and result
})
.then(function(result){
// use result
});
.catch(function(err){
// use err
});
SDK.callMethod('Entity.list')
.query({
key: 'value'
})
.exec(function(err, result){
// use err and result
})
.then(function(result){
// use result
});
.catch(function(err){
// use err
});
SDK.callMethod('Entity.get')
.args({
entityId: 'value'
})
.exec(function(err, result){
// use err and result
})
.then(function(result){
// use result
});
.catch(function(err){
// use err
});
SDK.callMethod('Entity.update')
.args({
entityId: 'value'
})
.data({
key: 'value'
})
.exec(function(err, result){
// use err and result
})
.then(function(result){
// use result
});
.catch(function(err){
// use err
});
SDK.callMethod('Entity.delete')
.args({
entityId: 'value'
})
.exec(function(err, result){
// use err and result
})
.then(function(result){
// use result
});
.catch(function(err){
// use err
});