on second thought, dont use locals since we are only using twilio in one place

Don't want to init a twilio client on every request when its only used
in on a single page
This commit is contained in:
Benjamin Palko 2024-12-31 12:06:24 -05:00
parent f88b960bbe
commit 5c4b717c28
4 changed files with 13 additions and 24 deletions

6
src/app.d.ts vendored
View file

@ -1,7 +1,5 @@
// See https://svelte.dev/docs/kit/types#app.d.ts // See https://svelte.dev/docs/kit/types#app.d.ts
import type { TwilioConfiguration } from '$lib/server/twilio';
// for information about these interfaces // for information about these interfaces
declare global { declare global {
namespace App { namespace App {
@ -9,10 +7,6 @@ declare global {
interface Locals { interface Locals {
user: import('lucia').User | null; user: import('lucia').User | null;
session: import('lucia').Session | null; session: import('lucia').Session | null;
twilio: {
config: TwilioConfiguration;
client: import('twilio').Twilio;
};
} }
// interface PageData {} // interface PageData {}
// interface PageState {} // interface PageState {}

View file

@ -1,12 +0,0 @@
import { TwilioConfig } from '$lib/server/twilio';
import twilio from 'twilio';
export async function handle({ event, resolve }) {
const twilioConfig = TwilioConfig();
const twilioClient = twilio(twilioConfig.twilio_account_sid, twilioConfig.twilio_auth_token);
event.locals.twilio = {
config: twilioConfig,
client: twilioClient,
};
return await resolve(event);
}

View file

@ -1,6 +1,8 @@
import { PhoneRegex } from '$lib/regex'; import { PhoneRegex } from '$lib/regex';
import { logger } from '$lib/server/logger'; import { logger } from '$lib/server/logger';
import { TwilioConfig } from '$lib/server/twilio';
import { fail, type Actions } from '@sveltejs/kit'; import { fail, type Actions } from '@sveltejs/kit';
import twilio from 'twilio';
import zod from 'zod'; import zod from 'zod';
export const actions = { export const actions = {
@ -29,12 +31,17 @@ export const actions = {
return fail(400, { error: 'invalid_message' }); return fail(400, { error: 'invalid_message' });
} }
const twilio = event.locals.twilio; const twilioConfig = TwilioConfig();
const twilioClient = twilio(
twilioConfig.twilio_account_sid,
twilioConfig.twilio_auth_token
);
try { try {
const result = await twilio.client.messages.create({ const result = await twilioClient.messages.create({
to: phone, to: phone,
body: message, body: message,
from: twilio.config.twilio_phone_number, from: twilioConfig.twilio_phone_number,
}); });
logger.debug(result); logger.debug(result);
} catch (e) { } catch (e) {