Queries and deserializes JsonAPI responses using the power of typescript.
This client uses Axios with the typing power of Typescript to construct requests to APIs using JSON:API.
- Build low-level queries. Maintain flexibility and a visual understanding of the JSON:API specification
- Save time by using typing to create queries and retrieve JSON:API data.
- Take advantage of automatic deserialization of your query returns, to retrieve objects that are easy to handle.
There are already many frontend clients for consuming APIs using JSON:API. But none gave me complete satisfaction:
- Most libraries return objects and their relationships with the JSON:API structure like
User.attributes.projects.attributes.name. This way of manipulating data is too cumbersome and moves away from simple OOP use. - Some libraries are too complicated to handle.
- Others are not flexible enough when creating queries.
- Finally, others are absolutely not typed.
Install the client using a package manager :
npm install --save @popsink/jsonapi-client
Import and instanciate the JsonApiService.
importJsonApiServicefrom'@popsink/jsonapi-client'constapi=newJsonApiService({baseURL: 'https://myservice.com'})Use the availables methods :
// Let's assume that the API returns users such as:typeTUser={username: stringnickname: string}// List the usersapi.list<TUser>('users/').then((users)=>{for(constuserofusers){console.log(user.username)// { id: 1, username: 'Tomy', nickname: 'Tom' }}})// Get a userapi.get<TUser>('users/12/').then((user)=>{console.log(user.username)// { id: 1, username: 'Tomy', nickname: 'Tom' }})// Create a userapi.create<TUser>('users/',{data: {type: 'user',attributes: {username: 'john',nickname: 'foo'}}})// Update a userapi.update<TUser>('users/',{data: {type: `users/12/`,attributes: {nickname: 'bar'}}})// Delete a userapi.remove('users/12/')// Use low level requestapi.request( ... )