2023-03-14 19:24:11 +00:00
|
|
|
import type { Entity } from '../types';
|
2023-03-24 00:22:26 +00:00
|
|
|
import type { AxiosResponse } from 'axios';
|
2023-03-14 19:24:11 +00:00
|
|
|
import type z from 'zod';
|
|
|
|
|
|
|
|
type EntitySchema<TEntity extends Entity = Entity> = z.ZodType<TEntity, z.ZodTypeDef, any>;
|
|
|
|
|
2023-03-22 21:06:10 +00:00
|
|
|
/**
|
|
|
|
* Tells us where to find/store the entity in the cache.
|
|
|
|
* This value is accepted in hooks, but needs to be parsed into an `EntitiesPath`
|
|
|
|
* before being passed to the store.
|
|
|
|
*/
|
|
|
|
type ExpandedEntitiesPath = [
|
|
|
|
/** Name of the entity type for use in the global cache, eg `'Notification'`. */
|
|
|
|
entityType: string,
|
|
|
|
/**
|
|
|
|
* Name of a particular index of this entity type.
|
|
|
|
* Multiple params get combined into one string with a `:` separator.
|
|
|
|
*/
|
|
|
|
...listKeys: string[],
|
|
|
|
]
|
|
|
|
|
|
|
|
/** Used to look up an entity in a list. */
|
|
|
|
type EntitiesPath = [entityType: string, listKey: string]
|
|
|
|
|
|
|
|
/** Used to look up a single entity by its ID. */
|
|
|
|
type EntityPath = [entityType: string, entityId: string]
|
|
|
|
|
2023-03-23 21:09:04 +00:00
|
|
|
/** Callback functions for entity actions. */
|
|
|
|
interface EntityCallbacks<Value, Error = unknown> {
|
|
|
|
onSuccess?(value: Value): void
|
|
|
|
onError?(error: Error): void
|
|
|
|
}
|
|
|
|
|
2023-03-23 19:14:53 +00:00
|
|
|
/**
|
|
|
|
* Passed into hooks to make requests.
|
2023-03-24 00:22:26 +00:00
|
|
|
* Must return an Axios response.
|
2023-03-23 19:14:53 +00:00
|
|
|
*/
|
2023-03-24 00:22:26 +00:00
|
|
|
type EntityFn<T> = (value: T) => Promise<AxiosResponse>
|
2023-03-23 19:14:53 +00:00
|
|
|
|
2023-03-22 21:06:10 +00:00
|
|
|
export type {
|
|
|
|
EntitySchema,
|
|
|
|
ExpandedEntitiesPath,
|
|
|
|
EntitiesPath,
|
|
|
|
EntityPath,
|
2023-03-23 21:09:04 +00:00
|
|
|
EntityCallbacks,
|
2023-03-24 00:22:26 +00:00
|
|
|
EntityFn,
|
2023-03-22 21:06:10 +00:00
|
|
|
};
|