Skip to content
This repository was archived by the owner on Nov 15, 2024. It is now read-only.

Commit 2bd1a40

Browse files
committed
feat: Reuse makeRequest
1 parent fe72bad commit 2bd1a40

2 files changed

Lines changed: 29 additions & 34 deletions

File tree

docs/classes/Seam.md

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/seam-connect/client.ts

Lines changed: 28 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import axios, {
2+
Axios,
23
AxiosInstance,
34
AxiosRequestConfig,
45
AxiosRequestHeaders,
5-
AxiosResponse,
66
} from "axios"
77
import axiosRetry from "axios-retry"
88
import { SeamAPIError, SeamMalformedInputError } from "../lib/api-error"
@@ -13,7 +13,7 @@ import {
1313
SuccessfulAPIResponse,
1414
} from "../types/globals"
1515
import { version } from "../../package.json"
16-
import { ClientSession, ClientSessionResponse } from "../types"
16+
import { ClientSessionResponse } from "../types"
1717

1818
export interface SeamClientOptions {
1919
/* Seam API Key */
@@ -137,26 +137,7 @@ export class Seam extends Routes {
137137
public async makeRequest<T>(
138138
request: AxiosRequestConfig
139139
): Promise<SuccessfulAPIResponse<T>> {
140-
try {
141-
const response = await this.client.request(request)
142-
return response.data
143-
} catch (error) {
144-
if (axios.isAxiosError(error) && error.response) {
145-
if (error.response.data.error?.type === "invalid_input") {
146-
throw new SeamMalformedInputError(
147-
error.response.data.error.validation_errors
148-
)
149-
}
150-
151-
throw new SeamAPIError(
152-
error.response.status,
153-
error.response.headers["seam-request-id"],
154-
(error.response.data as ErroredAPIResponse).error
155-
)
156-
}
157-
158-
throw error
159-
}
140+
return makeRequest<T>(this.client, request)
160141
}
161142

162143
static async getClientSessionToken(options: {
@@ -203,22 +184,36 @@ export class Seam extends Routes {
203184
baseURL: endpoint,
204185
headers,
205186
})
206-
try {
207-
const response: AxiosResponse & {
208-
data: { client_session: ClientSession }
209-
} = await client.post(
210-
"/internal/client_sessions/create",
211-
{},
212-
// { headers: { "seam-sdk-version": version } }
213-
{}
214-
)
215-
return await response.data
216-
} catch (error: any) {
187+
188+
return makeRequest(client, {
189+
method: "POST",
190+
url: "/internal/client_sessions/create",
191+
})
192+
}
193+
}
194+
195+
const makeRequest = async <T>(
196+
client: Axios,
197+
request: AxiosRequestConfig
198+
): Promise<SuccessfulAPIResponse<T>> => {
199+
try {
200+
const response = await client.request(request)
201+
return response.data
202+
} catch (error) {
203+
if (axios.isAxiosError(error) && error.response) {
204+
if (error.response.data.error?.type === "invalid_input") {
205+
throw new SeamMalformedInputError(
206+
error.response.data.error.validation_errors
207+
)
208+
}
209+
217210
throw new SeamAPIError(
218211
error.response.status,
219212
error.response.headers["seam-request-id"],
220213
(error.response.data as ErroredAPIResponse).error
221214
)
222215
}
216+
217+
throw error
223218
}
224219
}

0 commit comments

Comments
 (0)