syncano-server
Advanced tools
Comparing version 0.7.2-28 to 0.7.2-29
@@ -69,3 +69,3 @@ 'use strict'; | ||
event: event, | ||
socket: socket, | ||
socket: socket.connect.bind(socket), | ||
data: new Proxy(new _data2.default(), { | ||
@@ -72,0 +72,0 @@ get: function get(target, className) { |
@@ -26,4 +26,8 @@ 'use strict'; | ||
/** | ||
* Syncano account query builder | ||
* Connection between scokets. | ||
* @property {Function} | ||
* @example {@lang javascript} | ||
* const Tags = socket('tags') | ||
* const latestTags = await Tags.get('list', { sort: 'latest' }) | ||
* const createdTag = await Tags.post('create', { name: 'nature' }) | ||
*/ | ||
@@ -41,3 +45,3 @@ var Socket = function (_QueryBuilder) { | ||
key: 'url', | ||
value: function url(socket) { | ||
value: function url(endpoint) { | ||
var _instance = this.instance, | ||
@@ -47,13 +51,22 @@ instanceName = _instance.instanceName, | ||
return 'https://' + instanceName + '.' + spaceHost + '/' + socket + '/'; | ||
return 'https://' + instanceName + '.' + spaceHost + '/' + this.socketName + '/' + endpoint + '/'; | ||
} | ||
}, { | ||
key: 'connect', | ||
value: function connect(socketName) { | ||
this.socketName = socketName; | ||
return this; | ||
} | ||
}, { | ||
key: 'parseBody', | ||
value: function parseBody(body) { | ||
var isBodyAnObject = (typeof body === 'undefined' ? 'undefined' : _typeof(body)) === 'object'; | ||
return isBodyAnObject ? JSON.stringify(_extends({}, body)) : body; | ||
} | ||
}, { | ||
key: 'client', | ||
value: function client(socket) { | ||
key: 'post', | ||
value: function post(endpoint) { | ||
var body = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; | ||
@@ -64,3 +77,3 @@ var options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {}; | ||
return fetch(this.url(socket), _extends({ | ||
return fetch(this.url(endpoint), _extends({ | ||
method: 'POST', | ||
@@ -71,6 +84,33 @@ body: this.parseBody(body) | ||
}, { | ||
key: 'post', | ||
value: function post() { | ||
return this.client.apply(this, arguments); | ||
key: 'get', | ||
value: function get(endpoint) { | ||
var data = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; | ||
var options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {}; | ||
return this.post(endpoint, _extends({}, data, { _method: 'GET' }), options); | ||
} | ||
}, { | ||
key: 'delete', | ||
value: function _delete(endpoint) { | ||
var data = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; | ||
var options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {}; | ||
return this.post(endpoint, _extends({}, data, { _method: 'DELETE' }), options); | ||
} | ||
}, { | ||
key: 'put', | ||
value: function put(endpoint) { | ||
var data = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; | ||
var options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {}; | ||
return this.post(endpoint, _extends({}, data, { _method: 'PUT' }), options); | ||
} | ||
}, { | ||
key: 'patch', | ||
value: function patch(endpoint) { | ||
var data = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; | ||
var options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {}; | ||
return this.post(endpoint, _extends({}, data, { _method: 'PATCH' }), options); | ||
} | ||
}]); | ||
@@ -77,0 +117,0 @@ |
{ | ||
"name": "syncano-server", | ||
"version": "0.7.2-28", | ||
"version": "0.7.2-29", | ||
"description": "A library to intereact with the Syncano API on a server side", | ||
@@ -5,0 +5,0 @@ "main": "lib/index.js", |
@@ -106,2 +106,11 @@ [![XO code style](https://img.shields.io/badge/code_style-XO-5ed9c7.svg)](https://github.com/sindresorhus/xo) [![CircleCI](https://circleci.com/gh/Syncano/syncano-server-js/tree/devel.svg?style=shield&circle-token=0340c11444db6f3dc227cf310f4d8ff1bd90dee8)](https://circleci.com/gh/Syncano/syncano-server-js/tree/devel) | ||
### Socket connection | ||
```js | ||
const Tags = socket('tags') | ||
Tags.get('list', {sort: 'latest'}).then(latestTags => {}) | ||
Tags.post('create', {name: 'nature'}).then(createdTag => {}) | ||
``` | ||
Check [documentation](http://syncano.github.io/syncano-server-js/) to learn more. |
@@ -46,3 +46,3 @@ import Data from './data' | ||
event, | ||
socket, | ||
socket: socket.connect.bind(socket), | ||
data: new Proxy(new Data(), { | ||
@@ -49,0 +49,0 @@ get(target, className) { |
import QueryBuilder from './query-builder' | ||
/** | ||
* Syncano account query builder | ||
* Connection between scokets. | ||
* @property {Function} | ||
* @example {@lang javascript} | ||
* const Tags = socket('tags') | ||
* const latestTags = await Tags.get('list', { sort: 'latest' }) | ||
* const createdTag = await Tags.post('create', { name: 'nature' }) | ||
*/ | ||
export default class Socket extends QueryBuilder { | ||
url(socket) { | ||
url(endpoint) { | ||
const {instanceName, spaceHost} = this.instance | ||
return `https://${instanceName}.${spaceHost}/${socket}/` | ||
return `https://${instanceName}.${spaceHost}/${this.socketName}/${endpoint}/` | ||
} | ||
connect(socketName) { | ||
this.socketName = socketName | ||
return this | ||
} | ||
parseBody(body) { | ||
const isBodyAnObject = typeof body === 'object' | ||
return isBodyAnObject ? JSON.stringify({...body}) : body | ||
} | ||
client(socket, body = {}, options = {}) { | ||
post(endpoint, body = {}, options = {}) { | ||
const fetch = this.fetch.bind(this) | ||
return fetch(this.url(socket), { | ||
return fetch(this.url(endpoint), { | ||
method: 'POST', | ||
@@ -28,5 +40,17 @@ body: this.parseBody(body), | ||
post() { | ||
return this.client(...arguments) | ||
get(endpoint, data = {}, options = {}) { | ||
return this.post(endpoint, {...data, _method: 'GET'}, options) | ||
} | ||
delete(endpoint, data = {}, options = {}) { | ||
return this.post(endpoint, {...data, _method: 'DELETE'}, options) | ||
} | ||
put(endpoint, data = {}, options = {}) { | ||
return this.post(endpoint, {...data, _method: 'PUT'}, options) | ||
} | ||
patch(endpoint, data = {}, options = {}) { | ||
return this.post(endpoint, {...data, _method: 'PATCH'}, options) | ||
} | ||
} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
550976
2120
116