feat: Add support for providers to validate their options at loading time (#8853)

* feat: Add support for providers to validate their options at loading time

* fix missing removal

* fix integration tests

* add tests
This commit is contained in:
Adrien de Peretti
2024-08-29 09:08:49 +02:00
committed by GitHub
parent b8572165cb
commit 77b874f272
11 changed files with 281 additions and 205 deletions
@@ -1,9 +1,10 @@
import { generateJwtToken, MedusaError } from "@medusajs/utils"
import { GoogleAuthService } from "../../src/services/google"
jest.setTimeout(100000)
import { http, HttpResponse } from "msw"
import { setupServer } from "msw/node"
jest.setTimeout(100000)
const sampleIdPayload = {
iss: "https://accounts.google.com",
azp: "199301612397-l1lrg08vd6dvu98r43l7ul0ri2rd2b6r.apps.googleusercontent.com",
@@ -97,15 +98,10 @@ describe("Google auth provider", () => {
it("throw an error if required options are not passed", async () => {
let msg = ""
try {
new GoogleAuthService(
{
logger: console as any,
},
{
clientID: "test",
clientSecret: "test",
} as any
)
GoogleAuthService.validateOptions({
clientID: "test",
clientSecret: "test",
} as any)
} catch (e) {
msg = e.message
}
@@ -19,12 +19,25 @@ export class GoogleAuthService extends AbstractAuthModuleProvider {
protected config_: LocalServiceConfig
protected logger_: Logger
static validateOptions(options: GoogleAuthProviderOptions) {
if (!options.clientID) {
throw new Error("Google clientID is required")
}
if (!options.clientSecret) {
throw new Error("Google clientSecret is required")
}
if (!options.callbackURL) {
throw new Error("Google callbackUrl is required")
}
}
constructor(
{ logger }: InjectedDependencies,
options: GoogleAuthProviderOptions
) {
super({}, { provider: "google", displayName: "Google Authentication" })
this.validateConfig(options)
this.config_ = options
this.logger_ = logger
}
@@ -173,18 +186,4 @@ export class GoogleAuthService extends AbstractAuthModuleProvider {
return { success: true, location: authUrl.toString() }
}
private validateConfig(config: LocalServiceConfig) {
if (!config.clientID) {
throw new Error("Google clientID is required")
}
if (!config.clientSecret) {
throw new Error("Google clientSecret is required")
}
if (!config.callbackURL) {
throw new Error("Google callbackUrl is required")
}
}
}
@@ -13,12 +13,12 @@ import {
} from "@medusajs/types"
import {
AbstractPaymentProvider,
MedusaError,
PaymentActions,
PaymentSessionStatus,
isDefined,
isPaymentProviderError,
isPresent,
MedusaError,
PaymentActions,
PaymentSessionStatus,
} from "@medusajs/utils"
import {
ErrorCodes,
@@ -36,6 +36,12 @@ abstract class StripeBase extends AbstractPaymentProvider<StripeOptions> {
protected stripe_: Stripe
protected container_: MedusaContainer
static validateOptions(options: StripeOptions): void {
if (!isDefined(options.apiKey)) {
throw new Error("Required option `apiKey` is missing in Stripe plugin")
}
}
protected constructor(container: MedusaContainer, options: StripeOptions) {
// @ts-ignore
super(...arguments)
@@ -43,23 +49,11 @@ abstract class StripeBase extends AbstractPaymentProvider<StripeOptions> {
this.container_ = container
this.options_ = options
this.stripe_ = this.init()
}
protected init() {
this.validateOptions(this.config)
return new Stripe(this.config.apiKey)
this.stripe_ = new Stripe(options.apiKey)
}
abstract get paymentIntentOptions(): PaymentIntentOptions
private validateOptions(options: StripeOptions): void {
if (!isDefined(options.apiKey)) {
throw new Error("Required option `apiKey` is missing in Stripe plugin")
}
}
get options(): StripeOptions {
return this.options_
}
@@ -370,7 +364,7 @@ abstract class StripeBase extends AbstractPaymentProvider<StripeOptions> {
return this.stripe_.webhooks.constructEvent(
data.rawData as string | Buffer,
signature,
this.config.webhookSecret
this.options_.webhookSecret
)
}
protected buildError(