From 2f0e32d503a7c0cd75d277b18dd2150c75dfa11b Mon Sep 17 00:00:00 2001 From: "Pimm \"de Chinchilla\" Hogeling" Date: Tue, 9 Jul 2024 14:36:44 +0200 Subject: [PATCH] Change interface of alias. --- src/binders/chargebacks/ChargebacksBinder.ts | 2 +- src/binders/customers/CustomersBinder.ts | 3 +- .../mandates/CustomerMandatesBinder.ts | 3 +- .../payments/CustomerPaymentsBinder.ts | 2 +- .../CustomerSubscriptionsBinder.ts | 3 +- src/binders/methods/MethodsBinder.ts | 2 +- src/binders/orders/OrdersBinder.ts | 3 +- .../orders/orderlines/OrderLinesBinder.ts | 2 +- .../orders/shipments/OrderShipmentsBinder.ts | 2 +- src/binders/payments/PaymentsBinder.ts | 3 +- .../captures/PaymentCapturesBinder.ts | 2 +- .../chargebacks/PaymentChargebacksBinder.ts | 2 +- .../payments/refunds/PaymentRefundsBinder.ts | 3 +- src/binders/permissions/PermissionsBinder.ts | 2 +- src/binders/profiles/ProfilesBinder.ts | 2 +- src/binders/refunds/RefundsBinder.ts | 2 +- .../refunds/orders/OrderRefundsBinder.ts | 2 +- .../subscriptions/SubscriptionsBinder.ts | 2 +- .../payments/SubscriptionPaymentsBinder.ts | 2 +- src/createMollieClient.ts | 27 ++++++++------- src/plumbing/alias.ts | 34 +++++++++++++------ 21 files changed, 57 insertions(+), 48 deletions(-) diff --git a/src/binders/chargebacks/ChargebacksBinder.ts b/src/binders/chargebacks/ChargebacksBinder.ts index 201aa70f..d71569e5 100644 --- a/src/binders/chargebacks/ChargebacksBinder.ts +++ b/src/binders/chargebacks/ChargebacksBinder.ts @@ -13,7 +13,7 @@ const pathSegment = 'chargebacks'; export default class ChargebacksBinder extends Binder { constructor(protected readonly networkClient: TransformingNetworkClient) { super(); - alias(this, 'page', 'all', 'list'); + alias(this, { page: ['all', 'list'] }); } /** diff --git a/src/binders/customers/CustomersBinder.ts b/src/binders/customers/CustomersBinder.ts index e9ec8aaa..a26f949a 100644 --- a/src/binders/customers/CustomersBinder.ts +++ b/src/binders/customers/CustomersBinder.ts @@ -15,8 +15,7 @@ const pathSegment = 'customers'; export default class CustomersBinder extends Binder { constructor(protected readonly networkClient: TransformingNetworkClient) { super(); - alias(this, 'page', 'all', 'list'); - alias(this, 'delete', 'cancel') + alias(this, { page: ['all', 'list'], delete: 'cancel' }); } /** diff --git a/src/binders/customers/mandates/CustomerMandatesBinder.ts b/src/binders/customers/mandates/CustomerMandatesBinder.ts index 5e099abf..95633ad7 100644 --- a/src/binders/customers/mandates/CustomerMandatesBinder.ts +++ b/src/binders/customers/mandates/CustomerMandatesBinder.ts @@ -17,8 +17,7 @@ function getPathSegments(customerId: string) { export default class CustomerMandatesBinder extends Binder { constructor(protected readonly networkClient: TransformingNetworkClient) { super(); - alias(this, 'page', 'all', 'list'); - alias(this, 'revoke', 'cancel', 'delete'); + alias(this, { page: ['all', 'list'], revoke: ['cancel', 'delete'] }); } /** diff --git a/src/binders/customers/payments/CustomerPaymentsBinder.ts b/src/binders/customers/payments/CustomerPaymentsBinder.ts index 9d013625..420da5ff 100644 --- a/src/binders/customers/payments/CustomerPaymentsBinder.ts +++ b/src/binders/customers/payments/CustomerPaymentsBinder.ts @@ -17,7 +17,7 @@ function getPathSegments(customerId: string) { export default class CustomerPaymentsBinder extends Binder { constructor(protected readonly networkClient: TransformingNetworkClient) { super(); - alias(this, 'page', 'all', 'list'); + alias(this, { page: ['all', 'list'] }); } /** diff --git a/src/binders/customers/subscriptions/CustomerSubscriptionsBinder.ts b/src/binders/customers/subscriptions/CustomerSubscriptionsBinder.ts index fab96691..e1816103 100644 --- a/src/binders/customers/subscriptions/CustomerSubscriptionsBinder.ts +++ b/src/binders/customers/subscriptions/CustomerSubscriptionsBinder.ts @@ -17,8 +17,7 @@ function getPathSegments(customerId: string) { export default class CustomerSubscriptionsBinder extends Binder { constructor(protected readonly networkClient: TransformingNetworkClient) { super(); - alias(this, 'page', 'all', 'list'); - alias(this, 'cancel', 'delete'); + alias(this, { page: ['all', 'list'], cancel: 'delete' }); } /** diff --git a/src/binders/methods/MethodsBinder.ts b/src/binders/methods/MethodsBinder.ts index 5fb91935..e178a9da 100644 --- a/src/binders/methods/MethodsBinder.ts +++ b/src/binders/methods/MethodsBinder.ts @@ -12,7 +12,7 @@ const pathSegment = 'methods'; export default class MethodsBinder extends Binder { constructor(protected readonly networkClient: TransformingNetworkClient) { super(); - alias(this, 'list', 'all', 'page'); + alias(this, { list: ['all', 'page'] }); } /** diff --git a/src/binders/orders/OrdersBinder.ts b/src/binders/orders/OrdersBinder.ts index 65f0be50..c5101f6b 100644 --- a/src/binders/orders/OrdersBinder.ts +++ b/src/binders/orders/OrdersBinder.ts @@ -37,8 +37,7 @@ export const pathSegment = 'orders'; export default class OrdersBinder extends Binder { constructor(protected readonly networkClient: TransformingNetworkClient) { super(); - alias(this, 'page', 'all', 'list'); - alias(this, 'cancel', 'delete'); + alias(this, { page: ['all', 'list'], cancel: 'delete' }); } /** diff --git a/src/binders/orders/orderlines/OrderLinesBinder.ts b/src/binders/orders/orderlines/OrderLinesBinder.ts index d32fc0c6..f97cadcd 100644 --- a/src/binders/orders/orderlines/OrderLinesBinder.ts +++ b/src/binders/orders/orderlines/OrderLinesBinder.ts @@ -16,7 +16,7 @@ function getPathSegments(orderId: string) { export default class OrderLinesBinder extends Binder { constructor(protected readonly networkClient: TransformingNetworkClient) { super(); - alias(this, 'cancel', 'delete'); + alias(this, { cancel: 'delete' }); } /** diff --git a/src/binders/orders/shipments/OrderShipmentsBinder.ts b/src/binders/orders/shipments/OrderShipmentsBinder.ts index 25670ec3..5ac01fee 100644 --- a/src/binders/orders/shipments/OrderShipmentsBinder.ts +++ b/src/binders/orders/shipments/OrderShipmentsBinder.ts @@ -16,7 +16,7 @@ export function getPathSegments(orderId: string) { export default class OrderShipmentsBinder extends Binder { constructor(protected readonly networkClient: TransformingNetworkClient) { super(); - alias(this, 'list', 'all', 'page'); + alias(this, { list: ['all', 'page'] }); } /** diff --git a/src/binders/payments/PaymentsBinder.ts b/src/binders/payments/PaymentsBinder.ts index 2a32c44a..17f9feac 100644 --- a/src/binders/payments/PaymentsBinder.ts +++ b/src/binders/payments/PaymentsBinder.ts @@ -15,8 +15,7 @@ const pathSegment = 'payments'; export default class PaymentsBinder extends Binder { constructor(protected readonly networkClient: TransformingNetworkClient) { super(); - alias(this, 'page', 'all', 'list'); - alias(this, 'cancel', 'delete'); + alias(this, { page: ['all', 'list'], cancel: 'delete' }); } /** diff --git a/src/binders/payments/captures/PaymentCapturesBinder.ts b/src/binders/payments/captures/PaymentCapturesBinder.ts index 27aa4fae..2bec810e 100644 --- a/src/binders/payments/captures/PaymentCapturesBinder.ts +++ b/src/binders/payments/captures/PaymentCapturesBinder.ts @@ -17,7 +17,7 @@ function getPathSegments(paymentId: string) { export default class PaymentCapturesBinder extends Binder { constructor(protected readonly networkClient: TransformingNetworkClient) { super(); - alias(this, 'page', 'all', 'list'); + alias(this, { page: ['all', 'list'] }); } /** diff --git a/src/binders/payments/chargebacks/PaymentChargebacksBinder.ts b/src/binders/payments/chargebacks/PaymentChargebacksBinder.ts index 0a339a28..521a27ac 100644 --- a/src/binders/payments/chargebacks/PaymentChargebacksBinder.ts +++ b/src/binders/payments/chargebacks/PaymentChargebacksBinder.ts @@ -17,7 +17,7 @@ function getPathSegments(paymentId: string) { export default class PaymentChargebacksBinder extends Binder { constructor(protected readonly networkClient: TransformingNetworkClient) { super(); - alias(this, 'page', 'all', 'list'); + alias(this, { page: ['all', 'list'] }); } /** diff --git a/src/binders/payments/refunds/PaymentRefundsBinder.ts b/src/binders/payments/refunds/PaymentRefundsBinder.ts index 48b594ce..c223436b 100644 --- a/src/binders/payments/refunds/PaymentRefundsBinder.ts +++ b/src/binders/payments/refunds/PaymentRefundsBinder.ts @@ -17,8 +17,7 @@ function getPathSegments(paymentId: string) { export default class PaymentRefundsBinder extends Binder { constructor(protected readonly networkClient: TransformingNetworkClient) { super(); - alias(this, 'page', 'all', 'list'); - alias(this, 'cancel', 'delete'); + alias(this, { page: ['all', 'list'], cancel: 'delete' }); } /** diff --git a/src/binders/permissions/PermissionsBinder.ts b/src/binders/permissions/PermissionsBinder.ts index b4a3d24e..3e3478da 100644 --- a/src/binders/permissions/PermissionsBinder.ts +++ b/src/binders/permissions/PermissionsBinder.ts @@ -11,7 +11,7 @@ const pathSegment = 'permissions'; export default class PermissionsBinder extends Binder { constructor(protected readonly networkClient: TransformingNetworkClient) { super(); - alias(this, 'list', 'page'); + alias(this, { list: 'page' }); } /** diff --git a/src/binders/profiles/ProfilesBinder.ts b/src/binders/profiles/ProfilesBinder.ts index d89cdaa4..24763900 100644 --- a/src/binders/profiles/ProfilesBinder.ts +++ b/src/binders/profiles/ProfilesBinder.ts @@ -15,7 +15,7 @@ const pathSegment = 'profiles'; export default class ProfilesBinder extends Binder { constructor(protected readonly networkClient: TransformingNetworkClient) { super(); - alias(this, 'page', 'list'); + alias(this, { page: 'list' }); } /** diff --git a/src/binders/refunds/RefundsBinder.ts b/src/binders/refunds/RefundsBinder.ts index a5d906eb..4611f441 100644 --- a/src/binders/refunds/RefundsBinder.ts +++ b/src/binders/refunds/RefundsBinder.ts @@ -13,7 +13,7 @@ const pathSegment = 'refunds'; export default class RefundsBinder extends Binder { constructor(protected readonly networkClient: TransformingNetworkClient) { super(); - alias(this, 'page', 'all', 'list'); + alias(this, { page: ['all', 'list'] }); } /** diff --git a/src/binders/refunds/orders/OrderRefundsBinder.ts b/src/binders/refunds/orders/OrderRefundsBinder.ts index 630450a6..5d44159b 100644 --- a/src/binders/refunds/orders/OrderRefundsBinder.ts +++ b/src/binders/refunds/orders/OrderRefundsBinder.ts @@ -17,7 +17,7 @@ export function getPathSegments(orderId: string) { export default class OrderRefundsBinder extends Binder { constructor(protected readonly networkClient: TransformingNetworkClient) { super(); - alias(this, 'page', 'all', 'list'); + alias(this, { page: ['all', 'list'] }); } /** diff --git a/src/binders/subscriptions/SubscriptionsBinder.ts b/src/binders/subscriptions/SubscriptionsBinder.ts index 9451bd51..a7a1a810 100644 --- a/src/binders/subscriptions/SubscriptionsBinder.ts +++ b/src/binders/subscriptions/SubscriptionsBinder.ts @@ -13,7 +13,7 @@ const pathSegment = 'subscriptions'; export default class SubscriptionsBinder extends Binder { constructor(protected readonly networkClient: TransformingNetworkClient) { super(); - alias(this, 'page', 'list'); + alias(this, { page: 'list' }); } /** diff --git a/src/binders/subscriptions/payments/SubscriptionPaymentsBinder.ts b/src/binders/subscriptions/payments/SubscriptionPaymentsBinder.ts index 984086e3..1c3643e8 100644 --- a/src/binders/subscriptions/payments/SubscriptionPaymentsBinder.ts +++ b/src/binders/subscriptions/payments/SubscriptionPaymentsBinder.ts @@ -17,7 +17,7 @@ function getPathSegments(customerId: string, subscriptionId: string): string { export default class SubscriptionPaymentsBinder extends Binder { constructor(protected readonly networkClient: TransformingNetworkClient) { super(); - alias(this, 'page', 'list'); + alias(this, { page: 'list' }); } /** diff --git a/src/createMollieClient.ts b/src/createMollieClient.ts index 6dd6210b..d6cf0e68 100644 --- a/src/createMollieClient.ts +++ b/src/createMollieClient.ts @@ -167,19 +167,20 @@ export default function createMollieClient(options: Options) { settlementRefunds: new SettlementRefundsBinder(transformingNetworkClient), settlementChargebacks: new SettlementChargebacksBinder(transformingNetworkClient), }, - client => { - alias(client, 'paymentRefunds', 'payments_refunds'); - alias(client, 'paymentChargebacks', 'payments_chargebacks'); - alias(client, 'paymentCaptures', 'payments_captures'); - alias(client, 'customerPayments', 'customers_payments'); - alias(client, 'customerMandates', 'customers_mandates'); - alias(client, 'subscriptionPayments', 'subscriptions_payments'); - alias(client, 'customerSubscriptions', 'customers_subscriptions'); - alias(client, 'orderRefunds', 'orders_refunds'); - alias(client, 'orderLines', 'orders_lines'); - alias(client, 'orderPayments', 'orders_payments'); - alias(client, 'orderShipments', 'orders_shipments'); - }, + client => + alias(client, { + paymentRefunds: 'payments_refunds', + paymentChargebacks: 'payments_chargebacks', + paymentCaptures: 'payments_captures', + customerPayments: 'customers_payments', + customerMandates: 'customers_mandates', + subscriptionPayments: 'subscriptions_payments', + customerSubscriptions: 'customers_subscriptions', + orderRefunds: 'orders_refunds', + orderLines: 'orders_lines', + orderPayments: 'orders_payments', + orderShipments: 'orders_shipments', + }), ); } diff --git a/src/plumbing/alias.ts b/src/plumbing/alias.ts index 5caf71de..5a8c77ab 100644 --- a/src/plumbing/alias.ts +++ b/src/plumbing/alias.ts @@ -1,17 +1,31 @@ -import { run } from 'ruply'; +import { apply, run } from 'ruply'; + +function createDescriptor(value: T) { + return { + configurable: true, + /* enumerable: false, */ + writable: true, + value, + } satisfies PropertyDescriptor; +} /** * Defines new properties on the passed target object which take the value of the original property. The newly defined * properties are not enumerable and purposely do not exist in the TypeScript type of the target object. */ -export default function alias(target: T, property: keyof T & string, ...aliases: Array) { - run( - { - configurable: true, - /* enumerable: false, */ - writable: true, - value: target[property], - }, - descriptor => aliases.forEach(alias => Object.defineProperty(target, alias, descriptor)), +export default function alias(target: T, aliases: Record | string>) { + Object.defineProperties( + target, + (Object.entries(aliases) as Array<[P, Array | string]>).reduce( + (descriptors, [property, aliases]) => + apply(descriptors, descriptors => { + if (Array.isArray(aliases)) { + aliases.forEach(alias => (descriptors[alias] = createDescriptor(target[property]))); + } /* if ('string' == typeof aliases) */ else { + descriptors[aliases as string] = createDescriptor(target[property]); + } + }), + {} as PropertyDescriptorMap, + ), ); }