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
import { createApp } from 'vue'
process.env.API_IMPLEMENTATION = 'django';
process.env.API_IMPLEMENTATION = 'tryton';
// process.env.API_IMPLEMENTATION = 'django';
let apiImplementation = new ApiImplementation();
const api = apiImplementation.getApi();

View File

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

View File

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

View File

@ -11,6 +11,8 @@ import Vuetify, { transformAssetUrls } from 'vite-plugin-vuetify'
import { defineConfig } from 'vite'
import { fileURLToPath, URL } from 'node:url'
// https://vitejs.dev/config/
export default defineConfig({
plugins: [
@ -63,6 +65,13 @@ export default defineConfig({
},
server: {
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: {
outDir: '../../static/frontend/',

View File

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