-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
migrate getBookFromISBN to class service
- Loading branch information
1 parent
b015410
commit 83e3480
Showing
9 changed files
with
57 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,51 +1,62 @@ | ||
import type { InsertBook } from "@sovoli/db/schema"; | ||
|
||
import type { Book } from "./models"; | ||
import type { ISBNdbBook } from "./models"; | ||
import { env } from "~/env"; | ||
import { AsyncResilience } from "~/utils/retry/AsyncResilience"; | ||
import { retryAsync } from "~/utils/retry/retry-async"; | ||
import { BaseService } from "../baseService"; | ||
import { transformISBNdbToInsertBook } from "./transformISBNdbToBook"; | ||
|
||
// Response object structure for getBookByISBN | ||
export interface GetBookFromISBNdbResponse { | ||
book: Book; // Book object for the given ISBN | ||
export interface GetBookFromISBNdbResult { | ||
book: InsertBook | null; | ||
} | ||
|
||
export interface GetBookFromISBNdbOptions { | ||
isbn: string; | ||
} | ||
|
||
export const getBookFromISBNdb = async ({ | ||
isbn, | ||
}: GetBookFromISBNdbOptions): Promise<InsertBook | null> => { | ||
const apiKey = env.ISBN_DB_API_KEY; | ||
const url = `https://api2.isbndb.com/book/${isbn}`; | ||
|
||
const fetchBookFunc = async () => { | ||
const response = await fetch(url, { | ||
headers: { | ||
Authorization: apiKey, | ||
"Content-Type": "application/json", | ||
}, | ||
}); | ||
|
||
if (response.status === 429) { | ||
throw new Error("Rate limit hit"); | ||
export class GetBookFromISBNdb extends BaseService< | ||
GetBookFromISBNdbOptions, | ||
GetBookFromISBNdbResult | ||
> { | ||
protected async execute({ isbn }: GetBookFromISBNdbOptions) { | ||
const apiKey = env.ISBN_DB_API_KEY; | ||
const url = `https://api2.isbndb.com/book/${isbn}`; | ||
|
||
const fetchBookFunc = async () => { | ||
const response = await fetch(url, { | ||
headers: { | ||
Authorization: apiKey, | ||
"Content-Type": "application/json", | ||
}, | ||
}); | ||
|
||
if (response.status === 429) { | ||
throw new Error("Rate limit hit"); | ||
} | ||
|
||
return response; | ||
}; | ||
|
||
const response = await retryAsync( | ||
fetchBookFunc, | ||
AsyncResilience.exponentialBackoffWithJitter(), | ||
); | ||
|
||
if (!response.ok) { | ||
this.logger.error("Failed to fetch book data"); | ||
return { | ||
book: null, | ||
}; | ||
} | ||
|
||
return response; | ||
}; | ||
const data = (await response.json()) as { | ||
book: ISBNdbBook; | ||
}; | ||
const book = transformISBNdbToInsertBook(data.book); | ||
|
||
const response = await retryAsync( | ||
fetchBookFunc, | ||
AsyncResilience.exponentialBackoffWithJitter(), | ||
); | ||
|
||
if (!response.ok) { | ||
console.error(response); | ||
throw new Error("Failed to fetch book data"); | ||
return { | ||
book, | ||
}; | ||
} | ||
|
||
const data = (await response.json()) as GetBookFromISBNdbResponse; | ||
return transformISBNdbToInsertBook(data.book); | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters