Skip to content
Snippets Groups Projects
cozy-client.d.ts 1.78 KiB
Newer Older
  • Learn to ignore specific revisions
  • Hugo NOUTS's avatar
    Hugo NOUTS committed
    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 AppMetaData = {
      slug: string
      version: string
    }
    
    
    Hugo NOUTS's avatar
    Hugo NOUTS committed
    type Offline = {
      getDatabase: unknown
    }
    export type Client = {
      find(doctype: string): typeof QueryDefinition
      query(doctype: string): typeof QueryDefinition
    
    Gauthier LEFEVRE's avatar
    Gauthier LEFEVRE committed
      destroy(document: Document): typeof QueryDefinition
      save(document: Document): typeof QueryDefinition
    
    Hugo NOUTS's avatar
    Hugo NOUTS committed
      create(
        doctype: string,
    
    Gauthier LEFEVRE's avatar
    Gauthier LEFEVRE committed
        entry: object,
        relationships?: object
    
    Hugo NOUTS's avatar
    Hugo NOUTS committed
      ): typeof QueryDefinition
    
      collection(doctype: string): function
    
    Hugo NOUTS's avatar
    Hugo NOUTS committed
      offline: typeof Offline
      getStackClient: function
    
      appMetadata: AppMetaData
    
    Hugo NOUTS's avatar
    Hugo NOUTS committed
    }
    
    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[]
    }
    
    Yoan VALLET's avatar
    Yoan VALLET committed
    
    export function fromEnv(env: NodeJS.ProcessEnv, schema: object): () => Client