mirror of
https://github.com/immich-app/immich.git
synced 2024-11-15 18:08:48 -07:00
refactor: sdk init (#9563)
This commit is contained in:
parent
975f2351ec
commit
136bb69bd0
@ -1,4 +1,4 @@
|
|||||||
import { defaults, getMyUserInfo, isHttpError } from '@immich/sdk';
|
import { getMyUserInfo, init, isHttpError } from '@immich/sdk';
|
||||||
import { glob } from 'fast-glob';
|
import { glob } from 'fast-glob';
|
||||||
import { createHash } from 'node:crypto';
|
import { createHash } from 'node:crypto';
|
||||||
import { createReadStream } from 'node:fs';
|
import { createReadStream } from 'node:fs';
|
||||||
@ -46,8 +46,7 @@ export const connect = async (url: string, key: string) => {
|
|||||||
// noop
|
// noop
|
||||||
}
|
}
|
||||||
|
|
||||||
defaults.baseUrl = url;
|
init({ baseUrl: url, apiKey: key });
|
||||||
defaults.headers = { 'x-api-key': key };
|
|
||||||
|
|
||||||
const [error] = await withError(getMyUserInfo());
|
const [error] = await withError(getMyUserInfo());
|
||||||
if (isHttpError(error)) {
|
if (isHttpError(error)) {
|
||||||
|
@ -16,7 +16,6 @@ import {
|
|||||||
createPerson,
|
createPerson,
|
||||||
createSharedLink,
|
createSharedLink,
|
||||||
createUser,
|
createUser,
|
||||||
defaults,
|
|
||||||
deleteAssets,
|
deleteAssets,
|
||||||
getAllAssets,
|
getAllAssets,
|
||||||
getAllJobsStatus,
|
getAllJobsStatus,
|
||||||
@ -24,6 +23,7 @@ import {
|
|||||||
getConfigDefaults,
|
getConfigDefaults,
|
||||||
login,
|
login,
|
||||||
searchMetadata,
|
searchMetadata,
|
||||||
|
setBaseUrl,
|
||||||
signUpAdmin,
|
signUpAdmin,
|
||||||
updateAdminOnboarding,
|
updateAdminOnboarding,
|
||||||
updateAlbumUser,
|
updateAlbumUser,
|
||||||
@ -255,8 +255,8 @@ export const utils = {
|
|||||||
});
|
});
|
||||||
},
|
},
|
||||||
|
|
||||||
setApiEndpoint: () => {
|
initSdk: () => {
|
||||||
defaults.baseUrl = app;
|
setBaseUrl(app);
|
||||||
},
|
},
|
||||||
|
|
||||||
adminSetup: async (options?: AdminSetupOptions) => {
|
adminSetup: async (options?: AdminSetupOptions) => {
|
||||||
@ -462,7 +462,7 @@ export const utils = {
|
|||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
utils.setApiEndpoint();
|
utils.initSdk();
|
||||||
|
|
||||||
if (!existsSync(`${testAssetDir}/albums`)) {
|
if (!existsSync(`${testAssetDir}/albums`)) {
|
||||||
throw new Error(
|
throw new Error(
|
||||||
|
@ -3,7 +3,7 @@ import { utils } from 'src/utils';
|
|||||||
|
|
||||||
test.describe('Registration', () => {
|
test.describe('Registration', () => {
|
||||||
test.beforeAll(() => {
|
test.beforeAll(() => {
|
||||||
utils.setApiEndpoint();
|
utils.initSdk();
|
||||||
});
|
});
|
||||||
|
|
||||||
test.beforeEach(async () => {
|
test.beforeEach(async () => {
|
||||||
|
@ -17,7 +17,7 @@ test.describe('Shared Links', () => {
|
|||||||
let sharedLinkPassword: SharedLinkResponseDto;
|
let sharedLinkPassword: SharedLinkResponseDto;
|
||||||
|
|
||||||
test.beforeAll(async () => {
|
test.beforeAll(async () => {
|
||||||
utils.setApiEndpoint();
|
utils.initSdk();
|
||||||
await utils.resetDatabase();
|
await utils.resetDatabase();
|
||||||
admin = await utils.adminSetup();
|
admin = await utils.adminSetup();
|
||||||
asset = await utils.createAsset(admin.accessToken);
|
asset = await utils.createAsset(admin.accessToken);
|
||||||
|
@ -13,12 +13,11 @@ npm i --save @immich/sdk
|
|||||||
For a more detailed example, check out the [`@immich/cli`](https://github.com/immich-app/immich/tree/main/cli).
|
For a more detailed example, check out the [`@immich/cli`](https://github.com/immich-app/immich/tree/main/cli).
|
||||||
|
|
||||||
```typescript
|
```typescript
|
||||||
import { defaults, getAllAlbums, getAllAssets, getMyUserInfo } from "@immich/sdk";
|
import { getAllAlbums, getAllAssets, getMyUserInfo, init } from "@immich/sdk";
|
||||||
|
|
||||||
const API_KEY = "<API_KEY>"; // process.env.IMMICH_API_KEY
|
const API_KEY = "<API_KEY>"; // process.env.IMMICH_API_KEY
|
||||||
|
|
||||||
defaults.baseUrl = "https://demo.immich.app/api";
|
init({ baseUrl: "https://demo.immich.app/api", apiKey: API_KEY });
|
||||||
defaults.headers = { "x-api-key": API_KEY };
|
|
||||||
|
|
||||||
const user = await getMyUserInfo();
|
const user = await getMyUserInfo();
|
||||||
const assets = await getAllAssets({ take: 1000 });
|
const assets = await getAllAssets({ take: 1000 });
|
||||||
|
@ -1,2 +1,25 @@
|
|||||||
|
import { defaults } from './fetch-client.js';
|
||||||
|
|
||||||
export * from './fetch-client.js';
|
export * from './fetch-client.js';
|
||||||
export * from './fetch-errors.js';
|
export * from './fetch-errors.js';
|
||||||
|
|
||||||
|
export interface InitOptions {
|
||||||
|
baseUrl: string;
|
||||||
|
apiKey: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
export const init = ({ baseUrl, apiKey }: InitOptions) => {
|
||||||
|
setBaseUrl(baseUrl);
|
||||||
|
setApiKey(apiKey);
|
||||||
|
};
|
||||||
|
|
||||||
|
export const getBaseUrl = () => defaults.baseUrl;
|
||||||
|
|
||||||
|
export const setBaseUrl = (baseUrl: string) => {
|
||||||
|
defaults.baseUrl = baseUrl;
|
||||||
|
};
|
||||||
|
|
||||||
|
export const setApiKey = (apiKey: string) => {
|
||||||
|
defaults.headers = defaults.headers || {};
|
||||||
|
defaults.headers['x-api-key'] = apiKey;
|
||||||
|
};
|
||||||
|
@ -5,8 +5,8 @@ import {
|
|||||||
AssetJobName,
|
AssetJobName,
|
||||||
JobName,
|
JobName,
|
||||||
ThumbnailFormat,
|
ThumbnailFormat,
|
||||||
defaults,
|
|
||||||
finishOAuth,
|
finishOAuth,
|
||||||
|
getBaseUrl,
|
||||||
linkOAuthAccount,
|
linkOAuthAccount,
|
||||||
startOAuth,
|
startOAuth,
|
||||||
unlinkOAuthAccount,
|
unlinkOAuthAccount,
|
||||||
@ -155,7 +155,7 @@ const createUrl = (path: string, parameters?: Record<string, unknown>) => {
|
|||||||
const url = new URL(path, 'https://example.com');
|
const url = new URL(path, 'https://example.com');
|
||||||
url.search = searchParameters.toString();
|
url.search = searchParameters.toString();
|
||||||
|
|
||||||
return defaults.baseUrl + url.pathname + url.search + url.hash;
|
return getBaseUrl() + url.pathname + url.search + url.hash;
|
||||||
};
|
};
|
||||||
|
|
||||||
export const getAssetFileUrl = (...[assetId, isWeb, isThumb]: [string, boolean, boolean]) => {
|
export const getAssetFileUrl = (...[assetId, isWeb, isThumb]: [string, boolean, boolean]) => {
|
||||||
|
@ -10,7 +10,7 @@ import { createAlbum } from '$lib/utils/album-utils';
|
|||||||
import { encodeHTMLSpecialChars } from '$lib/utils/string-utils';
|
import { encodeHTMLSpecialChars } from '$lib/utils/string-utils';
|
||||||
import {
|
import {
|
||||||
addAssetsToAlbum as addAssets,
|
addAssetsToAlbum as addAssets,
|
||||||
defaults,
|
getBaseUrl,
|
||||||
getDownloadInfo,
|
getDownloadInfo,
|
||||||
updateAssets,
|
updateAssets,
|
||||||
type AlbumResponseDto,
|
type AlbumResponseDto,
|
||||||
@ -121,7 +121,7 @@ export const downloadArchive = async (fileName: string, options: DownloadInfoDto
|
|||||||
// TODO use sdk once it supports progress events
|
// TODO use sdk once it supports progress events
|
||||||
const { data } = await downloadRequest({
|
const { data } = await downloadRequest({
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
url: defaults.baseUrl + '/download/archive' + (key ? `?key=${key}` : ''),
|
url: getBaseUrl() + '/download/archive' + (key ? `?key=${key}` : ''),
|
||||||
data: { assetIds: archive.assetIds },
|
data: { assetIds: archive.assetIds },
|
||||||
signal: abort.signal,
|
signal: abort.signal,
|
||||||
onDownloadProgress: (event) => downloadManager.update(downloadKey, event.loaded),
|
onDownloadProgress: (event) => downloadManager.update(downloadKey, event.loaded),
|
||||||
@ -177,7 +177,7 @@ export const downloadFile = async (asset: AssetResponseDto) => {
|
|||||||
// TODO use sdk once it supports progress events
|
// TODO use sdk once it supports progress events
|
||||||
const { data } = await downloadRequest({
|
const { data } = await downloadRequest({
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
url: defaults.baseUrl + `/download/asset/${id}` + (key ? `?key=${key}` : ''),
|
url: getBaseUrl() + `/download/asset/${id}` + (key ? `?key=${key}` : ''),
|
||||||
signal: abort.signal,
|
signal: abort.signal,
|
||||||
onDownloadProgress: (event) => downloadManager.update(downloadKey, event.loaded, event.total),
|
onDownloadProgress: (event) => downloadManager.update(downloadKey, event.loaded, event.total),
|
||||||
});
|
});
|
||||||
|
@ -6,7 +6,7 @@ import { ExecutorQueue } from '$lib/utils/executor-queue';
|
|||||||
import {
|
import {
|
||||||
Action,
|
Action,
|
||||||
checkBulkUpload,
|
checkBulkUpload,
|
||||||
defaults,
|
getBaseUrl,
|
||||||
getSupportedMediaTypes,
|
getSupportedMediaTypes,
|
||||||
type AssetFileUploadResponseDto,
|
type AssetFileUploadResponseDto,
|
||||||
} from '@immich/sdk';
|
} from '@immich/sdk';
|
||||||
@ -119,7 +119,7 @@ async function fileUploader(asset: File, albumId: string | undefined = undefined
|
|||||||
if (!responseData) {
|
if (!responseData) {
|
||||||
uploadAssetsStore.updateAsset(deviceAssetId, { message: 'Uploading...' });
|
uploadAssetsStore.updateAsset(deviceAssetId, { message: 'Uploading...' });
|
||||||
const response = await uploadRequest<AssetFileUploadResponseDto>({
|
const response = await uploadRequest<AssetFileUploadResponseDto>({
|
||||||
url: defaults.baseUrl + '/asset/upload' + (key ? `?key=${key}` : ''),
|
url: getBaseUrl() + '/asset/upload' + (key ? `?key=${key}` : ''),
|
||||||
data: formData,
|
data: formData,
|
||||||
onUploadProgress: (event) => uploadAssetsStore.updateProgress(deviceAssetId, event.loaded, event.total),
|
onUploadProgress: (event) => uploadAssetsStore.updateProgress(deviceAssetId, event.loaded, event.total),
|
||||||
});
|
});
|
||||||
|
Loading…
Reference in New Issue
Block a user