From 63394d3a8aebf7d87d413a474db14a7b5e91fb62 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matej=20Glasn=C3=A1k?= Date: Wed, 1 Feb 2023 15:04:03 +0100 Subject: [PATCH] . --- app/src/plugins/_testing/testing.vue | 51 ++--- app/src/plugins/app/_config/main.ts | 5 + app/src/plugins/w@custom/axios/Readme.md | 53 ++++++ app/src/plugins/w@custom/axios/index.ts | 11 ++ .../plugins/w@custom/axios/models/axios.ts | 176 ++++++++++++++++++ app/src/plugins/w@custom/axios/types/index.ts | 3 + 6 files changed, 264 insertions(+), 35 deletions(-) create mode 100644 app/src/plugins/w@custom/axios/Readme.md create mode 100644 app/src/plugins/w@custom/axios/index.ts create mode 100644 app/src/plugins/w@custom/axios/models/axios.ts create mode 100644 app/src/plugins/w@custom/axios/types/index.ts diff --git a/app/src/plugins/_testing/testing.vue b/app/src/plugins/_testing/testing.vue index f261cbd..e1716f5 100644 --- a/app/src/plugins/_testing/testing.vue +++ b/app/src/plugins/_testing/testing.vue @@ -23,6 +23,10 @@ export default { }, methods: { + onChange($event) { + this.files = $event.target.files + }, + async createWithoutFile() { const masterTask = this._getMastertaskObject() @@ -34,47 +38,24 @@ export default { if (!this.files) return alert('No files uploaded') const masterTask = this._getMastertaskObject() - masterTask.data.files = this.files + masterTask.files = this.files - await axios.post('https://taskmanager.internal.studiotem.com/api/v1/mastertasks', this._objectToFormData(masterTask)) - console.log(await this.$axios.get_data('v1/mastertasks')) + console.log(await this.$axios.post_auth_data('v1/mastertasks', masterTask)) }, _getMastertaskObject() { - const employees = [{ - id: 1, - duration: 1000 - }] - return { - data: { - name: `name-${3}`, - description: `description`, - duration: 1231, - deadline: moment().format('YYYY-MM-DD HH:MM:SS'), - project_id: 1, - team_id: 1, - employees: employees - } + name: `name-${3}`, + description: `description`, + duration: 1231, + deadline: moment().format('YYYY-MM-DD HH:MM:SS'), + project_id: 1, + team_id: 1, + employees: [{ + id: 1, + duration: 1000 + }] } - }, - - onChange($event) { - this.files = $event.target.files - }, - - _buildFormData(formData, data, parentKey) { - if (data && typeof data === 'object' && !(data instanceof Date) && !(data instanceof File)) - Object.keys(data).forEach(key => this._buildFormData(formData, data[key], parentKey ? `${parentKey}[${key}]` : key)); - else - formData.append(parentKey, data == null ? '' : data); - }, - - _objectToFormData(data) { - const formData = new FormData() - this._buildFormData(formData, data) - - return formData } } } diff --git a/app/src/plugins/app/_config/main.ts b/app/src/plugins/app/_config/main.ts index ccae222..07945f5 100644 --- a/app/src/plugins/app/_config/main.ts +++ b/app/src/plugins/app/_config/main.ts @@ -8,6 +8,7 @@ import App from './App.vue' import router from './router' import store from '@/store' import w from '@/plugins/w/w' +import axiosCustom from '@/plugins/w@custom/axios' import vueCal from '@/plugins/app@vendor/vue-cal' import { Options } from '@/plugins/w/w/types' @@ -29,6 +30,9 @@ import '@vuepic/vue-datepicker/dist/main.css' loadFonts() const options: Options = { + axios: { + disabled: true + }, i18n: { languages: [ { @@ -82,6 +86,7 @@ const options: Options = { const app = createApp(App) .use(router) .use(store) + .use(axiosCustom) .use(w, options) .use(IonicVue) .use(vuetify) diff --git a/app/src/plugins/w@custom/axios/Readme.md b/app/src/plugins/w@custom/axios/Readme.md new file mode 100644 index 0000000..297ea48 --- /dev/null +++ b/app/src/plugins/w@custom/axios/Readme.md @@ -0,0 +1,53 @@ +# W / Axios +- This plugin will add axios (this.$axios) with custom functions +# Required internal plugins +- w/auth +# Installation +1. Install plugin in `main.ts` +```javascript +import axios from '@/plugins/w/axios' + +const app = createApp(App) + .use(auth) + .use(axios) +``` +2. Create `.env` in app root directory +3. Add `VUE_APP_WAXIOS_URL=defaultBaseUrl` to `.env` + +# Examples +1. Call without authorization header +```javascript +this.$axios.get('v1/courses') +``` +2. Call with authorization header +```javascript +this.$axios.get_auth('v1/courses') +``` +3. Call that will return directly response.data.data +```javascript +this.$axios.get_data('v1/courses') +this.$axios.get_auth_data('v1/courses') +``` + +# Public functions +```javascript +async get(url: string, config?: any) +async get_data(url: string, config?: any) +async get_auth(url: string, config?: any) +async get_auth_data(url: string, config?: any) + +async post(url: string, config?: any) +async post_data(url: string, config?: any) +async post_auth(url: string, config?: any) +async post_auth_data(url: string, config?: any) + +async patch(url: string, config?: any) +async patch_data(url: string, config?: any) +async patch_auth(url: string, config?: any) +async patch_auth_data(url: string, config?: any) + +async delete(url: string, config?: any) +async delete_data(url: string, config?: any) +async delete_auth(url: string, config?: any) +async delete_auth_data(url: string, config?: any) +``` \ No newline at end of file diff --git a/app/src/plugins/w@custom/axios/index.ts b/app/src/plugins/w@custom/axios/index.ts new file mode 100644 index 0000000..6ddb889 --- /dev/null +++ b/app/src/plugins/w@custom/axios/index.ts @@ -0,0 +1,11 @@ +import { App } from 'vue' +import { Options } from './types' +import axios from './models/axios' + +export default { + install: (app: App, options: Options) => { + if (options && options.disabled === true) return + + app.config.globalProperties.$axios = axios + } +} \ No newline at end of file diff --git a/app/src/plugins/w@custom/axios/models/axios.ts b/app/src/plugins/w@custom/axios/models/axios.ts new file mode 100644 index 0000000..affb14c --- /dev/null +++ b/app/src/plugins/w@custom/axios/models/axios.ts @@ -0,0 +1,176 @@ +import axios from 'axios' +import store from '@/store' + +axios.defaults.baseURL = process.env.VUE_APP_WAXIOS_URL + +export default { + async get(url: string, config?: any) { + return axios.get(url, config) + .then(response => response.data) + .catch(error => Promise.reject(error)) + }, + + async get_auth(url: string, config?: any) { + config = this._config(config) + + return axios.get(url, config) + .then(response => response.data) + .catch(error => Promise.reject(error)) + }, + + async get_data(url: string, config?: any) { + return axios.get(url, config) + .then(response => response.data.data) + .catch(error => Promise.reject(error.response || error)) + }, + + async get_auth_data(url: string, config?: any) { + config = this._config(config) + + return axios.get(url, config) + .then(response => Promise.resolve(response.data.data)) + .catch(error => Promise.reject(error.response || error)) + }, + + async post(url: string, params = null, config?: any) { + + return axios.post(url, this._objectToFormData(params), config) + .then(response => response.data) + .catch(error => Promise.reject(error)) + }, + + async post_auth(url: string, params = null, config?: any) { + config = this._config(config) + + return axios.post(url, this._objectToFormData(params), config) + .then(response => response.data) + .catch(error => Promise.reject(error)) + }, + + async post_data(url: string, params = null, config?: any) { + + return axios.post(url, this._objectToFormData(params), config) + .then(response => response.data.data) + .catch(error => Promise.reject(error.response || error)) + }, + + async post_auth_data(url: string, params = null, config?: any) { + config = this._config(config) + + return axios.post(url, this._objectToFormData(params), config) + .then(response => response.data.data) + .catch(error => Promise.reject(error.response || error)) + }, + + async delete(url: string, config?: any) { + return axios.delete(url, config) + .then(response => response.data) + .catch(error => Promise.reject(error)) + }, + + async delete_auth(url: string, config?: any) { + config = this._config(config) + + return axios.delete(url, config) + .then(response => response.data) + .catch(error => Promise.reject(error)) + }, + + async delete_data(url: string, config?: any) { + return axios.delete(url, config) + .then(response => response.data.data) + .catch(error => Promise.reject(error.response || error)) + }, + + async delete_auth_data(url: string, config?: any) { + config = this._config(config) + + return axios.delete(url, config) + .then(response => response.data.data) + .catch(error => Promise.reject(error.response || error)) + }, + + async patch(url: string, params = null, config?: any) { + return axios.patch(url, this._objectToFormData(params), config) + .then(response => response.data) + .catch(error => Promise.reject(error)) + }, + + async patch_auth(url: string, params = null, config?: any) { + config = this._config(config) + + return axios.patch(url, this._objectToFormData(params), config) + .then(response => response.data) + .catch(error => Promise.reject(error)) + }, + + async patch_data(url: string, params = null, config?: any) { + return axios.patch(url, this._objectToFormData(params), config) + .then(response => response.data.data) + .catch(error => Promise.reject(error.response || error)) + }, + + async patch_auth_data(url: string, params = null, config?: any) { + config = this._config(config) + + return axios.patch(url, this._objectToFormData(params), config) + .then(response => response.data.data) + .catch(error => Promise.reject(error.response || error)) + }, + + async put(url: string, params = null, config?: any) { + return axios.put(url, this._objectToFormData(params), config) + .then(response => response.data) + .catch(error => Promise.reject(error)) + }, + + async put_auth(url: string, params = null, config?: any) { + config = this._config(config) + + return axios.put(url, this._objectToFormData(params), config) + .then(response => response.data) + .catch(error => Promise.reject(error)) + }, + + async put_data(url: string, params = null, config?: any) { + return axios.put(url, this._objectToFormData(params), config) + .then(response => response.data.data) + .catch(error => Promise.reject(error.response || error)) + }, + + async put_auth_data(url: string, params = null, config?: any) { + config = this._config(config) + + return axios.put(url, this._objectToFormData(params), config) + .then(response => response.data.data) + .catch(error => Promise.reject(error.response || error)) + }, + + _config(config: any) { + if (config) console.error('NOT IMPLEMENTED') + + if (!store.getters['auth/token']) { + return {headers: {}} + } + + return { + headers: { + Authorization: `Bearer ${store.getters['auth/token']}` + } + } + }, + + _buildFormData(formData, data, parentKey) { + if (data && typeof data === 'object' && !(data instanceof Date) && !(data instanceof File)) + Object.keys(data).forEach(key => this._buildFormData(formData, data[key], parentKey ? `${parentKey}[${key}]` : key)); + else + formData.append(parentKey, data == null ? '' : data); + }, + + _objectToFormData(data) { + const formData = new FormData() + this._buildFormData(formData, {data: data}) + + return formData + } +} \ No newline at end of file diff --git a/app/src/plugins/w@custom/axios/types/index.ts b/app/src/plugins/w@custom/axios/types/index.ts new file mode 100644 index 0000000..860ff8d --- /dev/null +++ b/app/src/plugins/w@custom/axios/types/index.ts @@ -0,0 +1,3 @@ +export interface Options { + disabled?: Boolean +} \ No newline at end of file