Initial commit
This commit is contained in:
@@ -0,0 +1,75 @@
|
||||
import { command, form, getRequestEvent, query } from '$app/server';
|
||||
import { loginSchema, signupSchema } from '$lib/schema/auth';
|
||||
import { BACKEND_API_URL } from '$lib/server/config';
|
||||
import { redirect } from '@sveltejs/kit';
|
||||
|
||||
export const login = form(loginSchema, async (data, invalid) => {
|
||||
const { cookies, locals } = getRequestEvent();
|
||||
|
||||
// Create URL-encoded form data
|
||||
const formData = new URLSearchParams();
|
||||
formData.append('email', data.email);
|
||||
formData.append('password', data.password);
|
||||
|
||||
const response = await fetch(`${BACKEND_API_URL}/access/login`, {
|
||||
method: 'POST',
|
||||
body: formData,
|
||||
headers: { 'Content-Type': 'application/x-www-form-urlencoded' }
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
if (response.status === 401) {
|
||||
invalid(invalid.email('Invalid login credentials'));
|
||||
} else {
|
||||
const message = await response.text();
|
||||
console.error('Unknown error: ', message);
|
||||
invalid(invalid.email('An unknown error occurred'));
|
||||
}
|
||||
}
|
||||
|
||||
const token = await response.json();
|
||||
|
||||
cookies.set('authToken', token.access_token, {
|
||||
path: '/',
|
||||
httpOnly: true,
|
||||
secure: true,
|
||||
sameSite: 'strict',
|
||||
maxAge: 60 * 60 * 24 * 7 // 1 week
|
||||
});
|
||||
|
||||
redirect(303, '/');
|
||||
});
|
||||
|
||||
export const signup = form(signupSchema, async (data, invalid) => {
|
||||
const response = await fetch(`${BACKEND_API_URL}/access/signup`, {
|
||||
method: 'POST',
|
||||
body: JSON.stringify(data),
|
||||
headers: { 'Content-Type': 'application/json' }
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
if (response.status == 409) {
|
||||
invalid(invalid.email('Email is already in use by another account'));
|
||||
} else {
|
||||
const message = await response.text();
|
||||
console.error('Unknown error: ', message);
|
||||
invalid(invalid.email('An unknown error occurred'));
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
export const logout = command(async () => {
|
||||
const { cookies } = getRequestEvent();
|
||||
|
||||
cookies.delete('authToken', {
|
||||
path: '/'
|
||||
});
|
||||
});
|
||||
|
||||
export const getUser = query(async () => {
|
||||
const { locals } = getRequestEvent();
|
||||
if (!locals.user) {
|
||||
redirect(307, '/login');
|
||||
}
|
||||
return locals.user;
|
||||
});
|
||||
@@ -0,0 +1,16 @@
|
||||
import { getRequestEvent, query } from '$app/server';
|
||||
import { authorQuerySchema } from '$lib/schema/author';
|
||||
import { createQueryParams } from '$lib/utils';
|
||||
import { error } from '@sveltejs/kit';
|
||||
|
||||
export const listAuthors = query(authorQuerySchema, async (data) => {
|
||||
const { locals } = getRequestEvent();
|
||||
|
||||
const params = createQueryParams(data);
|
||||
|
||||
const response = await locals.api.get(`/authors?${params.toString()}`);
|
||||
|
||||
if (!response.ok) error(500, 'An unkown error occurred');
|
||||
|
||||
return await response.json();
|
||||
});
|
||||
@@ -0,0 +1,164 @@
|
||||
import { command, form, getRequestEvent, query } from '$app/server';
|
||||
import { error } from '@sveltejs/kit';
|
||||
import {
|
||||
bookCoverUpload,
|
||||
booksUpload,
|
||||
bookIdsSchema,
|
||||
bookQuerySchema,
|
||||
deleteBookFilesSchema,
|
||||
deleteBooksSchema,
|
||||
editBookMetadataSchema,
|
||||
updateBookProgressSchema,
|
||||
type Book,
|
||||
bookFilesUpload
|
||||
} from '$lib/schema/index';
|
||||
import { stringCoerce, type PaginatedResponse } from '$lib/schema/common';
|
||||
import { createQueryParams } from '$lib/utils';
|
||||
|
||||
export const getBook = query(stringCoerce, async (id): Promise<Book> => {
|
||||
const { locals } = getRequestEvent();
|
||||
|
||||
const response = await locals.api.get(`/books/${id}`);
|
||||
|
||||
if (!response.ok) {
|
||||
if (response.status == 404) error(404, 'The book does not exist');
|
||||
error(500, 'An unkown error occurred');
|
||||
}
|
||||
|
||||
return await response.json();
|
||||
});
|
||||
|
||||
export const listBooks = query(bookQuerySchema, async (data): Promise<PaginatedResponse<Book>> => {
|
||||
const { locals } = getRequestEvent();
|
||||
|
||||
const params = createQueryParams(data);
|
||||
|
||||
const response = await locals.api.get(`/books?${params.toString()}`);
|
||||
|
||||
if (!response.ok) error(500, 'An unkown error occurred');
|
||||
|
||||
return await response.json();
|
||||
});
|
||||
|
||||
export const updateBookMetadata = form(editBookMetadataSchema, async (data): Promise<Book> => {
|
||||
const { locals } = getRequestEvent();
|
||||
|
||||
const response = await locals.api.patch(`/books/${data.book_id}`, data);
|
||||
|
||||
if (!response.ok) {
|
||||
const message = await response.text();
|
||||
error(response.status, message);
|
||||
}
|
||||
|
||||
return await response.json();
|
||||
});
|
||||
|
||||
export const updateBookCover = form(bookCoverUpload, async ({ book_id, file }) => {
|
||||
const { locals } = getRequestEvent();
|
||||
|
||||
const formData = new FormData();
|
||||
formData.append('file', file);
|
||||
|
||||
const response = await locals.api.putMultipart(`/books/${book_id}/cover`, formData);
|
||||
|
||||
if (!response.ok) {
|
||||
const message = await response.text();
|
||||
error(response.status, message);
|
||||
}
|
||||
|
||||
return await response.json();
|
||||
});
|
||||
|
||||
export const uploadBooks = form(booksUpload, async ({ library_id, files }) => {
|
||||
const { locals } = getRequestEvent();
|
||||
|
||||
const formData = new FormData();
|
||||
files.forEach((file) => {
|
||||
formData.append('files', file);
|
||||
});
|
||||
|
||||
const response = await locals.api.postMultipart(
|
||||
`/books/fromFiles?library_id=${library_id}`,
|
||||
formData
|
||||
);
|
||||
|
||||
if (!response.ok) {
|
||||
const message = await response.text();
|
||||
error(response.status, message);
|
||||
}
|
||||
|
||||
return await response.json();
|
||||
});
|
||||
|
||||
export const uploadBookFiles = form(bookFilesUpload, async ({ book_id, files }) => {
|
||||
const { locals } = getRequestEvent();
|
||||
|
||||
const formData = new FormData();
|
||||
files.forEach((file) => {
|
||||
formData.append('files', file);
|
||||
});
|
||||
|
||||
const response = await locals.api.postMultipart(`/books/${book_id}/files`, formData);
|
||||
|
||||
if (!response.ok) {
|
||||
const message = await response.text();
|
||||
error(response.status, message);
|
||||
}
|
||||
|
||||
return await response.json();
|
||||
});
|
||||
|
||||
export const deleteBooks = command(deleteBooksSchema, async (data) => {
|
||||
const { locals } = getRequestEvent();
|
||||
|
||||
const params = createQueryParams(data);
|
||||
|
||||
const response = await locals.api.delete(`/books?${params.toString()}`);
|
||||
|
||||
if (!response.ok) {
|
||||
const message = await response.text();
|
||||
error(response.status, message);
|
||||
}
|
||||
});
|
||||
|
||||
export const deleteBookFiles = command(deleteBookFilesSchema, async ({ book_id, ...data }) => {
|
||||
const { locals } = getRequestEvent();
|
||||
|
||||
const params = createQueryParams(data);
|
||||
|
||||
const response = await locals.api.delete(`/books/${book_id}/files?${params.toString()}`);
|
||||
|
||||
if (!response.ok) {
|
||||
const message = await response.text();
|
||||
error(response.status, message);
|
||||
}
|
||||
});
|
||||
|
||||
export const updateBookProgress = command(
|
||||
updateBookProgressSchema,
|
||||
async ({ book_ids, ...data }) => {
|
||||
const { locals } = getRequestEvent();
|
||||
|
||||
const params = createQueryParams({ book_ids: book_ids });
|
||||
|
||||
const response = await locals.api.post(`/books/progress?${params.toString()}`, { ...data });
|
||||
|
||||
if (!response.ok) {
|
||||
const message = await response.text();
|
||||
error(response.status, message);
|
||||
}
|
||||
}
|
||||
);
|
||||
|
||||
export const markBooksAsComplete = command(bookIdsSchema, async (data) => {
|
||||
const { locals } = getRequestEvent();
|
||||
|
||||
const params = createQueryParams(data);
|
||||
|
||||
const response = await locals.api.post(`/books/completed?${params.toString()}`, {});
|
||||
|
||||
if (!response.ok) {
|
||||
const message = await response.text();
|
||||
error(response.status, message);
|
||||
}
|
||||
});
|
||||
@@ -0,0 +1,64 @@
|
||||
import { command, getRequestEvent, query } from '$app/server';
|
||||
import { bookshelfCreate, bookshelfQuerySchema, modifyBooksInShelf, type Bookshelf } from '$lib/schema/bookshelf';
|
||||
import { createQueryParams } from '$lib/utils';
|
||||
import { error } from '@sveltejs/kit';
|
||||
|
||||
export const listBookshelves = query(bookshelfQuerySchema, async (data) => {
|
||||
const { locals } = getRequestEvent();
|
||||
|
||||
const params = createQueryParams(data);
|
||||
|
||||
const response = await locals.api.get(`/shelves?${params.toString()}`);
|
||||
|
||||
if (!response.ok) {
|
||||
const message = await response.text();
|
||||
console.error('An unknown error occurred: ', message);
|
||||
error(500, 'An unkown error occurred');
|
||||
}
|
||||
|
||||
return await response.json();
|
||||
});
|
||||
|
||||
export const addBooksToShelf = command(modifyBooksInShelf, async ({ shelf_id, ...data }): Promise<Bookshelf> => {
|
||||
const { locals } = getRequestEvent();
|
||||
|
||||
const params = createQueryParams(data);
|
||||
|
||||
const response = await locals.api.post(`/shelves/${shelf_id}/books?${params.toString()}`, {});
|
||||
|
||||
if (!response.ok) {
|
||||
const message = await response.text();
|
||||
error(response.status, message);
|
||||
}
|
||||
|
||||
return await response.json()
|
||||
});
|
||||
|
||||
export const removeBooksFromShelf = command(modifyBooksInShelf, async ({ shelf_id, ...data }): Promise<Bookshelf> => {
|
||||
const { locals } = getRequestEvent();
|
||||
|
||||
const params = createQueryParams(data);
|
||||
|
||||
const response = await locals.api.delete(`/shelves/${shelf_id}/books?${params.toString()}`);
|
||||
|
||||
if (!response.ok) {
|
||||
const message = await response.text();
|
||||
error(response.status, message);
|
||||
}
|
||||
|
||||
return await response.json()
|
||||
});
|
||||
|
||||
|
||||
export const createBookshelf = command(bookshelfCreate, async (data) => {
|
||||
const { locals } = getRequestEvent();
|
||||
|
||||
const response = await locals.api.post(`/shelves`, data)
|
||||
|
||||
if (!response.ok) {
|
||||
const message = await response.text();
|
||||
error(response.status, message);
|
||||
}
|
||||
|
||||
return await response.json()
|
||||
})
|
||||
@@ -0,0 +1,7 @@
|
||||
export * from './auth.remote';
|
||||
export * from './author.remote';
|
||||
export * from './book.remote';
|
||||
export * from './bookshelf.remote';
|
||||
export * from './library.remote';
|
||||
export * from './publisher.remote';
|
||||
export * from './tag.remote';
|
||||
@@ -0,0 +1,30 @@
|
||||
import { form, getRequestEvent, query } from '$app/server';
|
||||
import { libraryCreateSchema, libraryQuerySchema } from '$lib/schema/library';
|
||||
import { createQueryParams } from '$lib/utils';
|
||||
import { error } from '@sveltejs/kit';
|
||||
|
||||
export const listLibraries = query(libraryQuerySchema, async (data) => {
|
||||
const { locals } = getRequestEvent();
|
||||
|
||||
const params = createQueryParams(data);
|
||||
|
||||
const response = await locals.api.get(`/libraries?${params.toString()}`);
|
||||
|
||||
if (!response.ok) error(500, 'An unkown error occurred');
|
||||
|
||||
return await response.json();
|
||||
});
|
||||
|
||||
export const createLibrary = form(libraryCreateSchema, async (data) => {
|
||||
const { locals } = getRequestEvent();
|
||||
|
||||
const response = await locals.api.post(`/libraries`, data);
|
||||
|
||||
if (!response.ok) error(500, 'An unknown error occurred');
|
||||
|
||||
return await response.json();
|
||||
});
|
||||
|
||||
export const deleteLibrary = query('unchecked', async (data) => {
|
||||
throw new Error('Not implemented');
|
||||
});
|
||||
@@ -0,0 +1,16 @@
|
||||
import { getRequestEvent, query } from '$app/server';
|
||||
import { publisherQuerySchema } from '$lib/schema/publisher';
|
||||
import { createQueryParams } from '$lib/utils';
|
||||
import { error } from '@sveltejs/kit';
|
||||
|
||||
export const listPublishers = query(publisherQuerySchema, async (data) => {
|
||||
const { locals } = getRequestEvent();
|
||||
|
||||
const params = createQueryParams(data);
|
||||
|
||||
const response = await locals.api.get(`/publishers?${params.toString()}`);
|
||||
|
||||
if (!response.ok) error(500, 'An unkown error occurred');
|
||||
|
||||
return await response.json();
|
||||
});
|
||||
@@ -0,0 +1,16 @@
|
||||
import { getRequestEvent, query } from '$app/server';
|
||||
import { tagQuerySchema } from '$lib/schema/tag';
|
||||
import { createQueryParams } from '$lib/utils';
|
||||
import { error } from '@sveltejs/kit';
|
||||
|
||||
export const listTags = query(tagQuerySchema, async (data) => {
|
||||
const { locals } = getRequestEvent();
|
||||
|
||||
const params = createQueryParams(data);
|
||||
|
||||
const response = await locals.api.get(`/tags?${params.toString()}`);
|
||||
|
||||
if (!response.ok) error(500, 'An unkown error occurred');
|
||||
|
||||
return await response.json();
|
||||
});
|
||||
Reference in New Issue
Block a user