From 812130a43d953ac2d9ea8f2376f758b95ecb3246 Mon Sep 17 00:00:00 2001 From: Jan Paepke <jan@paepke.at> Date: Tue, 10 Sep 2024 18:00:58 +0200 Subject: [PATCH] replace checkID with assert valid id --- src/binders/customers/CustomersBinder.ts | 15 ++----- .../mandates/CustomerMandatesBinder.ts | 31 ++++----------- .../payments/CustomerPaymentsBinder.ts | 15 ++----- .../CustomerSubscriptionsBinder.ts | 39 +++++-------------- src/binders/orders/OrdersBinder.ts | 15 ++----- .../orders/orderlines/OrderLinesBinder.ts | 15 ++----- .../orders/shipments/OrderShipmentsBinder.ts | 27 ++++--------- .../organizations/OrganizationsBinder.ts | 7 +--- .../paymentLinks/PaymentLinksBinder.ts | 7 +--- src/binders/payments/PaymentsBinder.ts | 15 ++----- .../captures/PaymentCapturesBinder.ts | 19 +++------ .../chargebacks/PaymentChargebacksBinder.ts | 19 +++------ .../payments/orders/OrderPaymentsBinder.ts | 7 +--- .../payments/refunds/PaymentRefundsBinder.ts | 31 ++++----------- src/binders/profiles/ProfilesBinder.ts | 15 ++----- .../ProfileGiftcardIssuersBinder.ts | 11 ++---- .../profiles/methods/ProfileMethodsBinder.ts | 11 ++---- .../ProfileVoucherIssuersBinder.ts | 11 ++---- .../refunds/orders/OrderRefundsBinder.ts | 15 ++----- .../payments/SubscriptionPaymentsBinder.ts | 19 +++------ src/plumbing/assertWellFormedId.ts | 39 +++++++++++++++++++ src/plumbing/checkId.ts | 29 -------------- 22 files changed, 130 insertions(+), 282 deletions(-) create mode 100644 src/plumbing/assertWellFormedId.ts delete mode 100644 src/plumbing/checkId.ts diff --git a/src/binders/customers/CustomersBinder.ts b/src/binders/customers/CustomersBinder.ts index a26f949a..53ecc486 100644 --- a/src/binders/customers/CustomersBinder.ts +++ b/src/binders/customers/CustomersBinder.ts @@ -2,9 +2,8 @@ import type TransformingNetworkClient from '../../communication/TransformingNetw import type Customer from '../../data/customers/Customer'; import { type CustomerData } from '../../data/customers/Customer'; import type Page from '../../data/page/Page'; -import ApiError from '../../errors/ApiError'; import alias from '../../plumbing/alias'; -import checkId from '../../plumbing/checkId'; +import assertWellFormedId from '../../plumbing/assertWellFormedId'; import renege from '../../plumbing/renege'; import type Callback from '../../types/Callback'; import Binder from '../Binder'; @@ -42,9 +41,7 @@ export default class CustomersBinder extends Binder<CustomerData, Customer> { public get(id: string, parameters: GetParameters, callback: Callback<Customer>): void; public get(id: string, parameters?: GetParameters) { if (renege(this, this.get, ...arguments)) return; - if (!checkId(id, 'customer')) { - throw new ApiError('The customer id is invalid'); - } + assertWellFormedId(id, 'customer'); return this.networkClient.get<CustomerData, Customer>(`${pathSegment}/${id}`, parameters); } @@ -86,9 +83,7 @@ export default class CustomersBinder extends Binder<CustomerData, Customer> { public update(id: string, parameters: UpdateParameters, callback: Callback<Customer>): void; public update(id: string, parameters: UpdateParameters) { if (renege(this, this.update, ...arguments)) return; - if (!checkId(id, 'customer')) { - throw new ApiError('The customer id is invalid'); - } + assertWellFormedId(id, 'customer'); return this.networkClient.patch<CustomerData, Customer>(`${pathSegment}/${id}`, parameters); } @@ -102,9 +97,7 @@ export default class CustomersBinder extends Binder<CustomerData, Customer> { public delete(id: string, parameters: DeleteParameters, callback: Callback<true>): void; public delete(id: string, parameters?: DeleteParameters) { if (renege(this, this.delete, ...arguments)) return; - if (!checkId(id, 'customer')) { - throw new ApiError('The customer id is invalid'); - } + assertWellFormedId(id, 'customer'); return this.networkClient.delete<CustomerData, true>(`${pathSegment}/${id}`, parameters); } } diff --git a/src/binders/customers/mandates/CustomerMandatesBinder.ts b/src/binders/customers/mandates/CustomerMandatesBinder.ts index 95633ad7..05dbb84f 100644 --- a/src/binders/customers/mandates/CustomerMandatesBinder.ts +++ b/src/binders/customers/mandates/CustomerMandatesBinder.ts @@ -2,9 +2,8 @@ import type TransformingNetworkClient from '../../../communication/TransformingN import { type MandateData } from '../../../data/customers/mandates/data'; import type Mandate from '../../../data/customers/mandates/Mandate'; import type Page from '../../../data/page/Page'; -import ApiError from '../../../errors/ApiError'; import alias from '../../../plumbing/alias'; -import checkId from '../../../plumbing/checkId'; +import assertWellFormedId from '../../../plumbing/assertWellFormedId'; import renege from '../../../plumbing/renege'; import type Callback from '../../../types/Callback'; import Binder from '../../Binder'; @@ -34,9 +33,7 @@ export default class CustomerMandatesBinder extends Binder<MandateData, Mandate> public create(parameters: CreateParameters) { if (renege(this, this.create, ...arguments)) return; const { customerId, ...data } = parameters; - if (!checkId(customerId, 'customer')) { - throw new ApiError('The customer id is invalid'); - } + assertWellFormedId(customerId, 'customer'); return this.networkClient.post<MandateData, Mandate>(getPathSegments(customerId), data); } @@ -50,13 +47,9 @@ export default class CustomerMandatesBinder extends Binder<MandateData, Mandate> public get(id: string, parameters: GetParameters, callback: Callback<Mandate>): void; public get(id: string, parameters: GetParameters) { if (renege(this, this.get, ...arguments)) return; - if (!checkId(id, 'mandate')) { - throw new ApiError('The customers_mandate id is invalid'); - } + assertWellFormedId(id, 'mandate'); const { customerId, ...query } = parameters; - if (!checkId(customerId, 'customer')) { - throw new ApiError('The customer id is invalid'); - } + assertWellFormedId(customerId, 'customer'); return this.networkClient.get<MandateData, Mandate>(`${getPathSegments(customerId)}/${id}`, query); } @@ -73,9 +66,7 @@ export default class CustomerMandatesBinder extends Binder<MandateData, Mandate> public page(parameters: PageParameters) { if (renege(this, this.page, ...arguments)) return; const { customerId, ...query } = parameters; - if (!checkId(customerId, 'customer')) { - throw new ApiError('The customer id is invalid'); - } + assertWellFormedId(customerId, 'customer'); return this.networkClient.page<MandateData, Mandate>(getPathSegments(customerId), 'mandates', query).then(result => this.injectPaginationHelpers(result, this.page, parameters)); } @@ -89,9 +80,7 @@ export default class CustomerMandatesBinder extends Binder<MandateData, Mandate> */ public iterate(parameters: IterateParameters) { const { customerId, valuesPerMinute, ...query } = parameters; - if (!checkId(customerId, 'customer')) { - throw new ApiError('The customer id is invalid'); - } + assertWellFormedId(customerId, 'customer'); return this.networkClient.iterate<MandateData, Mandate>(getPathSegments(customerId), 'mandates', query, valuesPerMinute); } @@ -105,13 +94,9 @@ export default class CustomerMandatesBinder extends Binder<MandateData, Mandate> public revoke(id: string, parameters: RevokeParameters, callback: Callback<true>): void; public revoke(id: string, parameters: RevokeParameters) { if (renege(this, this.revoke, ...arguments)) return; - if (!checkId(id, 'mandate')) { - throw new ApiError('The customers_mandate id is invalid'); - } + assertWellFormedId(id, 'mandate'); const { customerId, ...context } = parameters; - if (!checkId(customerId, 'customer')) { - throw new ApiError('The customer is invalid'); - } + assertWellFormedId(customerId, 'customer'); return this.networkClient.delete<MandateData, true>(`${getPathSegments(customerId)}/${id}`, context); } } diff --git a/src/binders/customers/payments/CustomerPaymentsBinder.ts b/src/binders/customers/payments/CustomerPaymentsBinder.ts index 420da5ff..8a4770f9 100644 --- a/src/binders/customers/payments/CustomerPaymentsBinder.ts +++ b/src/binders/customers/payments/CustomerPaymentsBinder.ts @@ -2,9 +2,8 @@ import type TransformingNetworkClient from '../../../communication/TransformingN import type Page from '../../../data/page/Page'; import { type PaymentData } from '../../../data/payments/data'; import type Payment from '../../../data/payments/Payment'; -import ApiError from '../../../errors/ApiError'; import alias from '../../../plumbing/alias'; -import checkId from '../../../plumbing/checkId'; +import assertWellFormedId from '../../../plumbing/assertWellFormedId'; import renege from '../../../plumbing/renege'; import type Callback from '../../../types/Callback'; import Binder from '../../Binder'; @@ -38,9 +37,7 @@ export default class CustomerPaymentsBinder extends Binder<PaymentData, Payment> public create(parameters: CreateParameters) { if (renege(this, this.create, ...arguments)) return; const { customerId, ...data } = parameters; - if (!checkId(customerId, 'customer')) { - throw new ApiError('The customer id is invalid'); - } + assertWellFormedId(customerId, 'customer'); return this.networkClient.post<PaymentData, Payment>(getPathSegments(customerId), data); } @@ -55,9 +52,7 @@ export default class CustomerPaymentsBinder extends Binder<PaymentData, Payment> public page(parameters: PageParameters) { if (renege(this, this.page, ...arguments)) return; const { customerId, ...query } = parameters; - if (!checkId(customerId, 'customer')) { - throw new ApiError('The customer id is invalid'); - } + assertWellFormedId(customerId, 'customer'); return this.networkClient.page<PaymentData, Payment>(getPathSegments(customerId), 'payments', query).then(result => this.injectPaginationHelpers(result, this.page, parameters)); } @@ -69,9 +64,7 @@ export default class CustomerPaymentsBinder extends Binder<PaymentData, Payment> */ public iterate(parameters: IterateParameters) { const { customerId, valuesPerMinute, ...query } = parameters; - if (!checkId(customerId, 'customer')) { - throw new ApiError('The customer id is invalid'); - } + assertWellFormedId(customerId, 'customer'); return this.networkClient.iterate<PaymentData, Payment>(getPathSegments(customerId), 'payments', query, valuesPerMinute); } } diff --git a/src/binders/customers/subscriptions/CustomerSubscriptionsBinder.ts b/src/binders/customers/subscriptions/CustomerSubscriptionsBinder.ts index e1816103..a84c6b3e 100644 --- a/src/binders/customers/subscriptions/CustomerSubscriptionsBinder.ts +++ b/src/binders/customers/subscriptions/CustomerSubscriptionsBinder.ts @@ -2,9 +2,8 @@ import type TransformingNetworkClient from '../../../communication/TransformingN import type Page from '../../../data/page/Page'; import { type SubscriptionData } from '../../../data/subscriptions/data'; import type Subscription from '../../../data/subscriptions/Subscription'; -import ApiError from '../../../errors/ApiError'; import alias from '../../../plumbing/alias'; -import checkId from '../../../plumbing/checkId'; +import assertWellFormedId from '../../../plumbing/assertWellFormedId'; import renege from '../../../plumbing/renege'; import type Callback from '../../../types/Callback'; import Binder from '../../Binder'; @@ -41,9 +40,7 @@ export default class CustomerSubscriptionsBinder extends Binder<SubscriptionData public create(parameters: CreateParameters) { if (renege(this, this.create, ...arguments)) return; const { customerId, ...data } = parameters; - if (!checkId(customerId, 'customer')) { - throw new ApiError('The customer id is invalid'); - } + assertWellFormedId(customerId, 'customer'); return this.networkClient.post<SubscriptionData, Subscription>(getPathSegments(customerId), data); } @@ -57,13 +54,9 @@ export default class CustomerSubscriptionsBinder extends Binder<SubscriptionData public get(id: string, parameters: GetParameters, callback: Callback<Subscription>): void; public get(id: string, parameters: GetParameters) { if (renege(this, this.get, ...arguments)) return; - if (!checkId(id, 'subscription')) { - throw new ApiError('The subscription id is invalid'); - } + assertWellFormedId(id, 'subscription'); const { customerId, ...query } = parameters; - if (!checkId(customerId, 'customer')) { - throw new ApiError('The customer id is invalid'); - } + assertWellFormedId(customerId, 'customer'); return this.networkClient.get<SubscriptionData, Subscription>(`${getPathSegments(customerId)}/${id}`, query); } @@ -78,9 +71,7 @@ export default class CustomerSubscriptionsBinder extends Binder<SubscriptionData public page(parameters: PageParameters) { if (renege(this, this.page, ...arguments)) return; const { customerId, ...query } = parameters; - if (!checkId(customerId, 'customer')) { - throw new ApiError('The customer id is invalid'); - } + assertWellFormedId(customerId, 'customer'); return this.networkClient.page<SubscriptionData, Subscription>(getPathSegments(customerId), 'subscriptions', query).then(result => this.injectPaginationHelpers(result, this.page, parameters)); } @@ -92,9 +83,7 @@ export default class CustomerSubscriptionsBinder extends Binder<SubscriptionData */ public iterate(parameters: IterateParameters) { const { customerId, valuesPerMinute, ...query } = parameters; - if (!checkId(customerId, 'customer')) { - throw new ApiError('The customer id is invalid'); - } + assertWellFormedId(customerId, 'customer'); return this.networkClient.iterate<SubscriptionData, Subscription>(getPathSegments(customerId), 'subscriptions', query, valuesPerMinute); } @@ -110,13 +99,9 @@ export default class CustomerSubscriptionsBinder extends Binder<SubscriptionData public update(id: string, parameters: UpdateParameters, callback: Callback<Subscription>): void; public update(id: string, parameters: UpdateParameters) { if (renege(this, this.update, ...arguments)) return; - if (!checkId(id, 'subscription')) { - throw new ApiError('The subscription id is invalid'); - } + assertWellFormedId(id, 'subscription'); const { customerId, ...data } = parameters; - if (!checkId(customerId, 'customer')) { - throw new ApiError('The customer is invalid'); - } + assertWellFormedId(customerId, 'customer'); return this.networkClient.patch<SubscriptionData, Subscription>(`${getPathSegments(customerId)}/${id}`, data); } @@ -130,13 +115,9 @@ export default class CustomerSubscriptionsBinder extends Binder<SubscriptionData public cancel(id: string, parameters: CancelParameters, callback: Callback<Subscription>): void; public cancel(id: string, parameters: CancelParameters) { if (renege(this, this.cancel, ...arguments)) return; - if (!checkId(id, 'subscription')) { - throw new ApiError('The subscription id is invalid'); - } + assertWellFormedId(id, 'subscription'); const { customerId, ...context } = parameters; - if (!checkId(customerId, 'customer')) { - throw new ApiError('The customer is invalid'); - } + assertWellFormedId(customerId, 'customer'); return this.networkClient.delete<SubscriptionData, Subscription>(`${getPathSegments(customerId)}/${id}`, context); } } diff --git a/src/binders/orders/OrdersBinder.ts b/src/binders/orders/OrdersBinder.ts index c5101f6b..7ac8de53 100644 --- a/src/binders/orders/OrdersBinder.ts +++ b/src/binders/orders/OrdersBinder.ts @@ -2,8 +2,7 @@ import type TransformingNetworkClient from '../../communication/TransformingNetw import type Page from '../../data/page/Page'; import { type OrderData } from '../../data/orders/data'; import type Order from '../../data/orders/Order'; -import ApiError from '../../errors/ApiError'; -import checkId from '../../plumbing/checkId'; +import assertWellFormedId from '../../plumbing/assertWellFormedId'; import renege from '../../plumbing/renege'; import type Callback from '../../types/Callback'; import Binder from '../Binder'; @@ -73,9 +72,7 @@ export default class OrdersBinder extends Binder<OrderData, Order> { public get(id: string, parameters: GetParameters, callback: Callback<Order>): void; public get(id: string, parameters?: GetParameters) { if (renege(this, this.get, ...arguments)) return; - if (!checkId(id, 'order')) { - throw new ApiError('The order id is invalid'); - } + assertWellFormedId(id, 'order'); return this.networkClient.get<OrderData, Order>(`${pathSegment}/${id}`, parameters); } @@ -120,9 +117,7 @@ export default class OrdersBinder extends Binder<OrderData, Order> { public update(id: string, parameters: UpdateParameters, callback: Callback<Order>): void; public update(id: string, parameters: UpdateParameters) { if (renege(this, this.update, ...arguments)) return; - if (!checkId(id, 'order')) { - throw new ApiError('The order id is invalid'); - } + assertWellFormedId(id, 'order'); return this.networkClient.patch<OrderData, Order>(`${pathSegment}/${id}`, parameters); } @@ -148,9 +143,7 @@ export default class OrdersBinder extends Binder<OrderData, Order> { public cancel(id: string, parameters: CancelParameters, callback: Callback<Order>): void; public cancel(id: string, parameters?: CancelParameters) { if (renege(this, this.cancel, ...arguments)) return; - if (!checkId(id, 'order')) { - throw new ApiError('The order id is invalid'); - } + assertWellFormedId(id, 'order'); return this.networkClient.delete<OrderData, Order>(`${pathSegment}/${id}`, parameters); } } diff --git a/src/binders/orders/orderlines/OrderLinesBinder.ts b/src/binders/orders/orderlines/OrderLinesBinder.ts index f97cadcd..2af7addd 100644 --- a/src/binders/orders/orderlines/OrderLinesBinder.ts +++ b/src/binders/orders/orderlines/OrderLinesBinder.ts @@ -1,9 +1,8 @@ import type TransformingNetworkClient from '../../../communication/TransformingNetworkClient'; import { type OrderData } from '../../../data/orders/data'; import type Order from '../../../data/orders/Order'; -import ApiError from '../../../errors/ApiError'; import alias from '../../../plumbing/alias'; -import checkId from '../../../plumbing/checkId'; +import assertWellFormedId from '../../../plumbing/assertWellFormedId'; import renege from '../../../plumbing/renege'; import type Callback from '../../../types/Callback'; import Binder from '../../Binder'; @@ -39,13 +38,9 @@ export default class OrderLinesBinder extends Binder<OrderData, Order> { public update(id: string, parameters: UpdateParameters, callback: Callback<Order>): void; public update(id: string, parameters: UpdateParameters) { if (renege(this, this.update, ...arguments)) return; - if (!checkId(id, 'orderline')) { - throw new ApiError('The orders_lines id is invalid'); - } + assertWellFormedId(id, 'orderline'); const { orderId, ...data } = parameters; - if (!checkId(orderId, 'order')) { - throw new ApiError('The order id is invalid'); - } + assertWellFormedId(orderId, 'order'); return this.networkClient.patch<OrderData, Order>(`${getPathSegments(orderId)}/${id}`, data); } @@ -73,9 +68,7 @@ export default class OrderLinesBinder extends Binder<OrderData, Order> { public cancel(parameters: CancelParameters) { if (renege(this, this.cancel, ...arguments)) return; const { orderId, ...data } = parameters; - if (!checkId(orderId, 'order')) { - throw new ApiError('The order id is invalid'); - } + assertWellFormedId(orderId, 'order'); return this.networkClient.delete<OrderData, true>(getPathSegments(orderId), data); } } diff --git a/src/binders/orders/shipments/OrderShipmentsBinder.ts b/src/binders/orders/shipments/OrderShipmentsBinder.ts index 5ac01fee..4996d90b 100644 --- a/src/binders/orders/shipments/OrderShipmentsBinder.ts +++ b/src/binders/orders/shipments/OrderShipmentsBinder.ts @@ -1,9 +1,8 @@ import type TransformingNetworkClient from '../../../communication/TransformingNetworkClient'; import type Shipment from '../../../data/orders/shipments/Shipment'; import { type ShipmentData } from '../../../data/orders/shipments/Shipment'; -import ApiError from '../../../errors/ApiError'; import alias from '../../../plumbing/alias'; -import checkId from '../../../plumbing/checkId'; +import assertWellFormedId from '../../../plumbing/assertWellFormedId'; import renege from '../../../plumbing/renege'; import type Callback from '../../../types/Callback'; import Binder from '../../Binder'; @@ -35,9 +34,7 @@ export default class OrderShipmentsBinder extends Binder<ShipmentData, Shipment> public create(parameters: CreateParameters) { if (renege(this, this.create, ...arguments)) return; const { orderId, ...data } = parameters; - if (!checkId(orderId, 'order')) { - throw new ApiError('The order id is invalid'); - } + assertWellFormedId(orderId, 'order'); return this.networkClient.post<ShipmentData, Shipment>(getPathSegments(orderId), data); } @@ -51,13 +48,9 @@ export default class OrderShipmentsBinder extends Binder<ShipmentData, Shipment> public get(id: string, parameters: GetParameters, callback: Callback<Shipment>): void; public get(id: string, parameters: GetParameters) { if (renege(this, this.get, ...arguments)) return; - if (!checkId(id, 'shipment')) { - throw new ApiError('The orders_shipments id is invalid'); - } + assertWellFormedId(id, 'shipment'); const { orderId, ...query } = parameters; - if (!checkId(orderId, 'order')) { - throw new ApiError('The order id is invalid'); - } + assertWellFormedId(orderId, 'order'); return this.networkClient.get<ShipmentData, Shipment>(`${getPathSegments(orderId)}/${id}`, query); } @@ -72,9 +65,7 @@ export default class OrderShipmentsBinder extends Binder<ShipmentData, Shipment> public list(parameters: ListParameters) { if (renege(this, this.list, ...arguments)) return; const { orderId, ...query } = parameters; - if (!checkId(orderId, 'order')) { - throw new ApiError('The order id is invalid'); - } + assertWellFormedId(orderId, 'order'); return this.networkClient.list<ShipmentData, Shipment>(getPathSegments(orderId), 'shipments', query); } @@ -88,13 +79,9 @@ export default class OrderShipmentsBinder extends Binder<ShipmentData, Shipment> public update(id: string, parameters: UpdateParameters, callback: Callback<Shipment>): void; public update(id: string, parameters: UpdateParameters) { if (renege(this, this.update, ...arguments)) return; - if (!checkId(id, 'shipment')) { - throw new ApiError('The orders_shipments id is invalid'); - } + assertWellFormedId(id, 'shipment'); const { orderId, ...data } = parameters; - if (!checkId(orderId, 'order')) { - throw new ApiError('The order id is invalid'); - } + assertWellFormedId(orderId, 'order'); return this.networkClient.patch<ShipmentData, Shipment>(`${getPathSegments(orderId)}/${id}`, data); } } diff --git a/src/binders/organizations/OrganizationsBinder.ts b/src/binders/organizations/OrganizationsBinder.ts index 79ff7541..17f234d2 100644 --- a/src/binders/organizations/OrganizationsBinder.ts +++ b/src/binders/organizations/OrganizationsBinder.ts @@ -1,8 +1,7 @@ import type TransformingNetworkClient from '../../communication/TransformingNetworkClient'; import type Organization from '../../data/organizations/Organizations'; import { type OrganizationData } from '../../data/organizations/Organizations'; -import ApiError from '../../errors/ApiError'; -import checkId from '../../plumbing/checkId'; +import assertWellFormedId from '../../plumbing/assertWellFormedId'; import renege from '../../plumbing/renege'; import type Callback from '../../types/Callback'; import Binder from '../Binder'; @@ -24,9 +23,7 @@ export default class OrganizationsBinder extends Binder<OrganizationData, Organi public get(id: string, callback: Callback<Organization>): void; public get(id: string) { if (renege(this, this.get, ...arguments)) return; - if (!checkId(id, 'organization')) { - throw new ApiError('The organization id is invalid'); - } + assertWellFormedId(id, 'organization'); return this.networkClient.get<OrganizationData, Organization>(`${pathSegment}/${id}`); } diff --git a/src/binders/paymentLinks/PaymentLinksBinder.ts b/src/binders/paymentLinks/PaymentLinksBinder.ts index 3f155fa3..76852e6e 100644 --- a/src/binders/paymentLinks/PaymentLinksBinder.ts +++ b/src/binders/paymentLinks/PaymentLinksBinder.ts @@ -2,8 +2,7 @@ import type TransformingNetworkClient from '../../communication/TransformingNetw import type Page from '../../data/page/Page'; import { type PaymentLinkData } from '../../data/paymentLinks/data'; import type PaymentLink from '../../data/paymentLinks/PaymentLink'; -import ApiError from '../../errors/ApiError'; -import checkId from '../../plumbing/checkId'; +import assertWellFormedId from '../../plumbing/assertWellFormedId'; import renege from '../../plumbing/renege'; import type Callback from '../../types/Callback'; import Binder from '../Binder'; @@ -40,9 +39,7 @@ export default class PaymentsLinksBinder extends Binder<PaymentLinkData, Payment public get(id: string, parameters: GetParameters, callback: Callback<PaymentLink>): void; public get(id: string, parameters?: GetParameters) { if (renege(this, this.get, ...arguments)) return; - if (!checkId(id, 'payment-link')) { - throw new ApiError('The payment link id is invalid'); - } + assertWellFormedId(id, 'payment-link'); return this.networkClient.get<PaymentLinkData, PaymentLink>(`${pathSegment}/${id}`, parameters); } diff --git a/src/binders/payments/PaymentsBinder.ts b/src/binders/payments/PaymentsBinder.ts index 17f9feac..dcb1beb9 100644 --- a/src/binders/payments/PaymentsBinder.ts +++ b/src/binders/payments/PaymentsBinder.ts @@ -2,9 +2,8 @@ import type TransformingNetworkClient from '../../communication/TransformingNetw import type Page from '../../data/page/Page'; import { type PaymentData } from '../../data/payments/data'; import type Payment from '../../data/payments/Payment'; -import ApiError from '../../errors/ApiError'; import alias from '../../plumbing/alias'; -import checkId from '../../plumbing/checkId'; +import assertWellFormedId from '../../plumbing/assertWellFormedId'; import renege from '../../plumbing/renege'; import type Callback from '../../types/Callback'; import Binder from '../Binder'; @@ -47,9 +46,7 @@ export default class PaymentsBinder extends Binder<PaymentData, Payment> { public get(id: string, parameters: GetParameters, callback: Callback<Payment>): void; public get(id: string, parameters?: GetParameters) { if (renege(this, this.get, ...arguments)) return; - if (!checkId(id, 'payment')) { - throw new ApiError('The payment id is invalid'); - } + assertWellFormedId(id, 'payment'); return this.networkClient.get<PaymentData, Payment>(`${pathSegment}/${id}`, parameters); } @@ -91,9 +88,7 @@ export default class PaymentsBinder extends Binder<PaymentData, Payment> { public update(id: string, parameters: UpdateParameters, callback: Callback<Payment>): void; public update(id: string, parameters: UpdateParameters) { if (renege(this, this.update, ...arguments)) return; - if (!checkId(id, 'payment')) { - throw new ApiError('The payment id is invalid'); - } + assertWellFormedId(id, 'payment'); return this.networkClient.patch<PaymentData, Payment>(`${pathSegment}/${id}`, parameters); } @@ -110,9 +105,7 @@ export default class PaymentsBinder extends Binder<PaymentData, Payment> { public cancel(id: string, parameters: CancelParameters, callback: Callback<Page<Payment>>): void; public cancel(id: string, parameters?: CancelParameters) { if (renege(this, this.cancel, ...arguments)) return; - if (!checkId(id, 'payment')) { - throw new ApiError('The payment id is invalid'); - } + assertWellFormedId(id, 'payment'); return this.networkClient.delete<PaymentData, Payment>(`${pathSegment}/${id}`, parameters); } } diff --git a/src/binders/payments/captures/PaymentCapturesBinder.ts b/src/binders/payments/captures/PaymentCapturesBinder.ts index 2bec810e..1bc319a5 100644 --- a/src/binders/payments/captures/PaymentCapturesBinder.ts +++ b/src/binders/payments/captures/PaymentCapturesBinder.ts @@ -2,9 +2,8 @@ import type TransformingNetworkClient from '../../../communication/TransformingN import type Page from '../../../data/page/Page'; import type Capture from '../../../data/payments/captures/Capture'; import { type CaptureData } from '../../../data/payments/captures/data'; -import ApiError from '../../../errors/ApiError'; import alias from '../../../plumbing/alias'; -import checkId from '../../../plumbing/checkId'; +import assertWellFormedId from '../../../plumbing/assertWellFormedId'; import renege from '../../../plumbing/renege'; import type Callback from '../../../types/Callback'; import Binder from '../../Binder'; @@ -33,13 +32,9 @@ export default class PaymentCapturesBinder extends Binder<CaptureData, Capture> public get(id: string, parameters: GetParameters, callback: Callback<Capture>): void; public get(id: string, parameters: GetParameters) { if (renege(this, this.get, ...arguments)) return; - if (!checkId(id, 'capture')) { - throw new ApiError('The capture id is invalid'); - } + assertWellFormedId(id, 'capture'); const { paymentId, ...query } = parameters; - if (!checkId(paymentId, 'payment')) { - throw new ApiError('The payment id is invalid'); - } + assertWellFormedId(paymentId, 'payment'); return this.networkClient.get<CaptureData, Capture>(`${getPathSegments(paymentId)}/${id}`, query); } @@ -57,9 +52,7 @@ export default class PaymentCapturesBinder extends Binder<CaptureData, Capture> public page(parameters: PageParameters) { if (renege(this, this.page, ...arguments)) return; const { paymentId, ...query } = parameters; - if (!checkId(paymentId, 'payment')) { - throw new ApiError('The payment id is invalid'); - } + assertWellFormedId(paymentId, 'payment'); return this.networkClient.page<CaptureData, Capture>(getPathSegments(paymentId), 'captures', query).then(result => this.injectPaginationHelpers(result, this.page, parameters)); } @@ -74,9 +67,7 @@ export default class PaymentCapturesBinder extends Binder<CaptureData, Capture> */ public iterate(parameters: IterateParameters) { const { paymentId, valuesPerMinute, ...query } = parameters; - if (!checkId(paymentId, 'payment')) { - throw new ApiError('The payment id is invalid'); - } + assertWellFormedId(paymentId, 'payment'); return this.networkClient.iterate<CaptureData, Capture>(getPathSegments(paymentId), 'captures', query, valuesPerMinute); } } diff --git a/src/binders/payments/chargebacks/PaymentChargebacksBinder.ts b/src/binders/payments/chargebacks/PaymentChargebacksBinder.ts index 521a27ac..f1881ebf 100644 --- a/src/binders/payments/chargebacks/PaymentChargebacksBinder.ts +++ b/src/binders/payments/chargebacks/PaymentChargebacksBinder.ts @@ -2,9 +2,8 @@ import type TransformingNetworkClient from '../../../communication/TransformingN import type Chargeback from '../../../data/chargebacks/Chargeback'; import { type ChargebackData } from '../../../data/chargebacks/Chargeback'; import type Page from '../../../data/page/Page'; -import ApiError from '../../../errors/ApiError'; import alias from '../../../plumbing/alias'; -import checkId from '../../../plumbing/checkId'; +import assertWellFormedId from '../../../plumbing/assertWellFormedId'; import renege from '../../../plumbing/renege'; import type Callback from '../../../types/Callback'; import Binder from '../../Binder'; @@ -32,13 +31,9 @@ export default class PaymentChargebacksBinder extends Binder<ChargebackData, Cha public get(id: string, parameters: GetParameters, callback: Callback<Chargeback>): void; public get(id: string, parameters: GetParameters) { if (renege(this, this.get, ...arguments)) return; - if (!checkId(id, 'refund')) { - throw new ApiError('The payments_refund id is invalid'); - } + assertWellFormedId(id, 'refund'); const { paymentId, ...query } = parameters; - if (!checkId(paymentId, 'payment')) { - throw new ApiError('The payment id is invalid'); - } + assertWellFormedId(paymentId, 'payment'); return this.networkClient.get<ChargebackData, Chargeback>(`${getPathSegments(paymentId)}/${id}`, query); } @@ -55,9 +50,7 @@ export default class PaymentChargebacksBinder extends Binder<ChargebackData, Cha public page(parameters: PageParameters) { if (renege(this, this.page, ...arguments)) return; const { paymentId, ...query } = parameters; - if (!checkId(paymentId, 'payment')) { - throw new ApiError('The payment id is invalid'); - } + assertWellFormedId(paymentId, 'payment'); return this.networkClient.page<ChargebackData, Chargeback>(getPathSegments(paymentId), 'chargebacks', query).then(result => this.injectPaginationHelpers(result, this.page, parameters)); } @@ -71,9 +64,7 @@ export default class PaymentChargebacksBinder extends Binder<ChargebackData, Cha */ public iterate(parameters: IterateParameters) { const { paymentId, valuesPerMinute, ...query } = parameters; - if (!checkId(paymentId, 'payment')) { - throw new ApiError('The payment id is invalid'); - } + assertWellFormedId(paymentId, 'payment'); return this.networkClient.iterate<ChargebackData, Chargeback>(getPathSegments(paymentId), 'chargebacks', query, valuesPerMinute); } } diff --git a/src/binders/payments/orders/OrderPaymentsBinder.ts b/src/binders/payments/orders/OrderPaymentsBinder.ts index 2ed1e7ef..4eceeb30 100644 --- a/src/binders/payments/orders/OrderPaymentsBinder.ts +++ b/src/binders/payments/orders/OrderPaymentsBinder.ts @@ -1,8 +1,7 @@ import type TransformingNetworkClient from '../../../communication/TransformingNetworkClient'; import { type PaymentData } from '../../../data/payments/data'; import type Payment from '../../../data/payments/Payment'; -import ApiError from '../../../errors/ApiError'; -import checkId from '../../../plumbing/checkId'; +import assertWellFormedId from '../../../plumbing/assertWellFormedId'; import renege from '../../../plumbing/renege'; import type Callback from '../../../types/Callback'; import Binder from '../../Binder'; @@ -33,9 +32,7 @@ export default class OrderPaymentsBinder extends Binder<PaymentData, Payment> { public create(parameters: CreateParameters) { if (renege(this, this.create, ...arguments)) return; const { orderId, ...data } = parameters; - if (!checkId(orderId, 'order')) { - throw new ApiError('The order id is invalid'); - } + assertWellFormedId(orderId, 'order'); return this.networkClient.post<PaymentData, Payment>(getPathSegments(orderId), data); } } diff --git a/src/binders/payments/refunds/PaymentRefundsBinder.ts b/src/binders/payments/refunds/PaymentRefundsBinder.ts index c223436b..801db36c 100644 --- a/src/binders/payments/refunds/PaymentRefundsBinder.ts +++ b/src/binders/payments/refunds/PaymentRefundsBinder.ts @@ -2,9 +2,8 @@ import type TransformingNetworkClient from '../../../communication/TransformingN import type Page from '../../../data/page/Page'; import { type RefundData } from '../../../data/refunds/data'; import type Refund from '../../../data/refunds/Refund'; -import ApiError from '../../../errors/ApiError'; import alias from '../../../plumbing/alias'; -import checkId from '../../../plumbing/checkId'; +import assertWellFormedId from '../../../plumbing/assertWellFormedId'; import renege from '../../../plumbing/renege'; import type Callback from '../../../types/Callback'; import Binder from '../../Binder'; @@ -31,9 +30,7 @@ export default class PaymentRefundsBinder extends Binder<RefundData, Refund> { public create(parameters: CreateParameters) { if (renege(this, this.create, ...arguments)) return; const { paymentId, ...data } = parameters; - if (!checkId(paymentId, 'payment')) { - throw new ApiError('The payment id is invalid'); - } + assertWellFormedId(paymentId, 'payment'); return this.networkClient.post<RefundData, Refund>(getPathSegments(paymentId), data); } @@ -49,13 +46,9 @@ export default class PaymentRefundsBinder extends Binder<RefundData, Refund> { public get(id: string, parameters: GetParameters, callback: Callback<Refund>): void; public get(id: string, parameters: GetParameters) { if (renege(this, this.get, ...arguments)) return; - if (!checkId(id, 'refund')) { - throw new ApiError('The payments_refund id is invalid'); - } + assertWellFormedId(id, 'refund'); const { paymentId, ...query } = parameters; - if (!checkId(paymentId, 'payment')) { - throw new ApiError('The payment id is invalid'); - } + assertWellFormedId(paymentId, 'payment'); return this.networkClient.get<RefundData, Refund>(`${getPathSegments(paymentId)}/${id}`, query); } @@ -72,9 +65,7 @@ export default class PaymentRefundsBinder extends Binder<RefundData, Refund> { public page(parameters: PageParameters) { if (renege(this, this.page, ...arguments)) return; const { paymentId, ...query } = parameters; - if (!checkId(paymentId, 'payment')) { - throw new ApiError('The payment id is invalid'); - } + assertWellFormedId(paymentId, 'payment'); return this.networkClient.page<RefundData, Refund>(getPathSegments(paymentId), 'refunds', query).then(result => this.injectPaginationHelpers(result, this.page, parameters)); } @@ -88,9 +79,7 @@ export default class PaymentRefundsBinder extends Binder<RefundData, Refund> { */ public iterate(parameters: IterateParameters) { const { paymentId, valuesPerMinute, ...query } = parameters; - if (!checkId(paymentId, 'payment')) { - throw new ApiError('The payment id is invalid'); - } + assertWellFormedId(paymentId, 'payment'); return this.networkClient.iterate<RefundData, Refund>(getPathSegments(paymentId), 'refunds', query, valuesPerMinute); } @@ -107,13 +96,9 @@ export default class PaymentRefundsBinder extends Binder<RefundData, Refund> { public cancel(id: string, parameters: CancelParameters, callback: Callback<Promise<true>>): void; public cancel(id: string, parameters: CancelParameters) { if (renege(this, this.cancel, ...arguments)) return; - if (!checkId(id, 'refund')) { - throw new ApiError('The payments_refund id is invalid'); - } + assertWellFormedId(id, 'refund'); const { paymentId, ...context } = parameters; - if (!checkId(paymentId, 'payment')) { - throw new ApiError('The payment id is invalid'); - } + assertWellFormedId(paymentId, 'payment'); return this.networkClient.delete<RefundData, true>(`${getPathSegments(paymentId)}/${id}`, context); } } diff --git a/src/binders/profiles/ProfilesBinder.ts b/src/binders/profiles/ProfilesBinder.ts index 24763900..a6b031a2 100644 --- a/src/binders/profiles/ProfilesBinder.ts +++ b/src/binders/profiles/ProfilesBinder.ts @@ -2,9 +2,8 @@ import type TransformingNetworkClient from '../../communication/TransformingNetw import type Page from '../../data/page/Page'; import { type ProfileData } from '../../data/profiles/data'; import type Profile from '../../data/profiles/Profile'; -import ApiError from '../../errors/ApiError'; import alias from '../../plumbing/alias'; -import checkId from '../../plumbing/checkId'; +import assertWellFormedId from '../../plumbing/assertWellFormedId'; import renege from '../../plumbing/renege'; import type Callback from '../../types/Callback'; import Binder from '../Binder'; @@ -42,9 +41,7 @@ export default class ProfilesBinder extends Binder<ProfileData, Profile> { public get(id: string, callback: Callback<Profile>): void; public get(id: string) { if (renege(this, this.get, ...arguments)) return; - if (!checkId(id, 'profile')) { - throw new ApiError('The profile id is invalid'); - } + assertWellFormedId(id, 'profile'); return this.networkClient.get<ProfileData, Profile>(`${pathSegment}/${id}`); } @@ -103,9 +100,7 @@ export default class ProfilesBinder extends Binder<ProfileData, Profile> { public update(id: string, parameters: UpdateParameters, callback: Callback<Profile>): void; public update(id: string, parameters: UpdateParameters) { if (renege(this, this.update, ...arguments)) return; - if (!checkId(id, 'profile')) { - throw new ApiError('The profile id is invalid'); - } + assertWellFormedId(id, 'profile'); return this.networkClient.patch<ProfileData, Profile>(`${pathSegment}/${id}`, parameters); } @@ -119,9 +114,7 @@ export default class ProfilesBinder extends Binder<ProfileData, Profile> { public delete(id: string, parameters: DeleteParameters, callback: Callback<true>): void; public delete(id: string, parameters?: DeleteParameters) { if (renege(this, this.delete, ...arguments)) return; - if (!checkId(id, 'profile')) { - throw new ApiError('The profile id is invalid'); - } + assertWellFormedId(id, 'profile'); return this.networkClient.delete<ProfileData, true>(`${pathSegment}/${id}`, parameters); } } diff --git a/src/binders/profiles/giftcardIssuers/ProfileGiftcardIssuersBinder.ts b/src/binders/profiles/giftcardIssuers/ProfileGiftcardIssuersBinder.ts index 6311ebcb..a16554a4 100644 --- a/src/binders/profiles/giftcardIssuers/ProfileGiftcardIssuersBinder.ts +++ b/src/binders/profiles/giftcardIssuers/ProfileGiftcardIssuersBinder.ts @@ -1,9 +1,8 @@ import type TransformingNetworkClient from '../../../communication/TransformingNetworkClient'; import { type IssuerData } from '../../../data/issuer/IssuerModel'; import type IssuerModel from '../../../data/issuer/IssuerModel'; -import ApiError from '../../../errors/ApiError'; import renege from '../../../plumbing/renege'; -import checkId from '../../../plumbing/checkId'; +import assertWellFormedId from '../../../plumbing/assertWellFormedId'; import type Callback from '../../../types/Callback'; import Binder from '../../Binder'; import { type Parameters } from './parameters'; @@ -28,9 +27,7 @@ export default class ProfileGiftcardIssuersBinder extends Binder<IssuerData, Iss public enable(parameters: Parameters) { if (renege(this, this.enable, ...arguments)) return; const { id, profileId, ...data } = parameters; - if (!checkId(profileId, 'profile')) { - throw new ApiError('The profile id is invalid'); - } + assertWellFormedId(profileId, 'profile'); return this.networkClient.post(`${getPathSegments(profileId)}/${id}`, data); } @@ -45,9 +42,7 @@ export default class ProfileGiftcardIssuersBinder extends Binder<IssuerData, Iss public disable(parameters: Parameters) { if (renege(this, this.disable, ...arguments)) return; const { id, profileId, ...context } = parameters; - if (!checkId(profileId, 'profile')) { - throw new ApiError('The profile id is invalid'); - } + assertWellFormedId(profileId, 'profile'); return this.networkClient.delete<IssuerData, true>(`${getPathSegments(profileId)}/${id}`, context); } } diff --git a/src/binders/profiles/methods/ProfileMethodsBinder.ts b/src/binders/profiles/methods/ProfileMethodsBinder.ts index 026705cc..da285920 100644 --- a/src/binders/profiles/methods/ProfileMethodsBinder.ts +++ b/src/binders/profiles/methods/ProfileMethodsBinder.ts @@ -1,9 +1,8 @@ import type TransformingNetworkClient from '../../../communication/TransformingNetworkClient'; import { type MethodData } from '../../../data/methods/data'; import type Method from '../../../data/methods/Method'; -import ApiError from '../../../errors/ApiError'; import renege from '../../../plumbing/renege'; -import checkId from '../../../plumbing/checkId'; +import assertWellFormedId from '../../../plumbing/assertWellFormedId'; import type Callback from '../../../types/Callback'; import Binder from '../../Binder'; import { type Parameters } from './parameters'; @@ -30,9 +29,7 @@ export default class ProfileMethodsBinder extends Binder<MethodData, Method> { public enable(parameters: Parameters) { if (renege(this, this.enable, ...arguments)) return; const { id, profileId, ...data } = parameters; - if (!checkId(profileId, 'profile')) { - throw new ApiError('The profile id is invalid'); - } + assertWellFormedId(profileId, 'profile'); return this.networkClient.post(`${getPathSegments(profileId)}/${id}`, data); } @@ -47,9 +44,7 @@ export default class ProfileMethodsBinder extends Binder<MethodData, Method> { public disable(parameters: Parameters) { if (renege(this, this.disable, ...arguments)) return; const { id, profileId, ...context } = parameters; - if (!checkId(profileId, 'profile')) { - throw new ApiError('The profile id is invalid'); - } + assertWellFormedId(profileId, 'profile'); return this.networkClient.delete<MethodData, true>(`${getPathSegments(profileId)}/${id}`, context); } } diff --git a/src/binders/profiles/voucherIssuers/ProfileVoucherIssuersBinder.ts b/src/binders/profiles/voucherIssuers/ProfileVoucherIssuersBinder.ts index 3a1b924d..198235c4 100644 --- a/src/binders/profiles/voucherIssuers/ProfileVoucherIssuersBinder.ts +++ b/src/binders/profiles/voucherIssuers/ProfileVoucherIssuersBinder.ts @@ -1,8 +1,7 @@ import type TransformingNetworkClient from '../../../communication/TransformingNetworkClient'; import type IssuerModel from '../../../data/issuer/IssuerModel'; import { type IssuerData } from '../../../data/issuer/IssuerModel'; -import ApiError from '../../../errors/ApiError'; -import checkId from '../../../plumbing/checkId'; +import assertWellFormedId from '../../../plumbing/assertWellFormedId'; import renege from '../../../plumbing/renege'; import type Callback from '../../../types/Callback'; import Binder from '../../Binder'; @@ -28,9 +27,7 @@ export default class ProfileVoucherIssuersBinder extends Binder<IssuerData, Issu public enable(parameters: CreateParameters) { if (renege(this, this.enable, ...arguments)) return; const { id, profileId, ...data } = parameters; - if (!checkId(profileId, 'profile')) { - throw new ApiError('The profile id is invalid'); - } + assertWellFormedId(profileId, 'profile'); return this.networkClient.post(`${getPathSegments(profileId)}/${id}`, data); } @@ -45,9 +42,7 @@ export default class ProfileVoucherIssuersBinder extends Binder<IssuerData, Issu public disable(parameters: Parameters) { if (renege(this, this.disable, ...arguments)) return; const { id, profileId, ...context } = parameters; - if (!checkId(profileId, 'profile')) { - throw new ApiError('The profile id is invalid'); - } + assertWellFormedId(profileId, 'profile'); return this.networkClient.delete<IssuerData, true>(`${getPathSegments(profileId)}/${id}`, context); } } diff --git a/src/binders/refunds/orders/OrderRefundsBinder.ts b/src/binders/refunds/orders/OrderRefundsBinder.ts index 5d44159b..9c3b8256 100644 --- a/src/binders/refunds/orders/OrderRefundsBinder.ts +++ b/src/binders/refunds/orders/OrderRefundsBinder.ts @@ -2,9 +2,8 @@ import type TransformingNetworkClient from '../../../communication/TransformingN import type Page from '../../../data/page/Page'; import { type RefundData } from '../../../data/refunds/data'; import type Refund from '../../../data/refunds/Refund'; -import ApiError from '../../../errors/ApiError'; import alias from '../../../plumbing/alias'; -import checkId from '../../../plumbing/checkId'; +import assertWellFormedId from '../../../plumbing/assertWellFormedId'; import renege from '../../../plumbing/renege'; import type Callback from '../../../types/Callback'; import Binder from '../../Binder'; @@ -37,9 +36,7 @@ export default class OrderRefundsBinder extends Binder<RefundData, Refund> { public create(parameters: CreateParameters) { if (renege(this, this.create, ...arguments)) return; const { orderId, ...data } = parameters; - if (!checkId(orderId, 'order')) { - throw new ApiError('The order id is invalid'); - } + assertWellFormedId(orderId, 'order'); return this.networkClient.post<RefundData, Refund>(getPathSegments(orderId), data); } @@ -56,9 +53,7 @@ export default class OrderRefundsBinder extends Binder<RefundData, Refund> { public page(parameters: PageParameters) { if (renege(this, this.page, ...arguments)) return; const { orderId, ...query } = parameters; - if (!checkId(orderId, 'order')) { - throw new ApiError('The order id is invalid'); - } + assertWellFormedId(orderId, 'order'); return this.networkClient.page<RefundData, Refund>(getPathSegments(orderId), 'refunds', query).then(result => this.injectPaginationHelpers(result, this.page, parameters)); } @@ -72,9 +67,7 @@ export default class OrderRefundsBinder extends Binder<RefundData, Refund> { */ public iterate(parameters: IterateParameters) { const { orderId, valuesPerMinute, ...query } = parameters; - if (!checkId(orderId, 'order')) { - throw new ApiError('The order id is invalid'); - } + assertWellFormedId(orderId, 'order'); return this.networkClient.iterate<RefundData, Refund>(getPathSegments(orderId), 'refunds', query, valuesPerMinute); } } diff --git a/src/binders/subscriptions/payments/SubscriptionPaymentsBinder.ts b/src/binders/subscriptions/payments/SubscriptionPaymentsBinder.ts index 1c3643e8..6c25cb69 100644 --- a/src/binders/subscriptions/payments/SubscriptionPaymentsBinder.ts +++ b/src/binders/subscriptions/payments/SubscriptionPaymentsBinder.ts @@ -2,9 +2,8 @@ import type TransformingNetworkClient from '../../../communication/TransformingN import type Page from '../../../data/page/Page'; import { type PaymentData } from '../../../data/payments/data'; import type Payment from '../../../data/payments/Payment'; -import ApiError from '../../../errors/ApiError'; import alias from '../../../plumbing/alias'; -import checkId from '../../../plumbing/checkId'; +import assertWellFormedId from '../../../plumbing/assertWellFormedId'; import renege from '../../../plumbing/renege'; import type Callback from '../../../types/Callback'; import Binder from '../../Binder'; @@ -31,12 +30,8 @@ export default class SubscriptionPaymentsBinder extends Binder<PaymentData, Paym public page(parameters: PageParameters) { if (renege(this, this.page, ...arguments)) return; const { customerId, subscriptionId, ...query } = parameters; - if (!checkId(customerId, 'customer')) { - throw new ApiError('The customer id is invalid'); - } - if (!checkId(subscriptionId, 'subscription')) { - throw new ApiError('The subscription id is invalid'); - } + assertWellFormedId(customerId, 'customer'); + assertWellFormedId(subscriptionId, 'subscription'); return this.networkClient.page<PaymentData, Payment>(getPathSegments(customerId, subscriptionId), 'payments', query).then(result => this.injectPaginationHelpers(result, this.page, parameters)); } @@ -48,12 +43,8 @@ export default class SubscriptionPaymentsBinder extends Binder<PaymentData, Paym */ public iterate(parameters: IterateParameters) { const { customerId, subscriptionId, valuesPerMinute, ...query } = parameters; - if (!checkId(customerId, 'customer')) { - throw new ApiError('The customer id is invalid'); - } - if (!checkId(subscriptionId, 'subscription')) { - throw new ApiError('The subscription id is invalid'); - } + assertWellFormedId(customerId, 'customer'); + assertWellFormedId(subscriptionId, 'subscription'); return this.networkClient.iterate<PaymentData, Payment>(getPathSegments(customerId, subscriptionId), 'payments', query, valuesPerMinute); } } diff --git a/src/plumbing/assertWellFormedId.ts b/src/plumbing/assertWellFormedId.ts new file mode 100644 index 00000000..610205ac --- /dev/null +++ b/src/plumbing/assertWellFormedId.ts @@ -0,0 +1,39 @@ +import ApiError from '../errors/ApiError'; +import type Maybe from '../types/Maybe'; + +const prefixes = { + 'capture': 'cpt_', + 'chargeback': 'chb_', + 'customer': 'cst_', + 'mandate': 'mdt_', + 'order': 'ord_', + 'orderline': 'odl_', + 'organization': 'org_', + 'payment': 'tr_', + 'payment-link': 'pl_', + 'profile': 'pfl_', + 'refund': 're_', + 'shipment': 'shp_', + 'subscription': 'sub_', +} satisfies Record<string, string>; + +type ResourceKind = keyof typeof prefixes; + +/** + * Returns whether the passed identifier seems plausible (`true`); or is definitely invalid (`false`). + */ +function checkId(value: Maybe<string>, resource: ResourceKind): value is string { + if (typeof value != 'string') { + return false; + } + return value.startsWith(prefixes[resource]); +} + +/** + * Asserts that the passed value is a well-formed identifier for the given resource as in 'this looks like it could be a valid identifier for this type of resource'. + */ +export default function assertWellFormedId(value: Maybe<string>, resource: ResourceKind): asserts value is string { + if (!checkId(value, resource)) { + throw new ApiError(`The ${resource} id appears invalid: ${value} (unexpected format)`); + } +} \ No newline at end of file diff --git a/src/plumbing/checkId.ts b/src/plumbing/checkId.ts deleted file mode 100644 index 7249822c..00000000 --- a/src/plumbing/checkId.ts +++ /dev/null @@ -1,29 +0,0 @@ -import type Maybe from '../types/Maybe'; - -type ResourceKind = 'capture' | 'chargeback' | 'customer' | 'mandate' | 'order' | 'orderline' | 'organization' | 'payment' | 'payment-link' | 'profile' | 'refund' | 'shipment' | 'subscription'; - -const prefixes = new Map<ResourceKind, string>([ - ['capture', 'cpt_'], - ['chargeback', 'chb_'], - ['customer', 'cst_'], - ['mandate', 'mdt_'], - ['order', 'ord_'], - ['orderline', 'odl_'], - ['organization', 'org_'], - ['payment', 'tr_'], - ['payment-link', 'pl_'], - ['profile', 'pfl_'], - ['refund', 're_'], - ['shipment', 'shp_'], - ['subscription', 'sub_'], -]); -/** - * Returns whether the passed identifier seems plausible (`true`); or is definitely invalid (`false`). - */ -export default function checkId(value: Maybe<string>, resource: ResourceKind): value is string { - if (typeof value != 'string') { - return false; - } - // eslint-disable-next-line @typescript-eslint/no-non-null-assertion - return value.startsWith(prefixes.get(resource)!); -}