-
-
Notifications
You must be signed in to change notification settings - Fork 3.6k
/
users.ts
68 lines (63 loc) · 1.86 KB
/
users.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
import type { DirectusUser } from '../../../schema/user.js';
import type { ApplyQueryFields, Query } from '../../../types/index.js';
import { throwIfEmpty } from '../../utils/index.js';
import type { RestCommand } from '../../types.js';
export type ReadUserOutput<
Schema extends object,
TQuery extends Query<Schema, Item>,
Item extends object = DirectusUser<Schema>,
> = ApplyQueryFields<Schema, Item, TQuery['fields']>;
/**
* List all users that exist in Directus.
*
* @param query The query parameters
*
* @returns An array of up to limit user objects. If no items are available, data will be an empty array.
*/
export const readUsers =
<Schema extends object, const TQuery extends Query<Schema, DirectusUser<Schema>>>(
query?: TQuery,
): RestCommand<ReadUserOutput<Schema, TQuery>[], Schema> =>
() => ({
path: `/users`,
params: query ?? {},
method: 'GET',
});
/**
* List an existing user by primary key.
*
* @param key The primary key of the user
* @param query The query parameters
*
* @returns Returns the requested user object.
* @throws Will throw if key is empty
*/
export const readUser =
<Schema extends object, const TQuery extends Query<Schema, DirectusUser<Schema>>>(
key: DirectusUser<Schema>['id'],
query?: TQuery,
): RestCommand<ReadUserOutput<Schema, TQuery>, Schema> =>
() => {
throwIfEmpty(String(key), 'Key cannot be empty');
return {
path: `/users/${key}`,
params: query ?? {},
method: 'GET',
};
};
/**
* Retrieve the currently authenticated user.
*
* @param query The query parameters
*
* @returns Returns the user object for the currently authenticated user.
*/
export const readMe =
<Schema extends object, const TQuery extends Query<Schema, DirectusUser<Schema>>>(
query?: TQuery,
): RestCommand<ReadUserOutput<Schema, TQuery>, Schema> =>
() => ({
path: `/users/me`,
params: query ?? {},
method: 'GET',
});