From 4524aa0d06da328a8d41212601278acdd7e978ce Mon Sep 17 00:00:00 2001 From: Michel Heusschen <59014050+michelheusschen@users.noreply.github.com> Date: Sun, 14 May 2023 04:52:29 +0200 Subject: [PATCH] refactor(web): use ImmichApi to create urls (#2435) --- web/src/api/api.ts | 19 +++++++++++++++---- web/src/api/types.ts | 12 ++++++++++++ web/src/api/utils.ts | 13 ------------- .../album-page/__tests__/album-card.spec.ts | 5 ----- .../components/album-page/album-card.svelte | 7 +++---- .../asset-viewer/album-list-item.svelte | 5 +++-- .../asset-viewer/detail-panel.svelte | 5 +++-- .../asset-viewer/video-viewer.svelte | 6 +++--- .../navigation-bar/account-info-panel.svelte | 5 ++--- .../navigation-bar/navigation-bar.svelte | 2 +- .../sharedlinks-page/shared-link-card.svelte | 10 ++++++++-- web/src/lib/utils/asset-utils.ts | 10 +--------- .../routes/(user)/share/[key]/+page.server.ts | 5 ++--- 13 files changed, 53 insertions(+), 51 deletions(-) create mode 100644 web/src/api/types.ts diff --git a/web/src/api/api.ts b/web/src/api/api.ts index 28406ef6b8..15f8b1e2bd 100644 --- a/web/src/api/api.ts +++ b/web/src/api/api.ts @@ -2,6 +2,7 @@ import { AlbumApi, APIKeyApi, AssetApi, + AssetApiFp, AuthenticationApi, Configuration, ConfigurationParameters, @@ -11,11 +12,12 @@ import { ServerInfoApi, ShareApi, SystemConfigApi, - ThumbnailFormat, - UserApi + UserApi, + UserApiFp } from './open-api'; import { BASE_PATH } from './open-api/base'; import { DUMMY_BASE_URL, toPathString } from './open-api/common'; +import type { ApiParams } from './types'; export class ImmichApi { public userApi: UserApi; @@ -75,15 +77,24 @@ export class ImmichApi { this.config.basePath = baseUrl; } - public getAssetFileUrl(assetId: string, isThumb?: boolean, isWeb?: boolean, key?: string) { + public getAssetFileUrl( + ...[assetId, isThumb, isWeb, key]: ApiParams + ) { const path = `/asset/file/${assetId}`; return this.createUrl(path, { isThumb, isWeb, key }); } - public getAssetThumbnailUrl(assetId: string, format?: ThumbnailFormat, key?: string) { + public getAssetThumbnailUrl( + ...[assetId, format, key]: ApiParams + ) { const path = `/asset/thumbnail/${assetId}`; return this.createUrl(path, { format, key }); } + + public getProfileImageUrl(...[userId]: ApiParams) { + const path = `/user/profile-image/${userId}`; + return this.createUrl(path); + } } export const api = new ImmichApi({ basePath: '/api' }); diff --git a/web/src/api/types.ts b/web/src/api/types.ts new file mode 100644 index 0000000000..0bb28cca88 --- /dev/null +++ b/web/src/api/types.ts @@ -0,0 +1,12 @@ +import type { Configuration } from './open-api'; + +/* eslint-disable @typescript-eslint/no-explicit-any */ +export type ApiFp = (configuration: Configuration) => Record any>; + +export type OmitLast = T extends readonly [...infer U, any?] + ? U + : [...T]; + +export type ApiParams> = OmitLast< + Parameters[K]> +>; diff --git a/web/src/api/utils.ts b/web/src/api/utils.ts index cb550c31a7..42e04f07e0 100644 --- a/web/src/api/utils.ts +++ b/web/src/api/utils.ts @@ -2,19 +2,6 @@ import { AxiosError, AxiosPromise } from 'axios'; import { api } from './api'; import { UserResponseDto } from './open-api'; -const _basePath = '/api'; - -export function getFileUrl(assetId: string, isThumb?: boolean, isWeb?: boolean, key?: string) { - const urlObj = new URL(`${window.location.origin}${_basePath}/asset/file/${assetId}`); - - if (isThumb !== undefined && isThumb !== null) - urlObj.searchParams.append('isThumb', `${isThumb}`); - if (isWeb !== undefined && isWeb !== null) urlObj.searchParams.append('isWeb', `${isWeb}`); - - if (key !== undefined && key !== null) urlObj.searchParams.append('key', key); - return urlObj.href; -} - export type ApiError = AxiosError<{ message: string }>; export const oauth = { diff --git a/web/src/lib/components/album-page/__tests__/album-card.spec.ts b/web/src/lib/components/album-page/__tests__/album-card.spec.ts index 411d3fbfe3..92de385415 100644 --- a/web/src/lib/components/album-page/__tests__/album-card.spec.ts +++ b/web/src/lib/components/album-page/__tests__/album-card.spec.ts @@ -79,11 +79,6 @@ describe('AlbumCard component', () => { const albumImgElement = sut.getByTestId('album-image'); const albumNameElement = sut.getByTestId('album-name'); const albumDetailsElement = sut.getByTestId('album-details'); - // TODO: is this expected? - expect(albumImgElement).toHaveAttribute( - 'src', - '/api/asset/thumbnail/thumbnailIdOne?format=WEBP' - ); expect(albumImgElement).toHaveAttribute('alt', album.id); await waitFor(() => expect(albumImgElement).toHaveAttribute('src', thumbnailUrl)); diff --git a/web/src/lib/components/album-page/album-card.svelte b/web/src/lib/components/album-page/album-card.svelte index b92391067d..7eedfdb273 100644 --- a/web/src/lib/components/album-page/album-card.svelte +++ b/web/src/lib/components/album-page/album-card.svelte @@ -23,10 +23,9 @@ export let isSharingView = false; export let user: UserResponseDto; - let imageData = `/api/asset/thumbnail/${album.albumThumbnailAssetId}?format=${ThumbnailFormat.Webp}`; - if (!album.albumThumbnailAssetId) { - imageData = noThumbnailUrl; - } + $: imageData = album.albumThumbnailAssetId + ? api.getAssetThumbnailUrl(album.albumThumbnailAssetId, ThumbnailFormat.Webp) + : noThumbnailUrl; const dispatchClick = createEventDispatcher(); const dispatchShowContextMenu = createEventDispatcher(); diff --git a/web/src/lib/components/asset-viewer/album-list-item.svelte b/web/src/lib/components/asset-viewer/album-list-item.svelte index b39fbb1e70..f313d4bdff 100644 --- a/web/src/lib/components/asset-viewer/album-list-item.svelte +++ b/web/src/lib/components/asset-viewer/album-list-item.svelte @@ -1,5 +1,5 @@