fastapi-rtk
Version:
A React component library for FastAPI in combination with FastAPI React Toolkit backend, built with Mantine, JsonForms, and Zustand.
446 lines (445 loc) • 11.3 kB
TypeScript
export function useApi(options: SafeContextOptions): ApiContext;
export type APIColumnInfo = {
/**
* - The description of the column.
*/
description: string;
/**
* - The label of the column.
*/
label: string;
/**
* - The name of the column.
*/
name: string;
/**
* - Whether the column is required.
*/
required: boolean;
/**
* - The type of the column.
*/
type: string;
/**
* - Whether the column is unique.
*/
unique: boolean;
};
export type APIColumnRelationInfo = {
/**
* - The description of the column.
*/
description: string;
/**
* - The label of the column.
*/
label: string;
/**
* - The name of the column.
*/
name: string;
/**
* - Whether the column is required.
*/
required: boolean;
/**
* - The type of the column.
*/
type: string;
/**
* - Whether the column is unique.
*/
unique: boolean;
/**
* - The count of items in the column.
*/
count: number;
/**
* - The values of the column.
*/
values: {
id: number | string;
value: string;
}[];
};
export type APIColumnFilter = {
/**
* - The name of the column to filter.
*/
name: string;
/**
* - The operator to use for filtering (e.g., 'eq', 'ne', 'gt', 'lt').
*/
operator: string;
};
export type APIFilter = {
/**
* - An array of filters to apply.
*/
filters: APIColumnFilter[];
/**
* - The label for the filter.
*/
label: string;
/**
* - The schema of the filter, containing column information.
*/
schema: APIColumnInfo | APIColumnRelationInfo;
};
export type APIJSONFormsSchema = {
/**
* - The type of the schema, typically 'object'.
*/
type: string;
/**
* - The properties of the schema.
*/
properties: Record<string, any>;
/**
* - An array of required properties.
*/
required?: string[];
};
export type APIJSONFormsUISchemaElement = {
/**
* - The type of the UI schema element (e.g., 'Control', 'Group', 'List', etc.).
*/
type: string;
/**
* - The JSON pointer to the schema property this element is associated with.
*/
scope: string;
/**
* - The label for the UI schema element.
*/
label?: string;
/**
* - The description for the UI schema element.
*/
description?: string;
/**
* - Additional options for the UI schema element.
*/
options?: Record<string, any>;
/**
* - A rule for conditional rendering of the UI schema element.
*/
rule?: Record<string, any>;
};
export type APIJSONFormsUISchema = {
/**
* - The type of the UI schema, typically 'VerticalLayout' or 'HorizontalLayout'.
*/
type: string;
/**
* - An array of UI schema elements.
*/
elements: Array<APIJSONFormsUISchemaElement | APIJSONFormsUISchema>;
/**
* - The label for the UI schema.
*/
label?: string;
/**
* - The description for the UI schema.
*/
description?: string;
/**
* - Additional options for the UI schema.
*/
options?: Record<string, any>;
/**
* - A rule for conditional rendering of the UI schema.
*/
rule?: Record<string, any>;
};
export type APIInfo = {
/**
* - The title for the add modal or section.
*/
add_title: string;
/**
* - List of columns that can be added.
*/
add_columns: Array<APIColumnInfo | APIColumnRelationInfo>;
/**
* - JSONForms schema for adding items.
*/
add_schema: APIJSONFormsSchema;
/**
* - JSONForms UI schema for adding items.
*/
add_uischema?: APIJSONFormsUISchema;
/**
* - Translations for the add form.
*/
add_translations?: Record<string, Record<string, string>>;
/**
* - The title for the edit modal or section.
*/
edit_title: string;
/**
* - List of columns that can be edited.
*/
edit_columns: Array<APIColumnInfo | APIColumnRelationInfo>;
/**
* - JSONForms schema for editing items.
*/
edit_schema: APIJSONFormsSchema;
/**
* - JSONForms UI schema for editing items.
*/
edit_uischema?: APIJSONFormsUISchema;
/**
* - Translations for the edit form.
*/
edit_translations?: Record<string, Record<string, string>>;
/**
* - A mapping of filter names to filter definitions.
*/
filters: Record<string, APIFilter>;
/**
* - Additional options for filters specified in the API.
*/
filter_options?: Record<string, any>;
/**
* - List of permissions for the API (e.g., 'can_post', 'can_put', 'can_delete').
*/
permissions: string[];
};
export type APIData = {
/**
* - The title of the list.
*/
list_title: string;
/**
* - An array of IDs for the items.
*/
ids: number[] | string[];
/**
* - A mapping of column names to their labels.
*/
label_columns: Record<string, string>;
/**
* - A mapping of column names to their descriptions.
*/
description_columns: Record<string, string>;
/**
* - An array of column names to be displayed in the list.
*/
list_columns: string[];
/**
* - An array of column names to be used for ordering.
*/
order_columns: string[];
/**
* - An array of objects representing the API response data.
*/
result: Record<string, any>[];
/**
* - The API path.
*/
path: string;
/**
* - The query parameters used in the API request.
*/
queryParams: Record<string, any>;
};
export type APIQueryParamsFilter = {
/**
* - The column name to filter on.
*/
col: string;
/**
* - The operator to use for filtering (e.g., 'eq', 'ne', 'gt', 'lt').
*/
opr: string;
/**
* - The value to filter by.
*/
value: any;
};
export type APISpecialKey = {
/**
* - The key to be used when retrieving general props for `NextGenDataGrid`. Typically `all`. but can be `_all` if `all` is in the list of columns.
*/
all: string;
/**
* - The key to be used when retrieving actions for `NextGenDataGrid`. Typically `actions`, but can be `_actions` if `actions` is in the list of columns.
*/
actions: string;
};
export type APIQueryParams = {
/**
* - An array of column names to be included in the response.
*/
columns: string[];
/**
* - An array of filters to apply to the query.
*/
filters: APIQueryParamsFilter[];
/**
* - The column name to order the results by.
*/
order_column?: string;
/**
* - The direction of the order ('asc' or 'desc').
*/
order_direction?: "asc" | "desc";
/**
* - The page number for pagination.
*/
page: number;
/**
* - The number of items per page for pagination.
*/
page_size: number;
/**
* - A global filter string to apply to the query.
*/
global_filter?: string;
};
export type APIStreaming = {
/**
* - The streaming data, organized by the page number.
*/
data: Record<string, Record<string, any>[]>;
/**
* - Indicates whether the streaming has ended.
*/
isEnd: boolean;
/**
* - A function to fetch the next page of streaming data.
*/
fetch: () => void;
};
export type APIDownloadOptions = {
/**
* - Whether to export the data in basic or detailed mode.
*/
export_mode?: "basic" | "detailed";
/**
* - The delimiter to use in the exported file (e.g., ',', ';'). Default is `,`.
*/
delimiter?: string;
/**
* - The character to use for quoting in the exported file. Default is `"`.
*/
quotechar?: string;
};
export type APIUploadOptions = {
/**
* - The delimiter used in the uploaded file (e.g., ',', ';'). Default is `,`.
*/
delimiter?: string;
/**
* - The character used for quoting in the uploaded file. Default is `"`.
*/
quotechar?: string;
};
export type ApiContext = {
/**
* - The API path.
*/
path: string;
/**
* - Information about the API, including schemas and UI schemas.
*/
info?: APIInfo;
/**
* - The data returned by the API.
*/
data?: APIData;
/**
* - The query parameters used in the API request.
*/
queryParams?: APIQueryParams;
/**
* - Indicates whether the API request is currently loading.
*/
loading: boolean;
/**
* - Indicates whether the API info request is currently loading.
*/
infoLoading: boolean;
/**
* - Indicates whether the API data request is currently loading.
*/
dataLoading: boolean;
/**
* - An error object if the API request failed.
*/
error?: {
message: string;
originalError: Error;
};
/**
* - An error object if the API info request failed.
*/
infoError?: Error;
/**
* - An error object if the API data request failed.
*/
dataError?: Error;
/**
* - An object containing streaming data and a function to fetch more data. Only available when the `ApiProvider` is configured for streaming.
*/
streaming: APIStreaming;
/**
* - An object containing special keys for the API query, such as 'all' and 'actions'.
*/
specialKey: APISpecialKey;
/**
* - A function to update the query parameters.
*/
setQueryParams: (partialQueryParams: Partial<APIQueryParams> | ((prev: APIQueryParams) => Partial<APIQueryParams>)) => void;
/**
* - A function to refetch the data from the API.
*/
refetch: (options?: {
force?: boolean;
}) => Promise<void>;
/**
* - A function to refetch the API information.
*/
refetchInfo: (options?: {
force?: boolean;
}) => Promise<void>;
/**
* - A function to retrieve a specific entry by ID.
*/
getEntry: (id: number | string) => Promise<Record<string, any>>;
/**
* - A function to add a new entry to the API.
*/
addEntry: (item: Record<string, any>) => Promise<Record<string, any>>;
/**
* - A function to edit an existing entry in the API.
*/
updateEntry: (id: number | string, item: Record<string, any>) => Promise<Record<string, any>>;
/**
* - A function to delete an entry from the API.
*/
deleteEntry: (id: number | string) => Promise<Record<string, any>>;
/**
* - A function to download data from the API.
*/
download: (label: string, options?: APIDownloadOptions) => Promise<void>;
/**
* - A function to upload a file to the API.
*/
upload: (file: File, options?: APIUploadOptions) => Promise<Record<string, any>>;
/**
* - A function to reset the error state.
*/
resetError: () => void;
};
export type SafeContextOptions = {
/**
* - Whether to throw an error if the context is not provided.
*/
throwOnError?: boolean;
/**
* - The default value to return if the context is not provided.
*/
defaultValue?: any;
};