import { BaseResponseMany, BaseResponseSingle, ColumnFilterOperator, ColumnInfo, ColumnRelationInfo, ConvertedInfo, FilterInfo, InfoResponse, JsonSchema, Layout, PrimaryKey, QueryFilter, QueryParams } from 'fastapi-rtk/api-types'; import { InfiniteData, UseInfiniteQueryResult, UseQueryResult } from '@tanstack/react-query'; import { Schema as ValidationSchema } from '../../../.external/lib/yup'; /** A non-null CRUD result row, mirroring the unwrapped `BaseResponseSingle.result`. Default row type for the API generics. */ export type EntryRow = NonNullable; export type APIColumnInfo = ColumnInfo; export type APIColumnRelationInfo = ColumnRelationInfo; export type APIColumnFilter = ColumnFilterOperator; export type APIFilter = FilterInfo; /** The backend `/_info` add/edit schema is a JsonForms JSON Schema. */ export type APIJSONFormsSchema = JsonSchema; /** The backend `/_info` add/edit UI schema is a JsonForms layout. */ export type APIJSONFormsUISchema = Layout; export type APIInfo = InfoResponse; /** A query filter (`QueryFilter`) plus the per-column grid-tracking `id` set by the grid UI (absent on the wire). */ export interface APIQueryParamsFilter extends QueryFilter { /** Per-column grid-tracking id; set by the grid UI, absent on the wire. */ id?: string; } /** A column-less query filter (operator + value); mirrors `Omit`. */ export type APIQueryParamsOprFilter = Omit; /** * The resolved query state held by the `ApiProvider`. Derived from the wire-bound `QueryParams` * bag: `page` / `page_size` / `columns` / `filters` are always present (vs. all-optional on the * patch bag), `order_column` / `order_direction` drop the `| null` the wire allows, `filters` * carry the grid-tracking `id`, and the download / streaming keys are excluded. */ export type APIQueryParams = Required> & Pick & { /** The column name to order the results by. */ order_column?: string; /** The direction of the order ('asc' or 'desc'). */ order_direction?: 'asc' | 'desc'; /** An array of filters to apply to the query. */ filters: APIQueryParamsFilter[]; /** An array of opr filters to apply to the query. */ opr_filters?: APIQueryParamsOprFilter[]; }; /** * The resolved list data held by the `ApiProvider`. The client-side counterpart of the wire * `BaseResponseMany`: `count` / `result` are guaranteed non-null after a successful fetch, and * `path` / `queryParams` (the resolved `APIQueryParams`) are always present. */ export interface APIData extends Omit { /** The total count of items existing in the database for the given query parameters. */ count: number; /** An array of objects representing the API response data. */ result: TData[]; /** The API path. */ path: string; /** The query parameters used in the API request. */ queryParams: APIQueryParams; } export interface 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 interface APIStreaming { /** The streaming data, organized by the page number. */ data: { [pageNumber: string]: TData[]; }; /** Indicates whether the streaming has ended. */ isEnd: boolean; /** A function to fetch the next page of streaming data. */ fetch: () => Promise; /** A function to reset the streaming data and start over. */ reset: () => Promise; } export interface 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 interface 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 interface APIError { message: string; originalError: Error; } export interface APISetErrorPayload { message: string; originalError: Error; data?: TData; } /** * `APIInfo` augmented with the normalized add/edit form blocks produced by * `convertInfo` (in core's ApiProvider) — `defaultValues`, the yup validation * schema, and the column list per form. Carried as the live shape of * `useApi().info` so consumers read `info.add.schema` / `info.edit.defaultValues` * without casting. */ export type APIInfoCanonical = ConvertedInfo & { path?: string; }; /** * Per-operation type overrides for `useApi`. Each key maps to a backend column projection and, * when set, replaces the uniform default for that operation only: `list` (list response rows, * `list_columns`), `get` (a single fetched entry and the record returned by `addEntry` / `updateEntry`, * `show_columns`), `add` (the `addEntry` payload, `add_columns`), `edit` (the `updateEntry` payload, * `edit_columns`). `upload` (`EntryRow[]`, loosely-typed import rows) and `deleteEntry` (`null`, the 204 no-content response) are not parameterized. */ export interface ApiTypeOverrides { list?: unknown; get?: unknown; add?: unknown; edit?: unknown; } /** Resolve one operation's type from the override map, falling back to `TDefault`. */ export type OpType = TOverrides extends Record ? T : TDefault; /** * @typeParam TList - Row shape of the list response (`data.result`). * @typeParam TGet - Shape of a single fetched/affected record (`getEntry`, and the record returned by `addEntry` / `updateEntry`). * @typeParam TAdd - Payload accepted by `addEntry`. * @typeParam TEdit - Payload accepted by `updateEntry`. */ export interface ApiContextValue { /** The API path. */ path: string; /** Information about the API, including schemas, UI schemas, and the normalized add/edit form blocks. */ info?: APIInfoCanonical; /** 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?: APIError; /** An error object if the API info request failed. */ infoError?: Error; /** An error object if the API data request failed. */ dataError?: Error; /** The raw React Query object for the info request. */ infoQuery: UseQueryResult; /** The raw React Query object for the data request. */ dataQuery: UseQueryResult | null>; /** The raw React Query infinite query object for streaming requests. */ streamingQuery: UseInfiniteQueryResult | null>>; /** Streaming (infinite-scroll) controller. Present only when the `ApiProvider` is configured for streaming; its presence is the streaming-mode signal. When set, `data.result` carries every row fetched so far (all pages) and `streaming.data` is the page-number-keyed breakdown. */ 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 | ((prev: APIQueryParams) => Partial)) => void; /** A function to refetch the data from the API. */ refetch: (options?: { force?: boolean; }) => Promise; /** A function to refetch the API information. */ refetchInfo: (options?: { force?: boolean; }) => Promise; /** Retrieve a specific entry by ID. Return defaults to the resolved `get` type; override per call (e.g. `getEntry(id)`) when the route is customized. */ getEntry: (id: PrimaryKey) => Promise; /** Add a new entry. Return defaults to the resolved `get` type; override per call for custom routes. */ addEntry: (item: TAdd) => Promise; /** Edit an existing entry. Return defaults to the resolved `get` type; override per call for custom routes. */ updateEntry: (id: PrimaryKey, item: TEdit) => Promise; /** Delete an entry. The stock endpoint responds 204 (no body), parsed to `null`, so the return defaults to `null`; override per call for custom `post_delete` payloads. */ deleteEntry: (id: PrimaryKey) => Promise; /** A function to download data from the API. */ download: (label?: string, options?: APIDownloadOptions) => Promise; /** Upload a CSV. The importer returns `list[dict[str, Any]]` (no response_model), so the default is `EntryRow[]` (loosely-typed rows), NOT the model/`TGet` shape; override per call if your route is typed. */ upload: (file: File, options?: APIUploadOptions) => Promise; /** A function to get the URL for a file item (file name or File object). */ getFileURL: (item: string | File) => string; /** A function to get the URL for an image item (image name or File object). */ getImageURL: (item: string | File) => string; /** A function to set the error state, surfacing errors thrown by the action functions (`addEntry`, `updateEntry`, ...) through the shared `ErrorDialog`. */ setError: (error: APISetErrorPayload | undefined) => void; /** A function to reset the error state. */ resetError: () => void; } /** * The `ApiContextValue` returned by `useApi`: the uniform `TDefault` applied to * every operation, with any `TOverrides` keys replacing it per operation. */ export type UseApiResult = ApiContextValue, OpType, OpType, OpType>;