refactor: sdk init (#9563)

This commit is contained in:
Jason Rasmussen 2024-05-17 16:48:29 -04:00 committed by GitHub
parent 975f2351ec
commit 136bb69bd0
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
9 changed files with 40 additions and 19 deletions

View File

@ -1,4 +1,4 @@
import { defaults, getMyUserInfo, isHttpError } from '@immich/sdk';
import { getMyUserInfo, init, isHttpError } from '@immich/sdk';
import { glob } from 'fast-glob';
import { createHash } from 'node:crypto';
import { createReadStream } from 'node:fs';
@ -46,8 +46,7 @@ export const connect = async (url: string, key: string) => {
// noop
}
defaults.baseUrl = url;
defaults.headers = { 'x-api-key': key };
init({ baseUrl: url, apiKey: key });
const [error] = await withError(getMyUserInfo());
if (isHttpError(error)) {

View File

@ -16,7 +16,6 @@ import {
createPerson,
createSharedLink,
createUser,
defaults,
deleteAssets,
getAllAssets,
getAllJobsStatus,
@ -24,6 +23,7 @@ import {
getConfigDefaults,
login,
searchMetadata,
setBaseUrl,
signUpAdmin,
updateAdminOnboarding,
updateAlbumUser,
@ -255,8 +255,8 @@ export const utils = {
});
},
setApiEndpoint: () => {
defaults.baseUrl = app;
initSdk: () => {
setBaseUrl(app);
},
adminSetup: async (options?: AdminSetupOptions) => {
@ -462,7 +462,7 @@ export const utils = {
},
};
utils.setApiEndpoint();
utils.initSdk();
if (!existsSync(`${testAssetDir}/albums`)) {
throw new Error(

View File

@ -3,7 +3,7 @@ import { utils } from 'src/utils';
test.describe('Registration', () => {
test.beforeAll(() => {
utils.setApiEndpoint();
utils.initSdk();
});
test.beforeEach(async () => {

View File

@ -17,7 +17,7 @@ test.describe('Shared Links', () => {
let sharedLinkPassword: SharedLinkResponseDto;
test.beforeAll(async () => {
utils.setApiEndpoint();
utils.initSdk();
await utils.resetDatabase();
admin = await utils.adminSetup();
asset = await utils.createAsset(admin.accessToken);

View File

@ -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).
```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
defaults.baseUrl = "https://demo.immich.app/api";
defaults.headers = { "x-api-key": API_KEY };
init({ baseUrl: "https://demo.immich.app/api", apiKey: API_KEY });
const user = await getMyUserInfo();
const assets = await getAllAssets({ take: 1000 });

View File

@ -1,2 +1,25 @@
import { defaults } from './fetch-client.js';
export * from './fetch-client.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;
};

View File

@ -5,8 +5,8 @@ import {
AssetJobName,
JobName,
ThumbnailFormat,
defaults,
finishOAuth,
getBaseUrl,
linkOAuthAccount,
startOAuth,
unlinkOAuthAccount,
@ -155,7 +155,7 @@ const createUrl = (path: string, parameters?: Record<string, unknown>) => {
const url = new URL(path, 'https://example.com');
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]) => {

View File

@ -10,7 +10,7 @@ import { createAlbum } from '$lib/utils/album-utils';
import { encodeHTMLSpecialChars } from '$lib/utils/string-utils';
import {
addAssetsToAlbum as addAssets,
defaults,
getBaseUrl,
getDownloadInfo,
updateAssets,
type AlbumResponseDto,
@ -121,7 +121,7 @@ export const downloadArchive = async (fileName: string, options: DownloadInfoDto
// TODO use sdk once it supports progress events
const { data } = await downloadRequest({
method: 'POST',
url: defaults.baseUrl + '/download/archive' + (key ? `?key=${key}` : ''),
url: getBaseUrl() + '/download/archive' + (key ? `?key=${key}` : ''),
data: { assetIds: archive.assetIds },
signal: abort.signal,
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
const { data } = await downloadRequest({
method: 'POST',
url: defaults.baseUrl + `/download/asset/${id}` + (key ? `?key=${key}` : ''),
url: getBaseUrl() + `/download/asset/${id}` + (key ? `?key=${key}` : ''),
signal: abort.signal,
onDownloadProgress: (event) => downloadManager.update(downloadKey, event.loaded, event.total),
});

View File

@ -6,7 +6,7 @@ import { ExecutorQueue } from '$lib/utils/executor-queue';
import {
Action,
checkBulkUpload,
defaults,
getBaseUrl,
getSupportedMediaTypes,
type AssetFileUploadResponseDto,
} from '@immich/sdk';
@ -119,7 +119,7 @@ async function fileUploader(asset: File, albumId: string | undefined = undefined
if (!responseData) {
uploadAssetsStore.updateAsset(deviceAssetId, { message: 'Uploading...' });
const response = await uploadRequest<AssetFileUploadResponseDto>({
url: defaults.baseUrl + '/asset/upload' + (key ? `?key=${key}` : ''),
url: getBaseUrl() + '/asset/upload' + (key ? `?key=${key}` : ''),
data: formData,
onUploadProgress: (event) => uploadAssetsStore.updateProgress(deviceAssetId, event.loaded, event.total),
});