generated from obsidianmd/obsidian-sample-plugin
-
Notifications
You must be signed in to change notification settings - Fork 92
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
integrate with omnivore api client (#204)
* integrate with omnivore api client * test api client library * update dependencies * fix dependecies * replace delete item by using api client
- Loading branch information
Showing
11 changed files
with
1,043 additions
and
1,435 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
Oops, something went wrong.