Skip to content

Commit

Permalink
[types] [improve] Fix type intellisense.
Browse files Browse the repository at this point in the history
  • Loading branch information
swashata committed Nov 24, 2024
1 parent 59339ad commit 9be134a
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 25 deletions.
10 changes: 5 additions & 5 deletions src/adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@
* Backward compatible adapter for the checkout service.
*/
import './global';
import { CheckoutPopupOptions, Checkout } from '.';
import { Checkout, CheckoutOptions } from '.';
import { IFSOldCheckout } from './lib/contracts/IFSOldCheckout';

class FSOldCheckout implements IFSOldCheckout {
private checkout: Checkout | null = null;

private options: CheckoutPopupOptions | null = null;
private options: CheckoutOptions | null = null;

configure(options: CheckoutPopupOptions, baseUrl?: string): IFSOldCheckout {
configure(options: CheckoutOptions, baseUrl?: string): IFSOldCheckout {
if (!this.checkout) {
this.checkout = new Checkout(options, false, baseUrl);

Expand All @@ -32,9 +32,9 @@ class FSOldCheckout implements IFSOldCheckout {
return this;
}

public open(options: Partial<CheckoutPopupOptions> = {}) {
public open(options: Partial<CheckoutOptions> = {}) {
const checkout =
this.checkout ?? this.configure(options as CheckoutPopupOptions);
this.checkout ?? this.configure(options as CheckoutOptions);

checkout.open({
...(this.options ?? {}),
Expand Down
4 changes: 2 additions & 2 deletions src/demo.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Checkout, CheckoutOptions } from '.';
import { Checkout, CheckoutOptions, CheckoutPopupOptions } from '.';

import './style.css';

Expand Down Expand Up @@ -39,7 +39,7 @@ document.addEventListener('DOMContentLoaded', () => {
}

function getEventLoggers(): Pick<
CheckoutOptions,
CheckoutPopupOptions,
| 'cancel'
| 'purchaseCompleted'
| 'success'
Expand Down
9 changes: 3 additions & 6 deletions src/lib/checkout.spec.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
import { screen } from '@testing-library/dom';
import { Checkout } from './checkout';
import { Checkout, CheckoutOptions } from './checkout';
import { sendMockedCanceledEvent } from '../../tests/utils';
import { createHydratedMock } from 'ts-auto-mock';
import {
CheckoutPopupOptions,
CheckoutPopupParams,
} from './contracts/CheckoutPopupOptions';
import { CheckoutPopupParams } from './contracts/CheckoutPopupOptions';
import { getQueryValueFromItem } from './utils/ops';

describe('CheckoutPopup', () => {
Expand Down Expand Up @@ -145,7 +142,7 @@ describe('CheckoutPopup', () => {
});

test('passes all undocumented query parameters', () => {
const option: CheckoutPopupOptions = {
const option: CheckoutOptions = {
plugin_id: 1,
public_key: 'pk_123456',
foo: 'bar',
Expand Down
8 changes: 6 additions & 2 deletions src/lib/checkout.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@ import type { CheckoutOptions } from './types';
import { Style } from './services/style';
import { Loader } from './services/loader';
import { CheckoutPopup } from './services/checkout-popup';
import { CheckoutPopupOptions } from './contracts/CheckoutPopupOptions';
import {
CheckoutPopupArbitraryParams,
CheckoutPopupOptions,
} from './contracts/CheckoutPopupOptions';
import { ExitIntent } from './services/exit-intent';
import { ILoader } from './contracts/ILoader';
import { IExitIntent } from './contracts/IExitIntent';
Expand All @@ -14,7 +17,8 @@ import { Cart } from './services/cart';
export type { PostmanEvents, CheckoutOptions };

export class Checkout {
private readonly options: CheckoutPopupOptions = {
private readonly options: CheckoutPopupOptions &
CheckoutPopupArbitraryParams = {
plugin_id: 0,
public_key: '',
};
Expand Down
25 changes: 20 additions & 5 deletions src/lib/contracts/CheckoutPopupOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@ export type CheckoutTrackingEvent =
| 'load'
| 'review-order';

/**
* All known parameters for the Checkout iFrame.
*
* @internal
*/
export interface CheckoutPopupParams {
/**
* Required product ID (whether it’s a plugin, theme, add-on, bundle, or SaaS).
Expand Down Expand Up @@ -356,6 +361,11 @@ export interface CheckoutPopupParams {
default_currency?: 'usd' | 'eur' | 'gbp';
}

/**
* All known events for the Checkout iFrame.
*
* @internal
*/
export interface CheckoutPopupEvents {
/**
* A callback handler that will execute once a user closes the checkout by
Expand Down Expand Up @@ -405,11 +415,16 @@ export interface CheckoutPopupEvents {
onExitIntent?: () => void;
}

/**
* All options (parameters and events) required and supported by the Freemius Checkout.
*/
export interface CheckoutPopupOptions
extends CheckoutPopupParams,
CheckoutPopupEvents {
/**
* Accept any arbitrary key-value pair to be passed to the checkout.
*/
[key: string]: any;
CheckoutPopupEvents {}

/**
* Accept any arbitrary key-value pair to be passed to the checkout.
*/
export interface CheckoutPopupArbitraryParams {
[key: Exclude<string, keyof CheckoutPopupOptions>]: any;
}
9 changes: 6 additions & 3 deletions src/lib/services/cart/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import { CheckoutPopupOptions } from '../../contracts/CheckoutPopupOptions';
import {
CheckoutPopupArbitraryParams,
CheckoutPopupOptions,
} from '../../contracts/CheckoutPopupOptions';
import { assertNotNull } from '../../utils/ops';

/**
Expand Down Expand Up @@ -40,10 +43,10 @@ export class Cart {
);
}

getCheckoutOptions(): CheckoutPopupOptions {
getCheckoutOptions(): CheckoutPopupOptions & CheckoutPopupArbitraryParams {
assertNotNull(this.queryParams, Cart.NO_CART_FOUND_MESSAGE);

const params: CheckoutPopupOptions = {
const params: CheckoutPopupOptions & CheckoutPopupArbitraryParams = {
plugin_id: '',
public_key: '',
};
Expand Down
8 changes: 6 additions & 2 deletions src/lib/types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import { CheckoutPopupOptions } from './contracts/CheckoutPopupOptions';
import {
CheckoutPopupArbitraryParams,
CheckoutPopupOptions,
} from './contracts/CheckoutPopupOptions';

/**
* All options required and supported by the Freemius Checkout.
Expand All @@ -16,7 +19,8 @@ export type CheckoutOptions = Omit<CheckoutPopupOptions, 'plugin_id'> & {
* The alt text for the loading image. By default 'Loading Freemius Checkout' will be used.
*/
loadingImageAlt?: string;
} & (
} & CheckoutPopupArbitraryParams &
(
| {
/**
* Required product ID (whether it’s a plugin, theme, add-on, bundle, or SaaS).
Expand Down

0 comments on commit 9be134a

Please sign in to comment.