@inertiajs/vue3 1.0.0-beta.2 → 1.0.0-beta.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,29 @@
1
+ import { Page } from '@inertiajs/core';
2
+ import { App as VueApp, DefineComponent, Plugin } from 'vue';
3
+ import { InertiaApp, InertiaAppProps } from './app';
4
+ interface CreateInertiaAppProps {
5
+ id?: string;
6
+ resolve: (name: string) => DefineComponent | Promise<DefineComponent> | {
7
+ default: DefineComponent;
8
+ };
9
+ setup: (props: {
10
+ el: Element;
11
+ App: InertiaApp;
12
+ props: InertiaAppProps;
13
+ plugin: Plugin;
14
+ }) => void | VueApp;
15
+ title?: (title: string) => string;
16
+ progress?: false | {
17
+ delay?: number;
18
+ color?: string;
19
+ includeCSS?: boolean;
20
+ showSpinner?: boolean;
21
+ };
22
+ page?: Page;
23
+ render?: (app: VueApp) => Promise<string>;
24
+ }
25
+ export default function createInertiaApp({ id, resolve, setup, title, progress, page, render, }: CreateInertiaAppProps): Promise<{
26
+ head: string[];
27
+ body: string;
28
+ }>;
29
+ export {};
@@ -0,0 +1,6 @@
1
+ import { DefineComponent } from 'vue';
2
+ export type InertiaHead = DefineComponent<{
3
+ title?: string;
4
+ }>;
5
+ declare const Head: InertiaHead;
6
+ export default Head;
@@ -0,0 +1,7 @@
1
+ export { router } from '@inertiajs/core';
2
+ export { usePage } from './app';
3
+ export { default as createInertiaApp } from './createInertiaApp';
4
+ export { default as Head } from './head';
5
+ export { default as Link } from './link';
6
+ export { default as useForm } from './useForm';
7
+ export { default as useRemember } from './useRemember';
@@ -0,0 +1,25 @@
1
+ import { Method, PageProps, Progress } from '@inertiajs/core';
2
+ import { DefineComponent } from 'vue';
3
+ interface InertiaLinkProps {
4
+ as?: string;
5
+ data?: object;
6
+ href: string;
7
+ method?: Method;
8
+ headers?: object;
9
+ onClick?: (event: MouseEvent | KeyboardEvent) => void;
10
+ preserveScroll?: boolean | ((props: PageProps) => boolean);
11
+ preserveState?: boolean | ((props: PageProps) => boolean) | null;
12
+ replace?: boolean;
13
+ only?: string[];
14
+ onCancelToken?: (cancelToken: import('axios').CancelTokenSource) => void;
15
+ onBefore?: () => void;
16
+ onStart?: () => void;
17
+ onProgress?: (progress: Progress) => void;
18
+ onFinish?: () => void;
19
+ onCancel?: () => void;
20
+ onSuccess?: () => void;
21
+ queryStringArrayFormat?: 'brackets' | 'indices';
22
+ }
23
+ type InertiaLink = DefineComponent<InertiaLinkProps>;
24
+ declare const Link: InertiaLink;
25
+ export default Link;
@@ -0,0 +1,3 @@
1
+ import { ComponentOptions } from 'vue';
2
+ declare const remember: ComponentOptions;
3
+ export default remember;
@@ -0,0 +1,24 @@
1
+ import { createHeadManager, Page, PageHandler, router } from '@inertiajs/core';
2
+ import { ComponentPublicInstance } from 'vue';
3
+ import useForm from './useForm';
4
+ export type VuePageHandlerArgs = Parameters<PageHandler>[0] & {
5
+ component: ComponentPublicInstance | Promise<ComponentPublicInstance>;
6
+ };
7
+ declare module '@inertiajs/core' {
8
+ interface Router {
9
+ form: typeof useForm;
10
+ }
11
+ }
12
+ declare module '@vue/runtime-core' {
13
+ interface ComponentCustomProperties {
14
+ $inertia: typeof router;
15
+ $page: Page;
16
+ $headManager: ReturnType<typeof createHeadManager>;
17
+ }
18
+ interface ComponentCustomOptions {
19
+ remember?: string | string[] | {
20
+ data: string | string[];
21
+ key?: string | (() => string);
22
+ };
23
+ }
24
+ }
@@ -0,0 +1,30 @@
1
+ import { Progress, VisitOptions } from '@inertiajs/core';
2
+ interface InertiaFormProps<TForm extends Record<string, unknown>> {
3
+ isDirty: boolean;
4
+ errors: Partial<Record<keyof TForm, string>>;
5
+ hasErrors: boolean;
6
+ processing: boolean;
7
+ progress: Progress | null;
8
+ wasSuccessful: boolean;
9
+ recentlySuccessful: boolean;
10
+ data(): TForm;
11
+ transform(callback: (data: TForm) => object): this;
12
+ defaults(): this;
13
+ defaults(field: keyof TForm, value: string): this;
14
+ defaults(fields: Record<keyof TForm, string>): this;
15
+ reset(...fields: (keyof TForm)[]): this;
16
+ clearErrors(...fields: (keyof TForm)[]): this;
17
+ setError(field: keyof TForm, value: string): this;
18
+ setError(errors: Record<keyof TForm, string>): this;
19
+ submit(method: string, url: string, options?: Partial<VisitOptions>): void;
20
+ get(url: string, options?: Partial<VisitOptions>): void;
21
+ post(url: string, options?: Partial<VisitOptions>): void;
22
+ put(url: string, options?: Partial<VisitOptions>): void;
23
+ patch(url: string, options?: Partial<VisitOptions>): void;
24
+ delete(url: string, options?: Partial<VisitOptions>): void;
25
+ cancel(): void;
26
+ }
27
+ type InertiaForm<TForm extends Record<string, unknown>> = TForm & InertiaFormProps<TForm>;
28
+ export default function useForm<TForm extends Record<string, unknown>>(data: TForm): InertiaForm<TForm>;
29
+ export default function useForm<TForm extends Record<string, unknown>>(rememberKey: string, data: TForm): InertiaForm<TForm>;
30
+ export {};
@@ -0,0 +1,6 @@
1
+ import { Ref } from 'vue';
2
+ export default function useRemember<T extends object>(data: T & {
3
+ __rememberable?: boolean;
4
+ __remember?: Function;
5
+ __restore?: Function;
6
+ }, key?: string): Ref<T> | T;
package/index.d.ts DELETED
@@ -1,117 +0,0 @@
1
- import * as Inertia from '@inertiajs/core'
2
- import { Ref, ComputedRef, App as VueApp, DefineComponent, Plugin } from 'vue'
3
-
4
- export interface InertiaAppProps {
5
- initialPage: Inertia.Page
6
- initialComponent?: object
7
- resolveComponent?: (name: string) => DefineComponent | Promise<DefineComponent>
8
- onHeadUpdate?: (elements: string[]) => void
9
- }
10
-
11
- type InertiaApp = DefineComponent<InertiaAppProps>
12
-
13
- export declare const App: InertiaApp
14
-
15
- export declare const plugin: Plugin
16
-
17
- export interface CreateInertiaAppProps {
18
- id?: string
19
- resolve: (name: string) => DefineComponent | Promise<DefineComponent> | { default: DefineComponent }
20
- setup: (props: { el: Element; app: InertiaApp; props: InertiaAppProps; plugin: Plugin }) => void | VueApp
21
- title?: (title: string) => string
22
- page?: Inertia.Page
23
- render?: (app: VueApp) => Promise<string>
24
- }
25
-
26
- export declare function createInertiaApp(props: CreateInertiaAppProps): Promise<{ head: string[]; body: string } | void>
27
-
28
- export interface InertiaLinkProps {
29
- as?: string
30
- data?: object
31
- href: string
32
- method?: string
33
- headers?: object
34
- onClick?: (event: MouseEvent | KeyboardEvent) => void
35
- preserveScroll?: boolean | ((props: Inertia.PageProps) => boolean)
36
- preserveState?: boolean | ((props: Inertia.PageProps) => boolean) | null
37
- replace?: boolean
38
- only?: string[]
39
- onCancelToken?: (cancelToken: import('axios').CancelTokenSource) => void
40
- onBefore?: () => void
41
- onStart?: () => void
42
- onProgress?: (progress: Inertia.Progress) => void
43
- onFinish?: () => void
44
- onCancel?: () => void
45
- onSuccess?: () => void
46
- }
47
-
48
- export type InertiaLink = DefineComponent<InertiaLinkProps>
49
-
50
- export declare const Link: InertiaLink
51
-
52
- export interface InertiaFormProps<TForm> {
53
- isDirty: boolean
54
- errors: Record<keyof TForm, string>
55
- hasErrors: boolean
56
- processing: boolean
57
- progress: Inertia.Progress | null
58
- wasSuccessful: boolean
59
- recentlySuccessful: boolean
60
- data(): TForm
61
- transform(callback: (data: TForm) => object): this
62
- defaults(): this
63
- defaults(field: keyof TForm, value: string): this
64
- defaults(fields: Record<keyof TForm, string>): this
65
- reset(...fields: (keyof TForm)[]): this
66
- clearErrors(...fields: (keyof TForm)[]): this
67
- setError(field: keyof TForm, value: string): this
68
- setError(errors: Record<keyof TForm, string>): this
69
- submit(method: string, url: string, options?: Partial<Inertia.VisitOptions>): void
70
- get(url: string, options?: Partial<Inertia.VisitOptions>): void
71
- post(url: string, options?: Partial<Inertia.VisitOptions>): void
72
- put(url: string, options?: Partial<Inertia.VisitOptions>): void
73
- patch(url: string, options?: Partial<Inertia.VisitOptions>): void
74
- delete(url: string, options?: Partial<Inertia.VisitOptions>): void
75
- cancel(): void
76
- }
77
-
78
- export type InertiaForm<TForm> = TForm & InertiaFormProps<TForm>
79
-
80
- export declare function useForm<TForm>(data: TForm): InertiaForm<TForm>
81
-
82
- export declare function useForm<TForm>(rememberKey: string, data: TForm): InertiaForm<TForm>
83
-
84
- export declare function useRemember(data: object, key?: string): Ref<object>
85
-
86
- export declare function usePage<PageProps>(): {
87
- props: ComputedRef<PageProps & Inertia.PageProps>
88
- url: ComputedRef<string>
89
- component: ComputedRef<string>
90
- version: ComputedRef<string | null>
91
- }
92
-
93
- export declare function defineLayout(component: object): void
94
-
95
- export type InertiaHead = DefineComponent<{
96
- title?: string
97
- }>
98
-
99
- export declare const Head: InertiaHead
100
-
101
- declare module '@vue/runtime-core' {
102
- export interface ComponentCustomProperties {
103
- $inertia: typeof Inertia.Inertia
104
- $page: Inertia.Page
105
- $headManager: ReturnType<typeof Inertia.createHeadManager>
106
- }
107
-
108
- export interface ComponentCustomOptions {
109
- remember?:
110
- | string
111
- | string[]
112
- | {
113
- data: string | string[]
114
- key?: string | (() => string)
115
- }
116
- }
117
- }
package/src/app.js DELETED
@@ -1,117 +0,0 @@
1
- import { createHeadManager, router } from '@inertiajs/core'
2
- import { computed, h, markRaw, ref, shallowRef } from 'vue'
3
- import remember from './remember'
4
- import useForm from './useForm'
5
-
6
- const component = ref(null)
7
- const page = ref({})
8
- const layout = shallowRef(null)
9
- const key = ref(null)
10
- let headManager = null
11
-
12
- export default {
13
- name: 'Inertia',
14
- props: {
15
- initialPage: {
16
- type: Object,
17
- required: true,
18
- },
19
- initialComponent: {
20
- type: Object,
21
- required: false,
22
- },
23
- resolveComponent: {
24
- type: Function,
25
- required: false,
26
- },
27
- titleCallback: {
28
- type: Function,
29
- required: false,
30
- default: (title) => title,
31
- },
32
- onHeadUpdate: {
33
- type: Function,
34
- required: false,
35
- default: () => () => {},
36
- },
37
- },
38
- setup({ initialPage, initialComponent, resolveComponent, titleCallback, onHeadUpdate }) {
39
- component.value = initialComponent ? markRaw(initialComponent) : null
40
- page.value = initialPage
41
- key.value = null
42
-
43
- const isServer = typeof window === 'undefined'
44
- headManager = createHeadManager(isServer, titleCallback, onHeadUpdate)
45
-
46
- if (!isServer) {
47
- router.init({
48
- initialPage,
49
- resolveComponent,
50
- swapComponent: async (args) => {
51
- component.value = markRaw(args.component)
52
- page.value = args.page
53
- key.value = args.preserveState ? key.value : Date.now()
54
- },
55
- })
56
-
57
- router.on('navigate', () => headManager.forceUpdate())
58
- }
59
-
60
- return () => {
61
- if (component.value) {
62
- component.value.inheritAttrs = !!component.value.inheritAttrs
63
-
64
- const child = h(component.value, {
65
- ...page.value.props,
66
- key: key.value,
67
- })
68
-
69
- if (layout.value) {
70
- component.value.layout = layout.value
71
- layout.value = null
72
- }
73
-
74
- if (component.value.layout) {
75
- if (typeof component.value.layout === 'function') {
76
- return component.value.layout(h, child)
77
- }
78
-
79
- return (Array.isArray(component.value.layout) ? component.value.layout : [component.value.layout])
80
- .concat(child)
81
- .reverse()
82
- .reduce((child, layout) => {
83
- layout.inheritAttrs = !!layout.inheritAttrs
84
- return h(layout, { ...page.value.props }, () => child)
85
- })
86
- }
87
-
88
- return child
89
- }
90
- }
91
- },
92
- }
93
-
94
- export const plugin = {
95
- install(app) {
96
- router.form = useForm
97
-
98
- Object.defineProperty(app.config.globalProperties, '$inertia', { get: () => router })
99
- Object.defineProperty(app.config.globalProperties, '$page', { get: () => page.value })
100
- Object.defineProperty(app.config.globalProperties, '$headManager', { get: () => headManager })
101
-
102
- app.mixin(remember)
103
- },
104
- }
105
-
106
- export function usePage() {
107
- return {
108
- props: computed(() => page.value.props),
109
- url: computed(() => page.value.url),
110
- component: computed(() => page.value.component),
111
- version: computed(() => page.value.version),
112
- }
113
- }
114
-
115
- export function defineLayout(component) {
116
- layout.value = component
117
- }
@@ -1,46 +0,0 @@
1
- import { setupProgress } from '@inertiajs/core'
2
- import { createSSRApp, h } from 'vue'
3
- import App, { plugin } from './app'
4
-
5
- export default async function createInertiaApp({ id = 'app', resolve, setup, title, progress = {}, page, render }) {
6
- const isServer = typeof window === 'undefined'
7
- const el = isServer ? null : document.getElementById(id)
8
- const initialPage = page || JSON.parse(el.dataset.page)
9
- const resolveComponent = (name) => Promise.resolve(resolve(name)).then((module) => module.default || module)
10
-
11
- let head = []
12
-
13
- const vueApp = await resolveComponent(initialPage.component).then((initialComponent) => {
14
- return setup({
15
- el,
16
- App,
17
- props: {
18
- initialPage,
19
- initialComponent,
20
- resolveComponent,
21
- titleCallback: title,
22
- onHeadUpdate: isServer ? (elements) => (head = elements) : null,
23
- },
24
- plugin,
25
- })
26
- })
27
-
28
- if (!isServer && progress) {
29
- setupProgress(progress)
30
- }
31
-
32
- if (isServer) {
33
- const body = await render(
34
- createSSRApp({
35
- render: () =>
36
- h('div', {
37
- id,
38
- 'data-page': JSON.stringify(initialPage),
39
- innerHTML: render(vueApp),
40
- }),
41
- }),
42
- )
43
-
44
- return { head, body }
45
- }
46
- }
package/src/head.js DELETED
@@ -1,93 +0,0 @@
1
- export default {
2
- props: {
3
- title: {
4
- type: String,
5
- required: false,
6
- },
7
- },
8
- data() {
9
- return {
10
- provider: this.$headManager.createProvider(),
11
- }
12
- },
13
- beforeUnmount() {
14
- this.provider.disconnect()
15
- },
16
- methods: {
17
- isUnaryTag(node) {
18
- return (
19
- [
20
- 'area',
21
- 'base',
22
- 'br',
23
- 'col',
24
- 'embed',
25
- 'hr',
26
- 'img',
27
- 'input',
28
- 'keygen',
29
- 'link',
30
- 'meta',
31
- 'param',
32
- 'source',
33
- 'track',
34
- 'wbr',
35
- ].indexOf(node.type) > -1
36
- )
37
- },
38
- renderTagStart(node) {
39
- node.props = node.props || {}
40
- node.props.inertia = node.props['head-key'] !== undefined ? node.props['head-key'] : ''
41
- const attrs = Object.keys(node.props).reduce((carry, name) => {
42
- const value = node.props[name]
43
- if (['key', 'head-key'].includes(name)) {
44
- return carry
45
- } else if (value === '') {
46
- return carry + ` ${name}`
47
- } else {
48
- return carry + ` ${name}="${value}"`
49
- }
50
- }, '')
51
- return `<${node.type}${attrs}>`
52
- },
53
- renderTagChildren(node) {
54
- return typeof node.children === 'string'
55
- ? node.children
56
- : node.children.reduce((html, child) => html + this.renderTag(child), '')
57
- },
58
- renderTag(node) {
59
- if (node.type.toString() === 'Symbol(Text)') {
60
- return node.children
61
- } else if (node.type.toString() === 'Symbol()') {
62
- return ''
63
- } else if (node.type.toString() === 'Symbol(Comment)') {
64
- return ''
65
- }
66
- let html = this.renderTagStart(node)
67
- if (node.children) {
68
- html += this.renderTagChildren(node)
69
- }
70
- if (!this.isUnaryTag(node)) {
71
- html += `</${node.type}>`
72
- }
73
- return html
74
- },
75
- addTitleElement(elements) {
76
- if (this.title && !elements.find((tag) => tag.startsWith('<title'))) {
77
- elements.push(`<title inertia>${this.title}</title>`)
78
- }
79
- return elements
80
- },
81
- renderNodes(nodes) {
82
- return this.addTitleElement(
83
- nodes
84
- .flatMap((node) => (node.type.toString() === 'Symbol(Fragment)' ? node.children : node))
85
- .map((node) => this.renderTag(node))
86
- .filter((node) => node),
87
- )
88
- },
89
- },
90
- render() {
91
- this.provider.update(this.renderNodes(this.$slots.default ? this.$slots.default() : []))
92
- },
93
- }
package/src/index.js DELETED
@@ -1,7 +0,0 @@
1
- export { router } from '@inertiajs/core'
2
- export { defineLayout, usePage } from './app'
3
- export { default as createInertiaApp } from './createInertiaApp'
4
- export { default as Head } from './head'
5
- export { default as Link } from './link'
6
- export { default as useForm } from './useForm'
7
- export { default as useRemember } from './useRemember'
package/src/link.js DELETED
@@ -1,92 +0,0 @@
1
- import { mergeDataIntoQueryString, router, shouldIntercept } from '@inertiajs/core'
2
- import { h } from 'vue'
3
-
4
- export default {
5
- name: 'Link',
6
- props: {
7
- as: {
8
- type: String,
9
- default: 'a',
10
- },
11
- data: {
12
- type: Object,
13
- default: () => ({}),
14
- },
15
- href: {
16
- type: String,
17
- },
18
- method: {
19
- type: String,
20
- default: 'get',
21
- },
22
- replace: {
23
- type: Boolean,
24
- default: false,
25
- },
26
- preserveScroll: {
27
- type: Boolean,
28
- default: false,
29
- },
30
- preserveState: {
31
- type: Boolean,
32
- default: null,
33
- },
34
- only: {
35
- type: Array,
36
- default: () => [],
37
- },
38
- headers: {
39
- type: Object,
40
- default: () => ({}),
41
- },
42
- queryStringArrayFormat: {
43
- type: String,
44
- default: 'brackets',
45
- },
46
- },
47
- setup(props, { slots, attrs }) {
48
- return (props) => {
49
- const as = props.as.toLowerCase()
50
- const method = props.method.toLowerCase()
51
- const [href, data] = mergeDataIntoQueryString(method, props.href || '', props.data, props.queryStringArrayFormat)
52
-
53
- if (as === 'a' && method !== 'get') {
54
- console.warn(
55
- `Creating POST/PUT/PATCH/DELETE <a> links is discouraged as it causes "Open Link in New Tab/Window" accessibility issues.\n\nPlease specify a more appropriate element using the "as" attribute. For example:\n\n<Link href="${href}" method="${method}" as="button">...</Link>`,
56
- )
57
- }
58
-
59
- return h(
60
- props.as,
61
- {
62
- ...attrs,
63
- ...(as === 'a' ? { href } : {}),
64
- onClick: (event) => {
65
- if (shouldIntercept(event)) {
66
- event.preventDefault()
67
-
68
- router.visit(href, {
69
- data: data,
70
- method: method,
71
- replace: props.replace,
72
- preserveScroll: props.preserveScroll,
73
- preserveState: props.preserveState ?? method !== 'get',
74
- only: props.only,
75
- headers: props.headers,
76
- onCancelToken: attrs.onCancelToken || (() => ({})),
77
- onBefore: attrs.onBefore || (() => ({})),
78
- onStart: attrs.onStart || (() => ({})),
79
- onProgress: attrs.onProgress || (() => ({})),
80
- onFinish: attrs.onFinish || (() => ({})),
81
- onCancel: attrs.onCancel || (() => ({})),
82
- onSuccess: attrs.onSuccess || (() => ({})),
83
- onError: attrs.onError || (() => ({})),
84
- })
85
- }
86
- },
87
- },
88
- slots,
89
- )
90
- }
91
- },
92
- }
package/src/remember.js DELETED
@@ -1,65 +0,0 @@
1
- import { router } from '@inertiajs/core'
2
- import cloneDeep from 'lodash.clonedeep'
3
-
4
- export default {
5
- created() {
6
- if (!this.$options.remember) {
7
- return
8
- }
9
-
10
- if (Array.isArray(this.$options.remember)) {
11
- this.$options.remember = { data: this.$options.remember }
12
- }
13
-
14
- if (typeof this.$options.remember === 'string') {
15
- this.$options.remember = { data: [this.$options.remember] }
16
- }
17
-
18
- if (typeof this.$options.remember.data === 'string') {
19
- this.$options.remember = { data: [this.$options.remember.data] }
20
- }
21
-
22
- const rememberKey =
23
- this.$options.remember.key instanceof Function
24
- ? this.$options.remember.key.call(this)
25
- : this.$options.remember.key
26
-
27
- const restored = router.restore(rememberKey)
28
-
29
- const rememberable = this.$options.remember.data.filter((key) => {
30
- return !(this[key] !== null && typeof this[key] === 'object' && this[key].__rememberable === false)
31
- })
32
-
33
- const hasCallbacks = (key) => {
34
- return (
35
- this[key] !== null &&
36
- typeof this[key] === 'object' &&
37
- typeof this[key].__remember === 'function' &&
38
- typeof this[key].__restore === 'function'
39
- )
40
- }
41
-
42
- rememberable.forEach((key) => {
43
- if (this[key] !== undefined && restored !== undefined && restored[key] !== undefined) {
44
- hasCallbacks(key) ? this[key].__restore(restored[key]) : (this[key] = restored[key])
45
- }
46
-
47
- this.$watch(
48
- key,
49
- () => {
50
- router.remember(
51
- rememberable.reduce(
52
- (data, key) => ({
53
- ...data,
54
- [key]: cloneDeep(hasCallbacks(key) ? this[key].__remember() : this[key]),
55
- }),
56
- {},
57
- ),
58
- rememberKey,
59
- )
60
- },
61
- { immediate: true, deep: true },
62
- )
63
- })
64
- },
65
- }
package/src/server.js DELETED
@@ -1 +0,0 @@
1
- export { default as default } from '@inertiajs/core/server'