-
-
Notifications
You must be signed in to change notification settings - Fork 3.6k
/
items.ts
90 lines (83 loc) · 2.97 KB
/
items.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
import type { ApplyQueryFields, CollectionType, Query, UnpackList } from '../../../types/index.js';
import type { RestCommand } from '../../types.js';
import { throwIfCoreCollection, throwIfEmpty } from '../../utils/index.js';
export type UpdateItemOutput<
Schema extends object,
Collection extends keyof Schema,
TQuery extends Query<Schema, Schema[Collection]>,
> = ApplyQueryFields<Schema, CollectionType<Schema, Collection>, TQuery['fields']>;
/**
* Update multiple items at the same time.
*
* @param collection The collection of the items
* @param keysOrQuery The primary keys or a query
* @param item The item data to update
* @param query Optional return data query
*
* @returns Returns the item objects for the updated items.
* @throws Will throw if keysOrQuery is empty
* @throws Will throw if collection is empty
* @throws Will throw if collection is a core collection
*/
export const updateItems =
<Schema extends object, Collection extends keyof Schema, const TQuery extends Query<Schema, Schema[Collection]>>(
collection: Collection,
keysOrQuery: string[] | number[] | Query<Schema, Schema[Collection]>,
item: Partial<UnpackList<Schema[Collection]>>,
query?: TQuery,
): RestCommand<UpdateItemOutput<Schema, Collection, TQuery>[], Schema> =>
() => {
let payload: Record<string, any> = {};
throwIfEmpty(String(collection), 'Collection cannot be empty');
throwIfCoreCollection(collection, 'Cannot use updateItems for core collections');
if (Array.isArray(keysOrQuery)) {
throwIfEmpty(keysOrQuery, 'keysOrQuery cannot be empty');
payload = { keys: keysOrQuery };
} else {
throwIfEmpty(Object.keys(keysOrQuery), 'keysOrQuery cannot be empty');
payload = { query: keysOrQuery };
}
payload['data'] = item;
return {
path: `/items/${collection as string}`,
params: query ?? {},
body: JSON.stringify(payload),
method: 'PATCH',
};
};
/**
* Update an existing item.
*
* @param collection The collection of the item
* @param key The primary key of the item
* @param item The item data to update
* @param query Optional return data query
*
* @returns Returns the item object of the item that was updated.
* @throws Will throw if key is empty
* @throws Will throw if collection is empty
* @throws Will throw if collection is a core collection
*/
export const updateItem =
<
Schema extends object,
Collection extends keyof Schema,
const TQuery extends Query<Schema, Schema[Collection]>,
Item = UnpackList<Schema[Collection]>,
>(
collection: Collection,
key: string | number,
item: Partial<Item>,
query?: TQuery,
): RestCommand<UpdateItemOutput<Schema, Collection, TQuery>, Schema> =>
() => {
throwIfEmpty(String(key), 'Key cannot be empty');
throwIfEmpty(String(collection), 'Collection cannot be empty');
throwIfCoreCollection(collection, 'Cannot use updateItem for core collections');
return {
path: `/items/${collection as string}/${key}`,
params: query ?? {},
body: JSON.stringify(item),
method: 'PATCH',
};
};