Registry API Refactoring (#144)

* Refactor core to use a flexible registry pattern

- Introduce a new registry mechanism for mailer providers
- Extract mailer provider enum to a separate file
- Implement dynamic mailer loading using a registry
- Update package dependencies and exports
- Improve modularity and extensibility of mailer implementation

* Refactor monitoring and billing services to use a flexible registry pattern

- Introduce a shared registry mechanism for dynamic service loading
- Replace static switch-based implementations with a registry-based approach
- Update instrumentation, CMS, and monitoring services to use the new registry
- Improve modularity and extensibility of service implementations
- Add Zod-based type-safe provider validation

* Simplify async registration in monitoring and billing services

- Remove unnecessary async wrappers for no-op registrations
- Update type definitions to support both async and sync registration functions
- Standardize registration approach for Paddle and Sentry providers

* Remove Tailwind package from packages where it is not being needed

* Remove Tailwind config references from pnpm-lock.yaml

* Update instrumentation registry to support dynamic monitoring providers

- Modify type definition to use NonNullable MonitoringProvider
- Import MonitoringProvider type from get-monitoring-provider
- Enhance type safety for instrumentation registration
This commit is contained in:
Giancarlo Buomprisco
2025-02-05 17:38:43 +07:00
committed by GitHub
parent 3140f0cf21
commit 4a47df81db
31 changed files with 414 additions and 287 deletions

View File

@@ -3,37 +3,44 @@ import 'server-only';
import { z } from 'zod';
import {
BillingConfig,
type BillingConfig,
type BillingProviderSchema,
BillingWebhookHandlerService,
} from '@kit/billing';
import { createRegistry } from '@kit/shared/registry';
export class BillingEventHandlerFactoryService {
static async GetProviderStrategy(
provider: z.infer<typeof BillingProviderSchema>,
config: BillingConfig,
): Promise<BillingWebhookHandlerService> {
switch (provider) {
case 'stripe': {
const { StripeWebhookHandlerService } = await import('@kit/stripe');
/**
* @description Creates a registry for billing webhook handlers
* @param config - The billing config
* @returns The billing webhook handler registry
*/
export function createBillingEventHandlerFactoryService(config: BillingConfig) {
// Create a registry for billing webhook handlers
const billingWebhookHandlerRegistry = createRegistry<
BillingWebhookHandlerService,
z.infer<typeof BillingProviderSchema>
>();
return new StripeWebhookHandlerService(config);
}
// Register the Stripe webhook handler
billingWebhookHandlerRegistry.register('stripe', async () => {
const { StripeWebhookHandlerService } = await import('@kit/stripe');
case 'lemon-squeezy': {
const { LemonSqueezyWebhookHandlerService } = await import(
'@kit/lemon-squeezy'
);
return new StripeWebhookHandlerService(config);
});
return new LemonSqueezyWebhookHandlerService(config);
}
// Register the Lemon Squeezy webhook handler
billingWebhookHandlerRegistry.register('lemon-squeezy', async () => {
const { LemonSqueezyWebhookHandlerService } = await import(
'@kit/lemon-squeezy'
);
case 'paddle': {
throw new Error('Paddle is not supported yet');
}
return new LemonSqueezyWebhookHandlerService(config);
});
default:
throw new Error(`Unsupported billing provider: ${provider as string}`);
}
}
// Register Paddle webhook handler (not implemented yet)
billingWebhookHandlerRegistry.register('paddle', () => {
throw new Error('Paddle is not supported yet');
});
return billingWebhookHandlerRegistry;
}

View File

@@ -5,7 +5,7 @@ import { SupabaseClient } from '@supabase/supabase-js';
import { BillingConfig } from '@kit/billing';
import { Database, Enums } from '@kit/supabase/database';
import { BillingEventHandlerFactoryService } from './billing-event-handler-factory.service';
import { createBillingEventHandlerFactoryService } from './billing-event-handler-factory.service';
import { createBillingEventHandlerService } from './billing-event-handler.service';
// a function that returns a Supabase client
@@ -25,10 +25,8 @@ export async function getBillingEventHandlerService(
provider: BillingProvider,
config: BillingConfig,
) {
const strategy = await BillingEventHandlerFactoryService.GetProviderStrategy(
provider,
config,
);
const strategy =
await createBillingEventHandlerFactoryService(config).get(provider);
return createBillingEventHandlerService(clientProvider, strategy);
}

View File

@@ -1,37 +0,0 @@
import 'server-only';
import { z } from 'zod';
import {
type BillingProviderSchema,
BillingStrategyProviderService,
} from '@kit/billing';
export class BillingGatewayFactoryService {
static async GetProviderStrategy(
provider: z.infer<typeof BillingProviderSchema>,
): Promise<BillingStrategyProviderService> {
switch (provider) {
case 'stripe': {
const { StripeBillingStrategyService } = await import('@kit/stripe');
return new StripeBillingStrategyService();
}
case 'lemon-squeezy': {
const { LemonSqueezyBillingStrategyService } = await import(
'@kit/lemon-squeezy'
);
return new LemonSqueezyBillingStrategyService();
}
case 'paddle': {
throw new Error('Paddle is not supported yet');
}
default:
throw new Error(`Unsupported billing provider: ${provider as string}`);
}
}
}

View File

@@ -0,0 +1,34 @@
import 'server-only';
import { z } from 'zod';
import {
type BillingProviderSchema,
BillingStrategyProviderService,
} from '@kit/billing';
import { createRegistry } from '@kit/shared/registry';
// Create a registry for billing strategy providers
export const billingStrategyRegistry = createRegistry<
BillingStrategyProviderService,
z.infer<typeof BillingProviderSchema>
>();
// Register the Stripe billing strategy
billingStrategyRegistry.register('stripe', async () => {
const { StripeBillingStrategyService } = await import('@kit/stripe');
return new StripeBillingStrategyService();
});
// Register the Lemon Squeezy billing strategy
billingStrategyRegistry.register('lemon-squeezy', async () => {
const { LemonSqueezyBillingStrategyService } = await import(
'@kit/lemon-squeezy'
);
return new LemonSqueezyBillingStrategyService();
});
// Register Paddle billing strategy (not implemented yet)
billingStrategyRegistry.register('paddle', () => {
throw new Error('Paddle is not supported yet');
});

View File

@@ -11,7 +11,7 @@ import {
UpdateSubscriptionParamsSchema,
} from '@kit/billing/schema';
import { BillingGatewayFactoryService } from './billing-gateway-factory.service';
import { billingStrategyRegistry } from './billing-gateway-registry';
export function createBillingGatewayService(
provider: z.infer<typeof BillingProviderSchema>,
@@ -138,6 +138,6 @@ class BillingGatewayService {
}
private getStrategy() {
return BillingGatewayFactoryService.GetProviderStrategy(this.provider);
return billingStrategyRegistry.get(this.provider);
}
}