UNPKG

@coinbasejs/commerce

Version:

Coinbase Developer Platform Commerce package

182 lines (176 loc) 4.12 kB
// ../utils/http.ts var baseHeaders = { "Content-Type": "application/json" }; async function get(url, options) { const resp = await fetch(url, { method: "GET", ...options, headers: { ...baseHeaders, ...options?.headers } }); if (!resp.ok) { throw new Error(`GET: request to ${url} failed`); } return await resp.json(); } async function post(url, options) { const resp = await fetch(url, { method: "POST", ...options, headers: { ...baseHeaders, ...options?.headers } }); if (!resp.ok) { throw new Error(`POST: request to ${url} failed`); } return await resp.json(); } // src/constants.ts var BASE_URL = "https://api.commerce.coinbase.com"; // src/charge.ts async function getCharge(id, config) { const baseUrl = config.baseUrl ?? BASE_URL; return await get(`${baseUrl}/charges/${id}`, { headers: { "X-CC-Api-Key": config.apiKey } }); } async function getCharges(config) { const baseUrl = config.baseUrl ?? BASE_URL; return await get(`${baseUrl}/charges`, { headers: { "X-CC-Api-Key": config.apiKey } }); } async function createCharge(parameters, config) { const baseUrl = config.baseUrl ?? BASE_URL; return await post(`${baseUrl}/charges`, { headers: { "X-CC-Api-Key": config.apiKey }, body: JSON.stringify(parameters) }); } // src/checkout.ts async function getCheckout(id, config) { const baseUrl = config.baseUrl ?? BASE_URL; return await get(`${baseUrl}/checkouts/${id}`, { headers: { "X-CC-Api-Key": config.apiKey } }); } async function getCheckouts(config) { const baseUrl = config.baseUrl ?? BASE_URL; return await get(`${baseUrl}/checkouts`, { headers: { "X-CC-Api-Key": config.apiKey } }); } async function createCheckout(parameters, config) { const baseUrl = config.baseUrl ?? BASE_URL; return await post(`${baseUrl}/checkouts`, { headers: { "X-CC-Api-Key": config.apiKey }, body: JSON.stringify(parameters) }); } // src/events.ts async function getEvent(id, config) { const baseUrl = config.baseUrl ?? BASE_URL; return await get(`${baseUrl}/events/${id}`, { headers: { "X-CC-Api-Key": config.apiKey } }); } async function getEvents(config) { const baseUrl = config.baseUrl ?? BASE_URL; return await get(`${baseUrl}/events`, { headers: { "X-CC-Api-Key": config.apiKey } }); } // src/client.ts function createClient(config) { const url = config.baseUrl ?? BASE_URL; const client = { __url: url, createCharge: async (parameters) => { const response = await createCharge(parameters, { apiKey: config.apiKey, baseUrl: url }); return response.data; }, createCheckout: async (parameters) => { const response = await createCheckout(parameters, { apiKey: config.apiKey, baseUrl: url }); return response.data; }, getCharge: async (id) => { const response = await getCharge(id, { apiKey: config.apiKey, baseUrl: url }); return response.data; }, getCharges: async () => { const response = await getCharges({ apiKey: config.apiKey, baseUrl: url }); return response.data; }, getCheckout: async (id) => { const response = await getCheckout(id, { apiKey: config.apiKey, baseUrl: url }); return response.data; }, getCheckouts: async () => { const response = await getCheckouts({ apiKey: config.apiKey, baseUrl: url }); return response.data; }, getEvent: async (id) => { const response = await getEvent(id, { apiKey: config.apiKey }); return response.data; }, getEvents: async () => { const response = await getEvents({ apiKey: config.apiKey }); return response.data; } }; return client; } export { createCharge, createCheckout, createClient, getCharge, getCharges, getCheckout, getCheckouts, getEvent, getEvents };