1
0
Fork 0
mirror of https://github.com/immich-app/immich.git synced 2025-01-21 03:02:44 +01:00

refactor(web): combine api and serverApi (#1833)

This commit is contained in:
Michel Heusschen 2023-02-23 03:49:13 +01:00 committed by GitHub
parent 10cb612fb1
commit 5c86e13239
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
13 changed files with 32 additions and 31 deletions

View file

@ -1,3 +1,4 @@
import { browser } from '$app/environment';
import { env } from '$env/dynamic/public'; import { env } from '$env/dynamic/public';
import { import {
AlbumApi, AlbumApi,
@ -56,10 +57,11 @@ class ImmichApi {
} }
} }
// Browser side (public) API client const api = new ImmichApi();
export const api = new ImmichApi();
// Server side API client if (!browser) {
export const serverApi = new ImmichApi(); const serverUrl = env.PUBLIC_IMMICH_SERVER_URL || 'http://immich-server:3001';
const immich_server_url = env.PUBLIC_IMMICH_SERVER_URL || 'http://immich-server:3001'; api.setBaseUrl(serverUrl);
serverApi.setBaseUrl(immich_server_url); }
export { api };

View file

@ -1,4 +1,4 @@
import { serverApi } from '@api'; import { api } from '@api';
import type { LayoutServerLoad } from './$types'; import type { LayoutServerLoad } from './$types';
export const load = (async ({ cookies }) => { export const load = (async ({ cookies }) => {
@ -8,8 +8,8 @@ export const load = (async ({ cookies }) => {
return { user: undefined }; return { user: undefined };
} }
serverApi.setAccessToken(accessToken); api.setAccessToken(accessToken);
const { data: user } = await serverApi.userApi.getMyUserInfo(); const { data: user } = await api.userApi.getMyUserInfo();
return { user }; return { user };
} catch (e) { } catch (e) {

View file

@ -1,5 +1,5 @@
export const prerender = false; export const prerender = false;
import { serverApi } from '@api'; import { api } from '@api';
import { redirect } from '@sveltejs/kit'; import { redirect } from '@sveltejs/kit';
import type { PageServerLoad } from './$types'; import type { PageServerLoad } from './$types';
@ -9,7 +9,7 @@ export const load: PageServerLoad = async ({ parent }) => {
throw redirect(302, '/photos'); throw redirect(302, '/photos');
} }
const { data } = await serverApi.userApi.getUserCount(true); const { data } = await api.userApi.getUserCount(true);
if (data.userCount > 0) { if (data.userCount > 0) {
// Redirect to login page if an admin is already registered. // Redirect to login page if an admin is already registered.

View file

@ -1,5 +1,5 @@
import { redirect } from '@sveltejs/kit'; import { redirect } from '@sveltejs/kit';
import { serverApi } from '@api'; import { api } from '@api';
import type { PageServerLoad } from './$types'; import type { PageServerLoad } from './$types';
export const load: PageServerLoad = async ({ parent }) => { export const load: PageServerLoad = async ({ parent }) => {
@ -11,7 +11,7 @@ export const load: PageServerLoad = async ({ parent }) => {
throw redirect(302, '/photos'); throw redirect(302, '/photos');
} }
const { data: allUsers } = await serverApi.userApi.getAllUsers(false); const { data: allUsers } = await api.userApi.getAllUsers(false);
return { return {
allUsers, allUsers,

View file

@ -1,5 +1,5 @@
import { redirect } from '@sveltejs/kit'; import { redirect } from '@sveltejs/kit';
import { serverApi } from '@api'; import { api } from '@api';
import type { PageServerLoad } from './$types'; import type { PageServerLoad } from './$types';
export const load: PageServerLoad = async ({ parent }) => { export const load: PageServerLoad = async ({ parent }) => {
@ -11,7 +11,7 @@ export const load: PageServerLoad = async ({ parent }) => {
throw redirect(302, '/photos'); throw redirect(302, '/photos');
} }
const { data: allUsers } = await serverApi.userApi.getAllUsers(false); const { data: allUsers } = await api.userApi.getAllUsers(false);
return { return {
user, user,

View file

@ -1,6 +1,6 @@
import { redirect } from '@sveltejs/kit'; import { redirect } from '@sveltejs/kit';
import type { PageServerLoad } from './$types'; import type { PageServerLoad } from './$types';
import { serverApi } from '@api'; import { api } from '@api';
export const load: PageServerLoad = async ({ parent }) => { export const load: PageServerLoad = async ({ parent }) => {
try { try {
@ -10,7 +10,7 @@ export const load: PageServerLoad = async ({ parent }) => {
throw Error('User is not logged in'); throw Error('User is not logged in');
} }
const { data: albums } = await serverApi.albumApi.getAllAlbums(); const { data: albums } = await api.albumApi.getAllAlbums();
return { return {
user: user, user: user,

View file

@ -1,7 +1,7 @@
import { redirect } from '@sveltejs/kit'; import { redirect } from '@sveltejs/kit';
import type { PageServerLoad } from './$types'; import type { PageServerLoad } from './$types';
import { serverApi } from '@api'; import { api } from '@api';
export const load: PageServerLoad = async ({ parent, params }) => { export const load: PageServerLoad = async ({ parent, params }) => {
const { user } = await parent(); const { user } = await parent();
@ -13,7 +13,7 @@ export const load: PageServerLoad = async ({ parent, params }) => {
const albumId = params['albumId']; const albumId = params['albumId'];
try { try {
const { data: album } = await serverApi.albumApi.getAlbumInfo(albumId); const { data: album } = await api.albumApi.getAlbumInfo(albumId);
return { return {
album, album,
meta: { meta: {

View file

@ -1,9 +1,9 @@
import { redirect } from '@sveltejs/kit'; import { redirect } from '@sveltejs/kit';
import type { PageServerLoad } from './$types'; import type { PageServerLoad } from './$types';
import { serverApi } from '@api'; import { api } from '@api';
export const load: PageServerLoad = async () => { export const load: PageServerLoad = async () => {
const { data } = await serverApi.userApi.getUserCount(true); const { data } = await api.userApi.getUserCount(true);
if (data.userCount === 0) { if (data.userCount === 0) {
// Admin not registered // Admin not registered
throw redirect(302, '/auth/register'); throw redirect(302, '/auth/register');

View file

@ -1,10 +1,9 @@
import { json } from '@sveltejs/kit'; import { json } from '@sveltejs/kit';
import { api, serverApi } from '@api'; import { api } from '@api';
import type { RequestHandler } from '@sveltejs/kit'; import type { RequestHandler } from '@sveltejs/kit';
export const POST = (async ({ cookies }) => { export const POST = (async ({ cookies }) => {
api.removeAccessToken(); api.removeAccessToken();
serverApi.removeAccessToken();
cookies.delete('immich_auth_type', { path: '/' }); cookies.delete('immich_auth_type', { path: '/' });
cookies.delete('immich_access_token', { path: '/' }); cookies.delete('immich_access_token', { path: '/' });

View file

@ -1,9 +1,9 @@
import { redirect } from '@sveltejs/kit'; import { redirect } from '@sveltejs/kit';
import type { PageServerLoad } from './$types'; import type { PageServerLoad } from './$types';
import { serverApi } from '@api'; import { api } from '@api';
export const load: PageServerLoad = async () => { export const load: PageServerLoad = async () => {
const { data } = await serverApi.userApi.getUserCount(true); const { data } = await api.userApi.getUserCount(true);
if (data.userCount != 0) { if (data.userCount != 0) {
// Admin has been registered, redirect to login // Admin has been registered, redirect to login
throw redirect(302, '/auth/login'); throw redirect(302, '/auth/login');

View file

@ -2,7 +2,7 @@ export const prerender = false;
import { error } from '@sveltejs/kit'; import { error } from '@sveltejs/kit';
import { getThumbnailUrl } from '$lib/utils/asset-utils'; import { getThumbnailUrl } from '$lib/utils/asset-utils';
import { serverApi, ThumbnailFormat } from '@api'; import { api, ThumbnailFormat } from '@api';
import type { PageServerLoad } from './$types'; import type { PageServerLoad } from './$types';
import featurePanelUrl from '$lib/assets/feature-panel.png'; import featurePanelUrl from '$lib/assets/feature-panel.png';
@ -12,7 +12,7 @@ export const load: PageServerLoad = async ({ params, parent }) => {
const { key } = params; const { key } = params;
try { try {
const { data: sharedLink } = await serverApi.shareApi.getMySharedLink({ params: { key } }); const { data: sharedLink } = await api.shareApi.getMySharedLink({ params: { key } });
const assetCount = sharedLink.assets.length; const assetCount = sharedLink.assets.length;
const assetId = sharedLink.album?.albumThumbnailAssetId || sharedLink.assets[0]?.id; const assetId = sharedLink.album?.albumThumbnailAssetId || sharedLink.assets[0]?.id;

View file

@ -1,13 +1,13 @@
export const prerender = false; export const prerender = false;
import { error } from '@sveltejs/kit'; import { error } from '@sveltejs/kit';
import { serverApi } from '@api'; import { api } from '@api';
import type { PageServerLoad } from './$types'; import type { PageServerLoad } from './$types';
export const load: PageServerLoad = async ({ params }) => { export const load: PageServerLoad = async ({ params }) => {
try { try {
const { key, assetId } = params; const { key, assetId } = params;
const { data: asset } = await serverApi.assetApi.getAssetById(assetId, { const { data: asset } = await api.assetApi.getAssetById(assetId, {
params: { key } params: { key }
}); });

View file

@ -1,7 +1,7 @@
import { redirect } from '@sveltejs/kit'; import { redirect } from '@sveltejs/kit';
export const prerender = false; export const prerender = false;
import { serverApi } from '@api'; import { api } from '@api';
import type { PageServerLoad } from './$types'; import type { PageServerLoad } from './$types';
export const load: PageServerLoad = async ({ parent }) => { export const load: PageServerLoad = async ({ parent }) => {
@ -11,7 +11,7 @@ export const load: PageServerLoad = async ({ parent }) => {
throw redirect(302, '/auth/login'); throw redirect(302, '/auth/login');
} }
const { data: sharedAlbums } = await serverApi.albumApi.getAllAlbums(true); const { data: sharedAlbums } = await api.albumApi.getAllAlbums(true);
return { return {
user: user, user: user,