mirror of
https://github.com/toeverything/AFFiNE.git
synced 2026-07-12 15:46:29 +08:00
refactor: move affine utils into @affine/workspace (#2611)
This commit is contained in:
@@ -2,16 +2,15 @@
|
||||
* @vitest-environment node
|
||||
*/
|
||||
import { loginResponseSchema } from '@affine/workspace/affine/login';
|
||||
import type { affineApis as API } from '@affine/workspace/affine/shared';
|
||||
import { beforeAll, describe, expect, it, vi } from 'vitest';
|
||||
|
||||
import type { affineApis as API } from '../apis';
|
||||
|
||||
let affineApis: typeof API;
|
||||
|
||||
beforeAll(async () => {
|
||||
// @ts-expect-error
|
||||
globalThis.window = undefined;
|
||||
affineApis = (await import('../apis')).affineApis;
|
||||
affineApis = (await import('@affine/workspace/affine/shared')).affineApis;
|
||||
});
|
||||
|
||||
describe('apis', () => {
|
||||
|
||||
@@ -1,79 +0,0 @@
|
||||
import { DebugLogger } from '@affine/debug';
|
||||
import { prefixUrl } from '@affine/env';
|
||||
import {
|
||||
createUserApis,
|
||||
createWorkspaceApis,
|
||||
} from '@affine/workspace/affine/api';
|
||||
import { currentAffineUserAtom } from '@affine/workspace/affine/atom';
|
||||
import type { LoginResponse } from '@affine/workspace/affine/login';
|
||||
import {
|
||||
createAffineAuth,
|
||||
parseIdToken,
|
||||
setLoginStorage,
|
||||
} from '@affine/workspace/affine/login';
|
||||
import { rootStore } from '@affine/workspace/atom';
|
||||
|
||||
export const affineAuth = createAffineAuth(prefixUrl);
|
||||
const affineApis = {} as ReturnType<typeof createUserApis> &
|
||||
ReturnType<typeof createWorkspaceApis>;
|
||||
Object.assign(affineApis, createUserApis(prefixUrl));
|
||||
Object.assign(affineApis, createWorkspaceApis(prefixUrl));
|
||||
|
||||
const _debugLogger = new DebugLogger('affine-debug-apis');
|
||||
|
||||
if (!globalThis.AFFINE_APIS) {
|
||||
globalThis.AFFINE_APIS = affineApis;
|
||||
globalThis.setLogin = (response: LoginResponse) => {
|
||||
rootStore.set(currentAffineUserAtom, parseIdToken(response.token));
|
||||
setLoginStorage(response);
|
||||
};
|
||||
const loginMockUser1 = async () => {
|
||||
const user1 = await import('@affine-test/fixtures/built-in-user1.json');
|
||||
const data = await fetch(prefixUrl + 'api/user/token', {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
body: JSON.stringify({
|
||||
type: 'DebugLoginUser',
|
||||
email: user1.email,
|
||||
password: user1.password,
|
||||
}),
|
||||
}).then(r => r.json());
|
||||
setLogin(data);
|
||||
};
|
||||
const loginMockUser2 = async () => {
|
||||
const user2 = await import('@affine-test/fixtures/built-in-user2.json');
|
||||
const data = await fetch(prefixUrl + 'api/user/token', {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
body: JSON.stringify({
|
||||
type: 'DebugLoginUser',
|
||||
email: user2.email,
|
||||
password: user2.password,
|
||||
}),
|
||||
}).then(r => r.json());
|
||||
setLogin(data);
|
||||
};
|
||||
|
||||
globalThis.AFFINE_DEBUG = {
|
||||
loginMockUser1,
|
||||
loginMockUser2,
|
||||
};
|
||||
}
|
||||
|
||||
declare global {
|
||||
// eslint-disable-next-line no-var
|
||||
var setLogin: typeof setLoginStorage;
|
||||
// eslint-disable-next-line no-var
|
||||
var AFFINE_APIS:
|
||||
| undefined
|
||||
| (ReturnType<typeof createUserApis> &
|
||||
ReturnType<typeof createWorkspaceApis>);
|
||||
// eslint-disable-next-line no-var
|
||||
var AFFINE_DEBUG: Record<string, unknown>;
|
||||
}
|
||||
|
||||
export { affineApis };
|
||||
Reference in New Issue
Block a user