Skip to content

Commit

Permalink
Merge pull request #23 from tiagosiebler/jerko
Browse files Browse the repository at this point in the history
feat(): added CBCommerce
  • Loading branch information
JJ-Cro authored Sep 15, 2024
2 parents e4f0d62 + ec5c0cb commit 72bf308
Show file tree
Hide file tree
Showing 2 changed files with 154 additions and 0 deletions.
151 changes: 151 additions & 0 deletions src/CBCommerceClient.ts
Original file line number Diff line number Diff line change
@@ -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<any> {
return this.postPrivate('/charges', {
body: params,
});
}

/**
* Returns All Charges
*
* Returns all charges.
*/
getAllCharges(): Promise<any> {
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<any> {
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<any> {
return this.postPrivate('/checkouts', {
body: params,
});
}

/**
* Returns All Checkout Sessions
*
* Returns all checkout sessions.
*/
getAllCheckouts(): Promise<any> {
return this.getPrivate('/checkouts');
}

/**
* Returns a Specific Checkout Session
*
* Returns a specific checkout session.
*/
getCheckout(params: { checkout_id: string }): Promise<any> {
return this.getPrivate(`/checkouts/${params.checkout_id}`);
}

/**
*
* Events Endpoints
*
*/

/**
* List Events
*
* Lists all events.
*/
listEvents(headers: { 'X-CC-Version': string }): Promise<any> {
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<any> {
return this.getPrivate(`/events/${params.event_id}`, { headers: headers });
}
}
3 changes: 3 additions & 0 deletions src/lib/requestUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
Expand All @@ -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 {
Expand Down

0 comments on commit 72bf308

Please sign in to comment.