Skip to content

Commit ba31c9b

Browse files
authored
feat: Add Collection.remove (#3560)
1 parent fcb7d7d commit ba31c9b

File tree

6 files changed

+561
-0
lines changed

6 files changed

+561
-0
lines changed

.changeset/pink-waves-judge.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
---
2+
'@data-client/endpoint': patch
3+
'@data-client/graphql': patch
4+
'@data-client/rest': patch
5+
---
6+
7+
Add Collection.remove
8+
9+
```ts
10+
ctrl.set(MyResource.getList.schema.remove, { id });
11+
```
12+
13+
```ts
14+
const removeItem = MyResource.delete.extend({
15+
schema: MyResource.getList.schema.remove
16+
})
17+
```

docs/rest/api/Collection.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -430,6 +430,20 @@ A creation schema that places at the _end_ of this collection
430430

431431
A creation schema that places at the _start_ of this collection
432432

433+
### remove
434+
435+
Remove item[s] from a collection by value.
436+
437+
```ts
438+
ctrl.set(MyResource.getList.schema.remove, { id });
439+
```
440+
441+
```ts
442+
const removeItem = MyResource.delete.extend({
443+
schema: MyResource.getList.schema.remove
444+
})
445+
```
446+
433447
### assign
434448

435449
A creation schema that [assigns](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/assign)

packages/endpoint/src/schemaTypes.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,11 @@ export interface CollectionInterface<
153153
*/
154154
unshift: CollectionArrayAdder<S>;
155155

156+
/** Schema to remove (by value) from a Collection
157+
* @see https://dataclient.io/rest/api/Collection#remove
158+
*/
159+
remove: CollectionArrayAdder<S>;
160+
156161
/** Schema to merge with a Values Collection
157162
* @see https://dataclient.io/rest/api/Collection#assign
158163
*/

packages/endpoint/src/schemas/Collection.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@ const unshiftMerge = (existing: any, incoming: any) => {
1919
const valuesMerge = (existing: any, incoming: any) => {
2020
return { ...existing, ...incoming };
2121
};
22+
const removeMerge = (existing: Array<any>, incoming: any) => {
23+
return existing.filter((item: any) => !incoming.includes(item));
24+
};
25+
2226
const createArray = (value: any) => [...value];
2327
const createValue = (value: any) => ({ ...value });
2428

@@ -49,6 +53,9 @@ export default class CollectionSchema<
4953
declare assign: S extends ValuesType<any> ? CollectionSchema<S, Args, Parent>
5054
: undefined;
5155

56+
declare remove: S extends ArrayType<any> ? CollectionSchema<S, Args, Parent>
57+
: undefined;
58+
5259
addWith<P extends any[] = Args>(
5360
merge: (existing: any, incoming: any) => any,
5461
createCollectionFilter?: (
@@ -118,6 +125,7 @@ export default class CollectionSchema<
118125
this.createIfValid = createArray;
119126
this.push = CreateAdder(this, pushMerge);
120127
this.unshift = CreateAdder(this, unshiftMerge);
128+
this.remove = CreateAdder(this, removeMerge);
121129
} else if (schema instanceof Values) {
122130
this.createIfValid = createValue;
123131
this.assign = CreateAdder(this, valuesMerge);

0 commit comments

Comments
 (0)