Skip to content

Commit

Permalink
update api
Browse files Browse the repository at this point in the history
  • Loading branch information
sywhb committed Mar 7, 2024
1 parent 575a8f7 commit 02619fe
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 65 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ const omnivore = new Omnivore({
baseUrl: 'https://api-prod.omnivore.app',
})

const items = await omnivore.search({
const items = await omnivore.items.search({
// ...
})
```
Expand All @@ -33,7 +33,7 @@ The library will throw an error if the request fails. You can catch the error an

```javascript
try {
const items = await omnivore.search({
const items = await omnivore.items.search({
// ...
})
} catch (error: unknown) {
Expand Down
134 changes: 71 additions & 63 deletions src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -299,10 +299,10 @@ export interface SaveByURLResponse {
}

export class Omnivore {
_client: Client
private client: Client

constructor(clientOptions: ClientOptions) {
this._client = new Client({
this.client = new Client({
url: `${clientOptions.baseUrl}/api/graphql`,
exchanges: [fetchExchange],
fetchOptions: () => ({
Expand All @@ -314,73 +314,81 @@ export class Omnivore {
})
}

async search(params: SearchParameters): Promise<SearchResponse> {
const { data, error } = await this._client
.query(SearchQuery, params)
.toPromise()
const search = data?.search
if (error || !search || search.__typename === 'SearchError') {
const err = buildOmnivoreError(search, error)
console.error('Search error', err)
throw err
}
public readonly items = {
search: async (params: SearchParameters): Promise<SearchResponse> => {
const { data, error } = await this.client
.query(SearchQuery, params)
.toPromise()

return search
}
const search = data?.search
if (error || !search || search.__typename === 'SearchError') {
const err = buildOmnivoreError(search, error)
console.error('Search error', err)
throw err
}

async updatesSince(since: string): Promise<UpdatesSinceResponse> {
const { data, error } = await this._client
.query(UpdatesSinceQuery, { since })
.toPromise()
const updatesSince = data?.updatesSince
if (
error ||
!updatesSince ||
updatesSince.__typename === 'UpdatesSinceError'
) {
const err = buildOmnivoreError(updatesSince, error)
console.error('UpdatesSince error', err)
throw err
}
return search
},

return updatesSince
}
updates: async (since: string): Promise<UpdatesSinceResponse> => {
const { data, error } = await this.client
.query(UpdatesSinceQuery, { since })
.toPromise()

async delete(id: string): Promise<DeleteResponse> {
const { data, error } = await this._client
.mutation(DeleteMutation, { input: { articleID: id, bookmark: false } })
.toPromise()
const deleteArticle = data?.setBookmarkArticle
if (
error ||
!deleteArticle ||
deleteArticle.__typename === 'SetBookmarkArticleError'
) {
const err = buildOmnivoreError(deleteArticle, error)
console.error('Delete error', err)
throw err
}
const updatesSince = data?.updatesSince
if (
error ||
!updatesSince ||
updatesSince.__typename === 'UpdatesSinceError'
) {
const err = buildOmnivoreError(updatesSince, error)
console.error('UpdatesSince error', err)
throw err
}

return { id: deleteArticle.bookmarkedArticle.id }
}
return updatesSince
},

async saveByURL(params: SaveByURLParameters): Promise<SaveByURLResponse> {
const { data, error } = await this._client
.mutation(saveByURLMutation, {
input: {
...params,
source: params.source || 'API-Client',
clientRequestId: params.clientRequestId || '',
},
})
.toPromise()
const saveUrl = data?.saveUrl
if (error || !saveUrl || saveUrl.__typename === 'SaveError') {
const err = buildOmnivoreError(saveUrl, error)
console.error('SaveByURL error', err)
throw err
}
delete: async (id: string): Promise<DeleteResponse> => {
const { data, error } = await this.client
.mutation(DeleteMutation, { input: { articleID: id, bookmark: false } })
.toPromise()

const deleteArticle = data?.setBookmarkArticle
if (
error ||
!deleteArticle ||
deleteArticle.__typename === 'SetBookmarkArticleError'
) {
const err = buildOmnivoreError(deleteArticle, error)
console.error('Delete error', err)
throw err
}

return { id: deleteArticle.bookmarkedArticle.id }
},

saveByUrl: async (
params: SaveByURLParameters,
): Promise<SaveByURLResponse> => {
const { data, error } = await this.client
.mutation(saveByURLMutation, {
input: {
...params,
source: params.source || 'API-Client',
clientRequestId: params.clientRequestId || '',
},
})
.toPromise()

const saveUrl = data?.saveUrl
if (error || !saveUrl || saveUrl.__typename === 'SaveError') {
const err = buildOmnivoreError(saveUrl, error)
console.error('SaveByURL error', err)
throw err
}

return { id: saveUrl.clientRequestId }
return { id: saveUrl.clientRequestId }
},
}
}

0 comments on commit 02619fe

Please sign in to comment.