Skip to content

Commit cfcca6c

Browse files
committed
Core configuration as extension
1 parent 63bbb25 commit cfcca6c

File tree

6 files changed

+73
-45
lines changed

6 files changed

+73
-45
lines changed

package.json

+6-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@westacks/vortex",
3-
"version": "0.0.4",
3+
"version": "0.0.5",
44
"license": "MIT",
55
"description": "Server-based routing for SPAs",
66
"author": "Dmytro Morozov <puny.flash@gmail.com>",
@@ -52,6 +52,11 @@
5252
"import": "./dist/server.js",
5353
"require": "./dist/server.cjs",
5454
"types": "./dist/server.d.ts"
55+
},
56+
"./extensions": {
57+
"import": "./dist/extensions.js",
58+
"require": "./dist/extensions.cjs",
59+
"types": "./dist/extensions/index.d.ts"
5560
}
5661
}
5762
}

src/extensions/core.ts

+62
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
import { getPage as page, setPage } from "../page";
2+
import type { VortexExtension, VortexConfig } from "../index";
3+
4+
type CoreConfig = {
5+
detect?: string
6+
version?: string
7+
component?: string
8+
location?: string
9+
only?: string
10+
}
11+
12+
const core = ({
13+
detect = 'x-vortex',
14+
version = 'x-vortex-version',
15+
component = 'x-vortex-component',
16+
location = 'x-vortex-location',
17+
only = 'x-vortex-only',
18+
}: CoreConfig): VortexExtension => ({ request, response }) => ({
19+
request: request.use(function (request) {
20+
if (!request.vortex) {
21+
return request
22+
}
23+
24+
const config = (typeof request.vortex === "object" ? request.vortex : {}) as VortexConfig
25+
26+
request.headers[detect] = true
27+
request.headers[version] = page()?.version
28+
request.headers[component] = page()?.component
29+
30+
if (config.only) {
31+
request.headers[only] = config.only.join(",")
32+
}
33+
34+
return request
35+
}),
36+
response: response.use(function (response) {
37+
if (!response.config.vortex || !response.headers[detect]) {
38+
return response
39+
}
40+
41+
const config = typeof response.config.vortex === "object" ? response.config.vortex : {}
42+
43+
setPage(response.data)
44+
45+
if (config?.preserve || (window.history.state.url === response.data.url && config?.preserve !== false)) {
46+
window.history.replaceState(response.data, "", response.data.url)
47+
} else {
48+
window.history.pushState(response.data, "", response.data.url)
49+
}
50+
51+
return response
52+
}, function (error) {
53+
if (error.response?.headers[location]) {
54+
window.location.href = error.response.headers[location];
55+
return Promise.resolve(error.response)
56+
}
57+
58+
return Promise.reject(error)
59+
})
60+
})
61+
62+
export default core

src/extensions/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export { default as core } from "./core"

src/index.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
export * from "./client";
22
export * from "./page";
3-
export { axios, extend, type VortexExtension } from "./router";
3+
export { axios, extend, type VortexExtension, type VortexConfig } from "./router";

src/router.ts

+2-43
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,9 @@ interface RouterDefaults<D = any> extends Omit<RouterRequestConfig<D>, 'headers'
3636
headers: HeadersDefaults;
3737
}
3838

39-
type VortexConfig = {
39+
export type VortexConfig = {
4040
preserve?: boolean
41+
only?: string[]
4142
}
4243

4344
interface RouterRequestConfig<D = any> extends AxiosRequestConfig<D> {
@@ -99,48 +100,6 @@ export function createRouter() {
99100
window.addEventListener("popstate", popstate)
100101

101102
axios.defaults.vortex = true
102-
103-
extend(({ request, response }) => ({
104-
request: request.use(function (request) {
105-
if (!request.vortex) {
106-
return request
107-
}
108-
109-
request.headers["x-vortex"] = true
110-
request.headers["x-vortex-version"] = page()?.version
111-
request.headers["x-vortex-component"] = page()?.component
112-
113-
return request
114-
}),
115-
response: response.use(function (response) {
116-
if (!response.config.vortex || !response.headers["x-vortex"]) {
117-
return response
118-
}
119-
120-
const config = typeof response.config.vortex === "object" ? response.config.vortex : {}
121-
122-
setPage(response.data)
123-
124-
if (config?.preserve || (window.history.state.url === response.data.url && config?.preserve !== false)) {
125-
window.history.replaceState(response.data, "", response.data.url)
126-
} else {
127-
window.history.pushState(response.data, "", response.data.url)
128-
}
129-
130-
return response
131-
}, function (error) {
132-
if (error.response?.headers["x-vortex-location"]) {
133-
window.location.href = error.response.headers["x-vortex-location"];
134-
return Promise.resolve(error.response)
135-
}
136-
137-
if (error.response?.headers["x-vortex"]) {
138-
return Promise.resolve(error.response)
139-
}
140-
141-
return Promise.reject(error)
142-
})
143-
}))
144103
}
145104

146105
export function destroyRouter() {

vite.config.ts

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ export default defineConfig({
1111
entry: {
1212
"index": resolve(__dirname, 'src/index.ts'),
1313
"server": resolve(__dirname, 'src/server.ts'),
14+
"extensions": resolve(__dirname, 'src/extensions/index.ts'),
1415
},
1516
},
1617
rollupOptions: {

0 commit comments

Comments
 (0)