export declare interface AtomicOperation { op: 'add' | 'update' | 'remove'; data?: BaseEntity | JsonApiResourceIdentifier | JsonApiResourceIdentifier[] | null; ref?: JsonApiReference; } export declare interface BaseEntity { id: string; lid?: string; type: string; [META]?: JsonApiMeta; } /** * Convert str from kebab-case to camelCase */ export declare function camel(str: string): string; export declare interface FetchOptions { fields?: Record; page?: PageOption; include?: string[]; filter?: string; headers?: HeadersInit; body?: BodyInit; signal?: AbortSignal; } export declare interface FetchParams { [key: string]: string; } export declare type JsonApi = ReturnType; export declare interface JsonApiAtomicDocument { 'atomic:operations'?: JsonApiAtomicOperation[]; 'atomic:results'?: JsonApiAtomicResult[]; errors?: JsonApiError[]; } export declare interface JsonApiAtomicOperation { op: 'add' | 'update' | 'remove'; data?: JsonApiResource | JsonApiResourceIdentifier | JsonApiResourceIdentifier[] | null; ref?: JsonApiReference; } export declare interface JsonApiAtomicResult { data: JsonApiResource; meta?: JsonApiMeta; } export declare interface JsonApiConfig { /** * The URL for the JSON:API endpoint */ endpoint: string; /** * Model definitions for the store */ modelDefinitions: ModelDefinition[]; /** * Whether to convert kebab-case names from JSON:API (older convention) to camelCase */ kebabCase?: boolean; } export declare interface JsonApiDocument { links?: JsonApiLinks; data?: JsonApiResource | JsonApiResource[]; errors?: JsonApiError[]; included?: JsonApiResource[]; meta?: JsonApiMeta; } export declare interface JsonApiError { id: string; status: string; code?: string; title: string; detail?: string; meta?: JsonApiMeta; } export declare type JsonApiFetcher = InstanceType; declare class JsonApiFetcherImpl implements JsonApiFetcher { endpoint: string; constructor(endpoint: string); createOptions(options?: FetchOptions, params?: FetchParams, body?: BodyInit): Options; fetchDocument(type: string, id?: string, options?: FetchOptions, params?: FetchParams): Promise; fetchAll(type: string, options?: FetchOptions, params?: FetchParams): Promise; fetchOne(type: string, id: string, options?: FetchOptions, params?: FetchParams): Promise; fetchHasMany(type: string, id: string, name: string, options?: FetchOptions, params?: FetchParams): Promise; fetchBelongsTo(type: string, id: string, name: string, options?: FetchOptions, params?: FetchParams): Promise; post(resource: JsonApiResource, options?: FetchOptions): Promise; patch(resource: JsonApiResource, options?: FetchOptions): Promise; postAtomic(doc: JsonApiAtomicDocument, options?: FetchOptions): Promise; } export declare type JsonApiLink = null | string | JsonApiLinkObject; export declare interface JsonApiLinkObject { href: string; rel?: string; describedby?: JsonApiLink; title?: string; type?: string; hreflang?: string | string[]; meta?: JsonApiMeta; } export declare interface JsonApiLinks { self?: JsonApiLink; related?: JsonApiLink; describedby?: JsonApiLink; first?: JsonApiLink; last?: JsonApiLink; prev?: JsonApiLink; next?: JsonApiLink; } export declare interface JsonApiMeta { totalPages?: number; total?: number; totalItems?: number; currentPage?: number; itemsPerPage?: number; timestamp?: string | number; version?: string; copyright?: string; [key: string]: unknown; } declare interface JsonApiReference extends JsonApiResourceIdentifier { relationship?: string; } export declare interface JsonApiRelationship { data: null | [] | JsonApiResourceIdentifier | JsonApiResourceIdentifier[]; } export declare interface JsonApiResource { id?: string; lid?: string; type: string; attributes: Record; relationships?: Record; meta?: JsonApiMeta; } export declare interface JsonApiResourceIdentifier { id?: string; lid?: string; type: string; } /** * Symbol key used to store JSON:API resource-level meta on a record, avoiding * collisions with a literal "meta" attribute field. */ export declare const META: unique symbol; /** * Model definition */ export declare interface ModelDefinition { /** * The JSON:API type for the model */ type: string; /** * Optional relationships for the model */ relationships?: Record; } declare interface Options { searchParams?: URLSearchParams; headers: Headers; method?: string; body?: BodyInit; signal?: AbortSignal; } export declare interface PageOption { size?: number; number?: number; } /** * Relationship definition */ export declare interface Relationship { /** The JSON:API type name of the related model */ type: string; /** The relationship type */ relationshipType: RelationshipType; } export declare enum RelationshipType { HasMany = 0, BelongsTo = 1 } export declare interface SerializeOptions { /** Whether to include to-many (HasMany) relationships when serializing a resource. Defaults to true. */ includeToManyRelationships?: boolean; } export declare function useJsonApi(config: JsonApiConfig, fetcher?: JsonApiFetcher): { findAll: (type: string, options?: FetchOptions, params?: FetchParams) => Promise<{ doc: JsonApiDocument; records: T[]; }>; findRecord: (type: string, id: string, options?: FetchOptions, params?: FetchParams) => Promise<{ doc: JsonApiDocument; record: T; }>; findRelated: (record: BaseEntity, relationshipName: string, options?: FetchOptions, params?: FetchParams) => Promise; createRecord: (type: string, properties: Partial & { id?: string; }) => T; saveRecord: (record: BaseEntity, options?: FetchOptions) => Promise; saveAtomic: (operations: AtomicOperation[], options?: FetchOptions, serializeOptions?: SerializeOptions) => Promise<{ doc: JsonApiAtomicDocument; records: BaseEntity[]; } | undefined>; }; export { }