Skip to content

Commit

Permalink
Change interface of alias.
Browse files Browse the repository at this point in the history
  • Loading branch information
Pimm committed Jul 9, 2024
1 parent 1d12874 commit 2f0e32d
Show file tree
Hide file tree
Showing 21 changed files with 57 additions and 48 deletions.
2 changes: 1 addition & 1 deletion src/binders/chargebacks/ChargebacksBinder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ const pathSegment = 'chargebacks';
export default class ChargebacksBinder extends Binder<ChargebackData, Chargeback> {
constructor(protected readonly networkClient: TransformingNetworkClient) {
super();
alias(this, 'page', 'all', 'list');
alias(this, { page: ['all', 'list'] });
}

/**
Expand Down
3 changes: 1 addition & 2 deletions src/binders/customers/CustomersBinder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@ const pathSegment = 'customers';
export default class CustomersBinder extends Binder<CustomerData, Customer> {
constructor(protected readonly networkClient: TransformingNetworkClient) {
super();
alias(this, 'page', 'all', 'list');
alias(this, 'delete', 'cancel')
alias(this, { page: ['all', 'list'], delete: 'cancel' });
}

/**
Expand Down
3 changes: 1 addition & 2 deletions src/binders/customers/mandates/CustomerMandatesBinder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@ function getPathSegments(customerId: string) {
export default class CustomerMandatesBinder extends Binder<MandateData, Mandate> {
constructor(protected readonly networkClient: TransformingNetworkClient) {
super();
alias(this, 'page', 'all', 'list');
alias(this, 'revoke', 'cancel', 'delete');
alias(this, { page: ['all', 'list'], revoke: ['cancel', 'delete'] });
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/binders/customers/payments/CustomerPaymentsBinder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ function getPathSegments(customerId: string) {
export default class CustomerPaymentsBinder extends Binder<PaymentData, Payment> {
constructor(protected readonly networkClient: TransformingNetworkClient) {
super();
alias(this, 'page', 'all', 'list');
alias(this, { page: ['all', 'list'] });
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@ function getPathSegments(customerId: string) {
export default class CustomerSubscriptionsBinder extends Binder<SubscriptionData, Subscription> {
constructor(protected readonly networkClient: TransformingNetworkClient) {
super();
alias(this, 'page', 'all', 'list');
alias(this, 'cancel', 'delete');
alias(this, { page: ['all', 'list'], cancel: 'delete' });
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/binders/methods/MethodsBinder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const pathSegment = 'methods';
export default class MethodsBinder extends Binder<MethodData, Method> {
constructor(protected readonly networkClient: TransformingNetworkClient) {
super();
alias(this, 'list', 'all', 'page');
alias(this, { list: ['all', 'page'] });
}

/**
Expand Down
3 changes: 1 addition & 2 deletions src/binders/orders/OrdersBinder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,7 @@ export const pathSegment = 'orders';
export default class OrdersBinder extends Binder<OrderData, Order> {
constructor(protected readonly networkClient: TransformingNetworkClient) {
super();
alias(this, 'page', 'all', 'list');
alias(this, 'cancel', 'delete');
alias(this, { page: ['all', 'list'], cancel: 'delete' });
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/binders/orders/orderlines/OrderLinesBinder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ function getPathSegments(orderId: string) {
export default class OrderLinesBinder extends Binder<OrderData, Order> {
constructor(protected readonly networkClient: TransformingNetworkClient) {
super();
alias(this, 'cancel', 'delete');
alias(this, { cancel: 'delete' });
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/binders/orders/shipments/OrderShipmentsBinder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export function getPathSegments(orderId: string) {
export default class OrderShipmentsBinder extends Binder<ShipmentData, Shipment> {
constructor(protected readonly networkClient: TransformingNetworkClient) {
super();
alias(this, 'list', 'all', 'page');
alias(this, { list: ['all', 'page'] });
}

/**
Expand Down
3 changes: 1 addition & 2 deletions src/binders/payments/PaymentsBinder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@ const pathSegment = 'payments';
export default class PaymentsBinder extends Binder<PaymentData, Payment> {
constructor(protected readonly networkClient: TransformingNetworkClient) {
super();
alias(this, 'page', 'all', 'list');
alias(this, 'cancel', 'delete');
alias(this, { page: ['all', 'list'], cancel: 'delete' });
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/binders/payments/captures/PaymentCapturesBinder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ function getPathSegments(paymentId: string) {
export default class PaymentCapturesBinder extends Binder<CaptureData, Capture> {
constructor(protected readonly networkClient: TransformingNetworkClient) {
super();
alias(this, 'page', 'all', 'list');
alias(this, { page: ['all', 'list'] });
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ function getPathSegments(paymentId: string) {
export default class PaymentChargebacksBinder extends Binder<ChargebackData, Chargeback> {
constructor(protected readonly networkClient: TransformingNetworkClient) {
super();
alias(this, 'page', 'all', 'list');
alias(this, { page: ['all', 'list'] });
}

/**
Expand Down
3 changes: 1 addition & 2 deletions src/binders/payments/refunds/PaymentRefundsBinder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@ function getPathSegments(paymentId: string) {
export default class PaymentRefundsBinder extends Binder<RefundData, Refund> {
constructor(protected readonly networkClient: TransformingNetworkClient) {
super();
alias(this, 'page', 'all', 'list');
alias(this, 'cancel', 'delete');
alias(this, { page: ['all', 'list'], cancel: 'delete' });
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/binders/permissions/PermissionsBinder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const pathSegment = 'permissions';
export default class PermissionsBinder extends Binder<PermissionData, Permission> {
constructor(protected readonly networkClient: TransformingNetworkClient) {
super();
alias(this, 'list', 'page');
alias(this, { list: 'page' });
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/binders/profiles/ProfilesBinder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const pathSegment = 'profiles';
export default class ProfilesBinder extends Binder<ProfileData, Profile> {
constructor(protected readonly networkClient: TransformingNetworkClient) {
super();
alias(this, 'page', 'list');
alias(this, { page: 'list' });
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/binders/refunds/RefundsBinder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ const pathSegment = 'refunds';
export default class RefundsBinder extends Binder<RefundData, Refund> {
constructor(protected readonly networkClient: TransformingNetworkClient) {
super();
alias(this, 'page', 'all', 'list');
alias(this, { page: ['all', 'list'] });
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/binders/refunds/orders/OrderRefundsBinder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export function getPathSegments(orderId: string) {
export default class OrderRefundsBinder extends Binder<RefundData, Refund> {
constructor(protected readonly networkClient: TransformingNetworkClient) {
super();
alias(this, 'page', 'all', 'list');
alias(this, { page: ['all', 'list'] });
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/binders/subscriptions/SubscriptionsBinder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ const pathSegment = 'subscriptions';
export default class SubscriptionsBinder extends Binder<SubscriptionData, Subscription> {
constructor(protected readonly networkClient: TransformingNetworkClient) {
super();
alias(this, 'page', 'list');
alias(this, { page: 'list' });
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ function getPathSegments(customerId: string, subscriptionId: string): string {
export default class SubscriptionPaymentsBinder extends Binder<PaymentData, Payment> {
constructor(protected readonly networkClient: TransformingNetworkClient) {
super();
alias(this, 'page', 'list');
alias(this, { page: 'list' });
}

/**
Expand Down
27 changes: 14 additions & 13 deletions src/createMollieClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
}),
);
}

Expand Down
34 changes: 24 additions & 10 deletions src/plumbing/alias.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,31 @@
import { run } from 'ruply';
import { apply, run } from 'ruply';

function createDescriptor<T>(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<T>(target: T, property: keyof T & string, ...aliases: Array<string>) {
run(
{
configurable: true,
/* enumerable: false, */
writable: true,
value: target[property],
},
descriptor => aliases.forEach(alias => Object.defineProperty(target, alias, descriptor)),
export default function alias<T, P extends keyof T>(target: T, aliases: Record<P, Array<string> | string>) {
Object.defineProperties(
target,
(Object.entries(aliases) as Array<[P, Array<string> | 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,
),
);
}

0 comments on commit 2f0e32d

Please sign in to comment.