From ec5c0cb6b73271346520691d9f762106d66b9afe Mon Sep 17 00:00:00 2001 From: Jerko J <83344666+JJ-Cro@users.noreply.github.com> Date: Sun, 15 Sep 2024 19:32:09 +0200 Subject: [PATCH] feat(): added CBCommerce --- src/CBCommerceClient.ts | 151 ++++++++++++++++++++++++++++++++++++++++ src/lib/requestUtils.ts | 3 + 2 files changed, 154 insertions(+) create mode 100644 src/CBCommerceClient.ts diff --git a/src/CBCommerceClient.ts b/src/CBCommerceClient.ts new file mode 100644 index 0000000..b9cbaa5 --- /dev/null +++ b/src/CBCommerceClient.ts @@ -0,0 +1,151 @@ +import { AxiosRequestConfig } from 'axios'; + +import { BaseRestClient } from './lib/BaseRestClient.js'; +import { + REST_CLIENT_TYPE_ENUM, + RestClientOptions, + RestClientType, +} from './lib/requestUtils.js'; + +/** + * REST client for Coinbase Prime API: + * https://docs.cdp.coinbase.com/commerce-onchain/docs/welcome + */ +export class CBCommerceClient extends BaseRestClient { + constructor( + restClientOptions: RestClientOptions = {}, + requestOptions: AxiosRequestConfig = {}, + ) { + super(restClientOptions, requestOptions); + return this; + } + + getClientType(): RestClientType { + return REST_CLIENT_TYPE_ENUM.commerce; + } + + /** + * + * Charges Endpoints + * + */ + + /** + * Creates a Charge + * + * Creates a charge. + */ + createCharge(params: { + buyer_locale?: string; + cancel_url?: string; + checkout_id?: string; + local_price: { + amount: string; + currency: string; + }; + metadata?: { + custom_field?: string; + custom_field_two?: string; + }; + pricing_type: string; + redirect_url?: string; + }): Promise { + return this.postPrivate('/charges', { + body: params, + }); + } + + /** + * Returns All Charges + * + * Returns all charges. + */ + getAllCharges(): Promise { + return this.getPrivate('/charges'); + } + + /** + * Returns the Charge with the Order Code + * + * Returns the charge with the order code. + */ + getCharge(params: { charge_code_or_charge_id: string }): Promise { + return this.getPrivate(`/charges/${params.charge_code_or_charge_id}`); + } + + /** + * + * Checkouts Endpoints + * + */ + + /** + * Creates a New Checkout + * + * Creates a new checkout. + */ + createCheckout(params: { + buyer_locale?: string; + total_price: { + amount: string; + currency: string; + }; + metadata?: { + custom_field?: string; + custom_field_two?: string; + }; + pricing_type: string; + requested_info?: string[]; + }): Promise { + return this.postPrivate('/checkouts', { + body: params, + }); + } + + /** + * Returns All Checkout Sessions + * + * Returns all checkout sessions. + */ + getAllCheckouts(): Promise { + return this.getPrivate('/checkouts'); + } + + /** + * Returns a Specific Checkout Session + * + * Returns a specific checkout session. + */ + getCheckout(params: { checkout_id: string }): Promise { + return this.getPrivate(`/checkouts/${params.checkout_id}`); + } + + /** + * + * Events Endpoints + * + */ + + /** + * List Events + * + * Lists all events. + */ + listEvents(headers: { 'X-CC-Version': string }): Promise { + return this.getPrivate('/events', { + headers: headers, + }); + } + + /** + * Show an Event + * + * Retrieves the details of an event. Supply the unique identifier of the event, which you might have received in a webhook. + */ + showEvent( + params: { event_id: string }, + headers: { 'X-CC-Version': string }, + ): Promise { + return this.getPrivate(`/events/${params.event_id}`, { headers: headers }); + } +} diff --git a/src/lib/requestUtils.ts b/src/lib/requestUtils.ts index 53eba69..45a5a6b 100644 --- a/src/lib/requestUtils.ts +++ b/src/lib/requestUtils.ts @@ -14,6 +14,8 @@ export const REST_CLIENT_TYPE_ENUM = { prime: 'prime', /** Coinbase International API */ international: 'international', + /** Coinbase Commerce API */ + commerce: 'commerce', } as const; export type RestClientType = @@ -26,6 +28,7 @@ const exchangeBaseURLMap = { [REST_CLIENT_TYPE_ENUM.prime]: 'https://api.prime.coinbase.com', // /v1 [REST_CLIENT_TYPE_ENUM.international]: 'https://api.international.coinbase.com', + [REST_CLIENT_TYPE_ENUM.commerce]: 'https://api.commerce.coinbase.com', } as const; export interface RestClientOptions {