//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,
}))
}
/**
* Query Data Source
* Query the rows (pages) of a database's data source, with optional filter and sorts. The filter is a Notion filter object, e.g. {"property": "Status", "status": {"equals": "Done"}}. Paginate with `next_cursor` as `start_cursor`.
*/
export async function main(
auth: RT.Notion,
data_source_id: DynSelect_data_source_id,
filter: { [key: string]: any } | undefined,
sorts: { [key: string]: any }[] | undefined,
start_cursor: string | undefined,
page_size: number | undefined
) {
const body: { [key: string]: any } = {}
if (filter && Object.keys(filter).length) body.filter = filter
if (sorts?.length) body.sorts = sorts
if (start_cursor !== undefined && start_cursor !== "") {
body.start_cursor = start_cursor
}
if (page_size !== undefined) body.page_size = page_size
const response = await fetch(
`https://api.notion.com/v1/data_sources/${data_source_id}/query`,
{
method: "POST",
headers: {
Authorization: `Bearer ${auth.token}`,
"Content-Type": "application/json",
Accept: "application/json",
"Notion-Version": "2026-03-11",
},
body: JSON.stringify(body),
}
)
if (!response.ok) {
throw new Error(`${response.status} ${await response.text()}`)
}
return await response.json()
}
Submitted by hugo989 4 hours ago