1
- import { mergeDataIntoQueryString , router , shouldIntercept , type VisitOptions } from '@inertiajs/core'
2
- import type { Action } from 'svelte/action'
1
+ import {
2
+ mergeDataIntoQueryString ,
3
+ router ,
4
+ shouldIntercept ,
5
+ type FormDataConvertible ,
6
+ type GlobalEventsMap ,
7
+ type VisitOptions ,
8
+ } from '@inertiajs/core'
9
+ import type { CancelTokenSource } from 'axios'
10
+ import type { ActionReturn } from 'svelte/action'
3
11
4
12
interface ActionElement extends HTMLElement {
5
13
href ?: string
6
14
}
7
15
8
- type ActionParameters = VisitOptions & { href ?: string }
16
+ type ActionParameters = Omit < VisitOptions , 'data' > & {
17
+ href ?: string
18
+ data ?: Record < string , FormDataConvertible >
19
+ }
20
+
21
+ type SelectedEventKeys = 'start' | 'progress' | 'finish' | 'before' | 'cancel' | 'success' | 'error'
22
+ type SelectedGlobalEventsMap = Pick < GlobalEventsMap , SelectedEventKeys >
23
+ type ActionAttributes = {
24
+ [ K in keyof SelectedGlobalEventsMap as `on:${K } `] ?: CustomEvent < SelectedGlobalEventsMap [ K ] [ 'details' ] >
25
+ } & {
26
+ 'on:cancel-token' ?: CustomEvent < CancelTokenSource >
27
+ }
9
28
10
- const link : Action < ActionElement , ActionParameters > = ( node , options = { } ) = > {
29
+ function link ( node : ActionElement , options : ActionParameters = { } ) : ActionReturn < ActionParameters , ActionAttributes > {
11
30
const [ href , data ] = hrefAndData ( options )
12
31
node . href = href
13
32
options . data = data
@@ -34,8 +53,8 @@ const link: Action<ActionElement, ActionParameters> = (node, options = {}) => {
34
53
event . preventDefault ( )
35
54
36
55
router . visit ( node . href , {
37
- onCancelToken : ( ) => fireEvent ( 'cancel-token' ) ,
38
- onBefore : ( visit ) => fireEvent ( 'before' , { detail : { visit } } ) ,
56
+ onCancelToken : ( token ) => fireEvent ( 'cancel-token' , { detail : { token } } ) ,
57
+ onBefore : ( visit ) => fireEvent ( 'before' , { cancelable : true , detail : { visit } } ) ,
39
58
onStart : ( visit ) => fireEvent ( 'start' , { detail : { visit } } ) ,
40
59
onProgress : ( progress ) => fireEvent ( 'progress' , { detail : { progress } } ) ,
41
60
onFinish : ( visit ) => fireEvent ( 'finish' , { detail : { visit } } ) ,
0 commit comments