Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: sdk init #9563

Merged
merged 1 commit into from
May 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions cli/src/utils.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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)) {
Expand Down
8 changes: 4 additions & 4 deletions e2e/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@ import {
createPerson,
createSharedLink,
createUser,
defaults,
deleteAssets,
getAllAssets,
getAllJobsStatus,
getAssetInfo,
getConfigDefaults,
login,
searchMetadata,
setBaseUrl,
signUpAdmin,
updateAdminOnboarding,
updateAlbumUser,
Expand Down Expand Up @@ -255,8 +255,8 @@ export const utils = {
});
},

setApiEndpoint: () => {
defaults.baseUrl = app;
initSdk: () => {
setBaseUrl(app);
},

adminSetup: async (options?: AdminSetupOptions) => {
Expand Down Expand Up @@ -462,7 +462,7 @@ export const utils = {
},
};

utils.setApiEndpoint();
utils.initSdk();

if (!existsSync(`${testAssetDir}/albums`)) {
throw new Error(
Expand Down
2 changes: 1 addition & 1 deletion e2e/src/web/specs/auth.e2e-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { utils } from 'src/utils';

test.describe('Registration', () => {
test.beforeAll(() => {
utils.setApiEndpoint();
utils.initSdk();
});

test.beforeEach(async () => {
Expand Down
2 changes: 1 addition & 1 deletion e2e/src/web/specs/shared-link.e2e-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
5 changes: 2 additions & 3 deletions open-api/typescript-sdk/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 });
Expand Down
23 changes: 23 additions & 0 deletions open-api/typescript-sdk/src/index.ts
Original file line number Diff line number Diff line change
@@ -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;
};
4 changes: 2 additions & 2 deletions web/src/lib/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import {
AssetJobName,
JobName,
ThumbnailFormat,
defaults,
finishOAuth,
getBaseUrl,
linkOAuthAccount,
startOAuth,
unlinkOAuthAccount,
Expand Down Expand Up @@ -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]) => {
Expand Down
6 changes: 3 additions & 3 deletions web/src/lib/utils/asset-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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),
Expand Down Expand Up @@ -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),
});
Expand Down
4 changes: 2 additions & 2 deletions web/src/lib/utils/file-uploader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { ExecutorQueue } from '$lib/utils/executor-queue';
import {
Action,
checkBulkUpload,
defaults,
getBaseUrl,
getSupportedMediaTypes,
type AssetFileUploadResponseDto,
} from '@immich/sdk';
Expand Down Expand Up @@ -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),
});
Expand Down
Loading