Added book getting by hash in service worker
This commit is contained in:
parent
487b6b91bd
commit
aa98de6bd5
@ -23,3 +23,9 @@ export const saveBook = async (book: BookT) =>
|
|||||||
* Returns all books saved in IndexedDB
|
* Returns all books saved in IndexedDB
|
||||||
*/
|
*/
|
||||||
export const getBooks = async () => (await openDB()).getAll("Books");
|
export const getBooks = async () => (await openDB()).getAll("Books");
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets book from IndexedDB by hash
|
||||||
|
*/
|
||||||
|
export const getBook = async (hash: string) =>
|
||||||
|
(await openDB()).get("Books", hash);
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
import { getBooks, saveBook } from "./db";
|
import { getBook, getBooks, saveBook } from "./db";
|
||||||
|
|
||||||
export interface PathHandler {
|
export interface PathHandler {
|
||||||
/** Path start for handler */
|
/** Path start for handler */
|
||||||
@ -43,3 +43,13 @@ export const handleBooks = async () => {
|
|||||||
|
|
||||||
return new Response(JSON.stringify(list));
|
return new Response(JSON.stringify(list));
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets book from database
|
||||||
|
*/
|
||||||
|
export const handleBook = async (request: Request, hash: string) => {
|
||||||
|
const book = await getBook(hash);
|
||||||
|
if (book) return new Response(JSON.stringify(book));
|
||||||
|
|
||||||
|
return new Response("No such book :(");
|
||||||
|
};
|
||||||
|
@ -2,10 +2,12 @@ import { fromCache, precache } from "./cache";
|
|||||||
import { openDB as createDB } from "./db";
|
import { openDB as createDB } from "./db";
|
||||||
import {
|
import {
|
||||||
handle,
|
handle,
|
||||||
|
handleBook,
|
||||||
handleBooks,
|
handleBooks,
|
||||||
handleBookUpload,
|
handleBookUpload,
|
||||||
PathHandler,
|
PathHandler,
|
||||||
} from "./fetchHandlers";
|
} from "./fetchHandlers";
|
||||||
|
import { getHash } from "./utils";
|
||||||
|
|
||||||
declare const self: ServiceWorkerGlobalScope;
|
declare const self: ServiceWorkerGlobalScope;
|
||||||
|
|
||||||
@ -21,8 +23,9 @@ self.addEventListener("fetch", (event) => {
|
|||||||
const path = new URL(request.url).pathname;
|
const path = new URL(request.url).pathname;
|
||||||
|
|
||||||
const handlers: PathHandler[] = [
|
const handlers: PathHandler[] = [
|
||||||
{ path: "/upload", getResponse: () => handleBookUpload(request) },
|
|
||||||
{ path: "/list", getResponse: () => handleBooks() },
|
{ path: "/list", getResponse: () => handleBooks() },
|
||||||
|
{ path: "/book/", getResponse: () => handleBook(request, getHash(path)) },
|
||||||
|
{ path: "/upload", getResponse: () => handleBookUpload(request) },
|
||||||
{ path: "", getResponse: () => fromCache(request) },
|
{ path: "", getResponse: () => fromCache(request) },
|
||||||
];
|
];
|
||||||
|
|
||||||
|
9
src/serviceWorker/utils.ts
Normal file
9
src/serviceWorker/utils.ts
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
/**
|
||||||
|
* Gets hash string from book path
|
||||||
|
*/
|
||||||
|
export const getHash = (path: string) => {
|
||||||
|
let hashLength = path.length - "/book/".length;
|
||||||
|
if (path.endsWith("/")) hashLength--;
|
||||||
|
|
||||||
|
return path.substr("/book/".length, hashLength);
|
||||||
|
};
|
Loading…
x
Reference in New Issue
Block a user