JSON Uniform Response for Javascript.
Json Uniform Response (JUR) is a way to deliver JSON REST API responses in a consistant manner, no matter the nature of the request. To know more, please read the standard.
This library implement version 2 of the standard.
On your web page, include the following script (4 KB) right before the body tag ending or after your scripts:
<script type="text/javascript" src="https://rawgit.com/khalyomede/jur-js/v0.1.0/dist/jur.min.js"></script>- Parse and get the response in the form of an object
- Fetch a property
- Fetch time properties in different unit of time
- Fetch the latency
const RESPONSE = '{"message":null,"request":"get","data":[{"id":1,"name":"New in PHP 7.2","author":"Carlo Daniele"},{"id":2,"name":"Help for new PHP project","author":"Khalyomede"}],"debug":{"elapsed":27,"issued_at":1529617930807795,"resolved_at":1529617930807822}}';
let response = new Jur().parse(RESPONSE);
console.log( response.toObject() );{
message: null,
request: 'get',
data: [
{ id: 1, name: 'New in PHP 7.2', author: 'Carlo Daniele' },
{ id: 2, name: 'Help for new PHP project', author: 'Khalyomede' }
],
debug: {
elapsed: 27,
issued_at: 1529617930807795,
resolved_at: 1529617930807822
}
}const RESPONSE = '{"message":null,"request":"get","data":[{"id":1,"name":"New in PHP 7.2","author":"Carlo Daniele"},{"id":2,"name":"Help for new PHP project","author":"Khalyomede"}],"debug":{"elapsed":27,"issued_at":1529617930807795,"resolved_at":1529617930807822}}';
let response = new Jur().parse(RESPONSE);
console.log( response.data() );[
{ id: 1, name: 'New in PHP 7.2', author: 'Carlo Daniele' },
{ id: 2, name: 'Help for new PHP project', author: 'Khalyomede' }
]You can use the following methods to extract all of the JUR properties:
.message().request().data().elapsed().issuedAt().resolvedAt()
These are all the methods that are related to time:
.elapsed().issuedAt().resolvedAt().latency()
const RESPONSE = '{"message":null,"request":"get","data":[{"id":1,"name":"New in PHP 7.2","author":"Carlo Daniele"},{"id":2,"name":"Help for new PHP project","author":"Khalyomede"}],"debug":{"elapsed":27,"issued_at":1529617930807795,"resolved_at":1529617930807822}}';
let response = new Jur().parse(RESPONSE);
console.log( response.issuedAt('second') );
console.log( response.issuedAt('millisecond') );
console.log( response.elapsed() );
console.log( response.elapsed('microsecond') );1529617931
1529617930808
27
27As you can see, without any parameter, the time is returned in microseconds.
This is possible thanks to the standard that enforce the time to be returned as microseconds.
The latency is the gap in time between the moment you sent a request to the endpoint and the moment the server received the request.
const RESPONSE = '{"message":null,"request":"get","data":[{"id":1,"name":"New in PHP 7.2","author":"Carlo Daniele"},{"id":2,"name":"Help for new PHP project","author":"Khalyomede"}],"debug":{"elapsed":27,"issued_at":1529617930807795,"resolved_at":1529617930807822}}';
let response = new Jur().issued();
// ... Making an ajax request here ...
response.parse(RESPONSE);
console.log( response.latency('second') );94034I have a high latency because I use a very old response generated some days ago. In real, you should have only few milliseconds of latency.