Skip to content
This repository has been archived by the owner on Jul 20, 2024. It is now read-only.

Commit

Permalink
feat: ✨ replaced rome with biome; ran format
Browse files Browse the repository at this point in the history
  • Loading branch information
AkashRajpurohit committed Feb 25, 2024
1 parent d3f8088 commit 9e73509
Show file tree
Hide file tree
Showing 9 changed files with 250 additions and 229 deletions.
6 changes: 3 additions & 3 deletions rome.json → biome.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"$schema": "./node_modules/rome/configuration_schema.json",
"$schema": "https://biomejs.dev/schemas/1.2.2/schema.json",
"organizeImports": {
"enabled": false
},
Expand All @@ -13,7 +13,7 @@
"enabled": true,
"formatWithErrors": false,
"indentStyle": "tab",
"indentSize": 2,
"indentWidth": 2,
"lineWidth": 100,
"ignore": []
},
Expand All @@ -25,4 +25,4 @@
"quoteProperties": "asNeeded"
}
}
}
}
10 changes: 5 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,18 @@
"dev": "wrangler dev",
"start": "wrangler dev --remote",
"deploy": "wrangler deploy",
"format": "rome format ./src --write",
"lint": "rome check ./src",
"lint:fix": "rome check ./src --apply"
"format": "biome format ./src --write",
"lint": "biome check ./src",
"lint:fix": "biome check ./src --apply"
},
"dependencies": {
"hono": "^4.0.0",
"zod": "^3.21.4"
},
"devDependencies": {
"@biomejs/biome": "^1.5.3",
"@cloudflare/workers-types": "^4.20230518.0",
"rome": "^12.1.3",
"typescript": "^5.1.3",
"wrangler": "^3.1.0"
}
}
}
164 changes: 95 additions & 69 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

32 changes: 16 additions & 16 deletions src/email.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,23 @@ import { Email } from './zod';
import { Bindings } from './types';

export const sendEmail = async (email: Email, env: Bindings) => {
const mcEmail = convertEmail(email, env);
const mcEmail = convertEmail(email, env);

// send email through MailChannels
const resp = await fetch('https://api.mailchannels.net/tx/v1/send', {
method: 'POST',
headers: {
'content-type': 'application/json',
},
body: JSON.stringify(mcEmail),
});
// send email through MailChannels
const resp = await fetch('https://api.mailchannels.net/tx/v1/send', {
method: 'POST',
headers: {
'content-type': 'application/json',
},
body: JSON.stringify(mcEmail),
});

const respJson = await resp.json();
const respJson = await resp.json();

return {
success: resp.status < 299 || resp.status >= 200,
status: resp.status,
message: resp.statusText,
body: respJson
}
return {
success: resp.status < 299 || resp.status >= 200,
status: resp.status,
message: resp.statusText,
body: respJson,
};
};
32 changes: 16 additions & 16 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,30 +12,30 @@ app.use('*', cors());

// Routes
app.get('/', (c) => {
return c.redirect('https://www.youtube.com/watch?v=dQw4w9WgXcQ', 302);
return c.redirect('https://www.youtube.com/watch?v=dQw4w9WgXcQ', 302);
});

app.post('/send', authMiddleware(), async (c) => {
const email = await c.req.json();
const result = emailSchema.safeParse(email);

if (!result.success) {
return c.json({ success: false, issues: result.error.flatten() }, 400);
}

try {
const response = await sendEmail(email, c.env);
return c.json(response, response?.status || 200);
} catch (err) {
return c.json({ success: false, message: err.message }, 400);
}
const email = await c.req.json();
const result = emailSchema.safeParse(email);

if (!result.success) {
return c.json({ success: false, issues: result.error.flatten() }, 400);
}

try {
const response = await sendEmail(email, c.env);
return c.json(response, response?.status || 200);
} catch (err) {
return c.json({ success: false, message: err.message }, 400);
}
});

app.notFound((c) => c.json({ ok: false, message: 'Not Found' }, 404));

app.onError((err, c) => {
console.error(err);
return c.json({ success: false, message: err.message }, 500);
console.error(err);
return c.json({ success: false, message: err.message }, 500);
});

export default app;
34 changes: 17 additions & 17 deletions src/middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,24 @@ import { Context, MiddlewareHandler } from 'hono';
import { Bindings } from './types';

export const authMiddleware = (): MiddlewareHandler => {
return async (c: Context<{ Bindings: Bindings }>, next) => {
const token = c.req.headers.get('Authorization');
return async (c: Context<{ Bindings: Bindings }>, next) => {
const token = c.req.headers.get('Authorization');

// Strict check for token existence
if (!c.env.TOKEN || c.env.TOKEN.length === 0) {
return c.json(
{
success: false,
message: 'You must set the TOKEN environment variable.',
},
401,
);
}
// Strict check for token existence
if (!c.env.TOKEN || c.env.TOKEN.length === 0) {
return c.json(
{
success: false,
message: 'You must set the TOKEN environment variable.',
},
401
);
}

if (token !== c.env.TOKEN) {
return c.json({ success: false, message: 'Missing authorization' }, 401);
}
if (token !== c.env.TOKEN) {
return c.json({ success: false, message: 'Missing authorization' }, 401);
}

await next();
};
await next();
};
};
Loading

0 comments on commit 9e73509

Please sign in to comment.