Skip to content

Commit 4ba810d

Browse files
committed
Make url, method, data properties of SeamHttpRequest
1 parent c65c834 commit 4ba810d

File tree

2 files changed

+31
-12
lines changed

2 files changed

+31
-12
lines changed

src/lib/seam/connect/seam-http-request.ts

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { AxiosRequestConfig } from 'axios'
1+
import type { Method } from 'axios'
22

33
import type { Client } from './client.js'
44
import { SeamHttpActionAttempts } from './index.js'
@@ -10,6 +10,13 @@ export interface SeamHttpRequestParent {
1010
readonly defaults: Required<SeamHttpRequestOptions>
1111
}
1212

13+
export interface SeamHttpRequestConfig<TBody> {
14+
url?: string
15+
method?: Method
16+
params?: any
17+
data?: TBody
18+
}
19+
1320
export type ResponseFromSeamHttpRequest<T> =
1421
T extends SeamHttpRequest<any, infer TResponse, infer TResourceKey>
1522
? TResourceKey extends keyof TResponse
@@ -27,27 +34,39 @@ export class SeamHttpRequest<
2734
>
2835
{
2936
readonly parent: SeamHttpRequestParent
30-
readonly requestConfig: AxiosRequestConfig<TBody>
37+
readonly config: SeamHttpRequestConfig<TBody>
3138
readonly resourceKey: TResourceKey
3239
readonly options: Pick<SeamHttpRequestOptions, 'waitForActionAttempt'>
3340

3441
constructor(
3542
parent: SeamHttpRequestParent,
36-
requestConfig: AxiosRequestConfig<TBody>,
43+
config: SeamHttpRequestConfig<TBody>,
3744
resourceKey: TResourceKey,
3845
options: Pick<SeamHttpRequestOptions, 'waitForActionAttempt'> = {},
3946
) {
4047
this.parent = parent
41-
this.requestConfig = requestConfig
48+
this.config = config
4249
this.resourceKey = resourceKey
4350
this.options = options
4451
}
4552

53+
public get url(): string {
54+
return this.config.url ?? ''
55+
}
56+
57+
public get method(): Method {
58+
return this.config.method ?? 'get'
59+
}
60+
61+
public get data(): TBody {
62+
return this.config.data as TBody
63+
}
64+
4665
async execute(): Promise<
4766
TResourceKey extends keyof TResponse ? TResponse[TResourceKey] : undefined
4867
> {
4968
const { client } = this.parent
50-
const response = await client.request(this.requestConfig)
69+
const response = await client.request(this.config)
5170
if (this.resourceKey === undefined) {
5271
return undefined as TResourceKey extends keyof TResponse
5372
? TResponse[TResourceKey]

test/seam/connect/client.test.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ import {
1111
} from '@seamapi/http/connect'
1212

1313
import {
14-
type ResponseFromSeamApiRequest,
15-
SeamApiRequest,
14+
type ResponseFromSeamHttpRequest,
15+
SeamHttpRequest,
1616
} from 'lib/seam/connect/seam-http-request.js'
1717

1818
test('SeamHttp: fromClient returns instance that uses client', async (t) => {
@@ -165,17 +165,17 @@ test.failing(
165165
},
166166
)
167167

168-
test('SeamHttp: request methods return a SeamApiRequest object', async (t) => {
168+
test('SeamHttp: request methods return a SeamHttpRequest object', async (t) => {
169169
const { seed, endpoint } = await getTestServer(t)
170170
const seam = new SeamHttp({
171171
client: SeamHttp.fromApiKey(seed.seam_apikey1_token, { endpoint }).client,
172172
})
173173

174174
const deviceRequest = seam.devices.get({ device_id: seed.august_device_1 })
175175

176-
t.true(deviceRequest instanceof SeamApiRequest)
177-
t.is(deviceRequest.requestConfig.url, '/devices/get')
178-
t.deepEqual(deviceRequest.requestConfig.data, {
176+
t.true(deviceRequest instanceof SeamHttpRequest)
177+
t.is(deviceRequest.url, '/devices/get')
178+
t.deepEqual(deviceRequest.data, {
179179
device_id: seed.august_device_1,
180180
})
181181
t.is(deviceRequest.resourceKey, 'device')
@@ -184,7 +184,7 @@ test('SeamHttp: request methods return a SeamApiRequest object', async (t) => {
184184
t.is(device.device_id, seed.august_device_1)
185185

186186
// Ensure that the type of the response is correct.
187-
type Expected = ResponseFromSeamApiRequest<typeof deviceRequest>
187+
type Expected = ResponseFromSeamHttpRequest<typeof deviceRequest>
188188

189189
const validDeviceType: Expected['device_type'] = 'august_lock'
190190
t.truthy(validDeviceType)

0 commit comments

Comments
 (0)