File

src/app/books/books-data/services/books.service.ts

Index

Methods

Constructor

constructor(http: HttpClient)
Parameters :
Name Type Optional
http HttpClient No

Methods

getBooksData
getBooksData(books: literal type[])
Parameters :
Name Type Optional
books literal type[] No
Returns : Observable<Book[]>
getBooksDataAPIUrl
getBooksDataAPIUrl(books: literal type[])
Parameters :
Name Type Optional
books literal type[] No
Returns : string
import { Injectable } from "@angular/core"
import { HttpClient } from '@angular/common/http'

import { Observable } from 'rxjs'
import { map } from 'rxjs/operators'

import { Book } from '../../../book/book-data/models/book.model'

@Injectable()
export class BooksService {
  constructor(private http: HttpClient) {}

  getBooksDataAPIUrl(books: {id: number, isbn: string}[]): string {
    return `https://openlibrary.org/api/books?bibkeys=ISBN:${books.map(book => book.isbn).join()}&format=json&jscmd=data`
  }

  getBooksData(books: {id: number, isbn: string}[]): Observable<Book[]> {
    return this.http.get(this.getBooksDataAPIUrl(books))
      .pipe(
        map((openLibraryBooksAPIResponse: any) => {
          // hydrate this API response data with out corresponding book ID's
          let normalizedResponse: Book[] = []

          for (let responseObjectKey in openLibraryBooksAPIResponse) {
            // 2 formats for the key, one is "ISBN:0923409234" and the other is the plain ISBN number "0923409234"
            let isbn: string = responseObjectKey

            if (responseObjectKey[0] === 'I' && responseObjectKey[1] === 'S' && responseObjectKey[2] === 'B' && responseObjectKey[3] === 'N' && responseObjectKey[4] === ':') {
              isbn = responseObjectKey.substr(5)
            }

            const book = books.find(book => book.isbn === isbn)

            normalizedResponse.push({
              id: book.id,
              ...openLibraryBooksAPIResponse[responseObjectKey]
            })
          }

          return normalizedResponse
        })
      )
  }
}

result-matching ""

    No results matching ""