11import { URLSearchParams , URL } from 'url' ;
2- import { z } from 'zod' ;
2+ import { z , ZodSchema } from 'zod' ;
33
44import axios , { AxiosRequestConfig } from 'axios' ;
55import { loginResponseSchema } from './types' ;
@@ -28,10 +28,10 @@ function isError(payload: unknown): payload is ApiError {
2828 return payload && typeof payload == 'object' && payload . error ;
2929}
3030
31- async function performRequest < TValues > (
32- config : AxiosRequestConfig ,
33- schema : z . Schema < TValues > ,
34- ) : Promise < TValues > {
31+ async function performRequest <
32+ TSchema extends ZodSchema ,
33+ KValues extends z . infer < TSchema > ,
34+ > ( config : AxiosRequestConfig , schema : TSchema ) : Promise < KValues > {
3535 try {
3636 let response = await axios . request ( {
3737 ...config ,
@@ -67,10 +67,10 @@ export abstract class BugzillaLink {
6767 this . instance = new URL ( 'rest/' , instance ) ;
6868 }
6969
70- protected abstract request < TValues > (
71- config : AxiosRequestConfig ,
72- schema : z . Schema < TValues > ,
73- ) : Promise < TValues > ;
70+ protected abstract request <
71+ TSchema extends ZodSchema ,
72+ KValues extends z . infer < TSchema > ,
73+ > ( config : AxiosRequestConfig , schema : TSchema ) : Promise < KValues > ;
7474
7575 protected buildURL ( path : string , query ?: SearchParams ) : URL {
7676 let url = new URL ( path , this . instance ) ;
@@ -80,11 +80,11 @@ export abstract class BugzillaLink {
8080 return url ;
8181 }
8282
83- public async get < TValues > (
83+ public async get < TSchema extends ZodSchema , KValues extends z . infer < TSchema > > (
8484 path : string ,
85- schema : z . Schema < TValues > ,
85+ schema : TSchema ,
8686 searchParams ?: SearchParams ,
87- ) : Promise < TValues > {
87+ ) : Promise < KValues > {
8888 return this . request (
8989 {
9090 url : this . buildURL ( path , searchParams ) . toString ( ) ,
@@ -93,12 +93,16 @@ export abstract class BugzillaLink {
9393 ) ;
9494 }
9595
96- public async post < R , TValues > (
96+ public async post <
97+ R ,
98+ TSchema extends ZodSchema ,
99+ KValues extends z . infer < TSchema > ,
100+ > (
97101 path : string ,
98- schema : z . Schema < TValues > ,
102+ schema : TSchema ,
99103 content : R ,
100104 searchParams ?: SearchParams ,
101- ) : Promise < TValues > {
105+ ) : Promise < KValues > {
102106 return this . request (
103107 {
104108 url : this . buildURL ( path , searchParams ) . toString ( ) ,
@@ -112,12 +116,16 @@ export abstract class BugzillaLink {
112116 ) ;
113117 }
114118
115- public async put < R , TValues > (
119+ public async put <
120+ R ,
121+ TSchema extends ZodSchema ,
122+ KValues extends z . infer < TSchema > ,
123+ > (
116124 path : string ,
117- schema : z . Schema < TValues > ,
125+ schema : TSchema ,
118126 content : R ,
119127 searchParams ?: SearchParams ,
120- ) : Promise < TValues > {
128+ ) : Promise < KValues > {
121129 return this . request (
122130 {
123131 url : this . buildURL ( path , searchParams ) . toString ( ) ,
@@ -133,10 +141,10 @@ export abstract class BugzillaLink {
133141}
134142
135143export class PublicLink extends BugzillaLink {
136- protected async request < TValues > (
137- config : AxiosRequestConfig ,
138- schema : z . Schema < TValues > ,
139- ) : Promise < TValues > {
144+ protected async request <
145+ TSchema extends ZodSchema ,
146+ KValues extends z . infer < TSchema > ,
147+ > ( config : AxiosRequestConfig , schema : TSchema ) : Promise < KValues > {
140148 return performRequest ( config , schema ) ;
141149 }
142150}
@@ -152,10 +160,10 @@ export class ApiKeyLink extends BugzillaLink {
152160 super ( instance ) ;
153161 }
154162
155- protected async request < TValues > (
156- config : AxiosRequestConfig ,
157- schema : z . Schema < TValues > ,
158- ) : Promise < TValues > {
163+ protected async request <
164+ TSchema extends ZodSchema ,
165+ KValues extends z . infer < TSchema > ,
166+ > ( config : AxiosRequestConfig , schema : TSchema ) : Promise < KValues > {
159167 return performRequest (
160168 {
161169 ...config ,
@@ -201,10 +209,10 @@ export class PasswordLink extends BugzillaLink {
201209 return loginInfo . token ;
202210 }
203211
204- protected async request < TValues > (
205- config : AxiosRequestConfig ,
206- schema : z . Schema < TValues > ,
207- ) : Promise < TValues > {
212+ protected async request <
213+ TSchema extends ZodSchema ,
214+ KValues extends z . infer < TSchema > ,
215+ > ( config : AxiosRequestConfig , schema : TSchema ) : Promise < KValues > {
208216 if ( ! this . token ) {
209217 this . token = await this . login ( ) ;
210218 }
0 commit comments