microsoft / typed-rest-client

Node Rest and Http Clients with typings for use with TypeScript
Other
675 stars 118 forks source link

HATEOAS (or hypermedia) feature #256

Closed imsergiobernal closed 3 years ago

imsergiobernal commented 3 years ago

Issue Description

HATEOAS are a good practice for RESTful APIs. I miss some of native compatibility in the lib.

As HAL is still a draft, maybe there would be nice to pass some kind of adapter in order to let client to decide the format that will recieve. This adapter could be an Interface/Class that this lib exports for flexibility and consistency.

GET /accounts

 {
   "_links": {
     "self": { "href": "/accounts" },
     "next": { "href": "/accounts?page=2" },
     "find": { "href": "/accounts{?email}", "templated": true }
   },
   "_embedded": {
     "accounts": [{
         "_links": {
           "self": { "href": "/accounts/123" },
           "profile": { "href": "/profiles/98712" },
         },
         "email": "some@gmail.com",
       },{
         "_links": {
           "self": { "href": "/accounts/124" },
           "profile": { "href": "/profiles/97213" },
         },
         "email": "another@gmail.com",
     }]
   },
 }

GET /accounts/123

{
  "_links": {
    "self": { "href": "/accounts/123" },
    "profile": { "href": "/profiles/98712" },
  },
   "email": "some@gmail.com",
  }
}
const client = new RestClient(...);

const accountsResponse = await client.get('accounts', { halAdapter: { json: (result) => ({ }) } }) // halAdapter works when header response is json-hal type

// if { halAdapter } passed, update signature to expect http call methods like .getEmbedded() or .get()
const profileResponse = await accountsResponse.result.getEmbedded(0).get('profile', { halAdapters: { json: (result) => ({ }) } })
// if no rel=profile, throw error

How many of you find this feature/compatibility useful?

github-actions[bot] commented 3 years ago

This issue has had no activity in 90 days. Please comment if it is not actually stale