Solar is a fast, small, and highly configurable JavaScript XHR wrapper library.
The main Idea is to have Request groups defined. So what are request groups? - Request groups are bundles of API endpoints which can define common basePaths, headers, responseTypes etc and other configs necessary under a group. A single API endpoint declares its method type, relative Url (bare minimum) and in addition specific headers or responseType, configs to override parent.
- By default provides two request groups - Auth and UnAuth
- Custom Request Groups to group APIs under group names to define group configs
- Custom Request Groups can inherit optionally from parent groups
- JSON based Config declaration or specific setup with method chaining
Once the configuration is done, Solar can be used to make XHR requests with just the api name or group name if any. If needed additional parameter payload can be passed in as argument apart from the regular success and error callbacks.
However, Solar can also be used to send XHR requests wihtout any configs for standalone requests.
Currently supports GET, POST, PUT
- Promises support
- HTTP Delete support
Solar is available only as standalone library from github repo. No CDN support as of now.
<script src="<your-path>/solar.min.js"></script>
How To Use Solar
$olar.setBaseUrl(baseUrl)
.setAuthenticatedEndpoints(authEndpoints)
.setUnAuthenticatedEndpoints(unAuthEndpoints)
.setCommonHeaders(commonHeaders)
.setAuthConfig(authConfig)
.setUnAuthConfig(unAuthConfig)
.addCustomRequestGroup('group1', group1)
.addCustomRequestGroup('group2', group2)
.addCustomRequestGroup('group3', group3);
$olar.loadConfig(solarConfig);
$olar.executeAuth('profile');
$olar.executeUnAuth('status');
$olar.executeCustom('group1', 'status');
As per configuration, Solar picks up the following - Http Method type, Auth specific/ common/ endpoint specific headers, Auth basePath, XHR Reponse Type. Additional headers can be passed on while sending the request. Function executeAuth takes in payload, success callback and error callback
$olar.executeAuth('profile-update',
{
data: { /* Post Data */
name: ['Sumeet Sarkar']
},
headers: { /* Headers */
'X-Auth-Dynamic': 'some-dynamic-data'
}
},
(data) => /* Success callback */ console.log(data),
(err) => /* Error callback */ console.error(err)
);
As per configuration, Solar picks up the following - Http Method type, Common/ endpoint specific headers, UnAuth basePath, XHR Reponse Type. Additional headers can be passed on while sending the request. Function executeUnAuth takes in payload, success callback and error callback
$olar.executeUnAuth('echo',
{
params: { /* Query Params */
version: 1234,
locale: 'en'
}
},
(data) => /* Success callback */ console.log(data),
(err) => /* Error callback */ console.error(err)
);
Solar picks up the api from the group specified and merges group specific & common configs together. If the custom group inherits a parent group like auth or unauth, then corresponding parent's configs are merged.
// call custom group api
$olar.executeCustom('group1', 'status',
{}, /* No Post Data */ /* No Query params */
(data) => /* Success callback */ console.log(data),
(err) => /* Error callback */ console.error(err)
);
$olar.request({
/* Method */
method: 'GET',
/* Url */
url: '/api/app/echo?version={version}&locale={locale}',
/* Query Params */
params: {
version: 1234,
locale: 'en'
},
/* Headers */
headers: {
'X-App-Name': 'Demo $olar',
'X-App-Key' : 'myappkey',
'Content-Type': 'application/json'
}
},
(data) => /* Success callback */ console.log(data),
(err) => /* Error callback */ console.error(err)
);
$olar.info();
npm run start-example
npm run build