Skip to content

Commit

Permalink
Refactor function to improve performance
Browse files Browse the repository at this point in the history
  • Loading branch information
Mini-Sylar committed Jan 23, 2024
1 parent 43dad96 commit 83c5b23
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 33 deletions.
18 changes: 0 additions & 18 deletions web/frontend/src/helpers/appBridge.js

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,19 +1,11 @@
import { authenticatedFetch } from '@shopify/app-bridge/utilities'
import { createApp } from '@shopify/app-bridge'
import { Redirect } from '@shopify/app-bridge/actions'
import { initAppBridge } from '../plugins/appBridge'

export function useAuthenticatedFetchVue() {
const host = new URLSearchParams(location.search).get('host') || window.__SHOPIFY_DEV_HOST

window.__SHOPIFY_DEV_HOST = host
const appBridge = createApp({
apiKey: process.env.SHOPIFY_API_KEY,
host: host,
forceRedirect: true
})
export function useAuthenticatedFetch() {
const appBridge = initAppBridge()
const app = appBridge
const fetchFunction = authenticatedFetch(app)

return async (uri, options) => {
const response = await fetchFunction(uri, options)
checkHeadersForReauthorization(response.headers, app)
Expand Down
4 changes: 2 additions & 2 deletions web/frontend/src/main.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import './assets/main.css'
import { shopifyAppBridgePlugin } from './helpers/appBridge.js'
import { ShopifyAppBridge } from '@/plugins/appBridge.js'

import { createApp } from 'vue'
import { createPinia } from 'pinia'
Expand All @@ -8,7 +8,7 @@ import App from './App.vue'
import router from './router'

const app = createApp(App)
app.use(shopifyAppBridgePlugin)
app.use(ShopifyAppBridge)
app.use(createPinia())
app.use(router)

Expand Down
23 changes: 23 additions & 0 deletions web/frontend/src/plugins/appBridge.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// @ts-nocheck

import { createApp } from '@shopify/app-bridge'
export const initAppBridge = () => {
const host = new URLSearchParams(location.search).get('host') || window.__SHOPIFY_DEV_HOST
window.__SHOPIFY_DEV_HOST = host
return createApp({
apiKey: process.env.SHOPIFY_API_KEY || '',
host: host,
forceRedirect: true
})
}

export const ShopifyAppBridge = {
/**
* @param {import('vue').App} app
*/
install: (app) => {
const useAppBridge = initAppBridge()
app.config.globalProperties.$useAppBridge = useAppBridge
app.provide('useAppBridge', useAppBridge)
}
}
4 changes: 2 additions & 2 deletions web/frontend/src/stores/counter.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { ref } from 'vue'
import { defineStore } from 'pinia'
import { useAuthenticatedFetchVue } from '../helpers/vueAuthenticatedFetch.js'
import { useAuthenticatedFetch } from '../helpers/useAuthenticatedFetch'

export const useProductCounterStore = defineStore('productCounter', () => {
const count = ref(0)
const fetch = useAuthenticatedFetchVue()
const fetch = useAuthenticatedFetch()

const getProducts = async () => {
try {
Expand Down

0 comments on commit 83c5b23

Please sign in to comment.