mollie / mollie-api-node

Official Mollie API client for Node
http://www.mollie.com
BSD 3-Clause "New" or "Revised" License
234 stars 63 forks source link

TypeError: Missing parameter "apiKey" or "accessToken". #349

Closed vrxj81 closed 2 months ago

vrxj81 commented 3 months ago

I'm trying to us the Mollie NodeJS API in a NestJS application, but when I use the createMollieClient function, I get the error TypeError: Missing parameter "apiKey" or "accessToken". while I'm providing the apiKey options. See code below:

import { PaymentsService } from './payments.service';
import {
  ConfigurableModuleClass,
  OPTIONS_TYPE,
  PAYMENT_MODULE_OPTIONS,
} from './backend-payments-util.module.definition';
import { MolliePaymentsProvider } from './providers/mollie.payments.provider';
import createMollieClient, { MollieClient, MollieOptions } from '@mollie/api-client';

@Module({})
export class BackendPaymentsUtilModule extends ConfigurableModuleClass {
  constructor(
    @Inject(PAYMENT_MODULE_OPTIONS) private options: string | symbol,
  ) {
    super();
  }

  static register(options: typeof OPTIONS_TYPE) {
    if (options.paymentProvider === 'mollie') {
      **const mollieClient: MollieClient = createMollieClient({
        apiKey: process.env['MOLLIE_API_KEY'] || '',
      } as MollieOptions);**
      return {
        ...super.register(options),
        providers: [
          {
            provide: PAYMENT_MODULE_OPTIONS,
            useValue: options,
          },
          {
            provide: PaymentsService,
            useFactory: () => new MolliePaymentsProvider(mollieClient),
          },
        ],
      };
    } else {
      throw new Error('Payment provider not supported');
    }
  }
}

I also tried the createMollieClient available like so: import { createMollieClient, MollieClient, MollieOptions } from '@mollie/api-client'; But same issue.

Pimm commented 3 months ago

Thanks for reaching out, Johan.

The current version of the client performs a falsy check on apiKey and accessToken. It will throw the "missing parameter" error for any falsy value, including an empty string. Could you check the value of process.env['MOLLIE_API_KEY'] (using console.log, for instance)?

Pimm commented 2 months ago

I'm closing this issue, as I suspect you accidentally passing an empty string to apiKey is what is causing your issue.

If I'm wrong, please comment and I'll reopen.

Hope your application turns out nicely!