From 0f6d49d821afa72c4a61e94e7a3a1377357dc727 Mon Sep 17 00:00:00 2001 From: einaralex Date: Tue, 12 Mar 2024 13:39:51 +0000 Subject: [PATCH] fix: upload document --- apps/examples/sdk-react/src/app/app.tsx | 25 +++++++++++++++++++++++++ libs/sdk/src/client.ts | 14 +++++--------- libs/sdk/test/client.test.ts | 12 ++++++------ 3 files changed, 36 insertions(+), 15 deletions(-) diff --git a/apps/examples/sdk-react/src/app/app.tsx b/apps/examples/sdk-react/src/app/app.tsx index 3178c7e..310fddc 100644 --- a/apps/examples/sdk-react/src/app/app.tsx +++ b/apps/examples/sdk-react/src/app/app.tsx @@ -44,6 +44,30 @@ export function App() { } }; fetchData(); + + const uploadFile = async (file: File) => { + console.log( + '%c file to upload', + 'color:white; padding: 30px; background-color: darkgreen', + file + ); + + const fileUploaded = await monerium?.uploadSupportingDocument( + file as File + ); + console.log( + '%c fileUploaded', + 'color:white; padding: 30px; background-color: darkgreen', + fileUploaded + ); + }; + + document + ?.getElementById('fileInput') + ?.addEventListener('change', function (event) { + const file = (event?.target as any)?.files?.[0]; + uploadFile(file); + }); }, [monerium, isAuthorized]); return ( @@ -64,6 +88,7 @@ export function App() { )}

{authCtx?.name || authCtx?.email}

+ {isAuthorized && } ); } diff --git a/libs/sdk/src/client.ts b/libs/sdk/src/client.ts index 4efd945..4ce78f1 100644 --- a/libs/sdk/src/client.ts +++ b/libs/sdk/src/client.ts @@ -342,16 +342,12 @@ export class MoneriumClient { * {@link https://monerium.dev/api-docs#operation/supporting-document} */ uploadSupportingDocument(document: File): Promise { - const searchParams = urlEncoded( - document as unknown as Record - ); + const formData = new FormData(); + formData.append('file', document as unknown as Blob); - return this.#api( - 'post', - 'files/supporting-document', - searchParams, - true - ); + return rest(`${this.#env.api}/files`, 'post', formData, { + Authorization: this.#authorizationHeader || '', + }); } // -- Helper Methods diff --git a/libs/sdk/test/client.test.ts b/libs/sdk/test/client.test.ts index 12ec576..f37a06e 100644 --- a/libs/sdk/test/client.test.ts +++ b/libs/sdk/test/client.test.ts @@ -14,6 +14,7 @@ import { STORAGE_CODE_VERIFIER, STORAGE_REFRESH_TOKEN, } from '../src/constants'; +import { rfc3339 } from '../src/utils'; import { Currency, Order, PaymentStandard } from '../src/types'; import { @@ -254,7 +255,6 @@ describe('MoneriumClient', () => { expect.objectContaining({ // id: '4b208818-44e3-11ed-adac-b2efc0e6677d', chain: 'ethereum', - network: 'sepolia', address: PUBLIC_KEY, }), ]) @@ -417,7 +417,9 @@ describe('MoneriumClient', () => { }); const date = new Date().toISOString(); - const placeOrderMessage = `Send EUR 10 to GR1601101250000000012300695 at ${date}`; + const rfc3339date = rfc3339(new Date(date)); + + const placeOrderMessage = `Send EUR 10 to GR1601101250000000012300695 at ${rfc3339date}`; const placeOrderSignatureHash = '0x23bf7e1b240d238b13cb293673c3419915402bb34435af62850b1d8e63f82c564fb73ab19691cf248594423dd01e441bb2ccb38ce2e2ecc514dfc3075bea829e1c'; @@ -440,10 +442,9 @@ describe('MoneriumClient', () => { memo: 'Powered by Monerium SDK', chainId: 11155111, chain: 'ethereum', - network: 'sepolia', }) .catch((err) => { - expect(err.message).toBe('Invalid signature'); + expect(err.errors?.signature).toBe('invalid signature'); }); }); @@ -479,11 +480,10 @@ describe('MoneriumClient', () => { message: placeOrderMessage, memo: 'Powered by Monerium SDK', chain: 'ethereum', - network: 'sepolia', } as any /** to bypass typeerror for chain and network */ ) .catch((err) => { - expect(err.message).toBe('Timestamp is expired'); + expect(err.errors?.message).toBe('timestamp is expired'); }); });