Skip to main content

clickupc_core/client/
doc.rs

1//! v3 Docs. Note: "Search for Docs" has no free-text query parameter, so
2//! name search is a client-side filter over the listing.
3
4use serde_json::{json, Value};
5
6use super::{ClickUpClient, Query};
7use crate::error::Result;
8
9/// Parent types accepted by Create a Doc.
10#[derive(Debug, Clone, Copy, PartialEq)]
11pub enum DocParent {
12    Space = 4,
13    Folder = 5,
14    List = 6,
15    Everything = 7,
16    Workspace = 12,
17}
18
19impl ClickUpClient {
20    fn docs_path(&self, suffix: &str) -> Result<String> {
21        Ok(format!(
22            "/v3/workspaces/{}/docs{suffix}",
23            self.workspace_id()?
24        ))
25    }
26
27    /// `GET /v3/workspaces/{ws}/docs`, following `next_cursor`, optionally
28    /// keeping only docs whose name contains `name_filter` (case-insensitive).
29    pub async fn search_docs(&self, name_filter: Option<&str>, limit: usize) -> Result<Vec<Value>> {
30        let needle = name_filter.map(str::to_lowercase);
31        let mut out = Vec::new();
32        let mut cursor: Option<String> = None;
33        for _ in 0..50 {
34            let q = Query::new()
35                .push("limit", 100)
36                .push_opt("cursor", cursor.as_ref());
37            let v: Value = self.get(&self.docs_path("")?, &q).await?;
38            for doc in v
39                .get("docs")
40                .and_then(Value::as_array)
41                .into_iter()
42                .flatten()
43            {
44                let keep = needle.as_ref().is_none_or(|n| {
45                    doc.get("name")
46                        .and_then(Value::as_str)
47                        .is_some_and(|name| name.to_lowercase().contains(n))
48                });
49                if keep {
50                    out.push(doc.clone());
51                }
52            }
53            if out.len() >= limit {
54                out.truncate(limit);
55                break;
56            }
57            cursor = v
58                .get("next_cursor")
59                .and_then(Value::as_str)
60                .filter(|c| !c.is_empty())
61                .map(str::to_string);
62            if cursor.is_none() {
63                break;
64            }
65        }
66        Ok(out)
67    }
68
69    /// `GET /v3/workspaces/{ws}/docs/{doc}`
70    pub async fn get_doc(&self, doc_id: &str) -> Result<Value> {
71        self.get(&self.docs_path(&format!("/{doc_id}"))?, &Query::new())
72            .await
73    }
74
75    /// `GET …/docs/{doc}/page_listing`
76    pub async fn doc_page_listing(&self, doc_id: &str) -> Result<Value> {
77        self.get(
78            &self.docs_path(&format!("/{doc_id}/page_listing"))?,
79            &Query::new(),
80        )
81        .await
82    }
83
84    /// `GET …/docs/{doc}/pages` (markdown content)
85    pub async fn doc_pages(&self, doc_id: &str) -> Result<Value> {
86        self.get(
87            &self.docs_path(&format!("/{doc_id}/pages"))?,
88            &Query::new().push("content_format", "text/md"),
89        )
90        .await
91    }
92
93    /// `GET …/docs/{doc}/pages/{page}` (markdown content)
94    pub async fn doc_page(&self, doc_id: &str, page_id: &str) -> Result<Value> {
95        self.get(
96            &self.docs_path(&format!("/{doc_id}/pages/{page_id}"))?,
97            &Query::new().push("content_format", "text/md"),
98        )
99        .await
100    }
101
102    /// `POST /v3/workspaces/{ws}/docs`
103    pub async fn create_doc(&self, name: &str, parent: Option<(&str, DocParent)>) -> Result<Value> {
104        let mut body = json!({ "name": name, "create_page": true });
105        if let Some((id, kind)) = parent {
106            body["parent"] = json!({ "id": id, "type": kind as i32 });
107        }
108        self.post(&self.docs_path("")?, &Query::new(), body).await
109    }
110
111    /// `POST …/docs/{doc}/pages`
112    pub async fn create_doc_page(
113        &self,
114        doc_id: &str,
115        name: &str,
116        content: Option<&str>,
117        parent_page_id: Option<&str>,
118    ) -> Result<Value> {
119        let mut body = json!({ "name": name, "content_format": "text/md" });
120        if let Some(c) = content {
121            body["content"] = json!(c);
122        }
123        if let Some(p) = parent_page_id {
124            body["parent_page_id"] = json!(p);
125        }
126        self.post(
127            &self.docs_path(&format!("/{doc_id}/pages"))?,
128            &Query::new(),
129            body,
130        )
131        .await
132    }
133
134    /// `PUT …/docs/{doc}/pages/{page}`; `mode` = replace | append | prepend.
135    pub async fn edit_doc_page(
136        &self,
137        doc_id: &str,
138        page_id: &str,
139        content: Option<&str>,
140        name: Option<&str>,
141        mode: &str,
142    ) -> Result<Value> {
143        let mut body = json!({ "content_format": "text/md", "content_edit_mode": mode });
144        if let Some(c) = content {
145            body["content"] = json!(c);
146        }
147        if let Some(n) = name {
148            body["name"] = json!(n);
149        }
150        self.put(
151            &self.docs_path(&format!("/{doc_id}/pages/{page_id}"))?,
152            &Query::new(),
153            body,
154        )
155        .await
156    }
157}