Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

integrate with omnivore api client #204

Merged
merged 5 commits into from
Mar 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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"
}
}
218 changes: 24 additions & 194 deletions src/api.ts
Original file line number Diff line number Diff line change
@@ -1,215 +1,45 @@
import { requestUrl } from 'obsidian'
import { Item, ItemFormat, Omnivore } from '@omnivore-app/api'

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
}

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({
authToken: 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({
authToken: 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
Loading