@mollie/api-client
Version:
Official Mollie API client for Node
30 lines (29 loc) • 1.86 kB
TypeScript
import { type ParameterDefaults } from '../Options';
type NonFunction<T> = T extends (...args: any) => unknown ? never : T;
/**
* The parameters object accepted by a binder method ‒ its single non-callback object argument. Methods in this library
* take the shape `(id?, parameters?, callback?)`, so this extracts the `parameters` type regardless of whether an `id`
* precedes it (the `id` is a `string`, the callback is a function, both of which are filtered out).
*/
type ParametersObject<F> = F extends (...args: any) => unknown ? NonFunction<Extract<Parameters<F>[number], object>> : never;
/**
* Maps each method of a binder to the client-default keys that method's parameters actually declare. Listing a key a
* method does not accept is a compile error, which prevents configuring a default the Mollie API would reject. (A
* homomorphic mapped type ‒ rather than a filtered one ‒ so it resolves against the polymorphic `this` at the call
* site; non-method properties resolve to `never` parameters and are simply never listed.)
*/
type DefaultsConfig<T> = {
[P in keyof T]?: Array<keyof ParameterDefaults & keyof ParametersObject<T[P]>>;
};
/**
* Wraps the binder methods named in `config` so that, when called, the client-level `defaults` are filled into the
* method's parameters object for the listed keys ‒ but only where the caller did not already specify a value. The
* wrapped methods are otherwise unchanged, and the passed parameters object is never mutated.
*
* Like `alias`, this is meant to be called from a binder constructor. It must be called BEFORE `alias`, so that any
* aliases are created from the wrapped methods.
*/
export default function withParameterDefaults<T extends object>(target: T, networkClient: {
parameterDefaults?: Readonly<ParameterDefaults>;
}, config: DefaultsConfig<T>): void;
export {};