Thanks to visit codestin.com
Credit goes to github.com

Skip to content

khalyomede/jur-js

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Jur.js

JSON Uniform Response for Javascript.

GitHub tag

Summary

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.

Installation

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>

Examples

Parse and get the response in the form of an object

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 
  } 
}

Fetch a property

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()

Fetch time properties in different unit of time

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
27

As 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.

Fetch the latency

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') );
94034

I 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.

About

JSON Uniform Response for Javascript.

Resources

Stars

Watchers

Forks

Packages

No packages published