From 2655a53aa9c0e6283f6a21083221765f05bbdd39 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juan=20Jos=C3=A9=20Ram=C3=ADrez?= Date: Wed, 23 Oct 2024 17:49:27 -0300 Subject: [PATCH] feat(request): accept uuid directly on getRequestLinkDetails --- src/request.ts | 24 +++++++++++++++--------- test/unit/request.test.ts | 14 ++++++++++++++ 2 files changed, 29 insertions(+), 9 deletions(-) diff --git a/src/request.ts b/src/request.ts index 58659ac..84bbd2c 100644 --- a/src/request.ts +++ b/src/request.ts @@ -22,11 +22,17 @@ export interface ICreateRequestLinkProps { APIKey?: string } -export interface IGetRequestLinkDetailsProps { - link: string +export type IGetRequestLinkDetailsProps = { APIKey?: string apiUrl?: string -} +} & ( + | { + link: string + } + | { + uuid: string + } +) export interface IPrepareRequestLinkFulfillmentTransactionProps { recipientAddress: string @@ -154,12 +160,12 @@ export async function createRequestLink({ } } -export async function getRequestLinkDetails({ - link, - APIKey, - apiUrl = 'https://api.peanut.to/', -}: IGetRequestLinkDetailsProps): Promise { - const uuid = getUuidFromLink(link) +export async function getRequestLinkDetails( + props: IGetRequestLinkDetailsProps +): Promise { + const { APIKey, apiUrl = 'https://api.peanut.to/' } = props + + const uuid = 'uuid' in props ? props.uuid : getUuidFromLink(props.link) const apiResponse = await fetch(normalizePath(`${apiUrl}/request-links/${uuid}`), { method: 'GET', diff --git a/test/unit/request.test.ts b/test/unit/request.test.ts index ccc72e6..f77950b 100644 --- a/test/unit/request.test.ts +++ b/test/unit/request.test.ts @@ -60,6 +60,20 @@ describe('Request', () => { expect(mockFetch).toHaveBeenCalledWith(`https://api.peanut.to/request-links/${linkUuid}`, expect.anything()) }) + + it('should accept uuid directly', async () => { + const linkUuid = 'ee3b904d-9809-4b97-b82b-34de1d1a7314' + mockFetch.mockResolvedValueOnce({ + json: jest.fn().mockResolvedValueOnce({ + link: `https://peanut.to/request/pay?id=${linkUuid}`, + }), + status: 200, + }) + + await getRequestLinkDetails({ uuid: linkUuid }) + + expect(mockFetch).toHaveBeenCalledWith(`https://api.peanut.to/request-links/${linkUuid}`, expect.anything()) + }) }) describe('submitRequestLinkFulfillment', () => {