Skip to content

Commit

Permalink
Merge pull request #41 from mondaycom/feature/romka/seamless-api-erro…
Browse files Browse the repository at this point in the history
…r-handling

seamless api client error upgrade
  • Loading branch information
RomKadria authored Nov 12, 2024
2 parents 14d9a54 + c43cbc0 commit 106e27c
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 7 deletions.
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions packages/api/CHANGELOG.MD
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Changelog

## [7.0.0]

### ⚠️ Breaking Changes

- **SeamlessApiClient error type**: The `SeamlessApiClient` class now aligns its error format with the ApiClient class. Errors are now nested under response.errors. Use the SeamlessApiClientError type provided by the package to handle these errors.

## [6.0.0]

### ⚠️ Breaking Changes
Expand Down
17 changes: 13 additions & 4 deletions packages/api/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -171,12 +171,15 @@ Basically, when you are making an api call from the client side of an app deploy
```typescript
import {
Board,
} from "@mondaycom/api";
} from "@mondaydotcomorg/api";

const { boards } = await SeamlessApiClient.request<{boards: [Board];}>(`query { boards(ids: some_id) { id name } }`);
// Option A - using pre defined types
const seamlessApiClient = new SeamlessApiClient();
const { boards } = await client.request<{boards: [Board];}>(`query { boards(ids: some_id) { id name } }`);

// or using your own types after integrating with @mondaycom/setup-api
// Option B - using your own types after integrating with @mondaydotcomorg/setup-api
import { GetBoardsQueryVariables, GetBoardsQuery } from "./generated/graphql";
const seamlessApiClient = new SeamlessApiClient();
const variables: GetBoardsQueryVariables = { ids: ["some_id"] };

export const getBoards = gql`
Expand All @@ -188,7 +191,13 @@ export const getBoards = gql`
}
`;

const data = await SeamlessApiClient.request<GetBoardsQuery>(getBoards, variables);
try {
const data = await seamlessApiClient.request<GetBoardsQuery>(getBoards, variables);
} catch (error) {
// If the error is from SeamlessApiClient, it will be of type SeamlessApiClientError, which you can import from our package. Also, error.type will also 'SeamlessApiClientError'.

console.log(error.response.errors)
}
```

### Type support
Expand Down
11 changes: 11 additions & 0 deletions packages/api/lib/errors/seamless-api-client-error.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
export class SeamlessApiClientError extends Error {
response: { errors: any };
type: string;

constructor(message: string, errors: any) {
super(message);
this.response = { errors };
this.name = this.constructor.name;
this.type = 'SeamlessApiClientError';
}
}
6 changes: 5 additions & 1 deletion packages/api/lib/seamless-api-client.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import { ApiVersionType, DEFAULT_VERSION, QueryVariables } from './constants';
import { SeamlessApiClientError } from './errors/seamless-api-client-error';

export { SeamlessApiClientError };

interface ListenerCallback {
(data: any): void;
Expand Down Expand Up @@ -65,7 +68,8 @@ export class SeamlessApiClient {
clearTimeout(timeoutId);
removeListener();
if (data.errorMessage) {
reject(new Error(`${data.errorMessage} - ${data.data}`));
const error = new SeamlessApiClientError(data.errorMessage, data.data.errors);
reject(error);
} else {
resolve(data as T);
}
Expand Down
2 changes: 1 addition & 1 deletion packages/api/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@mondaydotcomorg/api",
"version": "6.0.0",
"version": "7.0.2",
"description": "monday.com API client",
"main": "dist/cjs/index.js",
"module": "dist/esm/index.js",
Expand Down

0 comments on commit 106e27c

Please sign in to comment.