Skip to content
This repository has been archived by the owner on Feb 23, 2024. It is now read-only.

Commit

Permalink
Improve the type for callbacks registered by extensions
Browse files Browse the repository at this point in the history
The previous type was CanMakePaymentCallback, used for the payment methods's own canMakePayment callbacks, which could also return Promises. In order to eliminate the confusion, a new type has been created that defines callbacks that only return true or false.
  • Loading branch information
ralucaStan committed Sep 23, 2021
1 parent e84577d commit 86f74af
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
/**
* External dependencies
*/
import { CanMakePaymentCallback } from '@woocommerce/type-defs/payments';
import { CanMakePaymentExtensionCallback } from '@woocommerce/type-defs/payments';

type CanMakePaymentExtensionCallbacks = Record<
string,
CanMakePaymentCallback
CanMakePaymentExtensionCallback
>;
export type NamespacedCanMakePaymentExtensionsCallbacks = Record<
string,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
/**
* External dependencies
*/
import type { CanMakePaymentCallback } from '@woocommerce/type-defs/payments';
import type {
CanMakePaymentCallback,
CanMakePaymentExtensionCallback,
} from '@woocommerce/type-defs/payments';

/**
* Internal dependencies
Expand Down Expand Up @@ -38,7 +41,7 @@ export const canMakePaymentWithExtensions = (
// Gather all callbacks for paymentMethodName.
const namespacedCallbacks: Record<
ExtensionNamespace,
CanMakePaymentCallback
CanMakePaymentExtensionCallback
> = {};

Object.entries( extensionsCallbacks ).forEach(
Expand Down
4 changes: 2 additions & 2 deletions assets/js/blocks-registry/payment-methods/registry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import deprecated from '@wordpress/deprecated';
import type {
PaymentMethodConfiguration,
ExpressPaymentMethodConfiguration,
CanMakePaymentCallback,
CanMakePaymentExtensionCallback,
PaymentMethodConfigInstance,
PaymentMethods,
ExpressPaymentMethods,
Expand Down Expand Up @@ -81,7 +81,7 @@ export const registerExpressPaymentMethod = (
*/
export const registerPaymentMethodExtensionCallbacks = (
namespace: string,
callbacks: Record< string, CanMakePaymentCallback >
callbacks: Record< string, CanMakePaymentExtensionCallback >
): void => {
if ( canMakePaymentExtensionsCallbacks[ namespace ] ) {
// eslint-disable-next-line no-console
Expand Down
4 changes: 4 additions & 0 deletions assets/js/types/type-defs/payments.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ export type CanMakePaymentCallback = (
cartData: CanMakePaymentArgument
) => CanMakePaymentReturnType;

export type CanMakePaymentExtensionCallback = (
cartData: CanMakePaymentArgument
) => boolean;

export interface PaymentMethodIcon {
id: string;
src: string | null;
Expand Down

0 comments on commit 86f74af

Please sign in to comment.