0

Search by Title

by
Published Apr 25, 2023
Script notion Verified

The script

Submitted by hugo989 Bun
Verified 19 days ago
1
import { Client } from "@notionhq/client";
2

3
/**
4
 * Learn more at
5
 * https://developers.notion.com/reference/post-search
6
 */
7
type Notion = {
8
  token: string;
9
};
10
export async function main(
11
  auth: Notion,
12
  query?: string,
13
  filter?: { property: string; value: string },
14
  sort?: { timestamp: string; direction: string },
15
  start_cursor?: string,
16
  page_size?: number,
17
) {
18
  const client = new Client({ auth: auth.token });
19
  const args = removeObjectEmptyFields({
20
    query,
21
    filter,
22
    sort,
23
    start_cursor,
24
    page_size,
25
  });
26
  return await client.search(args);
27
}
28

29
function removeObjectEmptyFields(
30
  object?: Record<string, any>,
31
  removeEmptyArraysAndObjects = true,
32
  createNewObject = true,
33
) {
34
  if (!object || typeof object !== "object") return {}
35
  const obj = createNewObject ? { ...object } : object
36
  const emptyValues = [undefined, null, ""]
37
  for (const key in obj) {
38
    const value = obj[key]
39
    if (emptyValues.includes(value)) {
40
      delete obj[key]
41
    } else if (typeof value === "object") {
42
      if (Object.keys(value).length) {
43
        obj[key] = removeObjectEmptyFields(value, removeEmptyArraysAndObjects, false)
44
      }
45
      if (!Object.keys(value).length && removeEmptyArraysAndObjects) {
46
        delete obj[key]
47
      }
48
    }
49
  }
50
  return obj
51
}
52

Other submissions
  • Submitted by adam186 Deno
    Created 411 days ago
    1
    import { removeObjectEmptyFields } from "https://deno.land/x/[email protected]/mod.ts";
    2
    import { Client } from "npm:@notionhq/client";
    3
    
    
    4
    /**
    5
     * Learn more at
    6
     * https://developers.notion.com/reference/post-search
    7
     */
    8
    type Notion = {
    9
      token: string;
    10
    };
    11
    export async function main(
    12
      auth: Notion,
    13
      query?: string,
    14
      filter?: { property: string; value: string },
    15
      sort?: { timestamp: string; direction: string },
    16
      start_cursor?: string,
    17
      page_size?: number,
    18
    ) {
    19
      const client = new Client({ auth: auth.token });
    20
      const args = removeObjectEmptyFields({
    21
        query,
    22
        filter,
    23
        sort,
    24
        start_cursor,
    25
        page_size,
    26
      });
    27
      return await client.search(args);
    28
    }
    29