@ryneex/api-client 0.0.5 → 0.0.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/index.d.ts +21 -15
- package/index.js +16 -10
- package/package.json +2 -2
package/index.d.ts
CHANGED
|
@@ -2,33 +2,39 @@ import { UseQueryOptions, UseMutationOptions } from '@tanstack/react-query';
|
|
|
2
2
|
import { AxiosInstance, AxiosRequestConfig, AxiosResponse, AxiosError } from 'axios';
|
|
3
3
|
import z, { ZodError } from 'zod';
|
|
4
4
|
|
|
5
|
-
type ReactQueryOptions<
|
|
5
|
+
type ReactQueryOptions<TOutput, TInput = void> = Omit<UseQueryOptions<TOutput, ZodError<TOutput> | AxiosError>, "queryFn" | "queryKey"> & {
|
|
6
6
|
queryKey?: unknown[];
|
|
7
|
-
} & (
|
|
7
|
+
} & (TInput extends void ? {
|
|
8
8
|
data?: void;
|
|
9
9
|
} : {
|
|
10
|
-
data:
|
|
10
|
+
data: TInput;
|
|
11
11
|
}) & {
|
|
12
|
-
onSuccess?: (data:
|
|
13
|
-
onError?: (error: ZodError<
|
|
12
|
+
onSuccess?: (data: TOutput, variables: TInput) => void;
|
|
13
|
+
onError?: (error: ZodError<TOutput> | AxiosError, variables: TInput) => void;
|
|
14
14
|
};
|
|
15
|
-
type ReactMutationOptions<
|
|
15
|
+
type ReactMutationOptions<TOutput, TInput = void> = Omit<UseMutationOptions<TOutput, ZodError<TOutput> | AxiosError, TInput>, "mutationFn" | "mutationKey"> & {
|
|
16
16
|
mutationKey?: unknown[];
|
|
17
17
|
};
|
|
18
18
|
declare class BaseApiClient {
|
|
19
19
|
readonly axios: AxiosInstance;
|
|
20
20
|
constructor(axios: AxiosInstance);
|
|
21
|
-
createEndpoint<
|
|
21
|
+
createEndpoint<TOutput, TInput = void>({ method, path, axiosOptions: axiosOptionsFn, inputSchema, outputSchema, }: {
|
|
22
22
|
method: "GET" | "POST" | "PUT" | "PATCH" | "DELETE";
|
|
23
|
-
path: string | ((data:
|
|
24
|
-
axiosOptions?: (data:
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
}): ((data:
|
|
28
|
-
queryKey: (data:
|
|
29
|
-
queryOptions: (opts:
|
|
23
|
+
path: string | ((data: TInput) => string);
|
|
24
|
+
axiosOptions?: (data: TInput) => AxiosRequestConfig;
|
|
25
|
+
inputSchema?: z.ZodType<TInput>;
|
|
26
|
+
outputSchema: z.ZodType<TOutput>;
|
|
27
|
+
}): ((data: TInput) => Promise<AxiosResponse<TOutput>>) & {
|
|
28
|
+
queryKey: (data: TInput | void) => (string | NonNullable<TInput>)[];
|
|
29
|
+
queryOptions: (opts: TInput extends void ? ReactQueryOptions<TOutput> | void : ReactQueryOptions<TOutput, TInput>) => UseQueryOptions<TOutput, ZodError<TOutput> | AxiosError>;
|
|
30
30
|
mutationKey: () => string[];
|
|
31
|
-
mutationOptions: (opts: ReactMutationOptions<
|
|
31
|
+
mutationOptions: (opts: ReactMutationOptions<TOutput, TInput> | void) => UseMutationOptions<TOutput, ZodError<TOutput> | AxiosError, TInput>;
|
|
32
|
+
config: {
|
|
33
|
+
inputSchema: z.ZodType<TInput, unknown, z.core.$ZodTypeInternals<TInput, unknown>> | undefined;
|
|
34
|
+
outputSchema: z.ZodType<TOutput, unknown, z.core.$ZodTypeInternals<TOutput, unknown>>;
|
|
35
|
+
method: "GET" | "POST" | "PUT" | "PATCH" | "DELETE";
|
|
36
|
+
path: string | ((data: TInput) => string);
|
|
37
|
+
};
|
|
32
38
|
};
|
|
33
39
|
}
|
|
34
40
|
|
package/index.js
CHANGED
|
@@ -10,19 +10,19 @@ var BaseApiClient = class {
|
|
|
10
10
|
method,
|
|
11
11
|
path,
|
|
12
12
|
axiosOptions: axiosOptionsFn,
|
|
13
|
-
|
|
14
|
-
|
|
13
|
+
inputSchema,
|
|
14
|
+
outputSchema
|
|
15
15
|
}) {
|
|
16
16
|
const uuid = crypto.randomUUID();
|
|
17
17
|
const call = async (data) => {
|
|
18
|
-
if (
|
|
19
|
-
|
|
18
|
+
if (inputSchema) {
|
|
19
|
+
inputSchema.parse(data);
|
|
20
20
|
}
|
|
21
21
|
const axiosOptions = axiosOptionsFn?.(data);
|
|
22
22
|
const url = typeof path === "function" ? path(data) : path;
|
|
23
23
|
if (method === "GET") {
|
|
24
24
|
const response = await this.axios.get(url, axiosOptions);
|
|
25
|
-
|
|
25
|
+
outputSchema.parse(response.data);
|
|
26
26
|
return response;
|
|
27
27
|
}
|
|
28
28
|
if (method === "POST") {
|
|
@@ -31,7 +31,7 @@ var BaseApiClient = class {
|
|
|
31
31
|
axiosOptions?.data,
|
|
32
32
|
axiosOptions
|
|
33
33
|
);
|
|
34
|
-
|
|
34
|
+
outputSchema.parse(response.data);
|
|
35
35
|
return response;
|
|
36
36
|
}
|
|
37
37
|
if (method === "PUT") {
|
|
@@ -40,7 +40,7 @@ var BaseApiClient = class {
|
|
|
40
40
|
axiosOptions?.data,
|
|
41
41
|
axiosOptions
|
|
42
42
|
);
|
|
43
|
-
|
|
43
|
+
outputSchema.parse(response.data);
|
|
44
44
|
return response;
|
|
45
45
|
}
|
|
46
46
|
if (method === "PATCH") {
|
|
@@ -49,12 +49,12 @@ var BaseApiClient = class {
|
|
|
49
49
|
axiosOptions?.data,
|
|
50
50
|
axiosOptions
|
|
51
51
|
);
|
|
52
|
-
|
|
52
|
+
outputSchema.parse(response.data);
|
|
53
53
|
return response;
|
|
54
54
|
}
|
|
55
55
|
if (method === "DELETE") {
|
|
56
56
|
const response = await this.axios.delete(url, axiosOptions);
|
|
57
|
-
|
|
57
|
+
outputSchema.parse(response.data);
|
|
58
58
|
return response;
|
|
59
59
|
}
|
|
60
60
|
throw new Error(`API SDK: Unsupported method: ${method}`);
|
|
@@ -96,7 +96,13 @@ var BaseApiClient = class {
|
|
|
96
96
|
queryKey,
|
|
97
97
|
queryOptions,
|
|
98
98
|
mutationKey,
|
|
99
|
-
mutationOptions
|
|
99
|
+
mutationOptions,
|
|
100
|
+
config: {
|
|
101
|
+
inputSchema,
|
|
102
|
+
outputSchema,
|
|
103
|
+
method,
|
|
104
|
+
path
|
|
105
|
+
}
|
|
100
106
|
});
|
|
101
107
|
}
|
|
102
108
|
};
|