@@ -2,6 +2,7 @@ import { EndpointDefaults } from "./EndpointDefaults";
2
2
import { RequestOptions } from "./RequestOptions" ;
3
3
import { RequestParameters } from "./RequestParameters" ;
4
4
import { Route } from "./Route" ;
5
+ import { Url } from "./Url" ;
5
6
import { RequestMethod } from "./RequestMethod" ;
6
7
7
8
import { Endpoints } from "./generated/Endpoints" ;
@@ -21,33 +22,35 @@ type EndpointsByUrlAndMethod = UnionToIntersection<
21
22
url : TUrl ;
22
23
method : TMethod ;
23
24
} ;
24
- options : Endpoints [ K ] [ "parameters" ] & {
25
- url : TUrl ;
26
- method : TMethod ;
27
- } ;
25
+ parameters : Endpoints [ K ] [ "parameters" ] ;
28
26
request : Endpoints [ K ] [ "request" ] ;
29
27
} ;
30
28
} ;
31
29
} ;
32
30
} [ keyof Endpoints ]
33
31
> ;
34
32
35
- type UnknownEndpointParameters = RequestParameters & {
33
+ type UnknownRouteObject = {
36
34
method ?: RequestMethod ;
37
35
url : string ;
38
36
} ;
37
+ type UnknownEndpointParameters = RequestParameters & UnknownRouteObject ;
39
38
40
- type KnownOrUnknownEndpointParameters <
39
+ type KnownOrUnknownEndpointsByUrlAndMethod <
41
40
T extends UnknownEndpointParameters
42
41
> = T [ "url" ] extends keyof EndpointsByUrlAndMethod
43
42
? T [ "method" ] extends keyof EndpointsByUrlAndMethod [ T [ "url" ] ]
44
43
? EndpointsByUrlAndMethod [ T [ "url" ] ] [ T [ "method" ] ] extends {
45
- parameters : infer TOpt ;
44
+ parameters : infer TParams ;
45
+ request : infer TRequest ;
46
46
}
47
- ? TOpt
47
+ ? { parameters : TParams ; request : TRequest }
48
48
: never
49
49
: never
50
- : UnknownEndpointParameters ;
50
+ : {
51
+ parameters : UnknownEndpointParameters ;
52
+ request : RequestOptions ;
53
+ } ;
51
54
52
55
// https://stackoverflow.com/a/61281317/206879
53
56
type KnownOptions < T > = T extends {
@@ -58,19 +61,44 @@ type KnownOptions<T> = T extends {
58
61
? OptionValue
59
62
: never ;
60
63
61
- type KnownEndpoints = KnownOptions < EndpointsByUrlAndMethod > [ "route" ] ;
64
+ type KnownRouteObjects = KnownOptions < EndpointsByUrlAndMethod > [ "route" ] ;
65
+ type KnownRouteObject = { method : RequestMethod ; url : Url } ;
66
+ type RouteObjectFrom < T extends KnownRouteObject > = {
67
+ method : T [ "method" ] ;
68
+ url : T [ "url" ] ;
69
+ } ;
62
70
63
71
export interface EndpointInterface < D extends object = object > {
64
72
/**
65
73
* Transforms a GitHub REST API endpoint into generic request options
66
74
*
67
75
* @param {object } endpoint Must set `url` unless it's set defaults. Plus URL, query or body parameters, as well as `headers`, `mediaType.{format|previews}`, `request`, or `baseUrl`.
68
76
*/
69
- < O extends KnownEndpoints | UnknownEndpointParameters > (
70
- options : O & KnownOrUnknownEndpointParameters < O >
71
- ) : O extends KnownEndpoints
72
- ? EndpointsByUrlAndMethod [ O [ "url" ] ] [ O [ "method" ] ] [ "request" ]
73
- : RequestOptions ;
77
+ // WIP: does not allow optionsk for unknown routes, does not respect `D`
78
+ < O extends KnownRouteObjects > (
79
+ options : O &
80
+ KnownOrUnknownEndpointsByUrlAndMethod < O > [ "parameters" ] &
81
+ RequestParameters
82
+ ) : KnownOrUnknownEndpointsByUrlAndMethod < O > [ "request" ] ;
83
+
84
+ // WIP: does not validate required parameters for known route:
85
+ // <O extends RequestParameters>(
86
+ // options: O extends KnownRouteObject
87
+ // ? RouteObjectFrom<D & O> extends KnownRouteObjects
88
+ // ? KnownOrUnknownEndpointsByUrlAndMethod<
89
+ // RouteObjectFrom<D & O>
90
+ // >["parameters"]
91
+ // : O & { method?: string } & ("url" extends keyof D
92
+ // ? { url?: string }
93
+ // : { url: string })
94
+ // : O & { method?: string } & ("url" extends keyof D
95
+ // ? { url?: string }
96
+ // : { url: string })
97
+ // ): O extends KnownRouteObject
98
+ // ? RouteObjectFrom<D & O> extends KnownRouteObjects
99
+ // ? KnownOrUnknownEndpointsByUrlAndMethod<RouteObjectFrom<D & O>>["request"]
100
+ // : RequestOptions & Pick<D & O, keyof RequestOptions>
101
+ // : RequestOptions & Pick<D & O, keyof RequestOptions>;
74
102
75
103
/**
76
104
* Transforms a GitHub REST API endpoint into generic request options
0 commit comments