Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

improve: handle trailing and duplicate / in mint urls #87

Merged
merged 1 commit into from
Sep 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 12 additions & 10 deletions src/CashuMint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {
SplitResponse
} from './model/types/index.js';
import request from './request.js';
import { isObj } from './utils.js';
import { isObj, joinUrls } from './utils.js';

/**
* Class represents Cashu Mint API. This class contains Lower level functions that are implemented by CashuWallet.
Expand All @@ -31,7 +31,7 @@ class CashuMint {
* @param mintUrl
*/
public static async getInfo(mintUrl: string): Promise<GetInfoResponse> {
return request<GetInfoResponse>({ endpoint: `${mintUrl}/info` });
return request<GetInfoResponse>({ endpoint: joinUrls(mintUrl, 'info') });
}
/**
* fetches mints info at the /info endpoint
Expand All @@ -46,7 +46,9 @@ class CashuMint {
* @returns the mint will create and return a Lightning invoice for the specified amount
*/
public static async requestMint(mintUrl: string, amount: number): Promise<RequestMintResponse> {
return request<RequestMintResponse>({ endpoint: `${mintUrl}/mint?amount=${amount}` });
return request<RequestMintResponse>({
endpoint: `${joinUrls(mintUrl, 'mint')}?amount=${amount}`
});
}

/**
Expand All @@ -70,7 +72,7 @@ class CashuMint {
hash: string
) {
const data = await request<{ promises: Array<SerializedBlindedSignature> }>({
endpoint: `${mintUrl}/mint?hash=${hash}`,
endpoint: `${joinUrls(mintUrl, 'mint')}?hash=${hash}`,
method: 'POST',
requestBody: payloads
});
Expand Down Expand Up @@ -102,7 +104,7 @@ class CashuMint {
keysetId = keysetId.replace(/\//g, '_').replace(/\+/g, '-');
}
return request<MintKeys>({
endpoint: `${mintUrl}/keys${keysetId ? `/${keysetId}` : ''}`
endpoint: keysetId ? joinUrls(mintUrl, 'keys', keysetId) : joinUrls(mintUrl, 'keys')
});
}
/**
Expand All @@ -119,7 +121,7 @@ class CashuMint {
* @returns all the mints past and current keysets.
*/
public static async getKeySets(mintUrl: string): Promise<{ keysets: Array<string> }> {
return request<{ keysets: Array<string> }>({ endpoint: `${mintUrl}/keysets` });
return request<{ keysets: Array<string> }>({ endpoint: joinUrls(mintUrl, 'keysets') });
}

/**
Expand All @@ -138,7 +140,7 @@ class CashuMint {
*/
public static async split(mintUrl: string, splitPayload: SplitPayload): Promise<SplitResponse> {
const data = await request<SplitResponse>({
endpoint: `${mintUrl}/split`,
endpoint: joinUrls(mintUrl, 'split'),
method: 'POST',
requestBody: splitPayload
});
Expand All @@ -165,7 +167,7 @@ class CashuMint {
*/
public static async melt(mintUrl: string, meltPayload: MeltPayload): Promise<MeltResponse> {
const data = await request<MeltResponse>({
endpoint: `${mintUrl}/melt`,
endpoint: joinUrls(mintUrl, 'melt'),
method: 'POST',
requestBody: meltPayload
});
Expand Down Expand Up @@ -199,7 +201,7 @@ class CashuMint {
checkfeesPayload: { pr: string }
): Promise<{ fee: number }> {
const data = await request<{ fee: number }>({
endpoint: `${mintUrl}/checkfees`,
endpoint: joinUrls(mintUrl, 'checkfees'),
method: 'POST',
requestBody: checkfeesPayload
});
Expand Down Expand Up @@ -230,7 +232,7 @@ class CashuMint {
checkPayload: CheckSpendablePayload
): Promise<CheckSpendableResponse> {
const data = await request<CheckSpendableResponse>({
endpoint: `${mintUrl}/check`,
endpoint: joinUrls(mintUrl, 'check'),
method: 'POST',
requestBody: checkPayload
});
Expand Down
5 changes: 5 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,11 @@ export function checkResponse(data: { error?: string; detail?: string }) {
throw new Error(data.detail);
}
}

export function joinUrls(...parts: string[]): string {
return parts.map((part) => part.replace(/(^\/+|\/+$)/g, '')).join('/');
}

export {
hexToNumber,
splitAmount,
Expand Down
10 changes: 10 additions & 0 deletions test/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -256,4 +256,14 @@ describe('test cleanToken', () => {
expect(result.token[0].proofs[1].id).toBe('I2yN+iRYfkzT');
expect(result.token[0].proofs[2].id).toBe('test');
});
test('testing joining urls', () => {
const mint_url = 'https://8333.space:3338';
const info_url = utils.joinUrls(mint_url, 'info');

expect(info_url).toBe('https://8333.space:3338/info');

const mint_url_trailing_slash = 'https://8333.space:3338/';
const mint_info_url = utils.joinUrls(mint_url_trailing_slash, 'info');
expect(mint_info_url).toBe('https://8333.space:3338/info');
});
});