-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathfindCategoryByPath.ts
48 lines (47 loc) · 1.85 KB
/
findCategoryByPath.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import { removeStoreCodeFromRoute } from '@vue-storefront/core/lib/multistore'
import storeCodeFromRoute from '@vue-storefront/core/lib/storeCodeFromRoute'
import store from '@vue-storefront/core/store'
import { isServer } from '@vue-storefront/core/helpers'
import Vue from 'vue'
import fetch from 'isomorphic-fetch'
// Use with https://github.com/kodbruket/vsf-mapping-fallback
/*
* Config example:
"storePicker": {
"categoryByPath": "http://localhost:3000/api/ext/vsf-store-picker/categoryByPath?path={{path}}"
},
*/
export const findCategoryByPath = async (context, { url }) => {
const {config} = store.state
let storeCode = storeCodeFromRoute(url)
if (isServer) {
const { request: req } = Vue.prototype.$ssrRequestContext.server
if (req.header('x-vs-store-code')) {
storeCode = req.header('x-vs-store-code')
}
}
const category = (removeStoreCodeFromRoute(url) as string).replace(/\/?(\?.*)?$/, "") // remove trailing slash and/or qs variables if present
if (category && config.storeViews.multistore && config.storePicker && config.storePicker.categoryByPath) {
const categoryUrl = config.storePicker.categoryByPath.replace('{{path}}', category)
const response = await fetch(categoryUrl, {
headers: { 'Content-Type': 'application/json' },
mode: 'cors'
})
const {result} = await response.json()
const categorySlug = result.url_paths && result.url_paths[storeCode]
if (categorySlug) {
if (isServer) {
const { request: req, response: res } = Vue.prototype.$ssrRequestContext.server
const path = req.headers['x-vs-store-code'] ? `/${categorySlug}` : `/${storeCode}/${categorySlug}`
return res.set('location', path).status(301).send()
} else {
return {
name: 'category',
params: {
slug: categorySlug
}
}
}
}
}
}