Primary designed for dc-api-core.
// Use Node.JS require
const API = require('dc-api-client');
// OR use import for ES6+/TS
import API from 'dc-api-client'
const res = await API.Controller.action({
// Any supported data types.
// It can be any JSON-compitable type, such as strings, objects etc.
// Also you can use any Blob/File value.
});
Controller
- your controller nameaction
- your action name in controllerres
- Response object
Name | Type | Note |
---|---|---|
success | Boolean |
true if response code is 200 |
code | Number |
HTTP response code |
msg | any |
If backend returned JSON, it will be parsed, otherwise contains raw response. |
Sending user data to register
method in Auth
controller.
const API = require('dc-api-client');
const res = await API.Auth.register({
email: 'test@best.mail',
password: '123123'
});
console.log(res);
Example console output:
{
success: true,
code: 200,
msg: {
status: 'user_registred',
id: 42
}
}
Settings strored in API.settings
object.
Field | Type | Note |
---|---|---|
secure | Boolean |
true - use HTTPS / false - use HTTP |
base | String |
Hostname with prefix path |
dev | Function |
Enables or disables dev mode as setter, returns Bolean as getter |
const API = require('dc-api-client');
API.settings.secure = true;
API.settings.base = 'yourdomain.com:8080/api';
Where yourdomain.com
is your domain or IP address.
import API from 'dc-api-client';
API.settings.secure = false;
API.settings.base = `${location.hostname}:8080/api`;
WebSockets connection will be initialized when controller used for first time.
WS URI: ws[s]://<settings.base>/socket
API.Socket: {
// Calling <event> method in Socket controller on back-end
emit (event: String, ...arguments: any[]);
// Sets trigger for <event> reply
on (event: String, listener: (...arguments: any[]) => {});
// Sets one time trigger for <event> reply
once (event: String, listener: (...arguments: any[]) => {});
// Removing trigger for <event>
off (event: String, listener: (...arguments: any[]) => {});
};
Token will be delivered to backend with token
header.
You can override token from backend by sending token
header,
you don't should send this header with all requests.
Current token stores in localStorage. Read with localStorage.getItem('token')
or write localStorage.getItem('token', 'new-token')
.
Sends POST request in browser with form.
API.post(url: String, data: Object, newtab: Boolean = false);
API.send(controller: String, action: String, data: any, callback: Response => void);
Compatibility:
Internet Explorer | Edge | Firefox | Chrome | Safari | Opera |
---|---|---|---|---|---|
Not supported | 12+ | 22+ | 49+ | 10+ | 36+ |