Search code examples
node.jsaxiosnestjshateoas

How to get data for _links in nextjs external api call


I am calling an api in my nestjs application. The response of api has hateoas format like below

enter image description here

and here is my code

import { HttpService, Injectable } from '@nestjs/common';
import { Observable } from 'rxjs';
import { catchError, map, switchMap, tap } from 'rxjs/operators';

@Injectable()
export class IqProductQuantityService {
    constructor(
        private readonly httpService: HttpService
    ) {}

...
return this.httpService
                    .get<any>(url, {
                        headers: {
                            Authorization: `Bearer ${token}`
                        }
                    })
                    .pipe(
                        map((res) => res.data),
                        tap((data) => console.log(data)),
                        tap((data) => console.log(data?._links?._next))
                    );

The problem is when I get data, I receive Array of items, and there is no _links or _embeded data in the response, seems the axios or nestjs is parsing this data gracefuly to make the life easier, but at same time we lost the information of _links.next to do paging

This is what I receive: console.log(data):

[
  {
    Id: '82cf8651-c742-4352-aa70-001ee180707c',
    CompanyId: 13279,
    EntityId: 22235,
    IsSerialized: false,
    IsDropShippable: false,
    IsLot: false,
    QuantityInStock: 0,
    QuantityOnOrder: 0,
    QuantityTransferIn: 0,
    QuantityTransferOut: 0,
    UnitId: 0
  },
  {
    Id: '82cf8651-c742-4352-aa70-001ee180707c',
    CompanyId: 13279,
    EntityId: 22236,
    IsSerialized: false,
    IsDropShippable: false,
    IsLot: false,
    QuantityInStock: 0,
    QuantityOnOrder: 0,
    QuantityTransferIn: 0,
    QuantityTransferOut: 0,
    UnitId: 0
  },
  {
    Id: '82cf8651-c742-4352-aa70-001ee180707c',
    CompanyId: 13279,
    EntityId: 22237,
    IsSerialized: false,
    IsDropShippable: false,
    IsLot: false,
    QuantityInStock: 0,
    QuantityOnOrder: 0,
    QuantityTransferIn: 0,
    QuantityTransferOut: 0,
    UnitId: 0
  }
]

console.log(data?._links?._next): undefined

The question is how should I retrieve _links.next.href data?


Solution

  • After trying to reproduce the issue I found the root cause. I write it here in case someone else has same problem and finding this post.

    I found when I call the api in postman the response has Content_type: application/hal+json header, and in postman in request header we have Accept:*/*

    If you change it Accept:application/json you will receive the array of data (like what I received in my code)

    So what I did I changed my code to below and now I get all _link information on res.data

    return this.httpService
                        .get<any>(url, {
                            headers: {
                                Accept: 'application/hal+json',
                                Authorization: `Bearer ${token}`
                            }
                        })