Added book list handler in service worker

This commit is contained in:
Dmitriy Shishkov 2021-07-31 22:00:27 +03:00
parent d004e9681e
commit 487b6b91bd
No known key found for this signature in database
GPG Key ID: 14358F96FCDD8060
3 changed files with 22 additions and 2 deletions

View File

@ -18,3 +18,8 @@ export const openDB = () =>
*/
export const saveBook = async (book: BookT) =>
(await openDB()).add("Books", book);
/**
* Returns all books saved in IndexedDB
*/
export const getBooks = async () => (await openDB()).getAll("Books");

View File

@ -1,4 +1,4 @@
import { saveBook } from "./db";
import { getBooks, saveBook } from "./db";
export interface PathHandler {
/** Path start for handler */
@ -34,3 +34,12 @@ export const handleBookUpload = async (request: Request) => {
return new Response(JSON.stringify(err));
}
};
/**
* Gets all books from database
*/
export const handleBooks = async () => {
const list = await getBooks();
return new Response(JSON.stringify(list));
};

View File

@ -1,6 +1,11 @@
import { fromCache, precache } from "./cache";
import { openDB as createDB } from "./db";
import { handle, handleBookUpload, PathHandler } from "./fetchHandlers";
import {
handle,
handleBooks,
handleBookUpload,
PathHandler,
} from "./fetchHandlers";
declare const self: ServiceWorkerGlobalScope;
@ -17,6 +22,7 @@ self.addEventListener("fetch", (event) => {
const handlers: PathHandler[] = [
{ path: "/upload", getResponse: () => handleBookUpload(request) },
{ path: "/list", getResponse: () => handleBooks() },
{ path: "", getResponse: () => fromCache(request) },
];