Skip to content
Snippets Groups Projects
cozy-client.d.ts 1.6 KiB
Newer Older
  • Learn to ignore specific revisions
  • Hugo NOUTS's avatar
    Hugo NOUTS committed
    import { Component } from 'react'
    
    declare module 'cozy-client'
    
    type QueryDefinition = {
      doctype: string
      id: string
      ids: Array
      selector: object
      fields: Array
      indexedFields: Array
      sort: Array
      includes: string
      referenced: string
      limit: null | number
      skip: null | number
      cursor: unknown
    }
    
    type Offline = {
      getDatabase: unknown
    }
    export type Client = {
      find(doctype: string): typeof QueryDefinition
      query(doctype: string): typeof QueryDefinition
      destroy(document: any): typeof QueryDefinition
      save(document: any): typeof QueryDefinition
      create(
        doctype: string,
        entry: any,
        relationships?: any
      ): typeof QueryDefinition
      offline: typeof Offline
      getStackClient: function
    }
    
    export type QueryId = string
    
    export type QueryDefinitionBuilder = (client: Client) => QueryDefinition
    
    export type QueryOptions = {
      doc?: unknown
      query?: QueryDefinition | QueryDefinitionBuilder
      as?: QueryId
    }
    
    export type QueryOptionsBuilder<P> = (props: P) => QueryOptions
    
    export type QuerySpecs<P> = {
      [k in string]: QueryOptions | QueryOptionsBuilder<P>
    }
    
    export function queryConnect<C extends React.ElementType>(
      querySpecs: QuerySpecs<React.ComponentPropsWithoutRef<C>>
    ): (wrappedElement: C) => void
    
    export function withClient<C extends React.ElementType>(
      component: React.ComponentPropsWithoutRef<C>
    ): (wrappedElement: C) => void
    
    export type QueryState<D> = {
      id: unknown
      definition: unknown
      fetchStatus: 'pending' | 'loading' | 'loaded' | 'failed'
      lastFetch: null | number
      lastUpdate: null | number
      lastError: null | number
      hasMore: boolean
      count: number
      data: D[]
    }