Skip to content

Commit

Permalink
@mb.js/storage: add media url to uploadReference return (#469)
Browse files Browse the repository at this point in the history
  • Loading branch information
rubenmarcus authored Feb 12, 2024
1 parent 447c1bc commit cdf1d16
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 24 deletions.
30 changes: 14 additions & 16 deletions packages/storage/src/uploadReference.browser.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,14 @@ fetchMock.enableMocks();

const FAKE_API_KEY = 'foo';

const ArweaveResponseMock = {
id: '123',
block: 'abc',
name: 'test.txt',
mimeType: 'text/plain',
media_hash: 'odkMrNBZ9rAl9fi6hFy20qh89oDVG91px7iMnZJPxo4',
};

describe('upload tests in browser', () => {
beforeAll(() => {
jest.spyOn(console, 'warn').mockImplementation(() => null);
Expand All @@ -29,10 +37,7 @@ describe('upload tests in browser', () => {
fetchMock.mockResponseOnce(JSON.stringify({
status: 200,
json: () => Promise.resolve({
id: '123',
block: 'abc',
name: 'test.txt',
mimeType: 'text/plain',

}),
} as Response));

Expand All @@ -42,6 +47,7 @@ describe('upload tests in browser', () => {
expect((call as any)[1].headers['mb-api-key']).toBe(FAKE_API_KEY);
expect(result).toEqual({
status: 200,
media_url: 'https://arweave.net/undefined',
});
});

Expand All @@ -55,18 +61,14 @@ describe('upload tests in browser', () => {
fetchMock.mockResponse(JSON.stringify({
status: 200,
// eslint-disable-next-line @typescript-eslint/explicit-function-return-type
json: () => Promise.resolve({
id: '123',
block: 'abc',
name: 'test.txt',
mimeType: 'text/plain',
}),
json: () => Promise.resolve(ArweaveResponseMock),
} as Response));

const result = await uploadReference(referenceObject);

expect(result).toEqual({
status: 200,
media_url: 'https://arweave.net/undefined',
});
});

Expand All @@ -81,18 +83,14 @@ describe('upload tests in browser', () => {
fetchMock.mockResponse(JSON.stringify({
status: 200,
// eslint-disable-next-line @typescript-eslint/explicit-function-return-type
json: () => Promise.resolve({
id: '123',
block: 'abc',
name: 'test.txt',
mimeType: 'text/plain',
}),
json: () => Promise.resolve(ArweaveResponseMock),
} as Response));

const result = await uploadReference(referenceObject);

expect(result).toEqual({
status: 200,
media_url: 'https://arweave.net/undefined',
});
});

Expand Down
11 changes: 3 additions & 8 deletions packages/storage/src/uploads.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export type ArweaveResponse = {
block: string;
name: string;
mimeType: string;
media_url?: string;
};

type HttpError = {
Expand Down Expand Up @@ -120,15 +121,9 @@ export const uploadReference = async (
);
}

const result = (await request.json()) as {
id: string;
block: string;
name: string;
mimeType: string;
media_hash: string;
};
const result = (await request.json());

return result;
return { ...result, media_url: `https://arweave.net/${result.media_hash}` };
} catch (error: unknown) {
console.error('Uploading file to arweave failed');
throw error;
Expand Down

0 comments on commit cdf1d16

Please sign in to comment.