Skip to content
This repository was archived by the owner on Nov 15, 2024. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion .github/workflows/npm-semantic-release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,4 @@ jobs:
env:
NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
SENTRY_DSN: ${{ secrets.SENTRY_DSN }}
run: npx semantic-release
46 changes: 3 additions & 43 deletions docs/classes/Seam.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,12 @@
- [devices](Seam.md#devices)
- [events](Seam.md#events)
- [locks](Seam.md#locks)
- [shouldReportExceptions](Seam.md#shouldreportexceptions)
- [webhooks](Seam.md#webhooks)
- [workspaces](Seam.md#workspaces)

### Methods

- [makeRequest](Seam.md#makerequest)
- [wrapThrowable](Seam.md#wrapthrowable)

## Constructors

Expand All @@ -51,7 +49,7 @@ Routes.constructor

#### Defined in

[src/client.ts:52](https://github.com/seamapi/javascript/blob/main/src/client.ts#L52)
[src/client.ts:44](https://github.com/seamapi/javascript/blob/main/src/client.ts#L44)

## Properties

Expand Down Expand Up @@ -105,7 +103,7 @@ ___

#### Defined in

[src/client.ts:49](https://github.com/seamapi/javascript/blob/main/src/client.ts#L49)
[src/client.ts:42](https://github.com/seamapi/javascript/blob/main/src/client.ts#L42)

___

Expand Down Expand Up @@ -220,16 +218,6 @@ Routes.locks

___

### shouldReportExceptions

• `Private` **shouldReportExceptions**: `boolean`

#### Defined in

[src/client.ts:50](https://github.com/seamapi/javascript/blob/main/src/client.ts#L50)

___

### webhooks

• `Readonly` **webhooks**: `Object`
Expand Down Expand Up @@ -301,32 +289,4 @@ Routes.makeRequest

#### Defined in

[src/client.ts:110](https://github.com/seamapi/javascript/blob/main/src/client.ts#L110)

___

### wrapThrowable

▸ `Private` **wrapThrowable**<`T`\>(`fn`): `Promise`<`T`\>

Reports thrown exceptions to Sentry, if enabled.

#### Type parameters

| Name |
| :------ |
| `T` |

#### Parameters

| Name | Type |
| :------ | :------ |
| `fn` | () => `T` |

#### Returns

`Promise`<`T`\>

#### Defined in

[src/client.ts:140](https://github.com/seamapi/javascript/blob/main/src/client.ts#L140)
[src/client.ts:82](https://github.com/seamapi/javascript/blob/main/src/client.ts#L82)
23 changes: 4 additions & 19 deletions docs/interfaces/SeamClientOptions.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
- [apiKey](SeamClientOptions.md#apikey)
- [axiosOptions](SeamClientOptions.md#axiosoptions)
- [endpoint](SeamClientOptions.md#endpoint)
- [shouldReportExceptions](SeamClientOptions.md#shouldreportexceptions)
- [workspaceId](SeamClientOptions.md#workspaceid)

## Properties
Expand All @@ -20,7 +19,7 @@

#### Defined in

[src/client.ts:11](https://github.com/seamapi/javascript/blob/main/src/client.ts#L11)
[src/client.ts:10](https://github.com/seamapi/javascript/blob/main/src/client.ts#L10)

___

Expand All @@ -32,7 +31,7 @@ Extended options to pass to Axios

#### Defined in

[src/client.ts:29](https://github.com/seamapi/javascript/blob/main/src/client.ts#L29)
[src/client.ts:23](https://github.com/seamapi/javascript/blob/main/src/client.ts#L23)

___

Expand All @@ -44,21 +43,7 @@ Seam Endpoint to use, defaults to https://connect.getseam.com

#### Defined in

[src/client.ts:15](https://github.com/seamapi/javascript/blob/main/src/client.ts#L15)

___

### shouldReportExceptions

• `Optional` **shouldReportExceptions**: `boolean`

If true, SDK exceptions will be automatically reported to Seam. This defaults to `false` and is opt-in.

**`default`** false

#### Defined in

[src/client.ts:25](https://github.com/seamapi/javascript/blob/main/src/client.ts#L25)
[src/client.ts:14](https://github.com/seamapi/javascript/blob/main/src/client.ts#L14)

___

Expand All @@ -71,4 +56,4 @@ or undefined

#### Defined in

[src/client.ts:20](https://github.com/seamapi/javascript/blob/main/src/client.ts#L20)
[src/client.ts:19](https://github.com/seamapi/javascript/blob/main/src/client.ts#L19)
2 changes: 1 addition & 1 deletion docs/modules.md
Original file line number Diff line number Diff line change
Expand Up @@ -432,4 +432,4 @@ ___

#### Defined in

[src/client.ts:32](https://github.com/seamapi/javascript/blob/main/src/client.ts#L32)
[src/client.ts:26](https://github.com/seamapi/javascript/blob/main/src/client.ts#L26)
2 changes: 0 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@
"cjs-wrapper.js"
],
"dependencies": {
"@sentry/node": "7.15.0",
"axios": "0.25.0",
"axios-retry": "3.2.5",
"change-case": "4.1.2",
Expand Down Expand Up @@ -86,7 +85,6 @@
"prettier": "2.5.1",
"seamapi-types": "1.3.10",
"semantic-release": "19.0.2",
"sentry-testkit": "4.1.0",
"testcontainers": "8.4.0",
"ts-json-schema-generator": "0.98.0",
"tsup": "5.11.11",
Expand Down
84 changes: 16 additions & 68 deletions src/client.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import axios, { AxiosInstance, AxiosRequestConfig } from "axios"
import axiosRetry from "axios-retry"
import * as Sentry from "@sentry/node"
import { SeamAPIError, SeamMalformedInputError } from "./lib/api-error"
import { Routes } from "./routes"
import { ErroredAPIResponse, SuccessfulAPIResponse } from "./types/globals"
import { version, repository } from "../package.json"
import { version } from "../package.json"

export interface SeamClientOptions {
/* Seam API Key */
Expand All @@ -18,11 +17,6 @@ export interface SeamClientOptions {
* or undefined
**/
workspaceId?: string
/**
* If true, SDK exceptions will be automatically reported to Seam. This defaults to `false` and is opt-in.
* @default false
*/
shouldReportExceptions?: boolean
/**
* Extended options to pass to Axios
*/
Expand All @@ -36,7 +30,6 @@ export const getSeamClientOptionsWithDefaults = (
apiKey: process?.env?.SEAM_API_KEY,
endpoint: process?.env?.SEAM_API_URL || "https://connect.getseam.com",
workspaceId: process?.env?.SEAM_WORKSPACE_ID,
shouldReportExceptions: false,
}
if (typeof apiKeyOrOptions === "string") {
return { ...seamClientDefaults, apiKey: apiKeyOrOptions }
Expand All @@ -47,18 +40,12 @@ export const getSeamClientOptionsWithDefaults = (

export class Seam extends Routes {
public client: AxiosInstance
private shouldReportExceptions: boolean

constructor(apiKeyOrOptions?: string | SeamClientOptions) {
super()

const {
apiKey,
endpoint,
workspaceId,
axiosOptions,
shouldReportExceptions,
} = getSeamClientOptionsWithDefaults(apiKeyOrOptions)
const { apiKey, endpoint, workspaceId, axiosOptions } =
getSeamClientOptionsWithDefaults(apiKeyOrOptions)

const isRegularAPIKey = apiKey?.startsWith("seam_")

Expand All @@ -79,7 +66,7 @@ export class Seam extends Routes {
headers: {
...axiosOptions?.headers,
Authorization: `Bearer ${apiKey}`,
["User-Agent"]: `Javascript SDK v${version} (${repository})`,
["User-Agent"]: `Javascript SDK v${version} (https://github.com/seamapi/javascript)`,

// only needed for session key authentication
...(!workspaceId ? {} : { "Seam-Workspace": workspaceId }),
Expand All @@ -90,66 +77,27 @@ export class Seam extends Routes {
retries: 2,
retryDelay: axiosRetry.exponentialDelay,
})

this.shouldReportExceptions = shouldReportExceptions ?? false
if (this.shouldReportExceptions) {
Sentry.init({
// Replaced during build
dsn: process.env.SENTRY_DSN,
defaultIntegrations: false,
})

Sentry.setContext("sdk_info", {
repository,
version,
endpoint,
})
}
}

public async makeRequest<T>(
request: AxiosRequestConfig
): Promise<SuccessfulAPIResponse<T>> {
return await this.wrapThrowable(async () => {
try {
const response = await this.client.request(request)
return response.data
} catch (error) {
if (axios.isAxiosError(error) && error.response) {
if (error.response.data.error?.type === "invalid_input") {
throw new SeamMalformedInputError(
error.response.data.error.validation_errors
)
}

throw new SeamAPIError(
error.response.status,
error.response.headers["seam-request-id"],
(error.response.data as ErroredAPIResponse).error
try {
const response = await this.client.request(request)
return response.data
} catch (error) {
if (axios.isAxiosError(error) && error.response) {
if (error.response.data.error?.type === "invalid_input") {
throw new SeamMalformedInputError(
error.response.data.error.validation_errors
)
}

throw error
}
})
}

/**
* Reports thrown exceptions to Sentry, if enabled.
*/
private async wrapThrowable<T>(fn: () => T): Promise<T> {
try {
return await fn()
} catch (error) {
if (
this.shouldReportExceptions &&
// Only capture unknown errors
!(
error instanceof SeamMalformedInputError ||
error instanceof SeamAPIError
throw new SeamAPIError(
error.response.status,
error.response.headers["seam-request-id"],
(error.response.data as ErroredAPIResponse).error
)
) {
Sentry.captureException(error)
}

throw error
Expand Down
Loading