api.ts 4.9 KB

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