Skip to content

Commit c04458c

Browse files
committed
refactor: ape client types
fully type query, payload and return data with generics
1 parent 6968349 commit c04458c

File tree

2 files changed

+32
-21
lines changed

2 files changed

+32
-21
lines changed

frontend/src/ts/ape/adapters/axios-adapter.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ type AxiosClientDataMethod = (
1414
config: AxiosRequestConfig
1515
) => Promise<AxiosResponse>;
1616

17-
async function adaptRequestOptions(
18-
options: Ape.RequestOptions
17+
async function adaptRequestOptions<TQuery, TPayload>(
18+
options: Ape.RequestOptionsWithPayload<TQuery, TPayload>
1919
): Promise<AxiosRequestConfig> {
2020
const idToken = isAuthenticated()
2121
? await getIdToken(getAuthenticatedUser())
@@ -37,11 +37,11 @@ async function adaptRequestOptions(
3737
function apeifyClientMethod(
3838
clientMethod: AxiosClientMethod | AxiosClientDataMethod,
3939
methodType: Ape.HttpMethodTypes
40-
): Ape.HttpClientMethod {
41-
return async (
40+
): Ape.HttpClientMethod | Ape.HttpClientMethodWithPayload {
41+
return async function <TQuery, TPayload, TData>(
4242
endpoint: string,
43-
options: Ape.RequestOptions = {}
44-
): Ape.EndpointResponse => {
43+
options: Ape.RequestOptionsWithPayload<TQuery, TPayload> = {}
44+
): Ape.EndpointResponse<TData> {
4545
let errorMessage = "";
4646

4747
try {

frontend/src/ts/ape/types/ape.d.ts

Lines changed: 26 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,40 @@
11
declare namespace Ape {
2-
type RequestOptions = {
2+
type RequestOptions<TQuery> = {
33
headers?: Record<string, string>;
4-
searchQuery?: Record<string, any>;
5-
payload?: any;
4+
searchQuery?: Record<string, TQuery>;
65
};
76

8-
type HttpClientResponse<Data> = {
7+
type HttpClientMethod = <TQuery, TData>(
8+
endpoint: string,
9+
options?: Ape.RequestOptions<TQuery>
10+
) => Ape.EndpointResponse<TData>;
11+
12+
type RequestOptionsWithPayload<TQuery, TPayload> = {
13+
headers?: Record<string, string>;
14+
searchQuery?: Record<string, TQuery>;
15+
payload?: TPayload;
16+
};
17+
18+
type HttpClientMethodWithPayload = <TQuery, TPayload, TData>(
19+
endpoint: string,
20+
options?: Ape.RequestOptionsWithPayload<TQuery, TPayload>
21+
) => Ape.EndpointResponse<TData>;
22+
23+
type HttpClientResponse<TData> = {
924
status: number;
1025
message: string;
11-
data: Data | null;
26+
data: TData | null;
1227
};
1328

14-
type EndpointResponse<Data = any> = Promise<HttpClientResponse<Data>>;
15-
16-
type HttpClientMethod<Data = any> = (
17-
endpoint: string,
18-
config?: RequestOptions
19-
) => EndpointResponse<Data>;
29+
// todo: remove any after all ape endpoints are typed
30+
type EndpointResponse<TData = any> = Promise<HttpClientResponse<TData>>;
2031

2132
type HttpClient = {
2233
get: HttpClientMethod;
23-
post: HttpClientMethod;
24-
put: HttpClientMethod;
25-
patch: HttpClientMethod;
26-
delete: HttpClientMethod;
34+
post: HttpClientMethodWithPayload;
35+
put: HttpClientMethodWithPayload;
36+
patch: HttpClientMethodWithPayload;
37+
delete: HttpClientMethodWithPayload;
2738
};
2839

2940
type HttpMethodTypes = keyof HttpClient;

0 commit comments

Comments
 (0)