Replace Logger with getLogger and update next version

This commit replaces the use of Logger with getLogger in various parts of the code to handle logging. The Logger has been replaced with getLogger, which assists in getting logs in an asynchronous manner. In addition to this, it updates the next version in pnpm-lock.yaml from next@14.2.0-canary.61 to next@14.2.0-canary.62 and various other dependencies. Also made minor annotations and comments to the function 'isBrowser' and 'formatCurrency' in the 'utils.ts' file.
This commit is contained in:
giancarlo
2024-04-08 12:23:15 +08:00
parent 2b447167f7
commit 9fca45c2de
33 changed files with 369 additions and 250 deletions

View File

@@ -1,7 +1,7 @@
import { SupabaseClient } from '@supabase/supabase-js';
import { BillingWebhookHandlerService } from '@kit/billing';
import { Logger } from '@kit/shared/logger';
import { getLogger } from '@kit/shared/logger';
import { Database } from '@kit/supabase/database';
export class BillingEventHandlerService {
@@ -22,10 +22,11 @@ export class BillingEventHandlerService {
return this.strategy.handleWebhookEvent(event, {
onSubscriptionDeleted: async (subscriptionId: string) => {
const client = this.clientProvider();
const logger = await getLogger();
// Handle the subscription deleted event
// here we delete the subscription from the database
Logger.info(
logger.info(
{
namespace: this.namespace,
subscriptionId,
@@ -42,7 +43,7 @@ export class BillingEventHandlerService {
throw new Error('Failed to delete subscription');
}
Logger.info(
logger.info(
{
namespace: this.namespace,
subscriptionId,
@@ -52,6 +53,7 @@ export class BillingEventHandlerService {
},
onSubscriptionUpdated: async (subscription) => {
const client = this.clientProvider();
const logger = await getLogger();
const ctx = {
namespace: this.namespace,
@@ -61,14 +63,14 @@ export class BillingEventHandlerService {
customerId: subscription.target_customer_id,
};
Logger.info(ctx, 'Processing subscription updated event');
logger.info(ctx, 'Processing subscription updated event');
// Handle the subscription updated event
// here we update the subscription in the database
const { error } = await client.rpc('upsert_subscription', subscription);
if (error) {
Logger.error(
logger.error(
{
error,
...ctx,
@@ -79,12 +81,13 @@ export class BillingEventHandlerService {
throw new Error('Failed to update subscription');
}
Logger.info(ctx, 'Successfully updated subscription');
logger.info(ctx, 'Successfully updated subscription');
},
onCheckoutSessionCompleted: async (payload) => {
// Handle the checkout session completed event
// here we add the subscription to the database
const client = this.clientProvider();
const logger = await getLogger();
// Check if the payload contains an order_id
// if it does, we add an order, otherwise we add a subscription
@@ -97,17 +100,17 @@ export class BillingEventHandlerService {
customerId: payload.target_customer_id,
};
Logger.info(ctx, 'Processing order completed event...');
logger.info(ctx, 'Processing order completed event...');
const { error } = await client.rpc('upsert_order', payload);
if (error) {
Logger.error({ ...ctx, error }, 'Failed to add order');
logger.error({ ...ctx, error }, 'Failed to add order');
throw new Error('Failed to add order');
}
Logger.info(ctx, 'Successfully added order');
logger.info(ctx, 'Successfully added order');
} else {
const ctx = {
namespace: this.namespace,
@@ -117,25 +120,26 @@ export class BillingEventHandlerService {
customerId: payload.target_customer_id,
};
Logger.info(ctx, 'Processing checkout session completed event...');
logger.info(ctx, 'Processing checkout session completed event...');
const { error } = await client.rpc('upsert_subscription', payload);
if (error) {
Logger.error({ ...ctx, error }, 'Failed to add subscription');
logger.error({ ...ctx, error }, 'Failed to add subscription');
throw new Error('Failed to add subscription');
}
Logger.info(ctx, 'Successfully added subscription');
logger.info(ctx, 'Successfully added subscription');
}
},
onPaymentSucceeded: async (sessionId: string) => {
const client = this.clientProvider();
const logger = await getLogger();
// Handle the payment succeeded event
// here we update the payment status in the database
Logger.info(
logger.info(
{
namespace: this.namespace,
sessionId,
@@ -152,7 +156,7 @@ export class BillingEventHandlerService {
throw new Error('Failed to update payment status');
}
Logger.info(
logger.info(
{
namespace: this.namespace,
sessionId,
@@ -162,10 +166,11 @@ export class BillingEventHandlerService {
},
onPaymentFailed: async (sessionId: string) => {
const client = this.clientProvider();
const logger = await getLogger();
// Handle the payment failed event
// here we update the payment status in the database
Logger.info(
logger.info(
{
namespace: this.namespace,
sessionId,
@@ -182,7 +187,7 @@ export class BillingEventHandlerService {
throw new Error('Failed to update payment status');
}
Logger.info(
logger.info(
{
namespace: this.namespace,
sessionId,

View File

@@ -16,7 +16,7 @@ import {
RetrieveCheckoutSessionSchema,
UpdateSubscriptionParamsSchema,
} from '@kit/billing/schema';
import { Logger } from '@kit/shared/logger';
import { getLogger } from '@kit/shared/logger';
import { createLemonSqueezyBillingPortalSession } from './create-lemon-squeezy-billing-portal-session';
import { createLemonSqueezyCheckout } from './create-lemon-squeezy-checkout';
@@ -27,13 +27,12 @@ export class LemonSqueezyBillingStrategyService
async createCheckoutSession(
params: z.infer<typeof CreateBillingCheckoutSchema>,
) {
Logger.info(
const logger = await getLogger();
logger.info(
{
name: 'billing.lemon-squeezy',
customerId: params.customerId,
accountId: params.accountId,
returnUrl: params.returnUrl,
trialDays: params.trialDays,
...params,
},
'Creating checkout session...',
);
@@ -43,7 +42,7 @@ export class LemonSqueezyBillingStrategyService
if (error ?? !response?.data.id) {
console.log(error);
Logger.error(
logger.error(
{
name: 'billing.lemon-squeezy',
customerId: params.customerId,
@@ -56,7 +55,7 @@ export class LemonSqueezyBillingStrategyService
throw new Error('Failed to create checkout session');
}
Logger.info(
logger.info(
{
name: 'billing.lemon-squeezy',
customerId: params.customerId,
@@ -73,7 +72,9 @@ export class LemonSqueezyBillingStrategyService
async createBillingPortalSession(
params: z.infer<typeof CreateBillingPortalSessionSchema>,
) {
Logger.info(
const logger = await getLogger();
logger.info(
{
name: 'billing.lemon-squeezy',
customerId: params.customerId,
@@ -85,7 +86,7 @@ export class LemonSqueezyBillingStrategyService
await createLemonSqueezyBillingPortalSession(params);
if (error ?? !data) {
Logger.error(
logger.error(
{
name: 'billing.lemon-squeezy',
customerId: params.customerId,
@@ -97,7 +98,7 @@ export class LemonSqueezyBillingStrategyService
throw new Error('Failed to create billing portal session');
}
Logger.info(
logger.info(
{
name: 'billing.lemon-squeezy',
customerId: params.customerId,
@@ -111,7 +112,9 @@ export class LemonSqueezyBillingStrategyService
async cancelSubscription(
params: z.infer<typeof CancelSubscriptionParamsSchema>,
) {
Logger.info(
const logger = await getLogger();
logger.info(
{
name: 'billing.lemon-squeezy',
subscriptionId: params.subscriptionId,
@@ -123,7 +126,7 @@ export class LemonSqueezyBillingStrategyService
const { error } = await cancelSubscription(params.subscriptionId);
if (error) {
Logger.error(
logger.error(
{
name: 'billing.lemon-squeezy',
subscriptionId: params.subscriptionId,
@@ -135,7 +138,7 @@ export class LemonSqueezyBillingStrategyService
throw error;
}
Logger.info(
logger.info(
{
name: 'billing.lemon-squeezy',
subscriptionId: params.subscriptionId,
@@ -145,7 +148,7 @@ export class LemonSqueezyBillingStrategyService
return { success: true };
} catch (error) {
Logger.error(
logger.error(
{
name: 'billing.lemon-squeezy',
subscriptionId: params.subscriptionId,
@@ -161,7 +164,9 @@ export class LemonSqueezyBillingStrategyService
async retrieveCheckoutSession(
params: z.infer<typeof RetrieveCheckoutSessionSchema>,
) {
Logger.info(
const logger = await getLogger();
logger.info(
{
name: 'billing.lemon-squeezy',
sessionId: params.sessionId,
@@ -172,7 +177,7 @@ export class LemonSqueezyBillingStrategyService
const { data: session, error } = await getCheckout(params.sessionId);
if (error ?? !session?.data) {
Logger.error(
logger.error(
{
name: 'billing.lemon-squeezy',
sessionId: params.sessionId,
@@ -184,7 +189,7 @@ export class LemonSqueezyBillingStrategyService
throw new Error('Failed to retrieve checkout session');
}
Logger.info(
logger.info(
{
name: 'billing.lemon-squeezy',
sessionId: params.sessionId,
@@ -205,7 +210,9 @@ export class LemonSqueezyBillingStrategyService
}
async reportUsage(params: z.infer<typeof ReportBillingUsageSchema>) {
Logger.info(
const logger = await getLogger();
logger.info(
{
name: 'billing.lemon-squeezy',
subscriptionItemId: params.subscriptionItemId,
@@ -220,11 +227,11 @@ export class LemonSqueezyBillingStrategyService
});
if (error) {
Logger.error(
logger.error(
{
name: 'billing.lemon-squeezy',
subscriptionItemId: params.subscriptionItemId,
error: error.message,
error,
},
'Failed to report usage',
);
@@ -232,7 +239,7 @@ export class LemonSqueezyBillingStrategyService
throw new Error('Failed to report usage');
}
Logger.info(
logger.info(
{
name: 'billing.lemon-squeezy',
subscriptionItemId: params.subscriptionItemId,
@@ -246,19 +253,21 @@ export class LemonSqueezyBillingStrategyService
async updateSubscription(
params: z.infer<typeof UpdateSubscriptionParamsSchema>,
) {
const logger = await getLogger();
const ctx = {
name: 'billing.lemon-squeezy',
...params,
};
Logger.info(ctx, 'Updating subscription...');
logger.info(ctx, 'Updating subscription...');
const { error } = await updateSubscriptionItem(params.subscriptionItemId, {
quantity: params.quantity,
});
if (error) {
Logger.error(
logger.error(
{
...ctx,
error,
@@ -269,7 +278,7 @@ export class LemonSqueezyBillingStrategyService
throw error;
}
Logger.info(ctx, 'Subscription updated successfully');
logger.info(ctx, 'Subscription updated successfully');
return { success: true };
}

View File

@@ -1,6 +1,6 @@
import 'server-only';
import { Logger } from '@kit/shared/logger';
import { getLogger } from '@kit/shared/logger';
import { getLemonSqueezyEnv } from '../schema/lemon-squeezy-server-env.schema';
@@ -10,11 +10,12 @@ import { getLemonSqueezyEnv } from '../schema/lemon-squeezy-server-env.schema';
export async function initializeLemonSqueezyClient() {
const { lemonSqueezySetup } = await import('@lemonsqueezy/lemonsqueezy.js');
const env = getLemonSqueezyEnv();
const logger = await getLogger();
lemonSqueezySetup({
apiKey: env.secretKey,
onError(error) {
Logger.error(
logger.error(
{
name: `billing.lemon-squeezy`,
error: error.message,

View File

@@ -6,7 +6,7 @@ import {
BillingWebhookHandlerService,
getLineItemTypeById,
} from '@kit/billing';
import { Logger } from '@kit/shared/logger';
import { getLogger } from '@kit/shared/logger';
import { Database } from '@kit/supabase/database';
import { getLemonSqueezyEnv } from '../schema/lemon-squeezy-server-env.schema';
@@ -45,6 +45,9 @@ export class LemonSqueezyWebhookHandlerService
* @description Verifies the webhook signature - should throw an error if the signature is invalid
*/
async verifyWebhookSignature(request: Request) {
const logger = await getLogger();
// get the event name and signature from the headers
const eventName = request.headers.get('x-event-name');
const signature = request.headers.get('x-signature') as string;
@@ -53,8 +56,9 @@ export class LemonSqueezyWebhookHandlerService
const body = (await request.json()) as SubscriptionWebhook | OrderWebhook;
const rawBody = await reqClone.text();
// if no signature is found, throw an error
if (!signature) {
Logger.error(
logger.error(
{
eventName,
},
@@ -64,8 +68,9 @@ export class LemonSqueezyWebhookHandlerService
throw new Error('Signature header not found');
}
// if the signature is invalid, throw an error
if (!isSigningSecretValid(Buffer.from(rawBody), signature)) {
Logger.error(
logger.error(
{
eventName,
},
@@ -124,7 +129,9 @@ export class LemonSqueezyWebhookHandlerService
}
default: {
Logger.info(
const logger = await getLogger();
logger.info(
{
eventType: eventName,
name: this.namespace,
@@ -211,7 +218,9 @@ export class LemonSqueezyWebhookHandlerService
const { data: order, error } = await getOrder(orderId);
if (error ?? !order) {
Logger.error(
const logger = await getLogger();
logger.error(
{
orderId,
subscriptionId,

View File

@@ -11,7 +11,7 @@ import {
RetrieveCheckoutSessionSchema,
UpdateSubscriptionParamsSchema,
} from '@kit/billing/schema';
import { Logger } from '@kit/shared/logger';
import { getLogger } from '@kit/shared/logger';
import { createStripeBillingPortalSession } from './create-stripe-billing-portal-session';
import { createStripeCheckout } from './create-stripe-checkout';
@@ -24,8 +24,9 @@ export class StripeBillingStrategyService
params: z.infer<typeof CreateBillingCheckoutSchema>,
) {
const stripe = await this.stripeProvider();
const logger = await getLogger();
Logger.info(
logger.info(
{
name: 'billing.stripe',
customerId: params.customerId,
@@ -37,7 +38,7 @@ export class StripeBillingStrategyService
const { client_secret } = await createStripeCheckout(stripe, params);
if (!client_secret) {
Logger.error(
logger.error(
{
name: 'billing.stripe',
customerId: params.customerId,
@@ -49,7 +50,7 @@ export class StripeBillingStrategyService
throw new Error('Failed to create checkout session');
}
Logger.info(
logger.info(
{
name: 'billing.stripe',
customerId: params.customerId,
@@ -65,8 +66,9 @@ export class StripeBillingStrategyService
params: z.infer<typeof CreateBillingPortalSessionSchema>,
) {
const stripe = await this.stripeProvider();
const logger = await getLogger();
Logger.info(
logger.info(
{
name: 'billing.stripe',
customerId: params.customerId,
@@ -77,7 +79,7 @@ export class StripeBillingStrategyService
const session = await createStripeBillingPortalSession(stripe, params);
if (!session?.url) {
Logger.error(
logger.error(
{
name: 'billing.stripe',
customerId: params.customerId,
@@ -85,7 +87,7 @@ export class StripeBillingStrategyService
'Failed to create billing portal session',
);
} else {
Logger.info(
logger.info(
{
name: 'billing.stripe',
customerId: params.customerId,
@@ -101,8 +103,9 @@ export class StripeBillingStrategyService
params: z.infer<typeof CancelSubscriptionParamsSchema>,
) {
const stripe = await this.stripeProvider();
const logger = await getLogger();
Logger.info(
logger.info(
{
name: 'billing.stripe',
subscriptionId: params.subscriptionId,
@@ -115,7 +118,7 @@ export class StripeBillingStrategyService
invoice_now: params.invoiceNow ?? true,
});
Logger.info(
logger.info(
{
name: 'billing.stripe',
subscriptionId: params.subscriptionId,
@@ -125,7 +128,7 @@ export class StripeBillingStrategyService
return { success: true };
} catch (e) {
Logger.error(
logger.error(
{
name: 'billing.stripe',
subscriptionId: params.subscriptionId,
@@ -142,8 +145,9 @@ export class StripeBillingStrategyService
params: z.infer<typeof RetrieveCheckoutSessionSchema>,
) {
const stripe = await this.stripeProvider();
const logger = await getLogger();
Logger.info(
logger.info(
{
name: 'billing.stripe',
sessionId: params.sessionId,
@@ -155,7 +159,7 @@ export class StripeBillingStrategyService
const session = await stripe.checkout.sessions.retrieve(params.sessionId);
const isSessionOpen = session.status === 'open';
Logger.info(
logger.info(
{
name: 'billing.stripe',
sessionId: params.sessionId,
@@ -172,7 +176,7 @@ export class StripeBillingStrategyService
},
};
} catch (error) {
Logger.error(
logger.error(
{
name: 'billing.stripe',
sessionId: params.sessionId,
@@ -203,8 +207,9 @@ export class StripeBillingStrategyService
params: z.infer<typeof UpdateSubscriptionParamsSchema>,
) {
const stripe = await this.stripeProvider();
const logger = await getLogger();
Logger.info(
logger.info(
{
name: 'billing.stripe',
...params,
@@ -222,7 +227,7 @@ export class StripeBillingStrategyService
],
});
Logger.info(
logger.info(
{
name: 'billing.stripe',
...params,
@@ -232,7 +237,7 @@ export class StripeBillingStrategyService
return { success: true };
} catch (e) {
Logger.error(
logger.error(
{
name: 'billing.stripe',
...params,

View File

@@ -5,7 +5,7 @@ import {
BillingWebhookHandlerService,
getLineItemTypeById,
} from '@kit/billing';
import { Logger } from '@kit/shared/logger';
import { getLogger } from '@kit/shared/logger';
import { Database } from '@kit/supabase/database';
import { StripeServerEnvSchema } from '../schema/stripe-server-env.schema';
@@ -113,6 +113,8 @@ export class StripeWebhookHandlerService
}
default: {
const Logger = await getLogger();
Logger.info(
{
eventType: event.type,