Skip to content

Commit

Permalink
add MaybeArray utility type for consistency
Browse files Browse the repository at this point in the history
  • Loading branch information
janpaepke committed Sep 27, 2024
1 parent b381e69 commit abb1ceb
Show file tree
Hide file tree
Showing 9 changed files with 23 additions and 11 deletions.
3 changes: 2 additions & 1 deletion src/Options.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type MaybeArray from './types/MaybeArray';
import type Xor from './types/Xor';

type Options = Xor<
Expand All @@ -17,7 +18,7 @@ type Options = Xor<
/**
* One or an array of version strings of the software you are using, such as `'RockenbergCommerce/3.1.12'`.
*/
versionStrings?: string | string[];
versionStrings?: MaybeArray<string>;
/**
* The headers set in the requests sent to the Mollie API. `Authorization`, `User-Agent`, `Accept`,
* `Accept-Encoding`, and `Content-Type` are set by this library directly. Setting them here has no effect.
Expand Down
3 changes: 2 additions & 1 deletion src/binders/customers/payments/parameters.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { type PaymentMethod } from '../../../data/global';
import { type PaymentData } from '../../../data/payments/data';
import type MaybeArray from '../../../types/MaybeArray';
import { type IdempotencyParameter, type PaginationParameters, type ThrottlingParameter } from '../../../types/parameters';
import type PickOptional from '../../../types/PickOptional';

Expand All @@ -21,7 +22,7 @@ export type CreateParameters = ContextParameters &
*
* @see https://docs.mollie.com/reference/v2/payments-api/create-payment?path=method#parameters
*/
method?: PaymentMethod | PaymentMethod[];
method?: MaybeArray<PaymentMethod>;
} & IdempotencyParameter;

export type PageParameters = ContextParameters & PaginationParameters;
Expand Down
5 changes: 3 additions & 2 deletions src/binders/methods/parameters.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { type Amount, type Locale, type SequenceType } from '../../data/global';
import { type MethodInclude } from '../../data/methods/data';
import type MaybeArray from '../../types/MaybeArray';

export interface GetParameters {
/**
Expand All @@ -10,7 +11,7 @@ export interface GetParameters {
* @see https://docs.mollie.com/reference/v2/methods-api/get-method?path=locale#parameters
*/
locale?: Locale;
include?: MethodInclude[] | MethodInclude;
include?: MaybeArray<MethodInclude>;
profileId?: string;
testmode?: boolean;
/**
Expand Down Expand Up @@ -103,7 +104,7 @@ export interface ListParameters {
* @see https://docs.mollie.com/reference/v2/methods-api/list-methods?path=orderLineCategories#parameters
*/
orderLineCategories?: string[];
include?: MethodInclude[] | MethodInclude;
include?: MaybeArray<MethodInclude>;
profileId?: string;
testmode?: boolean;
}
3 changes: 2 additions & 1 deletion src/binders/orders/parameters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { type OrderLineData } from '../../data/orders/orderlines/OrderLine';
import { type IdempotencyParameter, type PaginationParameters, type ThrottlingParameter } from '../../types/parameters';
import { type CreateParameters as PaymentCreateParameters } from '../payments/parameters';
import type PickOptional from '../../types/PickOptional';
import type MaybeArray from '../../types/MaybeArray';

export type CreateParameters = Pick<OrderData, 'amount' | 'orderNumber' | 'consumerDateOfBirth' | 'webhookUrl' | 'locale' | 'expiresAt'> &
PickOptional<OrderData, 'billingAddress' | 'shippingAddress' | 'redirectUrl' | 'cancelUrl' | 'metadata'> & {
Expand Down Expand Up @@ -49,7 +50,7 @@ export type CreateParameters = Pick<OrderData, 'amount' | 'orderNumber' | 'consu
*
* @see https://docs.mollie.com/reference/v2/orders-api/create-order?path=method#parameters
*/
method?: PaymentMethod | PaymentMethod[];
method?: MaybeArray<PaymentMethod>;
/**
* Any payment specific properties (for example, the `dueDate` for bank transfer payments) can be passed here. See payment-parameters for the possible fields.
*
Expand Down
3 changes: 2 additions & 1 deletion src/binders/payments/orders/parameters.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { type PaymentMethod } from '../../../data/global';
import { type PaymentData } from '../../../data/payments/data';
import type MaybeArray from '../../../types/MaybeArray';
import { type IdempotencyParameter } from '../../../types/parameters';

interface ContextParameters {
Expand All @@ -21,7 +22,7 @@ export type CreateParameters = ContextParameters &
*
* @see https://docs.mollie.com/reference/v2/orders-api/create-order-payment?path=method#parameters
*/
method?: PaymentMethod | PaymentMethod[];
method?: MaybeArray<PaymentMethod>;
/**
* The ID of the customer for whom the payment is being created. This is used for recurring payments and single-click payments.
*
Expand Down
7 changes: 4 additions & 3 deletions src/binders/payments/parameters.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { type Address, type Amount, type PaymentMethod } from '../../data/global';
import { type Issuer } from '../../data/Issuer';
import { type PaymentData, type PaymentEmbed, type PaymentInclude } from '../../data/payments/data';
import type MaybeArray from '../../types/MaybeArray';
import { type IdempotencyParameter, type PaginationParameters, type ThrottlingParameter } from '../../types/parameters';
import type PickOptional from '../../types/PickOptional';

Expand All @@ -17,7 +18,7 @@ export type CreateParameters = Pick<PaymentData, 'amount' | 'description' | 'red
*
* @see https://docs.mollie.com/reference/v2/payments-api/create-payment?path=method#parameters
*/
method?: PaymentMethod | PaymentMethod[];
method?: MaybeArray<PaymentMethod>;
/**
* For digital goods in most jurisdictions, you must apply the VAT rate from your customer's country. Choose the VAT rates you have used for the order to ensure your customer's country matches the
* VAT country.
Expand Down Expand Up @@ -132,7 +133,7 @@ export type CreateParameters = Pick<PaymentData, 'amount' | 'description' | 'red
* @see https://docs.mollie.com/reference/v2/payments-api/create-payment?path=consumerAccount#sepa-direct-debit
*/
consumerAccount?: string;
include?: PaymentInclude[] | PaymentInclude;
include?: MaybeArray<PaymentInclude>;
profileId?: string;
testmode?: boolean;
/**
Expand Down Expand Up @@ -163,7 +164,7 @@ export type CreateParameters = Pick<PaymentData, 'amount' | 'description' | 'red

export interface GetParameters {
include?: PaymentInclude;
embed?: PaymentEmbed[] | PaymentEmbed;
embed?: MaybeArray<PaymentEmbed>;
testmode?: boolean;
}

Expand Down
3 changes: 2 additions & 1 deletion src/communication/NetworkClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import DemandingIterator from '../plumbing/iteration/DemandingIterator';
import HelpfulIterator from '../plumbing/iteration/HelpfulIterator';
import Throttler from '../plumbing/Throttler';
import type Maybe from '../types/Maybe';
import type MaybeArray from '../types/MaybeArray';
import { type IdempotencyParameter } from '../types/parameters';
import breakUrl from './breakUrl';
import buildUrl, { type SearchParameters } from './buildUrl';
Expand All @@ -23,7 +24,7 @@ import { URL } from 'url';
* Like `[].map` but with support for non-array inputs, in which case this function behaves as if an array was passed
* with the input as its sole element.
*/
function map<T, U>(input: Maybe<T | T[]>, callback: (value: T, index: number) => U, context?: any): U[] {
function map<T, U>(input: Maybe<MaybeArray<T>>, callback: (value: T, index: number) => U, context?: any): U[] {
if (Array.isArray(input)) {
return input.map(callback, context);
}
Expand Down
4 changes: 3 additions & 1 deletion src/communication/buildUrl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
import { URLSearchParams } from 'url';

import { apply, runIf } from 'ruply';

import type Maybe from '../types/Maybe';
import type MaybeArray from '../types/MaybeArray';

export type SearchParameters = Record<string, any>;

Expand All @@ -25,7 +27,7 @@ export default function buildUrl(originAndPathname: string, searchParameters?: S
return originAndPathname;
}
return `${originAndPathname}?${new URLSearchParams(
apply({} as Record<string, string | string[]>, flattenedEntries => {
apply({} as Record<string, MaybeArray<string>>, flattenedEntries => {
for (const [key, value] of searchEntries) {
if (value == undefined) {
continue;
Expand Down
3 changes: 3 additions & 0 deletions src/types/MaybeArray.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
type MaybeArray<T> = T | T[];

export default MaybeArray;

0 comments on commit abb1ceb

Please sign in to comment.