diff --git a/package-lock.json b/package-lock.json index c595750..a57a410 100644 --- a/package-lock.json +++ b/package-lock.json @@ -10,7 +10,9 @@ "dependencies": { "@aws-sdk/client-sesv2": "^3.734.0", "@sentry/cloudflare": "^8.51.0", - "mimetext": "^3.0.27" + "mimetext": "^3.0.27", + "reflect-metadata": "^0.2.2", + "tsyringe-neo": "^5.1.0" }, "devDependencies": { "@cloudflare/vitest-pool-workers": "^0.6.4", @@ -2903,6 +2905,12 @@ "dev": true, "license": "Unlicense" }, + "node_modules/reflect-metadata": { + "version": "0.2.2", + "resolved": "https://registry.npmjs.org/reflect-metadata/-/reflect-metadata-0.2.2.tgz", + "integrity": "sha512-urBwgfrvVP/eAyXx4hluJivBKzuEbSQs9rKWCrCkbSxNv8mxPcUZKeuoF3Uy4mJl3Lwprp6yy5/39VWigZ4K6Q==", + "license": "Apache-2.0" + }, "node_modules/regenerator-runtime": { "version": "0.14.1", "resolved": "https://registry.npmjs.org/regenerator-runtime/-/regenerator-runtime-0.14.1.tgz", @@ -3145,6 +3153,18 @@ "integrity": "sha512-oJFu94HQb+KVduSUQL7wnpmqnfmLsOA/nAh6b6EH0wCEoK0/mPeXU6c3wKDV83MkOuHPRHtSXKKU99IBazS/2w==", "license": "0BSD" }, + "node_modules/tsyringe-neo": { + "version": "5.1.0", + "resolved": "https://registry.npmjs.org/tsyringe-neo/-/tsyringe-neo-5.1.0.tgz", + "integrity": "sha512-x6QAckY85YLGE980TIR5Xle3s76nhBNYu0kr6UlLAWiPqt4S6PgbbvNV4cjZlItv3ka8adcGYG2Wy/I2yxcqdg==", + "license": "MIT", + "dependencies": { + "tslib": "^2.6.3" + }, + "engines": { + "node": ">= 18.0.0" + } + }, "node_modules/typescript": { "version": "5.7.3", "resolved": "https://registry.npmjs.org/typescript/-/typescript-5.7.3.tgz", diff --git a/package.json b/package.json index b04b427..d0ce055 100644 --- a/package.json +++ b/package.json @@ -22,6 +22,8 @@ "dependencies": { "@aws-sdk/client-sesv2": "^3.734.0", "@sentry/cloudflare": "^8.51.0", - "mimetext": "^3.0.27" + "mimetext": "^3.0.27", + "reflect-metadata": "^0.2.2", + "tsyringe-neo": "^5.1.0" } } diff --git a/src/app/config.ts b/src/app/config.ts new file mode 100644 index 0000000..326fbf4 --- /dev/null +++ b/src/app/config.ts @@ -0,0 +1,6 @@ +export class Config { + constructor( + public readonly awsAccessKeyId: string, + public readonly awsSecretAccessKey: string, + ) {} +} diff --git a/src/app/container.ts b/src/app/container.ts new file mode 100644 index 0000000..382822e --- /dev/null +++ b/src/app/container.ts @@ -0,0 +1,32 @@ +import { SESv2Client } from "@aws-sdk/client-sesv2"; +import { + container, + DependencyContainer, + instanceCachingFactory, +} from "tsyringe-neo"; + +import { Config } from "./config"; + +container.register(SESv2Client, { + useFactory: instanceCachingFactory((c) => { + const config = c.resolve(Config); + + return new SESv2Client({ + region: "ap-northeast-1", + credentials: { + accessKeyId: config.awsAccessKeyId, + secretAccessKey: config.awsSecretAccessKey, + }, + }); + }), +}); + +export function getContainer(env: Env): DependencyContainer { + const c = container.createChildContainer(); + + c.register(Config, { + useValue: new Config(env.AWS_ACCESS_KEY_ID, env.AWS_SECRET_ACCESS_KEY), + }); + + return c; +} diff --git a/src/index.ts b/src/index.ts index 7d84965..357848a 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,3 +1,5 @@ +import "reflect-metadata"; + import { withSentry } from "./vendor/sentry"; export default withSentry( diff --git a/worker-configuration.d.ts b/worker-configuration.d.ts index 231266c..e731647 100644 --- a/worker-configuration.d.ts +++ b/worker-configuration.d.ts @@ -1,6 +1,10 @@ -// Generated by Wrangler -// After adding bindings to `wrangler.json`, regenerate this interface via `npm run cf-typegen` +// Generated by Wrangler by running `wrangler types` + interface Env { - OPENAI_GATEWAY: string; + STORE: KVNamespace; SENTRY_DSN: string; + AWS_ACCESS_KEY_ID: string; + AWS_SECRET_ACCESS_KEY: string; + OPENAI_API_KEY: string; + CF_VERSION_METADATA: { id: string; tag: string }; }