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

@@ -14,10 +14,11 @@
},
"devDependencies": {
"@kit/eslint-config": "workspace:*",
"@kit/mailers-shared": "workspace:*",
"@kit/nodemailer": "workspace:*",
"@kit/prettier-config": "workspace:*",
"@kit/resend": "workspace:*",
"@kit/tailwind-config": "workspace:*",
"@kit/shared": "workspace:*",
"@kit/tsconfig": "workspace:*",
"@types/node": "^22.13.0",
"zod": "^3.24.1"

View File

@@ -1,37 +1,12 @@
import { z } from 'zod';
const MAILER_PROVIDER = z
.enum(['nodemailer', 'resend'])
.default('nodemailer')
.parse(process.env.MAILER_PROVIDER);
import { MAILER_PROVIDER } from './provider-enum';
import { mailerRegistry } from './registry';
/**
* @description Get the mailer based on the environment variable.
* @name getMailer
* @description Get the mailer based on the environment variable using the registry internally.
*/
export async function getMailer() {
switch (MAILER_PROVIDER) {
case 'nodemailer':
return getNodemailer();
case 'resend': {
const { createResendMailer } = await import('@kit/resend');
return createResendMailer();
}
default:
throw new Error(`Invalid mailer: ${MAILER_PROVIDER as string}`);
}
export function getMailer() {
return mailerRegistry.get(MAILER_PROVIDER);
}
async function getNodemailer() {
if (process.env.NEXT_RUNTIME === 'nodejs') {
const { createNodemailerService } = await import('@kit/nodemailer');
return createNodemailerService();
} else {
throw new Error(
'Nodemailer is not available on the edge runtime. Please use another mailer.',
);
}
}
export { MAILER_PROVIDER };

View File

@@ -0,0 +1,16 @@
import { z } from 'zod';
const MAILER_PROVIDERS = [
'nodemailer',
'resend',
// add more providers here
] as const;
const MAILER_PROVIDER = z
.enum(MAILER_PROVIDERS)
.default('nodemailer')
.parse(process.env.MAILER_PROVIDER);
export { MAILER_PROVIDER };
export type MailerProvider = (typeof MAILER_PROVIDERS)[number];

View File

@@ -0,0 +1,26 @@
import { Mailer } from '@kit/mailers-shared';
import { createRegistry } from '@kit/shared/registry';
import { MailerProvider } from './provider-enum';
const mailerRegistry = createRegistry<Mailer, MailerProvider>();
mailerRegistry.register('nodemailer', async () => {
if (process.env.NEXT_RUNTIME === 'nodejs') {
const { createNodemailerService } = await import('@kit/nodemailer');
return createNodemailerService();
} else {
throw new Error(
'Nodemailer is not available on the edge runtime. Please use another mailer.',
);
}
});
mailerRegistry.register('resend', async () => {
const { createResendMailer } = await import('@kit/resend');
return createResendMailer();
});
export { mailerRegistry };

View File

@@ -19,7 +19,6 @@
"@kit/eslint-config": "workspace:*",
"@kit/mailers-shared": "workspace:*",
"@kit/prettier-config": "workspace:*",
"@kit/tailwind-config": "workspace:*",
"@kit/tsconfig": "workspace:*",
"@types/nodemailer": "6.4.17",
"zod": "^3.24.1"

View File

@@ -16,7 +16,6 @@
"@kit/eslint-config": "workspace:*",
"@kit/mailers-shared": "workspace:*",
"@kit/prettier-config": "workspace:*",
"@kit/tailwind-config": "workspace:*",
"@kit/tsconfig": "workspace:*",
"@types/node": "^22.13.0",
"zod": "^3.24.1"

View File

@@ -15,7 +15,6 @@
"devDependencies": {
"@kit/eslint-config": "workspace:*",
"@kit/prettier-config": "workspace:*",
"@kit/tailwind-config": "workspace:*",
"@kit/tsconfig": "workspace:*",
"zod": "^3.24.1"
},