mirror of
https://github.com/toeverything/AFFiNE.git
synced 2026-07-19 19:16:29 +08:00
refactor: move affine utils into @affine/workspace (#2611)
This commit is contained in:
@@ -21,6 +21,12 @@ import { uuidv4, Workspace as BlockSuiteWorkspace } from '@blocksuite/store';
|
||||
import { beforeEach, describe, expect, test, vi } from 'vitest';
|
||||
import { WebSocket } from 'ws';
|
||||
|
||||
declare module '@blocksuite/store' {
|
||||
interface PageMeta {
|
||||
foo: string;
|
||||
}
|
||||
}
|
||||
|
||||
// @ts-expect-error
|
||||
globalThis.WebSocket = WebSocket;
|
||||
|
||||
|
||||
@@ -0,0 +1,70 @@
|
||||
import { prefixUrl } from '@affine/env';
|
||||
|
||||
import { rootStore } from '../atom';
|
||||
import { createUserApis, createWorkspaceApis } from './api/index';
|
||||
import { currentAffineUserAtom } from './atom';
|
||||
import type { LoginResponse } from './login';
|
||||
import { createAffineAuth, parseIdToken, setLoginStorage } from './login';
|
||||
|
||||
export const affineAuth = createAffineAuth(prefixUrl);
|
||||
const affineApis = {} as ReturnType<typeof createUserApis> &
|
||||
ReturnType<typeof createWorkspaceApis>;
|
||||
Object.assign(affineApis, createUserApis(prefixUrl));
|
||||
Object.assign(affineApis, createWorkspaceApis(prefixUrl));
|
||||
|
||||
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