Skip to content

Commit

Permalink
simplify implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
tomaszbakula committed Sep 27, 2024
1 parent f7baf91 commit f02a5ed
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 28 deletions.
7 changes: 7 additions & 0 deletions src/types/onramp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,13 @@ type BaseOnRampAppParams = {
* `{ "0x1": ["base"] }`
*/
addresses?: Record<string, string[]>;
/**
* The session token used to initialize the Coinbase Onramp experience.
* This token expires after a short period of time and can only be used once.
* A new token must be obtained for every new session.
* @see https://docs.cdp.coinbase.com/onramp/docs/api-initializing#getting-an-onramp-session-token
*/
sessionToken?: string;
/**
* This optional parameter will restrict the assets available for the user to buy/send. It acts as a filter on the
* networks specified in the {addresses} param.
Expand Down
17 changes: 2 additions & 15 deletions src/types/widget.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,10 @@ export type EmbeddedContentStyles = {
top?: string;
};

export type CBPayExperienceWithAppId<T> = {
appId: string;
sessionToken?: never;
} & CBPayExperienceBaseOptions<T>;

export type CBPayExperienceWithSessionToken<T> = {
sessionToken: string;
appId?: never;
} & CBPayExperienceBaseOptions<T>;

type CBPayExperienceBaseOptions<T> = {
export type CBPayExperienceOptions<T> = {
widgetParameters: T;
target?: string;
appId: string;
host?: string;
debug?: boolean;
theme?: Theme;
Expand All @@ -46,7 +37,3 @@ type CBPayExperienceBaseOptions<T> = {
experienceLoggedIn?: Experience;
experienceLoggedOut?: Experience;
};

export type CBPayExperienceOptions<T> =
| CBPayExperienceWithAppId<T>
| CBPayExperienceWithSessionToken<T>;
9 changes: 7 additions & 2 deletions src/utils/CoinbasePixel.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,14 @@ describe('CoinbasePixel', () => {
});

it('should initialize with sessionToken', () => {
const instance = createUntypedPixel({ sessionToken: 'test', appParams: defaultAppParams });
const sessionToken = 'test-session-token';
const instance = createUntypedPixel({
appId: 'test',
appParams: { ...defaultAppParams, sessionToken },
});

expect(instance.sessionToken).toEqual('test');
expect(instance.appId).toEqual('test');
expect(instance.appParams.sessionToken).toEqual(sessionToken);
});

it('should handle opening the embedded experience when logged out', () => {
Expand Down
13 changes: 2 additions & 11 deletions src/utils/CoinbasePixel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,7 @@ export type ExperienceListeners = {

export type CoinbasePixelConstructorParams = {
host?: string;
appId?: string;
sessionToken?: string;
appId: string;
appParams: JsonObject;
debug?: boolean;
theme?: Theme;
Expand All @@ -43,8 +42,7 @@ export type OpenExperienceOptions = {
export class CoinbasePixel {
private debug: boolean;
private host: string;
private appId: string | undefined;
private sessionToken: string | undefined;
private appId: string;
private eventStreamListeners: Partial<Record<EventMetadata['eventName'], (() => void)[]>> = {};
private unsubs: (() => void)[] = [];
private appParams: JsonObject;
Expand All @@ -54,14 +52,12 @@ export class CoinbasePixel {
constructor({
host = DEFAULT_HOST,
appId,
sessionToken,
appParams,
debug,
theme,
}: CoinbasePixelConstructorParams) {
this.host = host;
this.appId = appId;
this.sessionToken = sessionToken;
this.appParams = appParams;
this.debug = debug || false;
this.theme = theme;
Expand All @@ -79,7 +75,6 @@ export class CoinbasePixel {

const url = generateOnRampURL({
appId: this.appId,
sessionToken: this.sessionToken,
host: this.host,
theme: this.theme ?? undefined,
...this.appParams,
Expand Down Expand Up @@ -176,10 +171,6 @@ export class CoinbasePixel {
};

private startDirectSignin = (callback: () => void) => {
if (!this.appId) {
throw new Error('appId is required for direct signin');
}

const queryParams = new URLSearchParams();
queryParams.set('appId', this.appId);
queryParams.set('type', 'direct');
Expand Down

0 comments on commit f02a5ed

Please sign in to comment.