@@ -5,83 +5,92 @@ import { SeamHttpActionAttempts } from './index.js'
5
5
import type { SeamHttpRequestOptions } from './options.js'
6
6
import { resolveActionAttempt } from './resolve-action-attempt.js'
7
7
8
- export interface SeamHttpRequestParent {
8
+ interface SeamHttpRequestParent {
9
9
readonly client : Client
10
10
readonly defaults : Required < SeamHttpRequestOptions >
11
11
}
12
12
13
- export interface SeamHttpRequestConfig < TBody > {
14
- url ?: string
15
- method ?: Method
16
- params ?: any
17
- data ?: TBody
13
+ export interface SeamHttpRequestConfig < TBody , TResponseKey > {
14
+ readonly url ?: string
15
+ readonly method ?: Method
16
+ readonly params ?: any
17
+ readonly data ?: TBody
18
+ readonly responseKey : TResponseKey
19
+ readonly options : Pick < SeamHttpRequestOptions , 'waitForActionAttempt' >
18
20
}
19
21
20
22
export type ResponseFromSeamHttpRequest < T > =
21
- T extends SeamHttpRequest < any , infer TResponse , infer TResourceKey >
22
- ? TResourceKey extends keyof TResponse
23
- ? TResponse [ TResourceKey ]
23
+ T extends SeamHttpRequest < any , infer TResponse , infer TResponseKey >
24
+ ? TResponseKey extends keyof TResponse
25
+ ? TResponse [ TResponseKey ]
24
26
: undefined
25
27
: never
26
28
27
29
export class SeamHttpRequest <
28
30
const TBody ,
29
31
const TResponse ,
30
- const TResourceKey extends keyof TResponse | undefined ,
32
+ const TResponseKey extends keyof TResponse | undefined ,
31
33
> implements
32
34
PromiseLike <
33
- TResourceKey extends keyof TResponse ? TResponse [ TResourceKey ] : undefined
35
+ TResponseKey extends keyof TResponse ? TResponse [ TResponseKey ] : undefined
34
36
>
35
37
{
36
- readonly parent : SeamHttpRequestParent
37
- readonly config : SeamHttpRequestConfig < TBody >
38
- readonly resourceKey : TResourceKey
39
- readonly options : Pick < SeamHttpRequestOptions , 'waitForActionAttempt' >
38
+ readonly #parent: SeamHttpRequestParent
39
+ readonly #config: SeamHttpRequestConfig < TBody , TResponseKey >
40
40
41
41
constructor (
42
42
parent : SeamHttpRequestParent ,
43
- config : SeamHttpRequestConfig < TBody > ,
44
- resourceKey : TResourceKey ,
45
- options : Pick < SeamHttpRequestOptions , 'waitForActionAttempt' > = { } ,
43
+ config : SeamHttpRequestConfig < TBody , TResponseKey > ,
46
44
) {
47
- this . parent = parent
48
- this . config = config
49
- this . resourceKey = resourceKey
50
- this . options = options
45
+ this . #parent = parent
46
+ this . #config = config
47
+ }
48
+
49
+ public get responseKey ( ) : TResponseKey {
50
+ return this . #config. responseKey
51
51
}
52
52
53
53
public get url ( ) : string {
54
- return this . config . url ?? ''
54
+ return this . # config. url ?? ''
55
55
}
56
56
57
57
public get method ( ) : Method {
58
- return this . config . method ?? 'get'
58
+ return this . #config. method ?? 'get'
59
+ }
60
+
61
+ public get params ( ) {
62
+ return this . #config. params
59
63
}
60
64
61
65
public get data ( ) : TBody {
62
- return this . config . data as TBody
66
+ return this . # config. data as TBody
63
67
}
64
68
65
69
async execute ( ) : Promise <
66
- TResourceKey extends keyof TResponse ? TResponse [ TResourceKey ] : undefined
70
+ TResponseKey extends keyof TResponse ? TResponse [ TResponseKey ] : undefined
67
71
> {
68
- const { client } = this . parent
69
- const response = await client . request ( this . config )
70
- if ( this . resourceKey === undefined ) {
71
- return undefined as TResourceKey extends keyof TResponse
72
- ? TResponse [ TResourceKey ]
72
+ const { client } = this . #parent
73
+ const response = await client . request ( {
74
+ url : this . url ,
75
+ method : this . method ,
76
+ params : this . params ,
77
+ data : this . data ,
78
+ } )
79
+ if ( this . responseKey === undefined ) {
80
+ return undefined as TResponseKey extends keyof TResponse
81
+ ? TResponse [ TResponseKey ]
73
82
: undefined
74
83
}
75
- const data = response . data [ this . resourceKey ]
76
- if ( this . resourceKey === 'action_attempt' ) {
84
+ const data = response . data [ this . responseKey ]
85
+ if ( this . responseKey === 'action_attempt' ) {
77
86
const waitForActionAttempt =
78
- this . options . waitForActionAttempt ??
79
- this . parent . defaults . waitForActionAttempt
87
+ this . #config . options . waitForActionAttempt ??
88
+ this . # parent. defaults . waitForActionAttempt
80
89
if ( waitForActionAttempt !== false ) {
81
90
return await resolveActionAttempt (
82
91
data ,
83
92
SeamHttpActionAttempts . fromClient ( client , {
84
- ...this . parent . defaults ,
93
+ ...this . # parent. defaults ,
85
94
waitForActionAttempt : false ,
86
95
} ) ,
87
96
typeof waitForActionAttempt === 'boolean' ? { } : waitForActionAttempt ,
@@ -92,15 +101,15 @@ export class SeamHttpRequest<
92
101
}
93
102
94
103
then <
95
- TResult1 = TResourceKey extends keyof TResponse
96
- ? TResponse [ TResourceKey ]
104
+ TResult1 = TResponseKey extends keyof TResponse
105
+ ? TResponse [ TResponseKey ]
97
106
: undefined ,
98
107
TResult2 = never ,
99
108
> (
100
109
onfulfilled ?:
101
110
| ( (
102
- value : TResourceKey extends keyof TResponse
103
- ? TResponse [ TResourceKey ]
111
+ value : TResponseKey extends keyof TResponse
112
+ ? TResponse [ TResponseKey ]
104
113
: undefined ,
105
114
) => TResult1 | PromiseLike < TResult1 > )
106
115
| null
0 commit comments