api.ts 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. import {
  2. AlbumApi,
  3. LibraryApi,
  4. APIKeyApi,
  5. AssetApi,
  6. AssetApiFp,
  7. AssetJobName,
  8. AuthenticationApi,
  9. Configuration,
  10. ConfigurationParameters,
  11. JobApi,
  12. JobName,
  13. OAuthApi,
  14. PartnerApi,
  15. PersonApi,
  16. SearchApi,
  17. ServerInfoApi,
  18. SharedLinkApi,
  19. SystemConfigApi,
  20. UserApi,
  21. UserApiFp,
  22. } from './open-api';
  23. import { BASE_PATH } from './open-api/base';
  24. import { DUMMY_BASE_URL, toPathString } from './open-api/common';
  25. import type { ApiParams } from './types';
  26. export class ImmichApi {
  27. public albumApi: AlbumApi;
  28. public libraryApi: LibraryApi;
  29. public assetApi: AssetApi;
  30. public authenticationApi: AuthenticationApi;
  31. public jobApi: JobApi;
  32. public keyApi: APIKeyApi;
  33. public oauthApi: OAuthApi;
  34. public partnerApi: PartnerApi;
  35. public searchApi: SearchApi;
  36. public serverInfoApi: ServerInfoApi;
  37. public sharedLinkApi: SharedLinkApi;
  38. public personApi: PersonApi;
  39. public systemConfigApi: SystemConfigApi;
  40. public userApi: UserApi;
  41. private config: Configuration;
  42. private key?: string;
  43. get isSharedLink() {
  44. return !!this.key;
  45. }
  46. constructor(params: ConfigurationParameters) {
  47. this.config = new Configuration(params);
  48. this.albumApi = new AlbumApi(this.config);
  49. this.libraryApi = new LibraryApi(this.config);
  50. this.assetApi = new AssetApi(this.config);
  51. this.authenticationApi = new AuthenticationApi(this.config);
  52. this.jobApi = new JobApi(this.config);
  53. this.keyApi = new APIKeyApi(this.config);
  54. this.oauthApi = new OAuthApi(this.config);
  55. this.partnerApi = new PartnerApi(this.config);
  56. this.searchApi = new SearchApi(this.config);
  57. this.serverInfoApi = new ServerInfoApi(this.config);
  58. this.sharedLinkApi = new SharedLinkApi(this.config);
  59. this.personApi = new PersonApi(this.config);
  60. this.systemConfigApi = new SystemConfigApi(this.config);
  61. this.userApi = new UserApi(this.config);
  62. }
  63. private createUrl(path: string, params?: Record<string, unknown>) {
  64. const searchParams = new URLSearchParams();
  65. for (const key in params) {
  66. const value = params[key];
  67. if (value !== undefined && value !== null) {
  68. searchParams.set(key, value.toString());
  69. }
  70. }
  71. const url = new URL(path, DUMMY_BASE_URL);
  72. url.search = searchParams.toString();
  73. return (this.config.basePath || BASE_PATH) + toPathString(url);
  74. }
  75. public setKey(key: string) {
  76. this.key = key;
  77. }
  78. public getKey(): string | undefined {
  79. return this.key;
  80. }
  81. public setAccessToken(accessToken: string) {
  82. this.config.accessToken = accessToken;
  83. }
  84. public removeAccessToken() {
  85. this.config.accessToken = undefined;
  86. }
  87. public setBaseUrl(baseUrl: string) {
  88. this.config.basePath = baseUrl;
  89. }
  90. public getAssetFileUrl(...[assetId, isThumb, isWeb]: ApiParams<typeof AssetApiFp, 'serveFile'>) {
  91. const path = `/asset/file/${assetId}`;
  92. return this.createUrl(path, { isThumb, isWeb, key: this.getKey() });
  93. }
  94. public getAssetThumbnailUrl(...[assetId, format]: ApiParams<typeof AssetApiFp, 'getAssetThumbnail'>) {
  95. const path = `/asset/thumbnail/${assetId}`;
  96. return this.createUrl(path, { format, key: this.getKey() });
  97. }
  98. public getProfileImageUrl(...[userId]: ApiParams<typeof UserApiFp, 'getProfileImage'>) {
  99. const path = `/user/profile-image/${userId}`;
  100. return this.createUrl(path);
  101. }
  102. public getPeopleThumbnailUrl(personId: string) {
  103. const path = `/person/${personId}/thumbnail`;
  104. return this.createUrl(path);
  105. }
  106. public getJobName(jobName: JobName) {
  107. const names: Record<JobName, string> = {
  108. [JobName.ThumbnailGeneration]: 'Generate Thumbnails',
  109. [JobName.MetadataExtraction]: 'Extract Metadata',
  110. [JobName.Sidecar]: 'Sidecar Metadata',
  111. [JobName.ObjectTagging]: 'Tag Objects',
  112. [JobName.ClipEncoding]: 'Encode Clip',
  113. [JobName.RecognizeFaces]: 'Recognize Faces',
  114. [JobName.VideoConversion]: 'Transcode Videos',
  115. [JobName.StorageTemplateMigration]: 'Storage Template Migration',
  116. [JobName.Migration]: 'Migration',
  117. [JobName.BackgroundTask]: 'Background Tasks',
  118. [JobName.Search]: 'Search',
  119. [JobName.Library]: 'Library',
  120. };
  121. return names[jobName];
  122. }
  123. public getAssetJobName(job: AssetJobName) {
  124. const names: Record<AssetJobName, string> = {
  125. [AssetJobName.RefreshMetadata]: 'Refresh metadata',
  126. [AssetJobName.RegenerateThumbnail]: 'Refresh thumbnails',
  127. [AssetJobName.TranscodeVideo]: 'Refresh encoded videos',
  128. };
  129. return names[job];
  130. }
  131. public getAssetJobMessage(job: AssetJobName) {
  132. const messages: Record<AssetJobName, string> = {
  133. [AssetJobName.RefreshMetadata]: 'Refreshing metadata',
  134. [AssetJobName.RegenerateThumbnail]: `Regenerating thumbnails`,
  135. [AssetJobName.TranscodeVideo]: `Refreshing encoded video`,
  136. };
  137. return messages[job];
  138. }
  139. }
  140. export const api = new ImmichApi({ basePath: '/api' });