Skip to content
This repository was archived by the owner on Oct 28, 2024. It is now read-only.

Commit 55cf03e

Browse files
committed
inital commit for email service
0 parents commit 55cf03e

File tree

8 files changed

+1063
-0
lines changed

8 files changed

+1063
-0
lines changed

.gitignore

Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
# Logs
2+
logs
3+
*.log
4+
npm-debug.log*
5+
yarn-debug.log*
6+
yarn-error.log*
7+
lerna-debug.log*
8+
.pnpm-debug.log*
9+
.idea
10+
11+
# Diagnostic reports (https://nodejs.org/api/report.html)
12+
report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json
13+
14+
# Runtime data
15+
pids
16+
*.pid
17+
*.seed
18+
*.pid.lock
19+
20+
# Directory for instrumented libs generated by jscoverage/JSCover
21+
lib-cov
22+
23+
# Coverage directory used by tools like istanbul
24+
coverage
25+
*.lcov
26+
27+
# nyc test coverage
28+
.nyc_output
29+
30+
# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files)
31+
.grunt
32+
33+
# Bower dependency directory (https://bower.io/)
34+
bower_components
35+
36+
# node-waf configuration
37+
.lock-wscript
38+
39+
# Compiled binary addons (https://nodejs.org/api/addons.html)
40+
build/Release
41+
42+
# Dependency directories
43+
node_modules/
44+
jspm_packages/
45+
46+
# Snowpack dependency directory (https://snowpack.dev/)
47+
web_modules/
48+
49+
# TypeScript cache
50+
*.tsbuildinfo
51+
52+
# Optional npm cache directory
53+
.npm
54+
55+
# Optional eslint cache
56+
.eslintcache
57+
58+
# Optional stylelint cache
59+
.stylelintcache
60+
61+
# Microbundle cache
62+
.rpt2_cache/
63+
.rts2_cache_cjs/
64+
.rts2_cache_es/
65+
.rts2_cache_umd/
66+
67+
# Optional REPL history
68+
.node_repl_history
69+
70+
# Output of 'npm pack'
71+
*.tgz
72+
73+
# Yarn Integrity file
74+
.yarn-integrity
75+
76+
# dotenv environment variable files
77+
.env
78+
.dev.vars
79+
.env.development.local
80+
.env.test.local
81+
.env.production.local
82+
.env.local
83+
84+
# parcel-bundler cache (https://parceljs.org/)
85+
.cache
86+
.parcel-cache
87+
88+
# Next.js build output
89+
.next
90+
out
91+
92+
# Nuxt.js build / generate output
93+
.nuxt
94+
dist
95+
96+
# Gatsby files
97+
.cache/
98+
# Comment in the public line in if your project uses Gatsby and not Next.js
99+
# https://nextjs.org/blog/next-9-1#public-directory-support
100+
# public
101+
102+
# vuepress build output
103+
.vuepress/dist
104+
105+
# vuepress v2.x temp and cache directory
106+
.temp
107+
.cache
108+
109+
# Docusaurus cache and generated files
110+
.docusaurus
111+
112+
# Serverless directories
113+
.serverless/
114+
115+
# FuseBox cache
116+
.fusebox/
117+
118+
# DynamoDB Local files
119+
.dynamodb/
120+
121+
# TernJS port file
122+
.tern-port
123+
124+
# Stores VSCode versions used for testing VSCode extensions
125+
.vscode-test
126+
127+
# yarn v2
128+
.yarn/cache
129+
.yarn/unplugged
130+
.yarn/build-state.yml
131+
.yarn/install-state.gz
132+
.pnp.*
133+
134+
.DS_Store

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# Email Proxy

index.ts

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
import { Router, IRequest } from 'itty-router';
2+
3+
// Constants
4+
const API_ENDPOINT = 'https://api.mailchannels.net/tx/v1/send';
5+
const CONTENT_TYPE_JSON = 'application/json';
6+
7+
// Helper function to create a response
8+
const createResponse = (body: any, status: number) =>
9+
new Response(JSON.stringify(body), {
10+
headers: { 'content-type': CONTENT_TYPE_JSON },
11+
status,
12+
});
13+
14+
// Curry function for handling errors
15+
const handleError = (status: number) => (message: string, request: IRequest) =>
16+
createResponse({ error: message }, status);
17+
18+
// Middleware for authentication
19+
const authMiddleware = (token: string | null) => (request: IRequest) => {
20+
const requestToken = request.headers.get('Authorization');
21+
22+
const unauthorizedError = handleError(401)('Unauthorized', request);
23+
const missingTokenError = handleError(401)('You must set the TOKEN environment variable.', request);
24+
25+
return token
26+
? requestToken === token
27+
? null
28+
: unauthorizedError
29+
: missingTokenError;
30+
};
31+
32+
// Service function to send email
33+
const sendEmail = async (email: any) => {
34+
const resp = await fetch(API_ENDPOINT, {
35+
method: 'POST',
36+
headers: {
37+
'content-type': CONTENT_TYPE_JSON,
38+
},
39+
body: JSON.stringify(email),
40+
});
41+
42+
if (!resp.ok) {
43+
const errorMessage = `Error sending email: ${resp.status} ${resp.statusText}`;
44+
throw new Error(`${errorMessage}\n${await resp.text()}`);
45+
}
46+
};
47+
48+
// Route handler for sending email
49+
const sendEmailHandler = async (request: IRequest, env: Env) => {
50+
try {
51+
const authError = authMiddleware(env.TOKEN)(request);
52+
if (authError) return authError;
53+
54+
const email = await request.json();
55+
await sendEmail(email);
56+
57+
return createResponse({
58+
status: 'SUCCESS',
59+
statusCode: 1000,
60+
message: 'NA',
61+
}, 200);
62+
} catch (error) {
63+
console.error(`Error processing request: ${error}`);
64+
return handleError(500)('Internal Server Error', request);
65+
}
66+
};
67+
68+
// Create router
69+
const router = Router();
70+
71+
// Define routes
72+
router.post('/api/email', sendEmailHandler);
73+
router.all('*', (request: IRequest) => handleError(404)('Not Found', request));
74+
75+
// Export the router handler
76+
export default {
77+
fetch: router.handle,
78+
};

package.json

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"name": "email",
3+
"version": "0.0.0",
4+
"private": true,
5+
"scripts": {
6+
"deploy": "wrangler publish",
7+
"start": "wrangler dev"
8+
},
9+
"devDependencies": {
10+
"@cloudflare/workers-types": "^4.20230419.0",
11+
"itty-router": "^4.0.9",
12+
"prettier": "^2.8.8",
13+
"typescript": "^5.0.4"
14+
},
15+
"dependencies": {
16+
"wrangler": "^3.1.1",
17+
"zod": "^3.22.4"
18+
}
19+
}

0 commit comments

Comments
 (0)