Added book list handler in service worker
This commit is contained in:
parent
d004e9681e
commit
487b6b91bd
@ -18,3 +18,8 @@ export const openDB = () =>
|
|||||||
*/
|
*/
|
||||||
export const saveBook = async (book: BookT) =>
|
export const saveBook = async (book: BookT) =>
|
||||||
(await openDB()).add("Books", book);
|
(await openDB()).add("Books", book);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns all books saved in IndexedDB
|
||||||
|
*/
|
||||||
|
export const getBooks = async () => (await openDB()).getAll("Books");
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
import { saveBook } from "./db";
|
import { getBooks, saveBook } from "./db";
|
||||||
|
|
||||||
export interface PathHandler {
|
export interface PathHandler {
|
||||||
/** Path start for handler */
|
/** Path start for handler */
|
||||||
@ -34,3 +34,12 @@ export const handleBookUpload = async (request: Request) => {
|
|||||||
return new Response(JSON.stringify(err));
|
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));
|
||||||
|
};
|
||||||
|
@ -1,6 +1,11 @@
|
|||||||
import { fromCache, precache } from "./cache";
|
import { fromCache, precache } from "./cache";
|
||||||
import { openDB as createDB } from "./db";
|
import { openDB as createDB } from "./db";
|
||||||
import { handle, handleBookUpload, PathHandler } from "./fetchHandlers";
|
import {
|
||||||
|
handle,
|
||||||
|
handleBooks,
|
||||||
|
handleBookUpload,
|
||||||
|
PathHandler,
|
||||||
|
} from "./fetchHandlers";
|
||||||
|
|
||||||
declare const self: ServiceWorkerGlobalScope;
|
declare const self: ServiceWorkerGlobalScope;
|
||||||
|
|
||||||
@ -17,6 +22,7 @@ self.addEventListener("fetch", (event) => {
|
|||||||
|
|
||||||
const handlers: PathHandler[] = [
|
const handlers: PathHandler[] = [
|
||||||
{ path: "/upload", getResponse: () => handleBookUpload(request) },
|
{ path: "/upload", getResponse: () => handleBookUpload(request) },
|
||||||
|
{ path: "/list", getResponse: () => handleBooks() },
|
||||||
{ path: "", getResponse: () => fromCache(request) },
|
{ path: "", getResponse: () => fromCache(request) },
|
||||||
];
|
];
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user