Edits history of script submission #8893 for ' Read records from a grist table (grist)'

  • bun
    import { mapValues } from 'lodash-es'
    
    // https://hub.windmill.dev/resource_types/155/grist
    type Grist = {
      token: string,
      teamOrBaseURL: string
    }
    // https://hub.windmill.dev/resource_types/156/grist_document
    type GristDoc = {
      doc: string,
      gristInstance: Grist
    }
    
    export async function main(
      gristDoc: GristDoc,
      table: string,
      params?: { filter?: Object, sort?: string, limit?: number, hidden?: boolean },
    ): Promise<any> {
      const { gristInstance: grist, doc } = gristDoc
    
      // DOCS: https://support.getgrist.com/api/#tag/records/operation/listRecords
      const baseURL = grist.teamOrBaseURL.includes('.') ? grist.teamOrBaseURL : `https://${grist.teamOrBaseURL}.getgrist.com`
      let paramsString = ''
      if (params) {
        const encodedParams = new URLSearchParams(mapValues(params, v => typeof v === 'string' ? v : JSON.stringify(v))).toString()
        paramsString = `?${encodedParams}`
      }
      console.log(paramsString)
      const response = await fetch(
        `${baseURL}/api/docs/${doc}/tables/${table}/records`
        + paramsString,
        {
          method: 'GET',
          headers: {
            'Authorization': `Bearer ${grist.token}`,
            'Content-Type': 'application/json'
          },
        });
    
      if (!response.ok) {
        throw new Error(`Failed to write to Grist table: ${response.statusText} ${JSON.stringify(await response.json())}`);
      }
    
      return await response.json();
    }
    

    Submitted by manu [tennox]332 687 days ago