1
- import { Model } from '../../../model' ;
2
- import { RelationQueryBuilder } from '../builders/relationQueryBuilder' ;
3
- import { HttpMethod } from '../enums/httpMethod' ;
4
- import { AttachResult } from '../results/attachResult' ;
5
- import { ExtractModelAttributesType } from '../../../types/extractModelAttributesType' ;
6
- import { DetachResult } from '../results/detachResult' ;
7
- import { SyncResult } from '../results/syncResult' ;
8
- import { ToggleResult } from '../results/toggleResult' ;
9
- import { UpdatePivotResult } from '../results/updatePivotResult' ;
10
- import { ExtractModelPersistedAttributesType } from '../../../types/extractModelPersistedAttributesType' ;
11
- import { ExtractModelRelationsType } from '../../../types/extractModelRelationsType' ;
1
+ import { Model } from '../../../model' ;
2
+ import { RelationQueryBuilder } from '../builders/relationQueryBuilder' ;
3
+ import { HttpMethod } from '../enums/httpMethod' ;
4
+ import { AttachResult } from '../results/attachResult' ;
5
+ import { ExtractModelAttributesType } from '../../../types/extractModelAttributesType' ;
6
+ import { DetachResult } from '../results/detachResult' ;
7
+ import { SyncResult } from '../results/syncResult' ;
8
+ import { ToggleResult } from '../results/toggleResult' ;
9
+ import { UpdatePivotResult } from '../results/updatePivotResult' ;
10
+ import {
11
+ ExtractModelPersistedAttributesType
12
+ } from '../../../types/extractModelPersistedAttributesType' ;
13
+ import { ExtractModelRelationsType } from '../../../types/extractModelRelationsType' ;
12
14
13
15
export class BelongsToMany <
14
16
Relation extends Model ,
@@ -21,10 +23,10 @@ export class BelongsToMany<
21
23
keys : Array < number | string > ,
22
24
duplicates : boolean = false
23
25
) : Promise < AttachResult > {
24
- const response = await this . httpClient . request (
26
+ const response = await this . httpClient . request < { attached : Array < number | string > } > (
25
27
`/attach` ,
26
28
HttpMethod . POST ,
27
- { duplicates : duplicates ? 1 : 0 } ,
29
+ { duplicates : duplicates ? 1 : 0 } ,
28
30
{
29
31
resources : keys ,
30
32
}
@@ -37,37 +39,39 @@ export class BelongsToMany<
37
39
resources : Record < string , Pivot > ,
38
40
duplicates : boolean = false
39
41
) : Promise < AttachResult > {
40
- const response = await this . httpClient . request (
42
+ const response = await this . httpClient . request < { attached : Array < number | string > } > (
41
43
`/attach` ,
42
44
HttpMethod . POST ,
43
- { duplicates : duplicates ? 1 : 0 } ,
44
- { resources }
45
+ { duplicates : duplicates ? 1 : 0 } ,
46
+ { resources}
45
47
) ;
46
48
47
49
return new AttachResult ( response . data . attached ) ;
48
50
}
49
51
50
52
public async detach ( keys : Array < number | string > ) : Promise < DetachResult > {
51
- const response = await this . httpClient . request ( `/detach` , HttpMethod . DELETE , null , {
53
+ const response = await this . httpClient . request < { detached : Array < number | string > } > ( `/detach` , HttpMethod . DELETE , null , {
52
54
resources : keys ,
53
55
} ) ;
54
56
55
57
return new DetachResult ( response . data . detached ) ;
56
58
}
57
59
58
60
public async detachWithFields ( resources : Record < string , Pivot > ) : Promise < DetachResult > {
59
- const response = await this . httpClient . request ( `/detach` , HttpMethod . DELETE , null , {
61
+ const response = await this . httpClient . request < { detached : Array < number | string > } > ( `/detach` , HttpMethod . DELETE , null , {
60
62
resources,
61
63
} ) ;
62
64
63
65
return new DetachResult ( response . data . detached ) ;
64
66
}
65
67
66
68
public async sync ( keys : Array < number | string > , detaching : boolean = true ) : Promise < SyncResult > {
67
- const response = await this . httpClient . request (
69
+ const response = await this . httpClient . request <
70
+ { attached : Array < number | string > , updated : Array < number | string > , detached : Array < number | string > }
71
+ > (
68
72
`/sync` ,
69
73
HttpMethod . PATCH ,
70
- { detaching : detaching ? 1 : 0 } ,
74
+ { detaching : detaching ? 1 : 0 } ,
71
75
{
72
76
resources : keys ,
73
77
}
@@ -80,34 +84,40 @@ export class BelongsToMany<
80
84
resources : Record < string , Pivot > ,
81
85
detaching : boolean = true
82
86
) : Promise < SyncResult > {
83
- const response = await this . httpClient . request (
87
+ const response = await this . httpClient . request <
88
+ { attached : Array < number | string > , updated : Array < number | string > , detached : Array < number | string > }
89
+ > (
84
90
`/sync` ,
85
91
HttpMethod . PATCH ,
86
- { detaching : detaching ? 1 : 0 } ,
87
- { resources }
92
+ { detaching : detaching ? 1 : 0 } ,
93
+ { resources}
88
94
) ;
89
95
90
96
return new SyncResult ( response . data . attached , response . data . updated , response . data . detached ) ;
91
97
}
92
98
93
99
public async toggle ( keys : Array < number | string > ) : Promise < ToggleResult > {
94
- const response = await this . httpClient . request ( `/toggle` , HttpMethod . PATCH , null , {
100
+ const response = await this . httpClient . request <
101
+ { attached : Array < number | string > , detached : Array < number | string > }
102
+ > ( `/toggle` , HttpMethod . PATCH , null , {
95
103
resources : keys ,
96
104
} ) ;
97
105
98
106
return new ToggleResult ( response . data . attached , response . data . detached ) ;
99
107
}
100
108
101
109
public async toggleWithFields ( resources : Record < string , Pivot > ) : Promise < ToggleResult > {
102
- const response = await this . httpClient . request ( `/toggle` , HttpMethod . PATCH , null , {
110
+ const response = await this . httpClient . request <
111
+ { attached : Array < number | string > , detached : Array < number | string > }
112
+ > ( `/toggle` , HttpMethod . PATCH , null , {
103
113
resources,
104
114
} ) ;
105
115
106
116
return new ToggleResult ( response . data . attached , response . data . detached ) ;
107
117
}
108
118
109
119
public async updatePivot ( key : number | string , pivot : Pivot ) : Promise < UpdatePivotResult > {
110
- const response = await this . httpClient . request ( `/${ key } /pivot` , HttpMethod . PATCH , null , {
120
+ const response = await this . httpClient . request < { updated : Array < string | number > } > ( `/${ key } /pivot` , HttpMethod . PATCH , null , {
111
121
pivot,
112
122
} ) ;
113
123
0 commit comments