Skip to content

Commit

Permalink
Move credentials check.
Browse files Browse the repository at this point in the history
  • Loading branch information
Pimm committed Jul 5, 2024
1 parent c76a5d7 commit 70edc2e
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 23 deletions.
26 changes: 26 additions & 0 deletions src/Options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,30 @@ type Options = Xor<
apiEndpoint?: string;
} & Pick<AxiosRequestConfig, 'adapter' | 'proxy' | 'socketPath' | 'timeout'>;

const falsyDescriptions = new Map<any, string>([
[undefined, 'undefined'],
[null, 'null'],
['', 'an empty string'],
]);

/**
* Returns an error message (string) similar to `"Parameter "×" is null."` if a property with the passed key exists in
* the passed options object, or `null` if said property does not exist.
*/
function describeFalsyOption(options: Options, key: keyof Options) {
if (key in options == false) {
return null;
}
return `Parameter "${key}" is ${falsyDescriptions.get(options[key]) ?? options[key]}.`;
};

/**
* Throws a `TypeError` if the passed options object does not contain an `apiKey` or an `accessToken`.
*/
export function checkCredentials(options: Options) {
if (!options.apiKey && !options.accessToken) {
throw new TypeError(describeFalsyOption(options, 'apiKey') ?? describeFalsyOption(options, 'accessToken') ?? 'Missing parameter "apiKey" or "accessToken".');
}
}

export default Options;
25 changes: 2 additions & 23 deletions src/createMollieClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import { version as libraryVersion } from '../package.json';
import caCertificates from './cacert.pem';
import NetworkClient from './communication/NetworkClient';
import TransformingNetworkClient, { Transformers } from './communication/TransformingNetworkClient';
import { checkCredentials } from './Options';
import type Options from './Options';
import { run } from 'ruply';

// Transformers
import { transform as transformPayment } from './data/payments/Payment';
Expand Down Expand Up @@ -59,25 +59,6 @@ import SettlementsBinder from './binders/settlements/SettlementsBinder';
import SubscriptionsBinder from './binders/subscriptions/SubscriptionsBinder';
import SubscriptionPaymentsBinder from './binders/subscriptions/payments/SubscriptionPaymentsBinder';

/**
* Returns an error message (string) similar to `"Parameter "×" is null."` if a property with the passed key exists in
* the passed object, or `null` if said property does not exist.
*/
const describeFalsyOption = run(
new Map([
[undefined, 'undefined'],
[null, 'null'],
['', 'an empty string'],
]) as Map<any, string>,
descriptions =>
function describeFalsyOption<O extends Record<string, unknown>>(object: O, key: keyof O & string) {
if (key in object == false) {
return null;
}
return `Parameter "${key}" is ${descriptions.get(object[key]) ?? object[key]}.`;
},
);

/**
* Create Mollie client.
* @since 2.0.0
Expand All @@ -90,9 +71,7 @@ export default function createMollieClient(options: Options) {
);
}

if (!options.apiKey && !options.accessToken) {
throw new TypeError(describeFalsyOption(options, 'apiKey') ?? describeFalsyOption(options, 'accessToken') ?? 'Missing parameter "apiKey" or "accessToken".');
}
checkCredentials(options);

const networkClient = new NetworkClient({ ...options, libraryVersion, nodeVersion: process.version, caCertificates });

Expand Down

0 comments on commit 70edc2e

Please sign in to comment.