Skip to content

Commit

Permalink
use api client
Browse files Browse the repository at this point in the history
  • Loading branch information
sywhb committed May 16, 2024
1 parent f912bfe commit fc39658
Show file tree
Hide file tree
Showing 5 changed files with 154 additions and 123 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
},
"dependencies": {
"@logseq/libs": "^0.0.15",
"@omnivore-app/api": "^1.0.0",
"date-fns": "^2.29.3",
"diff-match-patch": "^1.0.5",
"lodash": "^4.17.21",
Expand Down
95 changes: 94 additions & 1 deletion pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

147 changes: 45 additions & 102 deletions src/api.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { Omnivore } from '@omnivore-app/api'

export interface SearchResponse {
data: {
search: {
Expand Down Expand Up @@ -26,61 +28,57 @@ export interface UpdatesSinceResponse {
}
}

export enum PageType {
Article = 'ARTICLE',
Book = 'BOOK',
File = 'FILE',
Profile = 'PROFILE',
Unknown = 'UNKNOWN',
Website = 'WEBSITE',
Tweet = 'TWEET',
Video = 'VIDEO',
Image = 'IMAGE',
}
export type PageType =
| 'ARTICLE'
| 'BOOK'
| 'FILE'
| 'PROFILE'
| 'UNKNOWN'
| 'WEBSITE'
| 'TWEET'
| 'VIDEO'
| 'IMAGE'
| 'HIGHLIGHTS'

export interface Article {
id: string
title: string
siteName?: string
originalArticleUrl: string
author?: string
description?: string
siteName?: string | null
originalArticleUrl: string | null
author?: string | null
description?: string | null
slug: string
labels?: Label[]
highlights?: Highlight[]
updatedAt: string
labels: Label[] | null
highlights: Highlight[] | null
updatedAt: string | null
savedAt: string
pageType: PageType
content?: string
publishedAt?: string
readAt?: string
content?: string | null
publishedAt?: string | null
readAt?: string | null
readingProgressPercent: number
isArchived: boolean
wordsCount?: number
archivedAt?: string
wordsCount?: number | null
archivedAt?: string | null
}

export interface Label {
name: string
}

export enum HighlightType {
Highlight = 'HIGHLIGHT',
Note = 'NOTE',
Redaction = 'REDACTION',
}
export type HighlightType = 'HIGHLIGHT' | 'NOTE' | 'REDACTION'

export interface Highlight {
id: string
quote: string | null
annotation: string | null
patch: string | null
updatedAt: string
labels?: Label[]
updatedAt: string | null
labels?: Label[] | null
type: HighlightType
highlightPositionPercent: number
color?: string
highlightPositionAnchorIndex: number
highlightPositionPercent: number | null
color?: string | null
highlightPositionAnchorIndex: number | null
}

const ENDPOINT = 'https://api-prod.omnivore.app/api/graphql'
Expand All @@ -100,78 +98,23 @@ export const getOmnivoreArticles = async (
format = 'html',
endpoint = ENDPOINT
): Promise<[Article[], boolean]> => {
const res = await fetch(endpoint, {
headers: requestHeaders(apiKey),
body: JSON.stringify({
query: `
query Search($after: String, $first: Int, $query: String, $includeContent: Boolean, $format: String) {
search(first: $first, after: $after, query: $query, includeContent: $includeContent, format: $format) {
... on SearchSuccess {
edges {
node {
id
title
slug
siteName
originalArticleUrl
url
author
updatedAt
description
savedAt
pageType
content
publishedAt
readAt
isArchived
readingProgressPercent
wordsCount
archivedAt
highlights {
id
quote
annotation
patch
updatedAt
highlightPositionPercent
highlightPositionAnchorIndex
labels {
name
}
type
color
}
labels {
name
}
}
}
pageInfo {
hasNextPage
}
}
... on SearchError {
errorCodes
}
}
}`,
variables: {
after: `${after}`,
first,
query: `${
updatedAt ? 'updated:' + updatedAt : ''
} sort:saved-asc ${query}`,
includeContent,
format,
},
}),
method: 'POST',
const omnivore = new Omnivore({
authToken: apiKey,
baseUrl: endpoint,
timeoutMs: 10000,
})

const result = await omnivore.items.search({
after: after.toString(),
first,
query: `${updatedAt ? 'updated:' + updatedAt : ''} sort:saved-asc ${query}`,
includeContent,
format: format as 'html' | 'markdown',
})

const jsonRes = (await res.json()) as SearchResponse
const articles = jsonRes.data.search.edges.map((e) => e.node)
const items = result.edges.map((e) => e.node)

return [articles, jsonRes.data.search.pageInfo.hasNextPage]
return [items, result.pageInfo.hasNextPage]
}

export const getDeletedOmnivoreArticles = async (
Expand Down
Loading

0 comments on commit fc39658

Please sign in to comment.