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:
parent
f88b960bbe
commit
5c4b717c28
4 changed files with 13 additions and 24 deletions
8
src/app.d.ts
vendored
8
src/app.d.ts
vendored
|
|
@ -1,7 +1,5 @@
|
|||
// See https://svelte.dev/docs/kit/types#app.d.ts
|
||||
|
||||
import type { TwilioConfiguration } from '$lib/server/twilio';
|
||||
|
||||
// for information about these interfaces
|
||||
declare global {
|
||||
namespace App {
|
||||
|
|
@ -9,10 +7,6 @@ declare global {
|
|||
interface Locals {
|
||||
user: import('lucia').User | null;
|
||||
session: import('lucia').Session | null;
|
||||
twilio: {
|
||||
config: TwilioConfiguration;
|
||||
client: import('twilio').Twilio;
|
||||
};
|
||||
}
|
||||
// interface PageData {}
|
||||
// interface PageState {}
|
||||
|
|
@ -20,4 +14,4 @@ declare global {
|
|||
}
|
||||
}
|
||||
|
||||
export {};
|
||||
export {};
|
||||
|
|
@ -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);
|
||||
}
|
||||
|
|
@ -1,6 +1,8 @@
|
|||
import { PhoneRegex } from '$lib/regex';
|
||||
import { logger } from '$lib/server/logger';
|
||||
import { TwilioConfig } from '$lib/server/twilio';
|
||||
import { fail, type Actions } from '@sveltejs/kit';
|
||||
import twilio from 'twilio';
|
||||
import zod from 'zod';
|
||||
|
||||
export const actions = {
|
||||
|
|
@ -29,12 +31,17 @@ export const actions = {
|
|||
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 {
|
||||
const result = await twilio.client.messages.create({
|
||||
const result = await twilioClient.messages.create({
|
||||
to: phone,
|
||||
body: message,
|
||||
from: twilio.config.twilio_phone_number,
|
||||
from: twilioConfig.twilio_phone_number,
|
||||
});
|
||||
logger.debug(result);
|
||||
} catch (e) {
|
||||
|
|
@ -45,4 +52,4 @@ export const actions = {
|
|||
success: true,
|
||||
};
|
||||
},
|
||||
} satisfies Actions;
|
||||
} satisfies Actions;
|
||||
|
|
@ -64,4 +64,4 @@
|
|||
.page {
|
||||
@apply flex flex-col items-center justify-around gap-24 py-[10%];
|
||||
}
|
||||
</style>
|
||||
</style>
|
||||
Loading…
Add table
Reference in a new issue