|
| 1 | +# Installation |
| 2 | + |
| 3 | +Vortex is designed to be modular and framework-agnostic. Out of the box, it comes with official adapters for four popular frontend frameworks: |
| 4 | + |
| 5 | +- **React** (`@westacks/vortex/react`) |
| 6 | +- **Vue** (`@westacks/vortex/vue`) |
| 7 | +- **Svelte** (`@westacks/vortex/svelte`) |
| 8 | +- **SolidJS** (`@westacks/vortex/solid-js`) |
| 9 | + |
| 10 | +## Resolving and rendering page components |
| 11 | + |
| 12 | +Depending on your app structure, you will need to create a logic to resolve and render your page components. The simplest setup - just a root component that will receive page component and its props, however you can customize it to your needs: |
| 13 | + |
| 14 | +```tsx |
| 15 | +import type { Page } from '@westacks/vortex' |
| 16 | + |
| 17 | +// Vite |
| 18 | +async function resolve(page: Page) { |
| 19 | + const pages = import.meta.glob('./pages/**/*.tsx') |
| 20 | + const component = await pages[`./pages/${page.component}.tsx`] |
| 21 | + return { component, props: page.props ?? {} } |
| 22 | +} |
| 23 | + |
| 24 | +// Webpack |
| 25 | +async function resolve(page: Page) { |
| 26 | + const component = require(`./pages/${page.component}.tsx`), |
| 27 | + return { component, props: page.props ?? {} } |
| 28 | +} |
| 29 | + |
| 30 | +export default function Root({ component, props }) { |
| 31 | + return <component.default {...props} /> |
| 32 | +} |
| 33 | +``` |
| 34 | + |
| 35 | +## Extensions and Plugins |
| 36 | + |
| 37 | +Vortex also have an extension API to enhance your application routing with different features. Out of the box, Vortex comes with pluging for [Inertia.js](https://inertiajs.com/) and [BProgress](https://bprogress.vercel.app/). You can install them when creating your app: |
| 38 | + |
| 39 | +```ts |
| 40 | +import { createVortex } from '@westacks/vortex'; |
| 41 | +import inertia from '@westacks/vortex/inertia'; |
| 42 | +import bprogress from '@westacks/vortex/bprogress'; |
| 43 | + |
| 44 | +createVortex((target, page, install, ssr) => { |
| 45 | + install(inertia(page.get()), bprogress()); |
| 46 | + // ...your app setup |
| 47 | +}); |
| 48 | +``` |
| 49 | + |
| 50 | +## Creating Your Own Extension |
| 51 | + |
| 52 | +You can easily plug your own extension into Vortex with followind template: |
| 53 | + |
| 54 | +```ts |
| 55 | +import type { VortexExtension } from '@westacks/vortex'; |
| 56 | + |
| 57 | +export default const extension: VortexExtension = ({ request, response }) => { |
| 58 | + // initial configuration |
| 59 | + |
| 60 | + // @see https://axios-http.com/docs/interceptors |
| 61 | + const req = request.use( |
| 62 | + (config) => config, // Modify request config, e.g. add headers etc |
| 63 | + (error) => Promise.reject(error), // Handle error |
| 64 | + ); |
| 65 | + |
| 66 | + const res = response.use( |
| 67 | + (response) => response, // Handle response data (after receiving response) |
| 68 | + (error) => Promise.reject(error), // Handle error |
| 69 | + ); |
| 70 | + |
| 71 | + return () => { |
| 72 | + // Cleanup |
| 73 | + request.eject(req) |
| 74 | + response.eject(res) |
| 75 | + } |
| 76 | +} |
| 77 | +``` |
| 78 | + |
| 79 | +```ts |
| 80 | +import { createVortex } from '@westacks/vortex'; |
| 81 | +import extension from './extension'; |
| 82 | + |
| 83 | +createVortex((target, page, install, ssr) => { |
| 84 | + install(extension); |
| 85 | + // ...your app setup |
| 86 | +}); |
| 87 | +``` |
0 commit comments