feat: add getUsage api (#1744)
This commit is contained in:
@@ -15,9 +15,11 @@ import { faker } from '@faker-js/faker';
|
|||||||
import { beforeEach, describe, expect, test, vi } from 'vitest';
|
import { beforeEach, describe, expect, test, vi } from 'vitest';
|
||||||
|
|
||||||
import {
|
import {
|
||||||
|
createUserApis,
|
||||||
createWorkspaceApis,
|
createWorkspaceApis,
|
||||||
createWorkspaceResponseSchema,
|
createWorkspaceResponseSchema,
|
||||||
RequestError,
|
RequestError,
|
||||||
|
usageResponseSchema,
|
||||||
} from '../api';
|
} from '../api';
|
||||||
import {
|
import {
|
||||||
createAffineAuth,
|
createAffineAuth,
|
||||||
@@ -27,6 +29,7 @@ import {
|
|||||||
} from '../login';
|
} from '../login';
|
||||||
|
|
||||||
let workspaceApis: ReturnType<typeof createWorkspaceApis>;
|
let workspaceApis: ReturnType<typeof createWorkspaceApis>;
|
||||||
|
let userApis: ReturnType<typeof createUserApis>;
|
||||||
let affineAuth: ReturnType<typeof createAffineAuth>;
|
let affineAuth: ReturnType<typeof createAffineAuth>;
|
||||||
let statusApis: ReturnType<typeof createStatusApis>;
|
let statusApis: ReturnType<typeof createStatusApis>;
|
||||||
|
|
||||||
@@ -45,6 +48,7 @@ beforeEach(() => {
|
|||||||
|
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
affineAuth = createAffineAuth('http://localhost:3000/');
|
affineAuth = createAffineAuth('http://localhost:3000/');
|
||||||
|
userApis = createUserApis('http://localhost:3000/');
|
||||||
workspaceApis = createWorkspaceApis('http://localhost:3000/');
|
workspaceApis = createWorkspaceApis('http://localhost:3000/');
|
||||||
statusApis = createStatusApis('http://localhost:3000/');
|
statusApis = createStatusApis('http://localhost:3000/');
|
||||||
});
|
});
|
||||||
@@ -203,13 +207,14 @@ describe('api', () => {
|
|||||||
const binary = Workspace.Y.encodeStateAsUpdate(workspace.doc);
|
const binary = Workspace.Y.encodeStateAsUpdate(workspace.doc);
|
||||||
const data = await workspaceApis.createWorkspace(new Blob([binary]));
|
const data = await workspaceApis.createWorkspace(new Blob([binary]));
|
||||||
createWorkspaceResponseSchema.parse(data);
|
createWorkspaceResponseSchema.parse(data);
|
||||||
|
const workspaceId = data.id;
|
||||||
const blobId = await workspaceApis.uploadBlob(
|
const blobId = await workspaceApis.uploadBlob(
|
||||||
data.id,
|
workspaceId,
|
||||||
imageBuffer,
|
imageBuffer,
|
||||||
'image/png'
|
'image/png'
|
||||||
);
|
);
|
||||||
expect(blobId).toBeTypeOf('string');
|
expect(blobId).toBeTypeOf('string');
|
||||||
const arrayBuffer = await workspaceApis.getBlob(blobId);
|
const arrayBuffer = await workspaceApis.getBlob(workspaceId, blobId);
|
||||||
expect(arrayBuffer).toBeInstanceOf(ArrayBuffer);
|
expect(arrayBuffer).toBeInstanceOf(ArrayBuffer);
|
||||||
expect(arrayBuffer.byteLength).toEqual(imageBuffer.byteLength);
|
expect(arrayBuffer.byteLength).toEqual(imageBuffer.byteLength);
|
||||||
expect(Buffer.from(arrayBuffer)).toEqual(imageBuffer);
|
expect(Buffer.from(arrayBuffer)).toEqual(imageBuffer);
|
||||||
@@ -237,4 +242,31 @@ describe('api', () => {
|
|||||||
timeout: 30000,
|
timeout: 30000,
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|
||||||
|
test(
|
||||||
|
'usage',
|
||||||
|
async () => {
|
||||||
|
const usageResponse = await userApis.getUsage();
|
||||||
|
usageResponseSchema.parse(usageResponse);
|
||||||
|
const id = await createWorkspace(workspaceApis);
|
||||||
|
const path = require.resolve('@affine-test/fixtures/smile.png');
|
||||||
|
const imageBuffer = await readFile(path);
|
||||||
|
const blobId = await workspaceApis.uploadBlob(
|
||||||
|
id,
|
||||||
|
imageBuffer,
|
||||||
|
'image/png'
|
||||||
|
);
|
||||||
|
const buffer = await workspaceApis.getBlob(id, blobId);
|
||||||
|
expect(buffer.byteLength).toEqual(imageBuffer.byteLength);
|
||||||
|
const newUsageResponse = await userApis.getUsage();
|
||||||
|
usageResponseSchema.parse(newUsageResponse);
|
||||||
|
expect(usageResponse.blob_usage.usage).not.equals(
|
||||||
|
newUsageResponse.blob_usage.usage
|
||||||
|
);
|
||||||
|
expect(newUsageResponse.blob_usage.usage).equals(96);
|
||||||
|
},
|
||||||
|
{
|
||||||
|
timeout: 30000,
|
||||||
|
}
|
||||||
|
);
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -39,8 +39,27 @@ export interface GetUserByEmailParams {
|
|||||||
workspace_id: string;
|
workspace_id: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export const usageResponseSchema = z.object({
|
||||||
|
blob_usage: z.object({
|
||||||
|
usage: z.number(),
|
||||||
|
max_usage: z.number(),
|
||||||
|
}),
|
||||||
|
});
|
||||||
|
|
||||||
|
export type UsageResponse = z.infer<typeof usageResponseSchema>;
|
||||||
|
|
||||||
export function createUserApis(prefixUrl = '/') {
|
export function createUserApis(prefixUrl = '/') {
|
||||||
return {
|
return {
|
||||||
|
getUsage: async (): Promise<UsageResponse> => {
|
||||||
|
const auth = getLoginStorage();
|
||||||
|
assertExists(auth);
|
||||||
|
return fetch(prefixUrl + 'api/resource/usage', {
|
||||||
|
method: 'GET',
|
||||||
|
headers: {
|
||||||
|
Authorization: auth.token,
|
||||||
|
},
|
||||||
|
}).then(r => r.json());
|
||||||
|
},
|
||||||
getUserByEmail: async (
|
getUserByEmail: async (
|
||||||
params: GetUserByEmailParams
|
params: GetUserByEmailParams
|
||||||
): Promise<User[] | null> => {
|
): Promise<User[] | null> => {
|
||||||
@@ -309,7 +328,7 @@ export function createWorkspaceApis(prefixUrl = '/') {
|
|||||||
if (mb > 10) {
|
if (mb > 10) {
|
||||||
throw new RequestError(MessageCode.blobTooLarge);
|
throw new RequestError(MessageCode.blobTooLarge);
|
||||||
}
|
}
|
||||||
return fetch(prefixUrl + 'api/blob', {
|
return fetch(prefixUrl + `api/workspace/${workspaceId}/blob`, {
|
||||||
method: 'PUT',
|
method: 'PUT',
|
||||||
body: arrayBuffer,
|
body: arrayBuffer,
|
||||||
headers: {
|
headers: {
|
||||||
@@ -318,10 +337,13 @@ export function createWorkspaceApis(prefixUrl = '/') {
|
|||||||
},
|
},
|
||||||
}).then(r => r.text());
|
}).then(r => r.text());
|
||||||
},
|
},
|
||||||
getBlob: async (blobId: string): Promise<ArrayBuffer> => {
|
getBlob: async (
|
||||||
|
workspaceId: string,
|
||||||
|
blobId: string
|
||||||
|
): Promise<ArrayBuffer> => {
|
||||||
const auth = getLoginStorage();
|
const auth = getLoginStorage();
|
||||||
assertExists(auth);
|
assertExists(auth);
|
||||||
return fetch(prefixUrl + `api/blob/${blobId}`, {
|
return fetch(prefixUrl + `api/workspace/${workspaceId}/blob/${blobId}`, {
|
||||||
method: 'GET',
|
method: 'GET',
|
||||||
headers: {
|
headers: {
|
||||||
Authorization: auth.token,
|
Authorization: auth.token,
|
||||||
|
|||||||
Reference in New Issue
Block a user