refactor(web): use ImmichApi to create urls (#2435)

This commit is contained in:
Michel Heusschen 2023-05-14 04:52:29 +02:00 committed by GitHub
parent 15fa8250cb
commit 4524aa0d06
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
13 changed files with 53 additions and 51 deletions

View file

@ -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<typeof AssetApiFp, 'serveFile'>
) {
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<typeof AssetApiFp, 'getAssetThumbnail'>
) {
const path = `/asset/thumbnail/${assetId}`;
return this.createUrl(path, { format, key });
}
public getProfileImageUrl(...[userId]: ApiParams<typeof UserApiFp, 'getProfileImage'>) {
const path = `/user/profile-image/${userId}`;
return this.createUrl(path);
}
}
export const api = new ImmichApi({ basePath: '/api' });

12
web/src/api/types.ts Normal file
View file

@ -0,0 +1,12 @@
import type { Configuration } from './open-api';
/* eslint-disable @typescript-eslint/no-explicit-any */
export type ApiFp = (configuration: Configuration) => Record<any, (...args: any) => any>;
export type OmitLast<T extends readonly unknown[]> = T extends readonly [...infer U, any?]
? U
: [...T];
export type ApiParams<F extends ApiFp, K extends keyof ReturnType<F>> = OmitLast<
Parameters<ReturnType<F>[K]>
>;

View file

@ -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 = {