@ryneex/api-client 0.0.6 → 0.0.51
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 +15 -21
- package/index.js +10 -16
- package/package.json +1 -1
package/index.d.ts
CHANGED
|
@@ -2,39 +2,33 @@ 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<TResponse, TData = void> = Omit<UseQueryOptions<TResponse, ZodError<TResponse> | AxiosError>, "queryFn" | "queryKey"> & {
|
|
6
6
|
queryKey?: unknown[];
|
|
7
|
-
} & (
|
|
7
|
+
} & (TData extends void ? {
|
|
8
8
|
data?: void;
|
|
9
9
|
} : {
|
|
10
|
-
data:
|
|
10
|
+
data: TData;
|
|
11
11
|
}) & {
|
|
12
|
-
onSuccess?: (data:
|
|
13
|
-
onError?: (error: ZodError<
|
|
12
|
+
onSuccess?: (data: TResponse, variables: TData) => void;
|
|
13
|
+
onError?: (error: ZodError<TResponse> | AxiosError, variables: TData) => void;
|
|
14
14
|
};
|
|
15
|
-
type ReactMutationOptions<
|
|
15
|
+
type ReactMutationOptions<TResponse, TData = void> = Omit<UseMutationOptions<TResponse, ZodError<TResponse> | AxiosError, TData>, "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<TResponse, TData = void>({ method, path, axiosOptions: axiosOptionsFn, dataSchema, responseSchema, }: {
|
|
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: TData) => string);
|
|
24
|
+
axiosOptions?: (data: TData) => AxiosRequestConfig;
|
|
25
|
+
dataSchema?: z.ZodType<TData>;
|
|
26
|
+
responseSchema: z.ZodType<TResponse>;
|
|
27
|
+
}): ((data: TData) => Promise<AxiosResponse<TResponse>>) & {
|
|
28
|
+
queryKey: (data: TData | void) => (string | NonNullable<TData>)[];
|
|
29
|
+
queryOptions: (opts: TData extends void ? ReactQueryOptions<TResponse> | void : ReactQueryOptions<TResponse, TData>) => UseQueryOptions<TResponse, ZodError<TResponse> | AxiosError>;
|
|
30
30
|
mutationKey: () => string[];
|
|
31
|
-
mutationOptions: (opts: ReactMutationOptions<
|
|
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
|
-
};
|
|
31
|
+
mutationOptions: (opts: ReactMutationOptions<TResponse, TData> | void) => UseMutationOptions<TResponse, ZodError<TResponse> | AxiosError, TData>;
|
|
38
32
|
};
|
|
39
33
|
}
|
|
40
34
|
|
package/index.js
CHANGED
|
@@ -10,19 +10,19 @@ var BaseApiClient = class {
|
|
|
10
10
|
method,
|
|
11
11
|
path,
|
|
12
12
|
axiosOptions: axiosOptionsFn,
|
|
13
|
-
|
|
14
|
-
|
|
13
|
+
dataSchema,
|
|
14
|
+
responseSchema
|
|
15
15
|
}) {
|
|
16
16
|
const uuid = crypto.randomUUID();
|
|
17
17
|
const call = async (data) => {
|
|
18
|
-
if (
|
|
19
|
-
|
|
18
|
+
if (dataSchema) {
|
|
19
|
+
dataSchema.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
|
+
responseSchema.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
|
+
responseSchema.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
|
+
responseSchema.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
|
+
responseSchema.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
|
+
responseSchema.parse(response.data);
|
|
58
58
|
return response;
|
|
59
59
|
}
|
|
60
60
|
throw new Error(`API SDK: Unsupported method: ${method}`);
|
|
@@ -96,13 +96,7 @@ var BaseApiClient = class {
|
|
|
96
96
|
queryKey,
|
|
97
97
|
queryOptions,
|
|
98
98
|
mutationKey,
|
|
99
|
-
mutationOptions
|
|
100
|
-
config: {
|
|
101
|
-
inputSchema,
|
|
102
|
-
outputSchema,
|
|
103
|
-
method,
|
|
104
|
-
path
|
|
105
|
-
}
|
|
99
|
+
mutationOptions
|
|
106
100
|
});
|
|
107
101
|
}
|
|
108
102
|
};
|