Created
April 29, 2025 08:43
-
-
Save DevDhaif/997cee661fb67a0552a382a1b6bb5520 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1. `vite.config.js`: | |
``` | |
import { defineConfig } from 'vite'; | |
import react from '@vitejs/plugin-react'; | |
import ENVIRONMENTS from './src/config/environments'; | |
const env = ENVIRONMENTS.DEV; | |
export default defineConfig({ | |
plugins: [react()], | |
server: { | |
proxy: { | |
'/api': { | |
target: env.asset, | |
changeOrigin: true, | |
secure: false, | |
rewrite: (path) => path.replace(/^\/api/, '/api/v3/domain'), | |
configure: (proxy) => { | |
proxy.on('proxyReq', (proxyReq) => { | |
proxyReq.setHeader('Origin', env.asset); | |
proxyReq.setHeader('Access-Control-Allow-Origin', '*'); | |
}); | |
proxy.on('proxyRes', (proxyRes) => { | |
proxyRes.headers['Access-Control-Allow-Origin'] = '*'; | |
proxyRes.headers['Access-Control-Allow-Methods'] = 'GET, POST, PUT, DELETE, OPTIONS'; | |
proxyRes.headers['Access-Control-Allow-Headers'] = 'Content-Type'; | |
}); | |
} | |
}, | |
'/adv-api': { | |
target: env.adv, | |
changeOrigin: true, | |
secure: false, | |
rewrite: (path) => path.replace(/^\/adv-api/, '/api/v1'), | |
headers: { | |
'Access-Control-Allow-Origin': '*' | |
} | |
} | |
}, | |
cors: { | |
origin: '*', | |
methods: 'GET,HEAD,PUT,PATCH,POST,DELETE', | |
preflightContinue: false, | |
optionsSuccessStatus: 204 | |
} | |
} | |
}); | |
``` | |
2. `ENVIRONMENTS.js`: | |
``` | |
const ENVIRONMENTS = { | |
PREPROD: { | |
asset: 'https://asset-preprod.adx-sa.com', | |
adv: 'https://adv-preprod.adx-sa.com', | |
qasset: 'https://qasset-preprod.adx-sa.com', | |
qadv: 'https://qadv-preprod.adx-sa.com' | |
}, | |
DEV: { | |
asset: 'https://asset-dev.devlnsider.net', | |
adv: 'https://adv-dev.devlnsider.net', | |
qasset: 'https://asset-q-dev.devlnsider.net', | |
qadv: 'https://adv-q-dev.devlnsider.net' | |
} | |
}; | |
export default ENVIRONMENTS; | |
``` | |
3. `client.js`: | |
``` | |
import axios from 'axios'; | |
import baseURLs from '../config/baseURLs'; | |
export const createApiClient = (basePath) => { | |
return axios.create({ | |
baseURL: basePath, | |
headers: { | |
'Content-Type': 'application/json', | |
'Accept': 'application/json' | |
}, | |
withCredentials: false | |
}); | |
}; | |
export const assetAPI = createApiClient(baseURLs.asset); | |
export const advAPI = createApiClient(baseURLs.adv); | |
``` | |
4. `baseURLs.js` | |
``` | |
const baseURLs = { | |
asset: "/api", | |
adv: "/adv-api" | |
}; | |
export default baseURLs; | |
``` | |
5. Example (`device.api.js`): | |
``` | |
import { assetAPI } from './client'; | |
export const DeviceService = { | |
registerDevice: (deviceInfo) => | |
assetAPI.post('/devices', deviceInfo) | |
} | |
``` | |
6. then in the hook : | |
``` | |
import { DeviceService } from '../api/device.api'; | |
const startRegistration = async (deviceInfo) => { | |
try { | |
const config = { | |
headers: { | |
'X-Requested-With': 'XMLHttpRequest', | |
'Access-Control-Allow-Origin': '*' | |
} | |
}; | |
const response = await DeviceService.registerDevice(deviceInfo, config); | |
``` |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment