From 7af17248b8dae9758f0e27792f8f4c8cb890b54d Mon Sep 17 00:00:00 2001 From: dayvvo Date: Thu, 25 Apr 2024 02:43:28 +0100 Subject: [PATCH 1/2] fix: handle trailing slash for mint url in CashuMint class --- src/CashuMint.ts | 4 ++-- src/utils.ts | 6 +++++- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/src/CashuMint.ts b/src/CashuMint.ts index 32bbe16b..ca93666b 100644 --- a/src/CashuMint.ts +++ b/src/CashuMint.ts @@ -19,7 +19,7 @@ import type { MeltQuoteResponse } from './model/types/index.js'; import request from './request.js'; -import { isObj, joinUrls } from './utils.js'; +import { isObj, joinUrls, sanitizeUrl } from './utils.js'; /** * Class represents Cashu Mint API. This class contains Lower level functions that are implemented by CashuWallet. @@ -32,7 +32,7 @@ class CashuMint { constructor(private _mintUrl: string, private _customRequest?: typeof request) {} get mintUrl() { - return this._mintUrl; + return sanitizeUrl(this._mintUrl); } /** diff --git a/src/utils.ts b/src/utils.ts index d9d28b5f..2ad744ca 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -189,7 +189,11 @@ export function checkResponse(data: { error?: string; detail?: string }) { export function joinUrls(...parts: Array): string { return parts.map((part) => part.replace(/(^\/+|\/+$)/g, '')).join('/'); -} +}; + +export function sanitizeUrl(url:string): string{ + return url.replace(/\/$/, ''); +}; export function decodeInvoice(bolt11Invoice: string): InvoiceData { const invoiceData: InvoiceData = {} as InvoiceData; From 0fd4266e1b27d6b07a33dbd327f0dbe74a043ae6 Mon Sep 17 00:00:00 2001 From: gandlaf21 Date: Mon, 20 May 2024 12:10:11 +0900 Subject: [PATCH 2/2] format and sanitize url in constructor --- src/CashuMint.ts | 7 +++++-- src/utils.ts | 6 +++--- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/src/CashuMint.ts b/src/CashuMint.ts index ca93666b..f2700d0f 100644 --- a/src/CashuMint.ts +++ b/src/CashuMint.ts @@ -29,10 +29,13 @@ class CashuMint { * @param _mintUrl requires mint URL to create this object * @param _customRequest if passed, use custom request implementation for network communication with the mint */ - constructor(private _mintUrl: string, private _customRequest?: typeof request) {} + constructor(private _mintUrl: string, private _customRequest?: typeof request) { + this._mintUrl = sanitizeUrl(_mintUrl); + this._customRequest = _customRequest; + } get mintUrl() { - return sanitizeUrl(this._mintUrl); + return this._mintUrl; } /** diff --git a/src/utils.ts b/src/utils.ts index 2ad744ca..dca0ee63 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -189,11 +189,11 @@ export function checkResponse(data: { error?: string; detail?: string }) { export function joinUrls(...parts: Array): string { return parts.map((part) => part.replace(/(^\/+|\/+$)/g, '')).join('/'); -}; +} -export function sanitizeUrl(url:string): string{ +export function sanitizeUrl(url: string): string { return url.replace(/\/$/, ''); -}; +} export function decodeInvoice(bolt11Invoice: string): InvoiceData { const invoiceData: InvoiceData = {} as InvoiceData;