Skip to content

Commit

Permalink
Add apiEndpoint option.
Browse files Browse the repository at this point in the history
  • Loading branch information
Pimm committed Oct 13, 2020
1 parent 31a38d1 commit ae3705f
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 37 deletions.
69 changes: 36 additions & 33 deletions src/createMollieClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,25 +31,29 @@ import RefundsResource from './resources/refunds/RefundsResource';
import SubscriptionsResource from './resources/subscriptions/SubscriptionsResource';
import SubscriptionsPaymentsResource from './resources/subscriptions/payments/SubscriptionsPaymentsResource';

export type MollieOptions = AxiosRequestConfig & {
export type MollieOptions = Xor<
{
/**
* The Mollie API key, starting with `'test_'` or `'live_'`.
*/
apiKey: string;
},
{
/**
* OAuth access token, starting with `'access_''.
*/
accessToken: string;
}
> & {
/**
* One or an array of version strings of the software you are using, such as `'RockenbergCommerce/3.1.12'`.
*/
versionStrings?: string | string[];
} & Xor<
{
/**
* The Mollie API key, starting with `'test_'` or `'live_'`.
*/
apiKey: string;
},
{
/**
* OAuth access token, starting with `'access_''.
*/
accessToken: string;
}
>;
/**
* The URL of the root of the Mollie API. Default: `'https://api.mollie.com:443/v2/'`.
*/
apiEndpoint?: string;
} & Pick<AxiosRequestConfig, 'adapter' | 'headers' | 'proxy' | 'socketPath' | 'timeout'>;

function preprocessVersionStrings(input: string | string[] | undefined): string[] {
if (Array.isArray(input)) {
Expand All @@ -60,14 +64,10 @@ function preprocessVersionStrings(input: string | string[] | undefined): string[
}
return [];
}
function createHttpClient({ apiKey, accessToken, versionStrings, ...axiosOptions }: MollieOptions): AxiosInstance {
axiosOptions.baseURL = 'https://api.mollie.com:443/v2/';

if (axiosOptions.headers == undefined) {
axiosOptions.headers = {};
}
function createHttpClient({ apiKey, accessToken, versionStrings, apiEndpoint = 'https://api.mollie.com:443/v2/', ...axiosOptions }: MollieOptions): AxiosInstance {
const headers: Record<string, string> = axiosOptions.headers ?? {};

axiosOptions.headers['User-Agent'] = [
headers['User-Agent'] = [
`Node/${process.version}`,
`Mollie/${libraryVersion}`,
...preprocessVersionStrings(versionStrings).map(versionString => {
Expand All @@ -91,19 +91,22 @@ function createHttpClient({ apiKey, accessToken, versionStrings, ...axiosOptions
].join(' ');

if (apiKey != undefined) {
axiosOptions.headers['Authorization'] = `Bearer ${apiKey}`;
headers['Authorization'] = `Bearer ${apiKey}`;
} /* if (accessToken != undefined) */ else {
axiosOptions.headers['Authorization'] = `Bearer ${accessToken}`;
axiosOptions.headers['User-Agent'] += ' OAuth/2.0';
headers['Authorization'] = `Bearer ${accessToken}`;
headers['User-Agent'] += ' OAuth/2.0';
}
axiosOptions.headers['Accept-Encoding'] = 'gzip';
axiosOptions.headers['Content-Type'] = 'application/json';

axiosOptions.httpsAgent = new https.Agent({
ca: caCertificates,
headers['Accept-Encoding'] = 'gzip';
headers['Content-Type'] = 'application/json';

return axios.create({
...axiosOptions,
baseURL: apiEndpoint,
headers,
httpsAgent: new https.Agent({
ca: caCertificates,
}),
});

return axios.create(axiosOptions);
}

/**
Expand All @@ -112,7 +115,7 @@ function createHttpClient({ apiKey, accessToken, versionStrings, ...axiosOptions
*/
export default function createMollieClient(options: MollieOptions) {
// Attempt to catch cases where this library is integrated into a frontend app.
if (['node', 'io.js'].indexOf(process?.release?.name) == -1) {
if (['node', 'io.js'].includes(process?.release?.name) == false) {
throw new Error(
`Unexpected process release name ${process?.release?.name}. This may indicate that the Mollie API client is integrated into a website or app. This is not recommended, please see https://github.com/mollie/mollie-api-node/#a-note-on-use-outside-of-nodejs. If this is a mistake, please let us know: https://github.com/mollie/mollie-api-node/issues`,
);
Expand Down
29 changes: 28 additions & 1 deletion tests/unit/networking.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ test('defaults', async () => {
});

async function requestWithVersionStrings(versionStrings: string | string[] | undefined) {
const { adapter, client } = wireMockClient(versionStrings);
const { adapter, client } = wireMockClient({ versionStrings });

adapter.onGet('/customers').reply(200, {
_embedded: {
Expand Down Expand Up @@ -91,3 +91,30 @@ test('customVersionStrings', async () => {

expect(headers['User-Agent'].endsWith('phpCookbookForNodeUsers/7.3.4')).toBe(true);
});

test('customApiEndpoint', async () => {
const { adapter, client } = wireMockClient({ apiEndpoint: 'https://null.house/' });

adapter.onGet('/customers').reply(200, {
_embedded: {
customers: [],
},
count: 0,
_links: {
documentation: {
href: 'https://docs.mollie.com/reference/v2/customers-api/list-customers',
type: 'text/html',
},
self: {
href: 'https://api.mollie.com/v2/customers?limit=50',
type: 'application/hal+json',
},
previous: null,
next: null,
},
});

await bluster(client.customers.all.bind(client.customers))();

expect(adapter.history.get[0].baseURL).toBe('https://null.house/');
});
6 changes: 3 additions & 3 deletions tests/wireMockClient.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import createMollieClient, { MollieClient } from '..';
import createMollieClient, { MollieClient, MollieOptions } from '..';
import { AxiosInstance } from 'axios';
import MockAdapter from 'axios-mock-adapter';
import {} from 'jest-bluster';

export default function wireMockClient(versionStrings?: string | string[]): { adapter: MockAdapter; client: MollieClient } {
export default function wireMockClient(options?: Omit<MollieOptions, 'accessToken' | 'adapter' | 'apiKey'>): { adapter: MockAdapter; client: MollieClient } {
const adapter = new MockAdapter((undefined as unknown) as AxiosInstance);
return {
adapter,
client: createMollieClient({
apiKey: 'mock-api-key',
adapter: adapter.adapter(),
versionStrings,
...options,
}),
};
}

0 comments on commit ae3705f

Please sign in to comment.