forked from gremo/react-directus
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtypes.ts
More file actions
136 lines (126 loc) · 4.25 KB
/
types.ts
File metadata and controls
136 lines (126 loc) · 4.25 KB
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
import * as React from 'react';
import { DirectusOptions, IDirectus, TypeMap, UserType } from '@directus/sdk';
import { DirectusAsset } from '@components/DirectusAsset';
import { DirectusImage } from '@components/DirectusImage';
/**
* Shape of a generic asset.
*/
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export type DirectusAsset = string | ({ id: string } & Record<string, any>);
/**
* Shape of the `DirectusAsset` component `render` prop.
*/
export type DirectusAssetRenderer = Omit<DirectusAssetProps, 'render'> & {
url?: string;
};
/**
* Shape of a generic asset component props.
*/
export interface DirectusAssetProps {
/** Directus CMS API url. */
apiUrl?: string;
/** The asset as `string` or `object` with an `id` property of type `string`. */
asset: DirectusAsset;
/** Add `Content-Disposition` header and force browser to download file. */
download?: boolean;
/** A function that returns the React element to be rendered. It will receive an object with the `url` key and all the passed props. */
render: (args: DirectusAssetRenderer) => JSX.Element;
}
/**
* Shape of the `DirectusImage` component `render` prop.
*/
export type DirectusImageRenderer = Omit<DirectusImageProps, 'render'> & {
url?: string;
};
/**
* Shape of `DirectusImage` component props.
*/
export interface DirectusImageProps extends Omit<DirectusAssetProps, 'download' | 'render'> {
/** The width of the thumbnail in pixels. */
width?: number;
/** The height of the thumbnail in pixels. */
height?: number;
/** The quality of the thumbnail (1 to 100). */
quality?: number;
/** The fit of the thumbnail while always preserving the aspect ratio. */
fit?: 'cover' | 'contain' | 'inside' | 'outside';
/** A function that returns the React element to be rendered. It will receive an object with the `url` key and all the passed props. */
render: (args: DirectusImageRenderer) => JSX.Element;
}
/**
* Shape of the context provider props.
*/
export interface DirectusProviderProps {
/** Directus CMS API url. */
apiUrl: string;
/** A set of options to pass to the Directus client. */
options?: DirectusOptions;
/**
* If `true`, the provider will try to login the user automatically on mount.
* @default false
*/
autoLogin?: boolean;
children: React.ReactNode;
}
export type AuthStates = 'loading' | 'authenticated' | 'unauthenticated';
/**
* Shape of the main context.
*/
export interface DirectusContextType<T extends TypeMap> {
apiUrl: string;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
directus: IDirectus<T>;
/** The context-aware `DirectusAsset` component, with pre-filled props. */
DirectusAsset: typeof DirectusAsset;
/** The context-aware `DirectusImage` component, with pre-filled props. */
DirectusImage: typeof DirectusImage;
/**
* Please use the data provided by the `useDirectusAuth` hook instead.
* @default 'loading'
* @internal
*/
_authState: AuthStates;
/**
* Please use the functions provided by the `useDirectusAuth` hook instead.
* @internal
*/
_setAuthState: React.Dispatch<React.SetStateAction<AuthStates>>;
/**
* Please use the data provided by the `useDirectusAuth` hook instead.
* @default null
* @internal
*/
_directusUser: UserType | null;
/**
* Please use the functions provided by the `useDirectusAuth` hook instead.
* @internal
*/
_setDirecctusUser: React.Dispatch<React.SetStateAction<UserType | null>>;
}
export type DirectusContextTypeGeneric<T extends TypeMap> = DirectusContextType<T> | null;
export interface DirectusAuthHook {
/**
* Login the user. If successful, the user will be stored in the context.
* Else, an error will be thrown.
* @param email - The user email.
* @param password - The user password.
* @throws {Error} - If the login fails.
*/
login: (email: string, password: string) => Promise<void>;
/**
* Logout the user. If successful, the user will be removed from the context.
* Else, an error will be thrown.
* @throws {Error} - If the logout fails.
*/
logout: () => Promise<void>;
/**
* Represents the current authentication state.
* @default 'loading'
*/
authState: AuthStates;
/**
* The current authenticated user.
* @default null
*/
user: UserType | null;
}