@@ -5,83 +5,92 @@ import { SeamHttpActionAttempts } from './index.js'
55import type { SeamHttpRequestOptions } from './options.js'
66import { resolveActionAttempt } from './resolve-action-attempt.js'
77
8- export interface SeamHttpRequestParent {
8+ interface SeamHttpRequestParent {
99 readonly client : Client
1010 readonly defaults : Required < SeamHttpRequestOptions >
1111}
1212
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' >
1820}
1921
2022export 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 ]
2426 : undefined
2527 : never
2628
2729export class SeamHttpRequest <
2830 const TBody ,
2931 const TResponse ,
30- const TResourceKey extends keyof TResponse | undefined ,
32+ const TResponseKey extends keyof TResponse | undefined ,
3133> implements
3234 PromiseLike <
33- TResourceKey extends keyof TResponse ? TResponse [ TResourceKey ] : undefined
35+ TResponseKey extends keyof TResponse ? TResponse [ TResponseKey ] : undefined
3436 >
3537{
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 >
4040
4141 constructor (
4242 parent : SeamHttpRequestParent ,
43- config : SeamHttpRequestConfig < TBody > ,
44- resourceKey : TResourceKey ,
45- options : Pick < SeamHttpRequestOptions , 'waitForActionAttempt' > = { } ,
43+ config : SeamHttpRequestConfig < TBody , TResponseKey > ,
4644 ) {
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
5151 }
5252
5353 public get url ( ) : string {
54- return this . config . url ?? ''
54+ return this . # config. url ?? ''
5555 }
5656
5757 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
5963 }
6064
6165 public get data ( ) : TBody {
62- return this . config . data as TBody
66+ return this . # config. data as TBody
6367 }
6468
6569 async execute ( ) : Promise <
66- TResourceKey extends keyof TResponse ? TResponse [ TResourceKey ] : undefined
70+ TResponseKey extends keyof TResponse ? TResponse [ TResponseKey ] : undefined
6771 > {
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 ]
7382 : undefined
7483 }
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' ) {
7786 const waitForActionAttempt =
78- this . options . waitForActionAttempt ??
79- this . parent . defaults . waitForActionAttempt
87+ this . #config . options . waitForActionAttempt ??
88+ this . # parent. defaults . waitForActionAttempt
8089 if ( waitForActionAttempt !== false ) {
8190 return await resolveActionAttempt (
8291 data ,
8392 SeamHttpActionAttempts . fromClient ( client , {
84- ...this . parent . defaults ,
93+ ...this . # parent. defaults ,
8594 waitForActionAttempt : false ,
8695 } ) ,
8796 typeof waitForActionAttempt === 'boolean' ? { } : waitForActionAttempt ,
@@ -92,15 +101,15 @@ export class SeamHttpRequest<
92101 }
93102
94103 then <
95- TResult1 = TResourceKey extends keyof TResponse
96- ? TResponse [ TResourceKey ]
104+ TResult1 = TResponseKey extends keyof TResponse
105+ ? TResponse [ TResponseKey ]
97106 : undefined ,
98107 TResult2 = never ,
99108 > (
100109 onfulfilled ?:
101110 | ( (
102- value : TResourceKey extends keyof TResponse
103- ? TResponse [ TResourceKey ]
111+ value : TResponseKey extends keyof TResponse
112+ ? TResponse [ TResponseKey ]
104113 : undefined ,
105114 ) => TResult1 | PromiseLike < TResult1 > )
106115 | null
0 commit comments