Skip to content

Commit 5c86e13

Browse files
refactor(web): combine api and serverApi (immich-app#1833)
1 parent 10cb612 commit 5c86e13

File tree

13 files changed

+32
-31
lines changed

13 files changed

+32
-31
lines changed

web/src/api/api.ts

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { browser } from '$app/environment';
12
import { env } from '$env/dynamic/public';
23
import {
34
AlbumApi,
@@ -56,10 +57,11 @@ class ImmichApi {
5657
}
5758
}
5859

59-
// Browser side (public) API client
60-
export const api = new ImmichApi();
60+
const api = new ImmichApi();
6161

62-
// Server side API client
63-
export const serverApi = new ImmichApi();
64-
const immich_server_url = env.PUBLIC_IMMICH_SERVER_URL || 'http://immich-server:3001';
65-
serverApi.setBaseUrl(immich_server_url);
62+
if (!browser) {
63+
const serverUrl = env.PUBLIC_IMMICH_SERVER_URL || 'http://immich-server:3001';
64+
api.setBaseUrl(serverUrl);
65+
}
66+
67+
export { api };

web/src/routes/+layout.server.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { serverApi } from '@api';
1+
import { api } from '@api';
22
import type { LayoutServerLoad } from './$types';
33

44
export const load = (async ({ cookies }) => {
@@ -8,8 +8,8 @@ export const load = (async ({ cookies }) => {
88
return { user: undefined };
99
}
1010

11-
serverApi.setAccessToken(accessToken);
12-
const { data: user } = await serverApi.userApi.getMyUserInfo();
11+
api.setAccessToken(accessToken);
12+
const { data: user } = await api.userApi.getMyUserInfo();
1313

1414
return { user };
1515
} catch (e) {

web/src/routes/+page.server.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
export const prerender = false;
2-
import { serverApi } from '@api';
2+
import { api } from '@api';
33
import { redirect } from '@sveltejs/kit';
44
import type { PageServerLoad } from './$types';
55

@@ -9,7 +9,7 @@ export const load: PageServerLoad = async ({ parent }) => {
99
throw redirect(302, '/photos');
1010
}
1111

12-
const { data } = await serverApi.userApi.getUserCount(true);
12+
const { data } = await api.userApi.getUserCount(true);
1313

1414
if (data.userCount > 0) {
1515
// Redirect to login page if an admin is already registered.

web/src/routes/admin/server-status/+page.server.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { redirect } from '@sveltejs/kit';
2-
import { serverApi } from '@api';
2+
import { api } from '@api';
33
import type { PageServerLoad } from './$types';
44

55
export const load: PageServerLoad = async ({ parent }) => {
@@ -11,7 +11,7 @@ export const load: PageServerLoad = async ({ parent }) => {
1111
throw redirect(302, '/photos');
1212
}
1313

14-
const { data: allUsers } = await serverApi.userApi.getAllUsers(false);
14+
const { data: allUsers } = await api.userApi.getAllUsers(false);
1515

1616
return {
1717
allUsers,

web/src/routes/admin/user-management/+page.server.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { redirect } from '@sveltejs/kit';
2-
import { serverApi } from '@api';
2+
import { api } from '@api';
33
import type { PageServerLoad } from './$types';
44

55
export const load: PageServerLoad = async ({ parent }) => {
@@ -11,7 +11,7 @@ export const load: PageServerLoad = async ({ parent }) => {
1111
throw redirect(302, '/photos');
1212
}
1313

14-
const { data: allUsers } = await serverApi.userApi.getAllUsers(false);
14+
const { data: allUsers } = await api.userApi.getAllUsers(false);
1515

1616
return {
1717
user,

web/src/routes/albums/+page.server.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { redirect } from '@sveltejs/kit';
22
import type { PageServerLoad } from './$types';
3-
import { serverApi } from '@api';
3+
import { api } from '@api';
44

55
export const load: PageServerLoad = async ({ parent }) => {
66
try {
@@ -10,7 +10,7 @@ export const load: PageServerLoad = async ({ parent }) => {
1010
throw Error('User is not logged in');
1111
}
1212

13-
const { data: albums } = await serverApi.albumApi.getAllAlbums();
13+
const { data: albums } = await api.albumApi.getAllAlbums();
1414

1515
return {
1616
user: user,

web/src/routes/albums/[albumId]/+page.server.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { redirect } from '@sveltejs/kit';
22

33
import type { PageServerLoad } from './$types';
4-
import { serverApi } from '@api';
4+
import { api } from '@api';
55

66
export const load: PageServerLoad = async ({ parent, params }) => {
77
const { user } = await parent();
@@ -13,7 +13,7 @@ export const load: PageServerLoad = async ({ parent, params }) => {
1313
const albumId = params['albumId'];
1414

1515
try {
16-
const { data: album } = await serverApi.albumApi.getAlbumInfo(albumId);
16+
const { data: album } = await api.albumApi.getAlbumInfo(albumId);
1717
return {
1818
album,
1919
meta: {

web/src/routes/auth/login/+page.server.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import { redirect } from '@sveltejs/kit';
22
import type { PageServerLoad } from './$types';
3-
import { serverApi } from '@api';
3+
import { api } from '@api';
44

55
export const load: PageServerLoad = async () => {
6-
const { data } = await serverApi.userApi.getUserCount(true);
6+
const { data } = await api.userApi.getUserCount(true);
77
if (data.userCount === 0) {
88
// Admin not registered
99
throw redirect(302, '/auth/register');

web/src/routes/auth/logout/+server.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
import { json } from '@sveltejs/kit';
2-
import { api, serverApi } from '@api';
2+
import { api } from '@api';
33
import type { RequestHandler } from '@sveltejs/kit';
44

55
export const POST = (async ({ cookies }) => {
66
api.removeAccessToken();
7-
serverApi.removeAccessToken();
87

98
cookies.delete('immich_auth_type', { path: '/' });
109
cookies.delete('immich_access_token', { path: '/' });

web/src/routes/auth/register/+page.server.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import { redirect } from '@sveltejs/kit';
22
import type { PageServerLoad } from './$types';
3-
import { serverApi } from '@api';
3+
import { api } from '@api';
44

55
export const load: PageServerLoad = async () => {
6-
const { data } = await serverApi.userApi.getUserCount(true);
6+
const { data } = await api.userApi.getUserCount(true);
77
if (data.userCount != 0) {
88
// Admin has been registered, redirect to login
99
throw redirect(302, '/auth/login');

0 commit comments

Comments
 (0)