Skip to content

Commit e4f8e58

Browse files
committed
chore: updated cors logic and added preflight support
1 parent ffbf812 commit e4f8e58

File tree

6 files changed

+17
-16
lines changed

6 files changed

+17
-16
lines changed

bun.lock

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
"pino-loki": "^2.4.0",
1313
},
1414
"devDependencies": {
15+
"@elysiajs/cors": "^1.2.0",
1516
"@sinclair/typebox": "^0.34.13",
1617
"bun-types": "^1.1.41",
1718
"eslint": "^9.17.0",
@@ -29,6 +30,8 @@
2930
"packages": {
3031
"@bufbuild/protobuf": ["@bufbuild/protobuf@2.2.3", "", {}, "sha512-tFQoXHJdkEOSwj5tRIZSPNUuXK3RaR7T1nUrPgbYX1pUbvqqaaZAsfo+NXBPsz5rZMSKVFrgK1WL8Q/MSLvprg=="],
3132

33+
"@elysiajs/cors": ["@elysiajs/cors@1.2.0", "", { "peerDependencies": { "elysia": ">= 1.2.0" } }, "sha512-qsJwDAg6WfdQRMfj6uSMcDPSpXvm/zQFeAX1uuJXhIgazH8itSfcDxcH9pMuXVRX1yQNi2pPwNQLJmAcw5mzvw=="],
34+
3235
"@elysiajs/swagger": ["@elysiajs/swagger@1.2.0", "", { "dependencies": { "@scalar/themes": "^0.9.52", "@scalar/types": "^0.0.12", "openapi-types": "^12.1.3", "pathe": "^1.1.2" }, "peerDependencies": { "elysia": ">= 1.2.0" } }, "sha512-OPx93DP6rM2VHjA3D44Xiz5MYm9AYlO2NGWPsnSsdyvaOCiL9wJj529583h7arX4iIEYE5LiLB0/A45unqbopw=="],
3336

3437
"@eslint-community/eslint-utils": ["@eslint-community/eslint-utils@4.4.1", "", { "dependencies": { "eslint-visitor-keys": "^3.4.3" }, "peerDependencies": { "eslint": "^6.0.0 || ^7.0.0 || >=8.0.0" } }, "sha512-s3O3waFUrMV8P/XaF/+ZTp1X9XBZW1a4B97ZnjQF2KYWaFD2A8KyFBsrsfSjEmjn3RGWAIuvlneuZm3CUK3jbA=="],

changelog.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
# 2.0.3
2+
3+
- Добавлена обработка Preflight CORS запросов
4+
- Изменена логика добавления CORS
5+
16
# 2.0.2
27

38
- Добавлена проверка на значение logToFile перед созданием папки logs

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "summarize-backend",
3-
"version": "2.0.2",
3+
"version": "2.0.3",
44
"author": "Toil",
55
"repository": {
66
"type": "git",
@@ -21,6 +21,7 @@
2121
"pino-loki": "^2.4.0"
2222
},
2323
"devDependencies": {
24+
"@elysiajs/cors": "^1.2.0",
2425
"@sinclair/typebox": "^0.34.13",
2526
"bun-types": "^1.1.41",
2627
"eslint": "^9.17.0",

src/config.ts

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,7 @@ export default Value.Parse(ConfigSchema, {
1414
desc: Bun.env.APP_DESC,
1515
contact_email: Bun.env.APP_CONTACT_EMAIL,
1616
},
17-
cors: {
18-
"Access-Control-Allow-Origin": "*",
19-
"Access-Control-Allow-Headers": "*",
20-
"Access-Control-Allow-Methods": "GET, POST, OPTIONS",
21-
"Access-Control-Max-Age": "86400",
22-
},
17+
cors: {},
2318
logging: {
2419
level: Bun.env.NODE_ENV === "production" ? "info" : "debug",
2520
logPath: path.join(__dirname, "..", "logs"),

src/index.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import fs from "node:fs/promises";
22

33
import { Elysia } from "elysia";
44
import { swagger } from "@elysiajs/swagger";
5+
import { cors } from "@elysiajs/cors";
56
import { HttpStatusCode } from "elysia-http-status-code";
67

78
import config from "./config";
@@ -47,11 +48,7 @@ const app = new Elysia({
4748
}),
4849
)
4950
.use(HttpStatusCode())
50-
.onRequest(({ set }) => {
51-
for (const [key, val] of Object.entries(config.cors)) {
52-
set.headers[key] = val;
53-
}
54-
})
51+
.use(cors(config.cors))
5552
.error({
5653
SHARING_URL_NOT_FOUND: SharingUrlNotFoundError,
5754
SHARING_API_TOKEN_NOT_FOUND: SharingAPITokenNotFoundError,

src/schemas/config.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,10 @@ export const ConfigSchema = t.Object({
3939
scalarCDN: t.Literal(scalarCDN, { readOnly: true, default: scalarCDN }),
4040
}),
4141
cors: t.Object({
42-
"Access-Control-Allow-Origin": t.String({ default: "*" }),
43-
"Access-Control-Allow-Headers": t.String({ default: "*" }),
44-
"Access-Control-Allow-Methods": t.String({ default: "POST, GET, OPTIONS" }),
45-
"Access-Control-Max-Age": t.String({ default: "86400" }),
42+
allowedHeaders: t.String({ default: "*" }),
43+
origin: t.String({ default: "*" }),
44+
methods: t.String({ default: "GET, POST, OPTIONS" }),
45+
maxAge: t.Number({ default: 86400 }),
4646
}),
4747
logging: t.Object({
4848
level: LoggingLevel,

0 commit comments

Comments
 (0)