Skip to content

Commit

Permalink
integrate with omnivore api client (omnivore-app#204)
Browse files Browse the repository at this point in the history
* integrate with omnivore api client

* test api client library

* update dependencies

* fix dependecies

* replace delete item by using api client
  • Loading branch information
sywhb authored and aaachen committed Apr 16, 2024
1 parent 75e4311 commit 5dee7c2
Show file tree
Hide file tree
Showing 11 changed files with 1,049 additions and 1,442 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ jobs:
- name: Setup Node.js
uses: actions/setup-node@v2
with:
node-version: 16
node-version: 18
cache: 'yarn'
- name: Install dependencies
run: yarn
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ jobs:
strategy:
matrix:
os: [ ubuntu-latest, macos-latest, windows-latest ]
node-version: [ 14.x, 16.x, 18.x, 19.x ]
node-version: [ 18.x, 20.x ]
max-parallel: 24
steps:
- uses: actions/checkout@v3
Expand Down
1 change: 0 additions & 1 deletion .npmrc

This file was deleted.

2 changes: 1 addition & 1 deletion esbuild.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ esbuild.build({
...builtins],
format: 'cjs',
watch: !prod,
target: 'es2018',
target: 'es2021',
logLevel: "info",
sourcemap: prod ? false : 'inline',
treeShaking: true,
Expand Down
7 changes: 3 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
"@types/luxon": "^3.1.0",
"@types/markdown-escape": "^1.1.0",
"@types/mustache": "^4.2.2",
"@types/node": "^16.11.6",
"@typescript-eslint/eslint-plugin": "5.29.0",
"@typescript-eslint/parser": "5.29.0",
"builtin-modules": "3.3.0",
Expand All @@ -50,16 +49,16 @@
"typescript": "4.7.4"
},
"dependencies": {
"@omnivore-app/api": "^1.0.3",
"diff-match-patch": "^1.0.5",
"lodash": "^4.17.21",
"luxon": "^3.1.1",
"markdown-escape": "^2.0.0",
"mustache": "^4.2.0",
"out-of-character": "^1.2.1",
"process": "^0.11.10"
"out-of-character": "^1.2.1"
},
"volta": {
"node": "16.15.1",
"node": "18.18.0",
"yarn": "1.22.19"
}
}
219 changes: 25 additions & 194 deletions src/api.ts
Original file line number Diff line number Diff line change
@@ -1,86 +1,6 @@
import { requestUrl } from 'obsidian'

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

export interface DeleteArticleResponse {
data: {
setBookmarkArticle: {
bookmarkedArticle: {
id: string
}
}
}
}

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

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

export interface Label {
name: string
}

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

export interface Highlight {
id: string
quote: string | null
annotation: string | null
patch: string | null
updatedAt: string
labels?: Label[]
type: HighlightType
highlightPositionPercent: number
color?: string
highlightPositionAnchorIndex: number
}
import { Item, ItemFormat, Omnivore } from '@omnivore-app/api'

// TODO: add to API package
// Highlight colors currently supported in Omnivore
export enum HighlightColors {
Yellow = 'yellow',
Expand All @@ -89,135 +9,46 @@ export enum HighlightColors {
Blue = 'blue',
}

const requestHeaders = (apiKey: string) => ({
'Content-Type': 'application/json',
authorization: apiKey,
'X-OmnivoreClient': 'obsidian-plugin',
})

export const loadArticles = async (
export const getItems = async (
endpoint: string,
apiKey: string,
after = 0,
first = 10,
updatedAt = '',
query = '',
includeContent = false,
format = 'html',
): Promise<[Article[], boolean]> => {
const res = await requestUrl({
url: 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
image
author
updatedAt
description
savedAt
pageType
content
publishedAt
readAt
wordsCount
isArchived
readingProgressPercent
archivedAt
contentReader
highlights {
id
quote
annotation
patch
updatedAt
type
highlightPositionPercent
highlightPositionAnchorIndex
labels {
name
}
color
}
labels {
name
}
}
}
pageInfo {
hasNextPage
}
}
... on SearchError {
errorCodes
}
}
}`,
variables: {
after: `${after}`,
first,
query: `${
updatedAt ? 'updated:' + updatedAt : ''
} sort:saved-asc ${query}`,
includeContent,
format,
},
}),
method: 'POST',
format: ItemFormat = 'html',
): Promise<[Item[], boolean]> => {
const omnivore = new Omnivore({
apiKey: apiKey,
baseUrl: endpoint,
timeoutMs: 10000, // 10 seconds
})

const jsonRes = res.json as SearchResponse
const articles = jsonRes.data.search.edges.map((e) => e.node)
const response = await omnivore.items.search({
after,
first,
query: `${updatedAt ? 'updated:' + updatedAt : ''} sort:saved-asc ${query}`,
includeContent,
format,
})

return [articles, jsonRes.data.search.pageInfo.hasNextPage]
const articles = response.edges.map((e) => e.node)
return [articles, response.pageInfo.hasNextPage]
}

export const deleteArticleById = async (
export const deleteItem = async (
endpoint: string,
apiKey: string,
articleId: string,
) => {
const res = await requestUrl({
url: endpoint,
headers: requestHeaders(apiKey),
body: JSON.stringify({
query: `
mutation SetBookmarkArticle($input: SetBookmarkArticleInput!) {
setBookmarkArticle(input: $input) {
... on SetBookmarkArticleSuccess {
bookmarkedArticle {
id
}
}
... on SetBookmarkArticleError {
errorCodes
}
}
}`,
variables: {
input: {
articleID: articleId,
bookmark: false,
},
},
}),
method: 'POST',
const omnivore = new Omnivore({
apiKey: apiKey,
baseUrl: endpoint,
timeoutMs: 10000, // 10 seconds
})

const jsonRes = res.json as DeleteArticleResponse
if (jsonRes.data.setBookmarkArticle.bookmarkedArticle.id === articleId) {
return true
}
await omnivore.items.delete({ id: articleId })

return false
return true
}
Loading

0 comments on commit 5dee7c2

Please sign in to comment.