amplifyquery
Version:
140 lines (139 loc) • 4.25 kB
TypeScript
/**
* Store state type
*/
export type StoreState<T> = {
items: T[];
itemsMap: Map<string, T>;
lastFetched: number | null;
isLoading: boolean;
error: Error | null;
setItems: (items: T[]) => void;
setItem: (item: T) => void;
removeItem: (id: string) => void;
setLoading: (isLoading: boolean) => void;
setError: (error: Error | null) => void;
resetState: () => void;
};
/**
* Hook interface type
*/
export type ModelHook<T> = {
items: T[];
isLoading: boolean;
error: Error | null;
getItem: (id: string) => T | null | undefined;
refresh: (options?: {
filter?: Record<string, any>;
}) => Promise<T[]>;
create: (data: Partial<T>) => Promise<T | null>;
update: (data: Partial<T> & {
id: string;
}) => Promise<T | null>;
delete: (id: string) => Promise<boolean>;
customList: (queryName: string, args: Record<string, any>, options?: {
forceRefresh?: boolean;
}) => Promise<T[]>;
};
/**
* Single item Hook interface type
*/
export type ItemHook<T> = {
item: T | null;
isLoading: boolean;
error: Error | null;
refresh: () => Promise<T | null>;
update: (data: Partial<T>) => Promise<T | null>;
delete: () => Promise<boolean>;
};
export type AuthMode = "apiKey" | "iam" | "identityPool" | "oidc" | "userPool" | "lambda" | "none";
export interface AuthOptions {
authMode?: AuthMode;
}
/**
* Amplify Data service type
*/
export interface AmplifyDataService<T> {
create: (data: Partial<T>, options?: AuthOptions) => Promise<T | null>;
createList: (dataList: Partial<T>[], options?: AuthOptions) => Promise<(T | null)[]>;
get: (id: string, options?: {
forceRefresh?: boolean;
authMode?: AuthMode;
}) => Promise<T | null>;
list: (options?: {
filter?: Record<string, any>;
forceRefresh?: boolean;
authMode?: AuthMode;
}) => Promise<T[]>;
customList: (queryName: string, args: Record<string, any>, options?: {
forceRefresh?: boolean;
authMode?: AuthMode;
}) => Promise<T[]>;
update: (data: Partial<T> & {
id: string;
}, options?: AuthOptions) => Promise<T | null>;
delete: (id: string, options?: AuthOptions) => Promise<boolean>;
deleteList: (ids: string[], options?: AuthOptions) => Promise<{
success: string[];
failed: string[];
}>;
upsert: (data: Partial<T> & {
id: string;
}, options?: AuthOptions) => Promise<T | null>;
getStore?: () => StoreState<T>;
loadFromCache: () => T[];
resetCache: () => void;
useHook: (options?: {
initialFetchOptions?: {
fetch?: boolean;
filter?: Record<string, any>;
};
customList?: {
queryName: string;
args: Record<string, any>;
forceRefresh?: boolean;
};
}) => ModelHook<T>;
useItemHook: (id: string) => ItemHook<T>;
modelName: string;
withExtensions: <E>(extensions: E) => AmplifyDataService<T> & E;
setAuthMode: (authMode: AuthMode) => void;
getAuthMode: () => AuthMode;
withAuthMode: (authMode: AuthMode) => AmplifyDataService<T>;
}
export interface SingletonAmplifyService<T> extends AmplifyDataService<T> {
getCurrent: (options?: {
forceRefresh?: boolean;
}) => Promise<T | null>;
updateCurrent: (data: Partial<T>) => Promise<T | null>;
upsertCurrent: (data: Partial<T>) => Promise<T | null>;
}
export interface BaseModel {
id: string;
createdAt: string;
updatedAt: string;
}
/**
* GraphQL client interface
* Compatible with clients generated by AWS Amplify's generateClient() function.
*/
export interface GraphQLClient {
models: Record<string, any>;
queries: Record<string, any>;
mutations: Record<string, any>;
}
import { QueryClientConfig } from "@tanstack/react-query";
/**
* Library configuration options
*/
export interface AmplifyQueryConfig {
client: GraphQLClient;
defaultAuthMode?: AuthMode;
modelOwnerQueryMap?: Record<string, string>;
isCachingEnabled?: boolean;
queryClientConfig?: QueryClientConfig;
storage?: {
mmkvId?: string;
cacheKey?: string;
maxAge?: number;
};
}