Edits history of script submission #23133 for ' Retrieve Data Source (notion)'

  • bunnative
    One script reply has been approved by the moderators
    Ap­pro­ved
    //native
    
    export type DynSelect_data_source_id = string
    
    export async function data_source_id(auth: RT.Notion) {
      const response = await fetch("https://api.notion.com/v1/search", {
        method: "POST",
        headers: {
          Authorization: `Bearer ${auth.token}`,
          "Content-Type": "application/json",
          Accept: "application/json",
          "Notion-Version": "2026-03-11",
        },
        body: JSON.stringify({
          filter: { property: "object", value: "data_source" },
        }),
      })
      if (!response.ok) {
        throw new Error(`${response.status} ${await response.text()}`)
      }
      const { results } = (await response.json()) as {
        results: { id: string; title: { plain_text: string }[] }[]
      }
      return results.map((d) => ({
        value: d.id,
        label: d.title.map((t) => t.plain_text).join("") || d.id,
      }))
    }
    
    /**
     * Retrieve Data Source
     * Get a data source's schema: its property names, types and select/status options. Read it before building a Query Data Source filter or Create Page properties.
     */
    export async function main(
      auth: RT.Notion,
      data_source_id: DynSelect_data_source_id
    ) {
      const response = await fetch(
        `https://api.notion.com/v1/data_sources/${data_source_id}`,
        {
          headers: {
            Authorization: `Bearer ${auth.token}`,
            Accept: "application/json",
            "Notion-Version": "2026-03-11",
          },
        }
      )
    
      if (!response.ok) {
        throw new Error(`${response.status} ${await response.text()}`)
      }
    
      return await response.json()
    }
    

    Submitted by hugo989 4 hours ago