-
-
Notifications
You must be signed in to change notification settings - Fork 3.6k
/
presets.ts
60 lines (55 loc) · 1.7 KB
/
presets.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
import type { DirectusPreset } from '../../../schema/preset.js';
import type { ApplyQueryFields, Query } from '../../../types/index.js';
import { throwIfEmpty } from '../../utils/index.js';
import type { RestCommand } from '../../types.js';
export type UpdatePresetOutput<
Schema extends object,
TQuery extends Query<Schema, Item>,
Item extends object = DirectusPreset<Schema>,
> = ApplyQueryFields<Schema, Item, TQuery['fields']>;
/**
* Update multiple existing presets.
* @param keys
* @param item
* @param query
* @returns Returns the preset objects for the updated presets.
* @throws Will throw if keys is empty
*/
export const updatePresets =
<Schema extends object, const TQuery extends Query<Schema, DirectusPreset<Schema>>>(
keys: DirectusPreset<Schema>['id'][],
item: Partial<DirectusPreset<Schema>>,
query?: TQuery,
): RestCommand<UpdatePresetOutput<Schema, TQuery>[], Schema> =>
() => {
throwIfEmpty(keys, 'Keys cannot be empty');
return {
path: `/presets`,
params: query ?? {},
body: JSON.stringify({ keys, data: item }),
method: 'PATCH',
};
};
/**
* Update an existing preset.
* @param key
* @param item
* @param query
* @returns Returns the preset object for the updated preset.
* @throws Will throw if key is empty
*/
export const updatePreset =
<Schema extends object, const TQuery extends Query<Schema, DirectusPreset<Schema>>>(
key: DirectusPreset<Schema>['id'],
item: Partial<DirectusPreset<Schema>>,
query?: TQuery,
): RestCommand<UpdatePresetOutput<Schema, TQuery>, Schema> =>
() => {
throwIfEmpty(String(key), 'Key cannot be empty');
return {
path: `/presets/${key}`,
params: query ?? {},
body: JSON.stringify(item),
method: 'PATCH',
};
};