Skip to content

Commit

Permalink
Prepend API_URL parameter to all methods sending API requests
Browse files Browse the repository at this point in the history
  • Loading branch information
sisou committed Aug 12, 2024
1 parent 9792d7c commit 718a641
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 19 deletions.
22 changes: 10 additions & 12 deletions OasisApi.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
let API_URL: string | undefined = 'https://api-sandbox.nimiqoasis.com/v1';

type OasisError = {
type: string,
title: string,
Expand Down Expand Up @@ -184,12 +182,8 @@ export type RawHtlc<TStatus = HtlcStatus> = Omit<Htlc<TStatus>, 'asset' | 'expir
expires: string,
};

export function init(url: string) {
if (!url) throw new Error('url must be provided');
API_URL = url;
}

async function api<T>(
API_URL: string,
path: string,
method: 'POST' | 'GET' | 'DELETE',
body?: Record<string, unknown>,
Expand All @@ -214,6 +208,7 @@ async function api<T>(
}

export async function createHtlc(
API_URL: string,
contract: Pick<RawHtlc, 'asset' | 'amount' | 'beneficiary' | 'hash' | 'preimage' | 'expires'> & {
includeFee: boolean,
},
Expand Down Expand Up @@ -262,6 +257,7 @@ export async function createHtlc(
headers['Authorization'] = `Bearer ${tokens.authorization}`;

const htlc = await api<RawHtlc<HtlcStatus.PENDING>>(
API_URL,
'/htlc',
'POST',
contract,
Expand All @@ -270,12 +266,13 @@ export async function createHtlc(
return convertHtlc(htlc);
}

export async function getHtlc(id: string): Promise<Htlc> {
const htlc = await api<RawHtlc<HtlcStatus>>(`/htlc/${id}`, 'GET');
export async function getHtlc(API_URL: string, id: string): Promise<Htlc> {
const htlc = await api<RawHtlc<HtlcStatus>>(API_URL, `/htlc/${id}`, 'GET');
return convertHtlc(htlc);
}

export async function settleHtlc(
API_URL: string,
id: string,
secret: string,
settlementJWS: string,
Expand All @@ -301,6 +298,7 @@ export async function settleHtlc(
headers['X-SMS-API-Token'] = tokens.smsApi;

const htlc = await api<RawHtlc<HtlcStatus.SETTLED>>(
API_URL,
`/htlc/${id}/settle`,
'POST',
{
Expand All @@ -312,7 +310,7 @@ export async function settleHtlc(
return convertHtlc(htlc);
}

export async function sandboxMockClearHtlc(id: string): Promise<boolean> {
export async function sandboxMockClearHtlc(API_URL: string, id: string): Promise<boolean> {
if (!API_URL) throw new Error('API URL not set, call init() first');

return fetch(`${API_URL}/mock/clear/${id}`, {
Expand All @@ -326,8 +324,8 @@ export async function sandboxMockClearHtlc(id: string): Promise<boolean> {
});
}

export async function exchangeAuthorizationToken(token: string): Promise<string> {
const response = await api<{ token: string }>('/auth', 'POST', undefined, {
export async function exchangeAuthorizationToken(API_URL: string, token: string): Promise<string> {
const response = await api<{ token: string }>(API_URL, '/auth', 'POST', undefined, {
'Authorization': `Bearer ${token}`,
});
return response.token;
Expand Down
12 changes: 5 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,9 @@ https://www.npmjs.com/package/@nimiq/oasis-api

## API

```ts
function init(url: string)
```

```ts
async function createHtlc(
API_URL: string,
contract: {
asset: Asset,
amount: number,
Expand All @@ -39,11 +36,12 @@ async function createHtlc(
```

```ts
async function getHtlc(id: string): Promise<Htlc>
async function getHtlc(API_URL: string, id: string): Promise<Htlc>
```

```ts
async function settleHtlc(
API_URL: string,
id: string,
secret: string,
settlementJWS: string,
Expand All @@ -52,11 +50,11 @@ async function settleHtlc(
```

```ts
async function sandboxMockClearHtlc(id: string): Promise<boolean>
async function sandboxMockClearHtlc(API_URL: string, id: string): Promise<boolean>
```

```ts
async function exchangeAuthorizationToken(token: string): Promise<string>
async function exchangeAuthorizationToken(API_URL: string, token: string): Promise<string>
```

## Helper methods
Expand Down

0 comments on commit 718a641

Please sign in to comment.