Skip to content

Commit

Permalink
update README
Browse files Browse the repository at this point in the history
  • Loading branch information
sywhb committed Mar 7, 2024
1 parent 41022ae commit 575a8f7
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 2 deletions.
48 changes: 48 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,51 @@
# Omnivore API

Omnivore API Client Library for Node.js

![Build status](https://github.com/omnivore-app/omnivore-api/actions/workflows/ci.yml/badge.svg)

## Installation

```bash
npm install @omnivore/api
```

## Usage

Import the `Omnivore` class and create a new instance with your `authToken` and `baseUrl`. Then you can use the instance to make requests to the Omnivore API.

```javascript
import { Omnivore } from '@omnivore/api'

const omnivore = new Omnivore({
authToken: 'your-auth-token',
baseUrl: 'https://api-prod.omnivore.app',
})

const items = await omnivore.search({
// ...
})
```

## Error handling

The library will throw an error if the request fails. You can catch the error and handle it as needed.

```javascript
try {
const items = await omnivore.search({
// ...
})
} catch (error: unknown) {
if (isOmnivoreError(error)) {
switch (error.code) {
case OmnivoreErrorCode.GraphQLError:
// Handle GraphQL error
case OmnivoreErrorCode.NetworkError:
// Handle network error
default:
// Handle other errors
}
}
}
```
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@
"eslint-config-prettier": "^9.1.0",
"eslint-plugin-prettier": "^5.1.3",
"prettier": "^3.2.5",
"typescript": "^5.3.3"
"typescript": "^5.3.3",
"react": "^17.0.2"
},
"volta": {
"node": "18.16.1",
Expand Down
8 changes: 8 additions & 0 deletions src/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,11 @@ export const buildOmnivoreError = (

return new GraphQLError(data.errorCodes)
}

export const isOmnivoreError = (error: any): error is OmnivoreError => {
return (
error instanceof GraphQLError ||
error instanceof NetworkError ||
error instanceof UnknownError
)
}
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,4 @@ export {
UpdatesSinceResponse,
} from './client'

export { OmnivoreError, OmnivoreErrorCode } from './errors'
export { OmnivoreError, OmnivoreErrorCode, isOmnivoreError } from './errors'

0 comments on commit 575a8f7

Please sign in to comment.