Przeglądaj źródła

refactor fetchers into a provider service

Olivier Massot 4 lat temu
rodzic
commit
0eee2f60ce

+ 12 - 2
nuxt.config.js

@@ -59,6 +59,7 @@ export default {
   // Modules: https://go.nuxtjs.dev/config-modules
   modules: [
     '@nuxtjs/i18n',
+    '@nuxtjs/axios',
     'nuxt-leaflet',
     [
       'nuxt-fontawesome', {
@@ -98,10 +99,19 @@ export default {
   // Build Configuration: https://go.nuxtjs.dev/config-build
   build: {
   },
+
+  // Env
   publicRuntimeConfig: {
-    baseURL: process.env.NODE_ENV === 'production' ? 'https://local.api.opentalent.fr' : 'https://local.api.opentalent.fr'
+    baseURL: process.env.NODE_ENV === 'production' ? 'https://local.api.opentalent.fr' : 'https://local.api.opentalent.fr',
+    axios: {
+      https: true,
+      browserBaseURL: process.env.NODE_ENV === 'production' ? 'https://local.new.api.opentalent.fr' : 'https://local.new.api.opentalent.fr'
+    }
   },
   privateRuntimeConfig: {
-    baseURL: process.env.NODE_ENV === 'production' ? 'https://local.api.opentalent.fr' : 'http://nginx'
+    baseURL: process.env.NODE_ENV === 'production' ? 'https://local.api.opentalent.fr' : 'http://nginx',
+    axios: {
+      baseURL: process.env.NODE_ENV === 'production' ? 'https://local.api.opentalent.fr' : 'http://nginx'
+    }
   }
 }

+ 2 - 1
package.json

@@ -1,6 +1,6 @@
 {
   "name": "frames",
-  "version": "1.0.0",
+  "version": "0.2.0",
   "private": true,
   "scripts": {
     "dev": "nuxt --hostname '0.0.0.0' --port 3004",
@@ -17,6 +17,7 @@
     "@fortawesome/free-solid-svg-icons": "^5.15.4",
     "@fortawesome/vue-fontawesome": "^2.0.2",
     "@nuxt/image": "^0.6.0",
+    "@nuxtjs/axios": "^5.13.6",
     "@nuxtjs/i18n": "^7.0.3",
     "core-js": "^3.15.1",
     "leaflet": "^1.7.1",

+ 6 - 23
pages/structures/_id.vue

@@ -177,30 +177,13 @@
 </template>
 
 <script>
-export default {
-  async asyncData ({ $config, params }) {
-    return await fetch(
-      `${$config.baseURL}/api/public/federation_structures/get?organization-id=${params.id}`
-    ).then(
-      res => res.json()
-    ).then(
-      (s) => {
-        s.n1Id = s.n1Id ? parseInt(s.n1Id) : null
-        s.n2Id = s.n2Id ? parseInt(s.n2Id) : null
-        s.n3Id = s.n3Id ? parseInt(s.n3Id) : null
-        s.n4Id = s.n4Id ? parseInt(s.n4Id) : null
-        s.n5Id = s.n5Id ? parseInt(s.n5Id) : null
-        s.practices = s.practices !== null ? s.practices.split(',') : []
-        s.latitude = s.latitude ? parseFloat(s.latitude) : null
-        s.longitude = s.longitude ? parseFloat(s.longitude) : null
-
-        s.facebook = 'https://facebook.com'
-        s.twitter = 'https://twitter.com'
-        s.instagram = 'https://instagram.com'
+import StructuresProvider from '~/services/data/StructuresProvider'
 
-        return { structure: s }
-      }
-    )
+export default {
+  async asyncData ({ params, $axios }) {
+    return await new StructuresProvider($axios).getById(Number(params.id)).then((res) => {
+      return { structure: res }
+    })
   },
   data () {
     return {

+ 16 - 30
pages/structures/index.vue

@@ -269,7 +269,8 @@
 <script>
 import departments from '@/enums/departments'
 import practices from '@/enums/practices'
-import sphericDistance from '@/lib/geo'
+import sphericDistance from '@/services/utils/geo'
+import StructuresProvider from '~/services/data/StructuresProvider'
 
 export default {
   data () {
@@ -294,37 +295,22 @@ export default {
     }
   },
   async fetch () {
-    await fetch(
-      `${this.$config.baseURL}/api/public/federation_structures/all`
-    ).then(
-      res => res.json()
-    ).then(
-      res => res.map((s) => {
-        s.n1Id = s.n1Id ? parseInt(s.n1Id) : null
-        s.n2Id = s.n2Id ? parseInt(s.n2Id) : null
-        s.n3Id = s.n3Id ? parseInt(s.n3Id) : null
-        s.n4Id = s.n4Id ? parseInt(s.n4Id) : null
-        s.n5Id = s.n5Id ? parseInt(s.n5Id) : null
-        s.practices = s.practices !== null ? s.practices.split(',') : []
-        s.latitude = s.latitude ? parseFloat(s.latitude) : null
-        s.longitude = s.longitude ? parseFloat(s.longitude) : null
-        return s
-      })
-    ).then((res) => {
-      this.structures = res
-      this.filteredStructures = res
+    await new StructuresProvider(this.$axios).getAll(12097).then(
+      (res) => {
+        this.structures = res
+        this.filteredStructures = res
 
-      // populate federations filter
-      for (const s of res) {
-        const f = {
-          id: s.n1Id,
-          name: s.n1Name
-        }
-        if (!this.federations.includes(f)) {
-          this.federations.push(f)
+        // populate federations filter
+        for (const s of res) {
+          const f = {
+            id: s.n1Id,
+            name: s.n1Name
+          }
+          if (!this.federations.includes(f)) {
+            this.federations.push(f)
+          }
         }
-      }
-    })
+      })
   },
   computed: {
     totalRecords () {

+ 42 - 0
services/data/BaseProvider.ts

@@ -0,0 +1,42 @@
+import { NuxtAxiosInstance } from '@nuxtjs/axios'
+import { AxiosRequestConfig } from 'axios'
+
+class BaseProvider {
+  protected connector: NuxtAxiosInstance;
+
+  constructor (connector: NuxtAxiosInstance) {
+    this.connector = connector
+  }
+
+  /**
+   * Initialisation du connecteur Axios
+   * @param {NuxtAxiosInstance} connector
+   */
+  initConnector (connector: NuxtAxiosInstance) {
+    this.connector = connector
+  }
+
+  /**
+   * Send a GET request
+   * @param {string} url
+   * @return {Promise<any>}
+   */
+  async get (url: string): Promise<any> {
+    const config: AxiosRequestConfig = {
+      url: `${url}`,
+      method: 'GET'
+    }
+    return await this.request(config)
+  }
+
+  /**
+   * Exécution de la requete
+   * @param {AxiosRequestConfig} config
+   * @return {Promise<any>}
+   */
+  async request (config: AxiosRequestConfig): Promise<any> {
+    return await this.connector.$request(config)
+  }
+}
+
+export default BaseProvider

+ 33 - 0
services/data/StructuresProvider.ts

@@ -0,0 +1,33 @@
+import BaseProvider from '~/services/data/BaseProvider'
+
+class StructuresProvider extends BaseProvider {
+  protected normalize (s: any) : Structure {
+    s.n1Id = s.n1Id ? parseInt(s.n1Id) : null
+    s.n2Id = s.n2Id ? parseInt(s.n2Id) : null
+    s.n3Id = s.n3Id ? parseInt(s.n3Id) : null
+    s.n4Id = s.n4Id ? parseInt(s.n4Id) : null
+    s.n5Id = s.n5Id ? parseInt(s.n5Id) : null
+    s.practices = s.practices !== null ? s.practices.split(',') : []
+    s.latitude = s.latitude ? parseFloat(s.latitude) : null
+    s.longitude = s.longitude ? parseFloat(s.longitude) : null
+    return s
+  }
+
+  async getAll (parentId: number): Promise<Array<Structure>> {
+    return await this.get(
+      `/api/public/federation_structures/all?parent-id=${parentId}`
+    ).then((res) => {
+      return res.map((s: any) => { return this.normalize(s) })
+    })
+  }
+
+  async getById (organizationId: number): Promise<Structure> {
+    return await this.get(
+      `/api/public/federation_structures/get?organization-id=${organizationId}`
+    ).then((s) => {
+      return this.normalize(s)
+    })
+  }
+}
+
+export default StructuresProvider

+ 0 - 0
lib/geo.js → services/utils/geo.js


+ 1 - 0
tsconfig.json

@@ -27,6 +27,7 @@
       "@nuxt/types",
       "@nuxtjs/i18n",
       "nuxt-leaflet",
+      "@nuxtjs/axios",
       "@types/node"
     ]
   },

+ 84 - 0
yarn.lock

@@ -1647,6 +1647,17 @@
     webpack-node-externals "^3.0.0"
     webpackbar "^4.0.0"
 
+"@nuxtjs/axios@^5.13.6":
+  version "5.13.6"
+  resolved "https://registry.yarnpkg.com/@nuxtjs/axios/-/axios-5.13.6.tgz#6f4bbd98a3a7799a5d2c0726c6ad2a98aa111881"
+  integrity sha512-XS+pOE0xsDODs1zAIbo95A0LKlilvJi8YW0NoXYuq3/jjxGgWDxizZ6Yx0AIIjZOoGsXJOPc0/BcnSEUQ2mFBA==
+  dependencies:
+    "@nuxtjs/proxy" "^2.1.0"
+    axios "^0.21.1"
+    axios-retry "^3.1.9"
+    consola "^2.15.3"
+    defu "^5.0.0"
+
 "@nuxtjs/eslint-config-typescript@^6.0.1":
   version "6.0.1"
   resolved "https://registry.yarnpkg.com/@nuxtjs/eslint-config-typescript/-/eslint-config-typescript-6.0.1.tgz#11e91a5e25aca6855ec7525080da694c4b3cd4d4"
@@ -1696,6 +1707,13 @@
     ufo "^0.7.9"
     vue-i18n "^8.25.0"
 
+"@nuxtjs/proxy@^2.1.0":
+  version "2.1.0"
+  resolved "https://registry.yarnpkg.com/@nuxtjs/proxy/-/proxy-2.1.0.tgz#fa7715a11d237fa1273503c4e9e137dd1bf5575b"
+  integrity sha512-/qtoeqXgZ4Mg6LRg/gDUZQrFpOlOdHrol/vQYMnKu3aN3bP90UfOUB3QSDghUUK7OISAJ0xp8Ld78aHyCTcKCQ==
+  dependencies:
+    http-proxy-middleware "^1.0.6"
+
 "@nuxtjs/vuetify@^1.12.1":
   version "1.12.1"
   resolved "https://registry.yarnpkg.com/@nuxtjs/vuetify/-/vuetify-1.12.1.tgz#87dd1e1517b0120c660f4a43a22627c9fe124189"
@@ -1915,6 +1933,13 @@
     "@types/relateurl" "*"
     "@types/uglify-js" "*"
 
+"@types/http-proxy@^1.17.5":
+  version "1.17.7"
+  resolved "https://registry.yarnpkg.com/@types/http-proxy/-/http-proxy-1.17.7.tgz#30ea85cc2c868368352a37f0d0d3581e24834c6f"
+  integrity sha512-9hdj6iXH64tHSLTY+Vt2eYOGzSogC+JQ2H7bdPWkuh7KXP5qLllWx++t+K9Wk556c3dkDdPws/SpMRi0sdCT1w==
+  dependencies:
+    "@types/node" "*"
+
 "@types/istanbul-lib-coverage@*", "@types/istanbul-lib-coverage@^2.0.0", "@types/istanbul-lib-coverage@^2.0.1":
   version "2.0.3"
   resolved "https://registry.yarnpkg.com/@types/istanbul-lib-coverage/-/istanbul-lib-coverage-2.0.3.tgz#4ba8ddb720221f432e443bd5f9117fd22cfd4762"
@@ -2835,6 +2860,20 @@ autoprefixer@^9.6.1:
     postcss "^7.0.32"
     postcss-value-parser "^4.1.0"
 
+axios-retry@^3.1.9:
+  version "3.2.0"
+  resolved "https://registry.yarnpkg.com/axios-retry/-/axios-retry-3.2.0.tgz#eb48e72f90b177fde62329b2896aa8476cfb90ba"
+  integrity sha512-RK2cLMgIsAQBDhlIsJR5dOhODPigvel18XUv1dDXW+4k1FzebyfRk+C+orot6WPZOYFKSfhLwHPwVmTVOODQ5w==
+  dependencies:
+    is-retry-allowed "^1.1.0"
+
+axios@^0.21.1:
+  version "0.21.4"
+  resolved "https://registry.yarnpkg.com/axios/-/axios-0.21.4.tgz#c67b90dc0568e5c1cf2b0b858c43ba28e2eda575"
+  integrity sha512-ut5vewkiu8jjGBdqpM44XxjuCjq9LAKeHVmoVfHVzy8eHgxxq8SbAVQNovDA8mVi05kP0Ea/n/UzcSHcTJQfNg==
+  dependencies:
+    follow-redirects "^1.14.0"
+
 babel-code-frame@^6.26.0:
   version "6.26.0"
   resolved "https://registry.yarnpkg.com/babel-code-frame/-/babel-code-frame-6.26.0.tgz#63fd43f7dc1e3bb7ce35947db8fe369a3f58c74b"
@@ -5093,6 +5132,11 @@ etag@^1.8.1, etag@~1.8.1:
   resolved "https://registry.yarnpkg.com/etag/-/etag-1.8.1.tgz#41ae2eeb65efa62268aebfea83ac7d79299b0887"
   integrity sha1-Qa4u62XvpiJorr/qg6x9eSmbCIc=
 
+eventemitter3@^4.0.0:
+  version "4.0.7"
+  resolved "https://registry.yarnpkg.com/eventemitter3/-/eventemitter3-4.0.7.tgz#2de9b68f6528d5644ef5c59526a1b4a07306169f"
+  integrity sha512-8guHBZCwKnFhYdHr2ysuRWErTwhoN2X8XELRlrRwpmfeY2jjuUN4taQMsULKUVo1K4DvZl+0pgfyoysHxvmvEw==
+
 events@^3.0.0:
   version "3.3.0"
   resolved "https://registry.yarnpkg.com/events/-/events-3.3.0.tgz#31a95ad0a924e2d2c419a813aeb2c4e878ea7400"
@@ -5423,6 +5467,11 @@ flush-write-stream@^1.0.0:
     inherits "^2.0.3"
     readable-stream "^2.3.6"
 
+follow-redirects@^1.0.0, follow-redirects@^1.14.0:
+  version "1.14.4"
+  resolved "https://registry.yarnpkg.com/follow-redirects/-/follow-redirects-1.14.4.tgz#838fdf48a8bbdd79e52ee51fb1c94e3ed98b9379"
+  integrity sha512-zwGkiSXC1MUJG/qmeIFH2HBJx9u0V46QGUe3YR1fXG8bXQxq7fLj0RjLZQ5nubr9qNJUZrH+xUcwXEoXNpfS+g==
+
 for-in@^1.0.2:
   version "1.0.2"
   resolved "https://registry.yarnpkg.com/for-in/-/for-in-1.0.2.tgz#81068d295a8142ec0ac726c6e2200c30fb6d5e80"
@@ -6034,6 +6083,26 @@ http-proxy-agent@^4.0.1:
     agent-base "6"
     debug "4"
 
+http-proxy-middleware@^1.0.6:
+  version "1.3.1"
+  resolved "https://registry.yarnpkg.com/http-proxy-middleware/-/http-proxy-middleware-1.3.1.tgz#43700d6d9eecb7419bf086a128d0f7205d9eb665"
+  integrity sha512-13eVVDYS4z79w7f1+NPllJtOQFx/FdUW4btIvVRMaRlUY9VGstAbo5MOhLEuUgZFRHn3x50ufn25zkj/boZnEg==
+  dependencies:
+    "@types/http-proxy" "^1.17.5"
+    http-proxy "^1.18.1"
+    is-glob "^4.0.1"
+    is-plain-obj "^3.0.0"
+    micromatch "^4.0.2"
+
+http-proxy@^1.18.1:
+  version "1.18.1"
+  resolved "https://registry.yarnpkg.com/http-proxy/-/http-proxy-1.18.1.tgz#401541f0534884bbf95260334e72f88ee3976549"
+  integrity sha512-7mz/721AbnJwIVbnaSv1Cz3Am0ZLT/UBwkC92VlxhXv/k/BBQfM2fXElQNC27BVGr0uwUpplYPQM9LnaBMR5NQ==
+  dependencies:
+    eventemitter3 "^4.0.0"
+    follow-redirects "^1.0.0"
+    requires-port "^1.0.0"
+
 http-shutdown@^1.2.2:
   version "1.2.2"
   resolved "https://registry.yarnpkg.com/http-shutdown/-/http-shutdown-1.2.2.tgz#41bc78fc767637c4c95179bc492f312c0ae64c5f"
@@ -6521,6 +6590,11 @@ is-plain-obj@^1.0.0, is-plain-obj@^1.1.0:
   resolved "https://registry.yarnpkg.com/is-plain-obj/-/is-plain-obj-1.1.0.tgz#71a50c8429dfca773c92a390a4a03b39fcd51d3e"
   integrity sha1-caUMhCnfync8kqOQpKA7OfzVHT4=
 
+is-plain-obj@^3.0.0:
+  version "3.0.0"
+  resolved "https://registry.yarnpkg.com/is-plain-obj/-/is-plain-obj-3.0.0.tgz#af6f2ea14ac5a646183a5bbdb5baabbc156ad9d7"
+  integrity sha512-gwsOE28k+23GP1B6vFl1oVh/WOzmawBrKwo5Ev6wMKzPkaXaCDIQKzLnvsA42DRlbVTWorkgTKIviAKCWkfUwA==
+
 is-plain-object@^2.0.3, is-plain-object@^2.0.4:
   version "2.0.4"
   resolved "https://registry.yarnpkg.com/is-plain-object/-/is-plain-object-2.0.4.tgz#2c163b3fafb1b606d9d17928f05c2a1c38e07677"
@@ -6546,6 +6620,11 @@ is-resolvable@^1.0.0:
   resolved "https://registry.yarnpkg.com/is-resolvable/-/is-resolvable-1.1.0.tgz#fb18f87ce1feb925169c9a407c19318a3206ed88"
   integrity sha512-qgDYXFSR5WvEfuS5dMj6oTMEbrrSaM0CrFk2Yiq/gXnBvD9pMa2jGXxyhGLfvhZpuMZe18CJpFxAt3CRs42NMg==
 
+is-retry-allowed@^1.1.0:
+  version "1.2.0"
+  resolved "https://registry.yarnpkg.com/is-retry-allowed/-/is-retry-allowed-1.2.0.tgz#d778488bd0a4666a3be8a1482b9f2baafedea8b4"
+  integrity sha512-RUbUeKwvm3XG2VYamhJL1xFktgjvPzL0Hq8C+6yrWIswDy3BIXGqCxhxkc30N9jqK311gVU137K8Ei55/zVJRg==
+
 is-scoped@^1.0.0:
   version "1.0.0"
   resolved "https://registry.yarnpkg.com/is-scoped/-/is-scoped-1.0.0.tgz#449ca98299e713038256289ecb2b540dc437cb30"
@@ -10011,6 +10090,11 @@ require-from-string@^2.0.2:
   resolved "https://registry.yarnpkg.com/require-from-string/-/require-from-string-2.0.2.tgz#89a7fdd938261267318eafe14f9c32e598c36909"
   integrity sha512-Xf0nWe6RseziFMu+Ap9biiUbmplq6S9/p+7w7YXP/JBHhrUDDUhwa+vANyubuqfZWTveU//DYVGsDG7RKL/vEw==
 
+requires-port@^1.0.0:
+  version "1.0.0"
+  resolved "https://registry.yarnpkg.com/requires-port/-/requires-port-1.0.0.tgz#925d2601d39ac485e091cf0da5c6e694dc3dcaff"
+  integrity sha1-kl0mAdOaxIXgkc8NpcbmlNw9yv8=
+
 requrl@^3.0.2:
   version "3.0.2"
   resolved "https://registry.yarnpkg.com/requrl/-/requrl-3.0.2.tgz#d376104193b02a2d874dde68454c2db2dfeb0fac"