Skip to content

feat(page): enrich page view JSON with metadata via official API#37

Open
coseto6125 wants to merge 1 commit into
lox:mainfrom
coseto6125:feat/page-view-metadata
Open

feat(page): enrich page view JSON with metadata via official API#37
coseto6125 wants to merge 1 commit into
lox:mainfrom
coseto6125:feat/page-view-metadata

Conversation

@coseto6125
Copy link
Copy Markdown

Summary

page view --json today omits page metadata (timestamps, authors, parent, icon, archived state) because the MCP notion-fetch tool doesn't expose those fields — the output.Page struct has CreatedTime/LastEditedTime but they're left as zero values.

This PR adds an opt-in enrichment path: when an official API token is configured (via auth api setup or NOTION_API_TOKEN), page view --json makes a supplementary GET /pages/<id> call to the Notion REST API and merges the result into the JSON output.

When no token is configured, the command behaves exactly as before — no network overhead, no behavior change.

This continues the MCP-first + REST-fallback pattern established in #17 and the groundwork laid by #28.

Changes

internal/api/client.go

  • Add PageMetadata, PartialUser, PageIcon, PageParent types mapping the Notion REST API response
  • Add GetPageMetadata(ctx, pageID) method calling GET /pages/<id>

internal/output/types.go

  • Add CreatedBy and LastEditedBy string fields to Page (timestamps were already defined)

cmd/page.go

  • renderFetchedPageView calls a new loadPageMetadata helper when JSON output is requested, and merges metadata via applyPageMetadata before calling printViewedPageFn
  • loadPageMetadata is swappable (test seam) and swallows errors from missing token / API failure so the primary output is never blocked
  • Text and --raw modes skip the API call entirely

cmd/page_view_test.go

  • 4 new tests: enrichment happy path, token-missing fallback, text-mode skip, parent/icon variant coverage

internal/api/client_test.go

  • 2 new tests: full response parsing, blank page ID rejection

README.md

  • Document the enrichment behavior under the Pages section

Behavior matrix

Mode API token Metadata enrichment Extra API call
--json configured Yes 1 × GET /pages/
--json missing/invalid No (fields empty) No (short-circuit)
default text either No No
--raw either No No

Test plan

  • go build ./... clean
  • go test ./... passes (existing + new tests)
  • go vet ./... clean
  • gofmt -l . empty
  • Manual smoke: NOTION_API_TOKEN=ntn_xxx notion-cli page view <url> --json | jq '{CreatedBy, LastEditedBy, Archived}' returns populated fields
  • Manual smoke without token: output matches previous v0.6.0 behavior

Notes

  • Intentionally silent on API errors: if the REST call fails, the MCP-sourced fields (ID, Title, URL, Content, Comments) still render, preserving the fundamental page view UX
  • Only JSON output triggers the API call — reasoning: the text renderer doesn't display these fields today, so there's no benefit to paying the extra latency
  • Archived combines archived || in_trash since the Notion API returns both flags

Refs #17

Extend `notion-cli page view --json` with the metadata fields the MCP
`notion-fetch` tool does not expose: CreatedTime, LastEditedTime,
CreatedBy, LastEditedBy, ParentType, ParentID, Archived, and Icon.

When an official API token is configured (via `auth api setup` or
NOTION_API_TOKEN), the CLI makes a supplementary GET /pages/<id> call
to the Notion REST API and merges the result into the output Page.
Without a token, the command behaves exactly as before — the metadata
fields remain at their zero values.

Failures in the metadata fetch (missing token, API error, network
issue) are deliberately silent: the primary MCP-sourced fields (ID,
Title, URL, Content, Comments) are still rendered so page view remains
usable even when the API fallback is misconfigured.

Non-JSON output paths (default text rendering, --raw) do not trigger
the extra API call, keeping the common case free of network overhead.

Refs lox#17 (MCP-first + REST-fallback architecture).
Copy link
Copy Markdown

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: e1f7804f38

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

Comment thread cmd/page.go
Comment on lines +731 to +735
switch parent.Type {
case "page_id":
return "page", parent.PageID
case "database_id":
return "database", parent.DatabaseID
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Handle data_source parent IDs in metadata mapping

This switch only maps database_id, but with newer Notion API versions pages in databases commonly come back as parent.type = "data_source_id"; in that case parentFromMetadata falls through and returns an empty ParentID. The new page view --json enrichment therefore drops parent IDs for data-source-backed pages, which breaks the advertised ParentType/ParentID metadata for a large class of pages.

Useful? React with 👍 / 👎.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant