Skip to content

Commit

Permalink
Merge pull request #124 from thanhdanh27600/staging
Browse files Browse the repository at this point in the history
specifice cors
  • Loading branch information
thanhdanh27600 authored Aug 28, 2023
2 parents aa73343 + 24b1d66 commit 327b4ea
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 10 deletions.
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
"add": "^2.0.6",
"axios": "^1.2.6",
"clsx": "^1.2.1",
"cors": "^2.8.5",
"crypto-js": "^4.1.1",
"dayjs": "^1.11.9",
"dotenv": "^16.3.1",
Expand Down Expand Up @@ -59,6 +60,7 @@
"zod": "^3.20.6"
},
"devDependencies": {
"@types/cors": "^2.8.13",
"@types/crypto-js": "^4.1.1",
"@types/geoip-country": "^4.0.0",
"@types/mixpanel-browser": "^2.38.1",
Expand Down
19 changes: 10 additions & 9 deletions src/api/axios.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import axios from 'axios';
import Cors from 'cors';
import { NextApiRequest, NextApiResponse } from 'next';
import { BASE_URL } from '../types/constants';
import { BASE_URL, BASE_URL_SHORT, alternateBrandUrl } from '../types/constants';

export const API = axios.create({
baseURL: BASE_URL,
Expand All @@ -22,17 +23,17 @@ export function withAuth(token?: string) {
};
}

const cors = Cors({
origin: [BASE_URL, BASE_URL_SHORT, ...alternateBrandUrl],
methods: ['GET', 'POST', 'PUT', 'PATCH'],
});

export const allowCors = (handler: any) => async (req: NextApiRequest, res: NextApiResponse) => {
res.setHeader('Access-Control-Allow-Credentials', 'true');
res.setHeader('Access-Control-Allow-Origin', '*');
res.setHeader('Access-Control-Allow-Methods', 'GET,OPTIONS,PATCH,DELETE,POST,PUT');
res.setHeader(
'Access-Control-Allow-Headers',
'X-CSRF-Token, X-Requested-With, Accept, Accept-Version, Content-Length, Content-MD5, Content-Type, Date, X-Api-Version',
);
if (req.method === 'OPTIONS') {
res.status(200).end();
return;
}
return await handler(req, res);
return cors(req, res, async () => {
return await handler(req, res);
});
};
1 change: 1 addition & 0 deletions src/types/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ export const localUrl = 'http://localhost:5000';
export const brandUrl = 'https://clickdi.top';
export const brandUrlShort = 'https://clid.top';
export const brandUrlShortDomain = 'clid.top';
export const alternateBrandUrl = ['https://vietnamese.cloud'];
export const isProduction = process.env.NODE_ENV === 'production';
export const isTest = process.env.NODE_ENV === 'test';
export const cdnUrl = 'https://cdn.jsdelivr.net/gh/thanhdanh27600/clickdi@production/public';
Expand Down
22 changes: 21 additions & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1168,6 +1168,13 @@
dependencies:
"@babel/types" "^7.20.7"

"@types/cors@^2.8.13":
version "2.8.13"
resolved "https://registry.yarnpkg.com/@types/cors/-/cors-2.8.13.tgz#b8ade22ba455a1b8cb3b5d3f35910fd204f84f94"
integrity sha512-RG8AStHlUiV5ysZQKq97copd2UmVYw3/pRMLefISZ3S1hK104Cwm7iLQ3fTKx+lsUH2CE8FlLaYeEA2LSeqYUA==
dependencies:
"@types/node" "*"

"@types/crypto-js@^4.1.1":
version "4.1.1"
resolved "https://registry.yarnpkg.com/@types/crypto-js/-/crypto-js-4.1.1.tgz#602859584cecc91894eb23a4892f38cfa927890d"
Expand Down Expand Up @@ -2024,6 +2031,14 @@ core-js@^3:
resolved "https://registry.yarnpkg.com/core-js/-/core-js-3.28.0.tgz#ed8b9e99c273879fdfff0edfc77ee709a5800e4a"
integrity sha512-GiZn9D4Z/rSYvTeg1ljAIsEqFm0LaN9gVtwDCrKL80zHtS31p9BAjmTxVqTQDMpwlMolJZOFntUG2uwyj7DAqw==

cors@^2.8.5:
version "2.8.5"
resolved "https://registry.yarnpkg.com/cors/-/cors-2.8.5.tgz#eac11da51592dd86b9f06f6e7ac293b3df875d29"
integrity sha512-KIHbLJqu73RGr/hnbrO9uBeixNGuvSQjul/jdFvS/KFSIH1hWVd1ng7zOHx+YrEfInLG7q4n6GHQ9cDtxv/P6g==
dependencies:
object-assign "^4"
vary "^1"

cosmiconfig@8.0.0:
version "8.0.0"
resolved "https://registry.yarnpkg.com/cosmiconfig/-/cosmiconfig-8.0.0.tgz#e9feae014eab580f858f8a0288f38997a7bebe97"
Expand Down Expand Up @@ -4353,7 +4368,7 @@ npm-run-path@^4.0.1:
dependencies:
path-key "^3.0.0"

object-assign@^4.1.1:
object-assign@^4, object-assign@^4.1.1:
version "4.1.1"
resolved "https://registry.npmjs.org/object-assign/-/object-assign-4.1.1.tgz"
integrity sha512-rJgTQnkUnH1sFw8yT6VSU3zD3sWmu6sZhIseY8VX+GRu3P6F7Fu+JNDoXfklElbLJSnc3FUQHVe4cU5hj+BcUg==
Expand Down Expand Up @@ -5688,6 +5703,11 @@ v8-to-istanbul@^9.0.1:
"@types/istanbul-lib-coverage" "^2.0.1"
convert-source-map "^1.6.0"

vary@^1:
version "1.1.2"
resolved "https://registry.yarnpkg.com/vary/-/vary-1.1.2.tgz#2299f02c6ded30d4a5961b0b9f74524a18f634fc"
integrity sha512-BNGbWLfd0eUPabhkXUVm0j8uuvREyTh5ovRa/dyow/BqAbZJyC+5fU+IzQOzmAKzYqYRAISoRhdQr3eIZ/PXqg==

void-elements@3.1.0:
version "3.1.0"
resolved "https://registry.yarnpkg.com/void-elements/-/void-elements-3.1.0.tgz#614f7fbf8d801f0bb5f0661f5b2f5785750e4f09"
Expand Down

0 comments on commit 327b4ea

Please sign in to comment.