Skip to content

Commit

Permalink
fix(commerce): make base url correctly fallback to default base url
Browse files Browse the repository at this point in the history
  • Loading branch information
roushou committed Jul 8, 2024
1 parent 2beda11 commit 45d0bba
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 32 deletions.
5 changes: 5 additions & 0 deletions .changeset/fresh-schools-turn.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@coinbasejs/commerce": patch
---

fix(commerce): make base url correctly fallback to default base url
24 changes: 15 additions & 9 deletions packages/commerce/src/charge.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import * as http from "@coinbasejs/utils/http";
import type { RequestConfig } from "./client";
import { BASE_URL } from "./constants";

export type GetChargeResponse = {
Expand All @@ -8,11 +9,12 @@ export type GetChargeResponse = {

export async function getCharge(
id: string,
apiKey: string,
config: RequestConfig,
): Promise<GetChargeResponse> {
return await http.get(`${BASE_URL}/charges/${id}`, {
const baseUrl = config.baseUrl ?? BASE_URL;
return await http.get(`${baseUrl}/charges/${id}`, {
headers: {
"X-CC-Api-Key": apiKey,
"X-CC-Api-Key": config.apiKey,
},
});
}
Expand All @@ -22,10 +24,13 @@ export type GetChargesResponse = {
warnings: string[];
};

export async function getCharges(apiKey: string): Promise<GetChargesResponse> {
return await http.get(`${BASE_URL}/charges`, {
export async function getCharges(
config: RequestConfig,
): Promise<GetChargesResponse> {
const baseUrl = config.baseUrl ?? BASE_URL;
return await http.get(`${baseUrl}/charges`, {
headers: {
"X-CC-Api-Key": apiKey,
"X-CC-Api-Key": config.apiKey,
},
});
}
Expand All @@ -49,11 +54,12 @@ export type CreateChargeResponse = {

export async function createCharge(
parameters: CreateChargeParameters,
apiKey: string,
config: RequestConfig,
): Promise<CreateChargeResponse> {
return await http.post(`${BASE_URL}/charges`, {
const baseUrl = config.baseUrl ?? BASE_URL;
return await http.post(`${baseUrl}/charges`, {
headers: {
"X-CC-Api-Key": apiKey,
"X-CC-Api-Key": config.apiKey,
},
body: JSON.stringify(parameters),
});
Expand Down
22 changes: 13 additions & 9 deletions packages/commerce/src/checkout.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import * as http from "@coinbasejs/utils/http";
import type { RequestConfig } from "./client";
import { BASE_URL } from "./constants";

type GetCheckoutResponse = {
Expand All @@ -8,11 +9,12 @@ type GetCheckoutResponse = {

export async function getCheckout(
id: string,
apiKey: string,
config: RequestConfig,
): Promise<GetCheckoutResponse> {
return await http.get(`${BASE_URL}/checkouts/${id}`, {
const baseUrl = config.baseUrl ?? BASE_URL;
return await http.get(`${baseUrl}/checkouts/${id}`, {
headers: {
"X-CC-Api-Key": apiKey,
"X-CC-Api-Key": config.apiKey,
},
});
}
Expand All @@ -23,11 +25,12 @@ type GetCheckoutsResponse = {
};

export async function getCheckouts(
apiKey: string,
config: RequestConfig,
): Promise<GetCheckoutsResponse> {
return await http.get(`${BASE_URL}/checkouts`, {
const baseUrl = config.baseUrl ?? BASE_URL;
return await http.get(`${baseUrl}/checkouts`, {
headers: {
"X-CC-Api-Key": apiKey,
"X-CC-Api-Key": config.apiKey,
},
});
}
Expand Down Expand Up @@ -59,11 +62,12 @@ export type CreateCheckoutResponse = {

export async function createCheckout(
parameters: CreateCheckoutParameters,
apiKey: string,
config: RequestConfig,
): Promise<CreateCheckoutResponse> {
return await http.post(`${BASE_URL}/checkouts`, {
const baseUrl = config.baseUrl ?? BASE_URL;
return await http.post(`${baseUrl}/checkouts`, {
headers: {
"X-CC-Api-Key": apiKey,
"X-CC-Api-Key": config.apiKey,
},
body: JSON.stringify(parameters),
});
Expand Down
43 changes: 35 additions & 8 deletions packages/commerce/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,38 +38,65 @@ export function createClient(config: ClientConfig): Client {
const client: Client = {
__url: url,
createCharge: async (parameters) => {
const response = await createCharge(parameters, config.apiKey);
const response = await createCharge(parameters, {
apiKey: config.apiKey,
baseUrl: url,
});
return response.data;
},
createCheckout: async (parameters) => {
const response = await createCheckout(parameters, config.apiKey);
const response = await createCheckout(parameters, {
apiKey: config.apiKey,
baseUrl: url,
});
return response.data;
},
getCharge: async (id) => {
const response = await getCharge(id, config.apiKey);
const response = await getCharge(id, {
apiKey: config.apiKey,
baseUrl: url,
});
return response.data;
},
getCharges: async () => {
const response = await getCharges(config.apiKey);
const response = await getCharges({
apiKey: config.apiKey,
baseUrl: url,
});
return response.data;
},
getCheckout: async (id) => {
const response = await getCheckout(id, config.apiKey);
const response = await getCheckout(id, {
apiKey: config.apiKey,
baseUrl: url,
});
return response.data;
},
getCheckouts: async () => {
const response = await getCheckouts(config.apiKey);
const response = await getCheckouts({
apiKey: config.apiKey,
baseUrl: url,
});
return response.data;
},
getEvent: async (id) => {
const response = await getEvent(id, config.apiKey);
const response = await getEvent(id, {
apiKey: config.apiKey,
});
return response.data;
},
getEvents: async () => {
const response = await getEvents(config.apiKey);
const response = await getEvents({
apiKey: config.apiKey,
});
return response.data;
},
};

return client;
}

export type RequestConfig = {
apiKey: string;
baseUrl?: string;
};
17 changes: 11 additions & 6 deletions packages/commerce/src/events.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import * as http from "@coinbasejs/utils/http";
import type { Charge } from "./charge";
import type { Checkout } from "./checkout";
import type { RequestConfig } from "./client";
import { BASE_URL } from "./constants";

export type GetEventResponse = {
Expand All @@ -10,11 +11,12 @@ export type GetEventResponse = {

export async function getEvent(
id: string,
apiKey: string,
config: RequestConfig,
): Promise<GetEventResponse> {
return await http.get(`${BASE_URL}/events/${id}`, {
const baseUrl = config.baseUrl ?? BASE_URL;
return await http.get(`${baseUrl}/events/${id}`, {
headers: {
"X-CC-Api-Key": apiKey,
"X-CC-Api-Key": config.apiKey,
},
});
}
Expand All @@ -34,10 +36,13 @@ export type GetEventsResponse = {
data: Array<Event>;
};

export async function getEvents(apiKey: string): Promise<GetEventsResponse> {
return await http.get(`${BASE_URL}/events`, {
export async function getEvents(
config: RequestConfig,
): Promise<GetEventsResponse> {
const baseUrl = config.baseUrl ?? BASE_URL;
return await http.get(`${baseUrl}/events`, {
headers: {
"X-CC-Api-Key": apiKey,
"X-CC-Api-Key": config.apiKey,
},
});
}
Expand Down

0 comments on commit 45d0bba

Please sign in to comment.