From 45d0bbafaff686225b9902ebf51e58e0b895becd Mon Sep 17 00:00:00 2001 From: roushou Date: Mon, 8 Jul 2024 23:03:41 +0700 Subject: [PATCH] fix(commerce): make base url correctly fallback to default base url --- .changeset/fresh-schools-turn.md | 5 ++++ packages/commerce/src/charge.ts | 24 ++++++++++------- packages/commerce/src/checkout.ts | 22 +++++++++------- packages/commerce/src/client.ts | 43 +++++++++++++++++++++++++------ packages/commerce/src/events.ts | 17 +++++++----- 5 files changed, 79 insertions(+), 32 deletions(-) create mode 100644 .changeset/fresh-schools-turn.md diff --git a/.changeset/fresh-schools-turn.md b/.changeset/fresh-schools-turn.md new file mode 100644 index 0000000..1ffc41d --- /dev/null +++ b/.changeset/fresh-schools-turn.md @@ -0,0 +1,5 @@ +--- +"@coinbasejs/commerce": patch +--- + +fix(commerce): make base url correctly fallback to default base url diff --git a/packages/commerce/src/charge.ts b/packages/commerce/src/charge.ts index 0e3dca7..92b12dc 100644 --- a/packages/commerce/src/charge.ts +++ b/packages/commerce/src/charge.ts @@ -1,4 +1,5 @@ import * as http from "@coinbasejs/utils/http"; +import type { RequestConfig } from "./client"; import { BASE_URL } from "./constants"; export type GetChargeResponse = { @@ -8,11 +9,12 @@ export type GetChargeResponse = { export async function getCharge( id: string, - apiKey: string, + config: RequestConfig, ): Promise { - 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, }, }); } @@ -22,10 +24,13 @@ export type GetChargesResponse = { warnings: string[]; }; -export async function getCharges(apiKey: string): Promise { - return await http.get(`${BASE_URL}/charges`, { +export async function getCharges( + config: RequestConfig, +): Promise { + const baseUrl = config.baseUrl ?? BASE_URL; + return await http.get(`${baseUrl}/charges`, { headers: { - "X-CC-Api-Key": apiKey, + "X-CC-Api-Key": config.apiKey, }, }); } @@ -49,11 +54,12 @@ export type CreateChargeResponse = { export async function createCharge( parameters: CreateChargeParameters, - apiKey: string, + config: RequestConfig, ): Promise { - 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), }); diff --git a/packages/commerce/src/checkout.ts b/packages/commerce/src/checkout.ts index f771f6a..5de2f45 100644 --- a/packages/commerce/src/checkout.ts +++ b/packages/commerce/src/checkout.ts @@ -1,4 +1,5 @@ import * as http from "@coinbasejs/utils/http"; +import type { RequestConfig } from "./client"; import { BASE_URL } from "./constants"; type GetCheckoutResponse = { @@ -8,11 +9,12 @@ type GetCheckoutResponse = { export async function getCheckout( id: string, - apiKey: string, + config: RequestConfig, ): Promise { - 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, }, }); } @@ -23,11 +25,12 @@ type GetCheckoutsResponse = { }; export async function getCheckouts( - apiKey: string, + config: RequestConfig, ): Promise { - 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, }, }); } @@ -59,11 +62,12 @@ export type CreateCheckoutResponse = { export async function createCheckout( parameters: CreateCheckoutParameters, - apiKey: string, + config: RequestConfig, ): Promise { - 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), }); diff --git a/packages/commerce/src/client.ts b/packages/commerce/src/client.ts index ae1060c..0b15861 100644 --- a/packages/commerce/src/client.ts +++ b/packages/commerce/src/client.ts @@ -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; +}; diff --git a/packages/commerce/src/events.ts b/packages/commerce/src/events.ts index be54771..8f647c7 100644 --- a/packages/commerce/src/events.ts +++ b/packages/commerce/src/events.ts @@ -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 = { @@ -10,11 +11,12 @@ export type GetEventResponse = { export async function getEvent( id: string, - apiKey: string, + config: RequestConfig, ): Promise { - 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, }, }); } @@ -34,10 +36,13 @@ export type GetEventsResponse = { data: Array; }; -export async function getEvents(apiKey: string): Promise { - return await http.get(`${BASE_URL}/events`, { +export async function getEvents( + config: RequestConfig, +): Promise { + const baseUrl = config.baseUrl ?? BASE_URL; + return await http.get(`${baseUrl}/events`, { headers: { - "X-CC-Api-Key": apiKey, + "X-CC-Api-Key": config.apiKey, }, }); }