@@ -47,6 +47,17 @@ export class AtomicOperationsService<T extends unknown[]>
47
47
this . jsonApiSdkConfig . operationUrl
48
48
) ;
49
49
50
+ const indexDeleteIfNotSkipEmpty = atomicBody
51
+ . reduce < number [ ] > ( ( acum , item , index ) => {
52
+ if (
53
+ item . op === 'remove' &&
54
+ ! this . generateAtomicBody [ index ] . isSkipEmpty
55
+ ) {
56
+ acum . push ( index ) ;
57
+ }
58
+ return acum ;
59
+ } , [ ] )
60
+ . sort ( ( a , b ) => a - b ) ;
50
61
return this . httpInnerClient . post < T > ( operationUrl , body ) . pipe (
51
62
map ( ( r ) => r [ KEY_MAIN_OUTPUT_SCHEMA ] ) ,
52
63
map (
@@ -56,14 +67,39 @@ export class AtomicOperationsService<T extends unknown[]>
56
67
? this . jsonApiUtilsService . getResultForRelation ( item )
57
68
: this . jsonApiUtilsService . convertResponseData ( item )
58
69
) as T
70
+ ) ,
71
+ map ( ( r ) =>
72
+ indexDeleteIfNotSkipEmpty . reduce (
73
+ ( acc , index , currentIndex ) => {
74
+ acc . splice ( index + currentIndex , 0 , 'EMPTY' ) ;
75
+ return acc ;
76
+ } ,
77
+ [ ...r ] as T
78
+ )
59
79
)
60
80
) ;
61
81
}
62
82
63
- public deleteOne < Entity extends EntityObject > (
83
+ deleteOne < Entity extends EntityObject > (
64
84
entity : Entity
65
- ) : AtomicOperations < T > {
66
- return this . setToBody ( 'deleteOne' , entity ) ;
85
+ ) : AtomicOperations < [ ...T ] > ;
86
+ deleteOne < Entity extends EntityObject > (
87
+ entity : Entity ,
88
+ skipEmpty : true
89
+ ) : AtomicOperations < [ ...T ] > ;
90
+ deleteOne < Entity extends EntityObject > (
91
+ entity : Entity ,
92
+ skipEmpty : false
93
+ ) : AtomicOperations < [ ...T , 'EMPTY' ] > ;
94
+ deleteOne < Entity extends EntityObject > (
95
+ entity : Entity ,
96
+ skipEmpty ?: boolean
97
+ ) : AtomicOperations < [ ...T , 'EMPTY' ] | [ ...T ] > {
98
+ return this . setToBody (
99
+ 'deleteOne' ,
100
+ entity ,
101
+ skipEmpty === undefined ? true : skipEmpty
102
+ ) ;
67
103
}
68
104
69
105
public patchOne < Entity extends EntityObject > (
@@ -109,6 +145,11 @@ export class AtomicOperationsService<T extends unknown[]>
109
145
operationType : Extract < keyof AtomicVoidOperation , 'deleteOne' > ,
110
146
entity : Entity
111
147
) : AtomicOperations < T > ;
148
+ private setToBody < Entity extends EntityObject > (
149
+ operationType : Extract < keyof AtomicVoidOperation , 'deleteOne' > ,
150
+ entity : Entity ,
151
+ skipEmpty : boolean
152
+ ) : AtomicOperations < [ ...T , 'EMPTY' ] > ;
112
153
private setToBody < Entity extends EntityObject > (
113
154
operationType : Exclude < keyof AtomicVoidOperation , 'deleteOne' > ,
114
155
entity : Entity
@@ -135,30 +176,40 @@ export class AtomicOperationsService<T extends unknown[]>
135
176
> (
136
177
operationType : keyof AtomicVoidOperation ,
137
178
entity : Entity ,
138
- relationType ?: Rel
179
+ relationType ?: Rel | boolean
139
180
) :
140
181
| AtomicOperations < [ ...T , Entity ] >
141
182
| AtomicOperations < [ ...T , ReturnIfArray < Entity [ Rel ] , string > ] >
142
- | AtomicOperations < [ ...T ] > {
183
+ | AtomicOperations < [ ...T ] >
184
+ | AtomicOperations < [ ...T , 'EMPTY' ] > {
143
185
const atomicBody = new GenerateAtomicBody < Entity , Rel > (
144
186
this . jsonApiUtilsService ,
145
187
this . jsonApiSdkConfig
146
188
) ;
147
- switch ( operationType ) {
148
- case 'postRelationships' :
149
- case 'patchRelationships' :
150
- case 'deleteRelationships' :
151
- if ( relationType ) atomicBody [ operationType ] ( entity , relationType ) ;
152
- break ;
153
- default :
154
- atomicBody [ operationType ] ( entity ) ;
155
- break ;
189
+
190
+ if ( typeof relationType === 'boolean' ) {
191
+ if ( operationType === 'deleteOne' ) {
192
+ atomicBody [ operationType ] ( entity , relationType ) ;
193
+ }
194
+ } else {
195
+ switch ( operationType ) {
196
+ case 'postRelationships' :
197
+ case 'patchRelationships' :
198
+ case 'deleteRelationships' :
199
+ if ( relationType ) atomicBody [ operationType ] ( entity , relationType ) ;
200
+ break ;
201
+ default :
202
+ atomicBody [ operationType ] ( entity , true ) ;
203
+ break ;
204
+ }
156
205
}
206
+
157
207
this . addBody ( atomicBody ) ;
158
208
159
209
return this as unknown as
160
210
| AtomicOperations < [ ...T , Entity ] >
161
211
| AtomicOperations < [ ...T , ReturnIfArray < Entity [ Rel ] , string > ] >
162
- | AtomicOperations < T > ;
212
+ | AtomicOperations < T >
213
+ | AtomicOperations < [ ...T , 'EMPTY' ] > ;
163
214
}
164
215
}
0 commit comments