Skip to content

Commit

Permalink
make baseUrl optional
Browse files Browse the repository at this point in the history
  • Loading branch information
sywhb committed Mar 7, 2024
1 parent 02619fe commit 308dc53
Show file tree
Hide file tree
Showing 3 changed files with 185 additions and 175 deletions.
187 changes: 13 additions & 174 deletions src/client.ts
Original file line number Diff line number Diff line change
@@ -1,184 +1,18 @@
import { Client, fetchExchange } from 'urql'
import { buildOmnivoreError } from './errors'
import { graphql } from './graphql'
import {
DeleteMutation,
SearchQuery,
UpdatesSinceQuery,
saveByURLMutation,
} from './graphql'

export interface ClientOptions {
authToken: string
baseUrl: string
baseUrl?: string
timeoutMs?: number
}

const LabelFragment = graphql(`
fragment LabelFragment on Label @_unmask {
name
color
createdAt
id
internal
source
description
}
`)

const HighlightFragment = graphql(
`
fragment HighlightFragment on Highlight @_unmask {
id
quote
annotation
patch
updatedAt
labels {
...LabelFragment
}
type
highlightPositionPercent
color
highlightPositionAnchorIndex
prefix
suffix
createdAt
}
`,
[LabelFragment],
)

const SearchItemFragment = graphql(
`
fragment SearchItemFragment on SearchItem @_unmask {
id
title
siteName
originalArticleUrl
author
description
slug
labels {
...LabelFragment
}
highlights {
...HighlightFragment
}
updatedAt
savedAt
pageType
content
publishedAt
url
image
readAt
wordsCount
readingProgressPercent
isArchived
archivedAt
contentReader
}
`,
[LabelFragment, HighlightFragment],
)

const PageInfoFragment = graphql(`
fragment PageInfoFragment on PageInfo @_unmask {
hasNextPage
hasPreviousPage
startCursor
endCursor
totalCount
}
`)

const SearchQuery = graphql(
`
query Search(
$after: String
$first: Int
$format: String
$includeContent: Boolean
$query: String
) {
search(
after: $after
first: $first
format: $format
includeContent: $includeContent
query: $query
) {
__typename
... on SearchSuccess {
edges {
node {
...SearchItemFragment
}
}
pageInfo {
...PageInfoFragment
}
}
... on SearchError {
errorCodes
}
}
}
`,
[SearchItemFragment, PageInfoFragment],
)

const UpdatesSinceQuery = graphql(
`
query UpdatesSince($since: Date!) {
updatesSince(since: $since) {
__typename
... on UpdatesSinceSuccess {
edges {
itemID
updateReason
node {
...SearchItemFragment
}
}
pageInfo {
...PageInfoFragment
}
}
... on UpdatesSinceError {
errorCodes
}
}
}
`,
[SearchItemFragment, PageInfoFragment],
)

const DeleteMutation = graphql(`
mutation Delete($input: SetBookmarkArticleInput!) {
setBookmarkArticle(input: $input) {
__typename
... on SetBookmarkArticleSuccess {
bookmarkedArticle {
id
}
}
... on SetBookmarkArticleError {
errorCodes
}
}
}
`)

const saveByURLMutation = graphql(`
mutation SaveByURL($input: SaveUrlInput!) {
saveUrl(input: $input) {
__typename
... on SaveSuccess {
clientRequestId
}
... on SaveError {
errorCodes
}
}
}
`)

export type PageType =
| 'ARTICLE'
| 'BOOK'
Expand Down Expand Up @@ -303,7 +137,7 @@ export class Omnivore {

constructor(clientOptions: ClientOptions) {
this.client = new Client({
url: `${clientOptions.baseUrl}/api/graphql`,
url: `${clientOptions.baseUrl || 'https://api-prod.omnivore.app'}/api/graphql`,
exchanges: [fetchExchange],
fetchOptions: () => ({
headers: {
Expand All @@ -314,7 +148,9 @@ export class Omnivore {
})
}

// Omnivore API methods
public readonly items = {
// search for items
search: async (params: SearchParameters): Promise<SearchResponse> => {
const { data, error } = await this.client
.query(SearchQuery, params)
Expand All @@ -330,6 +166,7 @@ export class Omnivore {
return search
},

// get updates since a given date
updates: async (since: string): Promise<UpdatesSinceResponse> => {
const { data, error } = await this.client
.query(UpdatesSinceQuery, { since })
Expand All @@ -349,6 +186,7 @@ export class Omnivore {
return updatesSince
},

// delete an item
delete: async (id: string): Promise<DeleteResponse> => {
const { data, error } = await this.client
.mutation(DeleteMutation, { input: { articleID: id, bookmark: false } })
Expand All @@ -368,6 +206,7 @@ export class Omnivore {
return { id: deleteArticle.bookmarkedArticle.id }
},

// save an item by URL
saveByUrl: async (
params: SaveByURLParameters,
): Promise<SaveByURLResponse> => {
Expand Down
2 changes: 2 additions & 0 deletions src/errors.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { CombinedError } from 'urql'

// Error codes are used to identify the type of error that occurred
export enum OmnivoreErrorCode {
GraphQLError = 'GRAPHQL_ERROR',
NetworkError = 'NETWORK_ERROR',
Expand Down Expand Up @@ -31,6 +32,7 @@ class UnknownError extends OmnivoreErrorBase<OmnivoreErrorCode> {
}
}

// OmnivoreError is a union type of all possible errors
export type OmnivoreError = GraphQLError | NetworkError | UnknownError

export const buildOmnivoreError = (
Expand Down
Loading

0 comments on commit 308dc53

Please sign in to comment.