-
Notifications
You must be signed in to change notification settings - Fork 8
/
index.ts
77 lines (73 loc) · 2.28 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
import {
CreateBookOptions,
DeleteBookOptions,
GetBookOptions,
ListBookOptions,
UpdateBookOptions,
} from "./requestOptions";
import {
AddBook201Response,
DeleteBook201Response,
ErrorResponse,
GetBook200Response,
ListBooks200Response,
UpdateBook201Response,
} from "./responses";
/**
* Interface used to define the REST API
*/
export interface BookLibrary {
"/book": {
get: (
input?: ListBookOptions
) => Promise<ListBooks200Response | ErrorResponse>;
post: (
input: CreateBookOptions
) => Promise<AddBook201Response | ErrorResponse>;
put: (
input: UpdateBookOptions
) => Promise<UpdateBook201Response | ErrorResponse>;
};
"/book/{bookId}": {
get: (
input?: GetBookOptions
) => Promise<GetBook200Response | ErrorResponse>;
delete: (
input?: DeleteBookOptions
) => Promise<DeleteBook201Response | ErrorResponse>;
};
}
/**
* Helper type that matches a string with a Template
* we use this to figure out if a string contains
* one or more path parameters. Path parameters
* are segments of the path that start with '/' and
* are enclosed by '{}'
*/
export type PathParameter<TPath extends string> =
// Define our template in terms of Head/{Parameter}Tail
TPath extends `${infer Head}/{${infer Parameter}}${infer Tail}`
? // We can call PathParameter<Tail> recursively to
// match the template against the Tail of the path
[pathParameter: string, ...params: PathParameter<Tail>]
: // If no parameters were found we get an empty tuple
[];
/**
* Defines the type for the path function that will be part
* of the client. This will only accept a string that
* matches any of the keys of our BookLibrary interface
*/
export type Path = <TPath extends keyof BookLibrary>(
path: TPath,
// Our PathParameter helper type gives us a tuple
// of the parameters that were found. If we spread
// the tuple, we get each single parameter as a positiona
// parameter of this function
...pathParam: PathParameter<TPath>
) => BookLibrary[TPath]; // We can access elements of an interface by key
/**
* This is our client factory function.
* It takes the BaseURL of the service and returns a client
* with a `path` function.
*/
export declare function BooksClient(baseUrl: string): { path: Path };