Fix(WIP): CORS

This commit is contained in:
Rodia 2025-01-18 20:18:06 -05:00
parent eb75a13857
commit a097bf7141
5 changed files with 94 additions and 55 deletions

View File

@ -14,7 +14,8 @@ import ApiImplementation from './services/api-implementation';
// Composables // Composables
import { createApp } from 'vue' import { createApp } from 'vue'
process.env.API_IMPLEMENTATION = 'django'; process.env.API_IMPLEMENTATION = 'tryton';
// process.env.API_IMPLEMENTATION = 'django';
let apiImplementation = new ApiImplementation(); let apiImplementation = new ApiImplementation();
const api = apiImplementation.getApi(); const api = apiImplementation.getApi();

View File

@ -9,7 +9,7 @@ class ApiImplementation {
if (implementation === 'django') { if (implementation === 'django') {
apiImplementation = new DjangoApi(); apiImplementation = new DjangoApi();
} else if (implementation === 'tryton'){ } else if (implementation === 'tryton'){
const url = 'http:localhost:8000'; const url = 'http://localhost:8000';
const key = '9a9ffc430146447d81e6698240199a4be2b0e774cb18474999d0f60e33b5b1eb1cfff9d9141346a98844879b5a9e787489c891ddc8fb45cc903b7244cab64fb1'; const key = '9a9ffc430146447d81e6698240199a4be2b0e774cb18474999d0f60e33b5b1eb1cfff9d9141346a98844879b5a9e787489c891ddc8fb45cc903b7244cab64fb1';
const db = 'tryton'; const db = 'tryton';
const applicationName = 'sale_don_confiao'; const applicationName = 'sale_don_confiao';

View File

@ -3,82 +3,107 @@ class TrytonApiClient {
this.baseUrl = `${url}/${db}/${applicationName}`; this.baseUrl = `${url}/${db}/${applicationName}`;
this.headers = { this.headers = {
'Authorization': `Bearer ${key}`, 'Authorization': `Bearer ${key}`,
'Content-Type': 'application/json' 'Content-Type': 'application/json',
}; };
} }
async getParties() { getCustomers() {
return this._fetch('/parties'); // const url = '/don_confiao/api/customers/';
const url = this.baseUrl + '/parties';
const customers = this.getRequest(url);
return customers;
} }
async getParty(id){ getProducts() {
return this._fetch(`/party/${id}`); const url = '/don_confiao/api/products/';
return this.getRequest(url);
} }
async postParty(name) { getPaymentMethods() {
return this._post('/parties', { name }); const url = '/don_confiao/payment_methods/all/select_format';
return this.getRequest(url);
} }
async getCategories() { getSummaryPurchase(purchaseId) {
return this._fetch('/categories'); const url = `/don_confiao/resumen_compra_json/${purchaseId}`;
return this.getRequest(url);
} }
async getProducts() { getPurchasesForReconciliation() {
return this._fetch('/products'); const url = '/don_confiao/purchases/for_reconciliation';
return this.getRequest(url);
} }
async searchProducts(query) { isValidAdminCode(code) {
return this._fetch(`/search_products/${query}`); const url = `/don_confiao/api/admin_code/validate/${code}`
return this.getRequest(url)
} }
async getSales() { createPurchase(purchase) {
return this._fetch('/sales'); const url = '/don_confiao/api/sales/';
return this.postRequest(url, purchase);
} }
async getSale(id) { createReconciliationJar(reconciliation) {
return this._fetch(`/sale/${id}`); const url = '/don_confiao/reconciliate_jar';
return this.postRequest(url, reconciliation);
} }
async postSale(saleData) { createCustomer(customer) {
return this._post('/post_sale', saleData); const url = '/don_confiao/api/customers/';
return this.postRequest(url, customer);
} }
async _fetch(endpoint) { getRequest(url) {
const response = await fetch(`${this.baseUrl}${endpoint}`, { return new Promise ((resolve, reject) => {
fetch(url, {
method: 'GET', method: 'GET',
headers: this.headers headers: this.headers
}).then(response => response.json())
.then(data => {
resolve(data);
})
.catch(error => {
reject(error);
});
}); });
return this._handleResponse(response);
} }
async _post(endpoint, data) { postRequest(url, content) {
const response = await fetch(`${this.baseUrl}${endpoint}`, { return new Promise((resolve, reject) => {
fetch(url, {
method: 'POST', method: 'POST',
headers: this.headers, headers: this.headers,
body: JSON.stringify(data) body: JSON.stringify(content)
}); })
return this._handleResponse(response); .then(response => {
}
async _handleResponse(response) {
if (!response.ok) { if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`); reject(new Error(`Error ${response.status}: ${response.statusText}`));
} else {
response.json().then(data => {
if (!data) {
reject(new Error('La respuesta no es un JSON válido'));
} else {
resolve(data);
} }
return response.json(); });
}
})
.catch(error => reject(error));
});
} }
} }
export default TrytonApiClient; // export default TrytonApiClient;
const url = 'http:localhost:8000'; // const url = 'http://localhost:8000';
const key = '9a9ffc430146447d81e6698240199a4be2b0e774cb18474999d0f60e33b5b1eb1cfff9d9141346a98844879b5a9e787489c891ddc8fb45cc903b7244cab64fb1'; // const key = '9a9ffc430146447d81e6698240199a4be2b0e774cb18474999d0f60e33b5b1eb1cfff9d9141346a98844879b5a9e787489c891ddc8fb45cc903b7244cab64fb1';
const db = 'tryton'; // const db = 'tryton';
const applicationName = 'sale_don_confiao'; // const applicationName = 'sale_don_confiao';
const apiClient = new TrytonApiClient(url, key, db, applicationName); // const apiClient = new TrytonApiClient(url, key, db, applicationName);
console.log( // console.log(
apiClient.getProducts() // apiClient.getCustomers()
.then(data => console.log(data)) // .then(data => console.log(data))
.catch(error => console.error('Error:', error))) // .catch(error => console.error('Error:', error)))

View File

@ -11,6 +11,8 @@ import Vuetify, { transformAssetUrls } from 'vite-plugin-vuetify'
import { defineConfig } from 'vite' import { defineConfig } from 'vite'
import { fileURLToPath, URL } from 'node:url' import { fileURLToPath, URL } from 'node:url'
// https://vitejs.dev/config/ // https://vitejs.dev/config/
export default defineConfig({ export default defineConfig({
plugins: [ plugins: [
@ -63,6 +65,13 @@ export default defineConfig({
}, },
server: { server: {
port: 3000, port: 3000,
proxy: {
'/sale_don_confiao': {
target: "http://localhost:8000", // Cambia esto a la URL de tu API
changeOrigin: true,
rewrite: (path) => path.replace(/^\/sale_don_confiao/, '/tryton/sale_don_confiao'), // Opcional: reescribe la ruta
},
},
}, },
build: { build: {
outDir: '../../static/frontend/', outDir: '../../static/frontend/',

View File

@ -28,6 +28,10 @@ DEBUG = True
ALLOWED_HOSTS = [] ALLOWED_HOSTS = []
CORS_ALLOWED_ORIGINS = [
"http://localhost:8000",
]
# Application definition # Application definition
INSTALLED_APPS = [ INSTALLED_APPS = [