Skip to content

Commit

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

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

163 changes: 26 additions & 137 deletions src/api.ts
Original file line number Diff line number Diff line change
@@ -1,167 +1,56 @@
import { Omnivore } from '@omnivore-app/api'
import { Item, ItemFormat, Omnivore } from '@omnivore-app/api'

export interface SearchResponse {
data: {
search: {
edges: { node: Article }[]
pageInfo: {
hasNextPage: boolean
}
}
}
}

export enum UpdateReason {
CREATED = 'CREATED',
UPDATED = 'UPDATED',
DELETED = 'DELETED',
}

export interface UpdatesSinceResponse {
data: {
updatesSince: {
edges: { updateReason: UpdateReason; node: Article }[]
pageInfo: {
hasNextPage: boolean
}
}
}
}

export type PageType =
| 'ARTICLE'
| 'BOOK'
| 'FILE'
| 'PROFILE'
| 'UNKNOWN'
| 'WEBSITE'
| 'TWEET'
| 'VIDEO'
| 'IMAGE'
| 'HIGHLIGHTS'

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

export interface Label {
name: string
}

export type HighlightType = 'HIGHLIGHT' | 'NOTE' | 'REDACTION'

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

const ENDPOINT = 'https://api-prod.omnivore.app/api/graphql'
const requestHeaders = (apiKey: string) => ({
'Content-Type': 'application/json',
authorization: apiKey,
'X-OmnivoreClient': 'logseq-plugin',
})

export const getOmnivoreArticles = async (
export const getOmnivoreItems = async (
apiKey: string,
after = 0,
first = 10,
updatedAt = '',
query = '',
includeContent = false,
format = 'html',
endpoint = ENDPOINT
): Promise<[Article[], boolean]> => {
format: ItemFormat = 'html',
baseUrl?: string
): Promise<[Item[], boolean]> => {
const omnivore = new Omnivore({
authToken: apiKey,
baseUrl: endpoint,
apiKey,
baseUrl,
timeoutMs: 10000,
})

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

const items = result.edges.map((e) => e.node)

return [items, result.pageInfo.hasNextPage]
}

export const getDeletedOmnivoreArticles = async (
export const getDeletedOmnivoreItems = async (
apiKey: string,
after = 0,
first = 10,
updatedAt = '',
endpoint = ENDPOINT
): Promise<[Article[], boolean]> => {
const res = await fetch(endpoint, {
headers: requestHeaders(apiKey),
body: JSON.stringify({
query: `
query UpdatesSince($after: String, $first: Int, $since: Date!) {
updatesSince(first: $first, after: $after, since: $since) {
... on UpdatesSinceSuccess {
edges {
updateReason
node {
id
slug
title
savedAt
}
}
pageInfo {
hasNextPage
}
}
... on UpdatesSinceError {
errorCodes
}
}
}`,
variables: {
after: `${after}`,
first,
since: updatedAt || '2021-01-01',
},
}),
method: 'POST',
baseUrl: string
): Promise<[Item[], boolean]> => {
const omnivore = new Omnivore({
apiKey,
baseUrl,
timeoutMs: 10000,
})

const result = await omnivore.items.updates({
after,
first,
since: updatedAt || '2021-01-01',
})

const jsonRes = (await res.json()) as UpdatesSinceResponse
const deletedArticles = jsonRes.data.updatesSince.edges
.filter((edge) => edge.updateReason === UpdateReason.DELETED)
.map((edge) => edge.node)
const deletedItems = result.edges
.filter((edge) => edge.updateReason === 'DELETED' && edge.node)
.map((edge) => edge.node) as Item[]

return [deletedArticles, jsonRes.data.updatesSince.pageInfo.hasNextPage]
return [deletedItems, result.pageInfo.hasNextPage]
}
Loading

0 comments on commit 66d2f36

Please sign in to comment.