@ryneex/api-client 0.0.51 → 0.0.85
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 +33 -16
- package/index.js +29 -13
- package/package.json +1 -1
package/index.d.ts
CHANGED
|
@@ -2,33 +2,50 @@ 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, TVariables> = Omit<UseQueryOptions<TOutput, ZodError<TOutput> | AxiosError>, "queryFn" | "queryKey"> & {
|
|
6
6
|
queryKey?: unknown[];
|
|
7
|
-
} & (
|
|
8
|
-
data?:
|
|
7
|
+
} & (object extends TData<TInput, TVariables> ? {
|
|
8
|
+
data?: undefined;
|
|
9
9
|
} : {
|
|
10
|
-
data: TData
|
|
10
|
+
data: TData<TInput, TVariables>;
|
|
11
11
|
}) & {
|
|
12
|
-
onSuccess?: (data:
|
|
13
|
-
onError?: (error: ZodError<
|
|
12
|
+
onSuccess?: (data: TOutput, payload: object extends TData<TInput, TVariables> ? void : TData<TInput, TVariables>) => void;
|
|
13
|
+
onError?: (error: ZodError<TOutput> | AxiosError, payload: object extends TData<TInput, TVariables> ? void : TData<TInput, TVariables>) => void;
|
|
14
14
|
};
|
|
15
|
-
type ReactMutationOptions<
|
|
15
|
+
type ReactMutationOptions<TOutput, TInput, TVariables> = Omit<UseMutationOptions<TOutput, ZodError<TOutput> | AxiosError, OptionalTData<TInput, TVariables>>, "mutationFn" | "mutationKey"> & {
|
|
16
16
|
mutationKey?: unknown[];
|
|
17
17
|
};
|
|
18
|
+
type TData<TInput, TVariables> = {} & (TInput extends void ? {
|
|
19
|
+
input?: undefined;
|
|
20
|
+
} : {
|
|
21
|
+
input: TInput;
|
|
22
|
+
}) & (TVariables extends void ? {
|
|
23
|
+
variables?: undefined;
|
|
24
|
+
} : {
|
|
25
|
+
variables: TVariables;
|
|
26
|
+
});
|
|
27
|
+
type OptionalTData<TInput, TVariables> = object extends TData<TInput, TVariables> ? void : TData<TInput, TVariables>;
|
|
18
28
|
declare class BaseApiClient {
|
|
19
29
|
readonly axios: AxiosInstance;
|
|
20
30
|
constructor(axios: AxiosInstance);
|
|
21
|
-
createEndpoint<
|
|
31
|
+
createEndpoint<TOutputSchema extends z.ZodType, TInputSchema extends z.ZodType | undefined, TVariablesSchema extends z.ZodType | undefined, TOutput = z.infer<TOutputSchema>, TInput = undefined extends TInputSchema ? void : z.infer<TInputSchema>, TVariables = undefined extends TVariablesSchema ? void : z.infer<TVariablesSchema>>({ method, path, axiosOptions: axiosOptionsFn, variablesSchema, inputSchema, outputSchema, }: {
|
|
22
32
|
method: "GET" | "POST" | "PUT" | "PATCH" | "DELETE";
|
|
23
|
-
path: string | ((data: TData) => string);
|
|
24
|
-
axiosOptions?: (data: TData) => AxiosRequestConfig;
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
33
|
+
path: string | ((data: TData<TInput, TVariables>) => string);
|
|
34
|
+
axiosOptions?: (data: TData<TInput, TVariables>) => AxiosRequestConfig;
|
|
35
|
+
variablesSchema?: TVariablesSchema;
|
|
36
|
+
inputSchema?: TInputSchema;
|
|
37
|
+
outputSchema: TOutputSchema;
|
|
38
|
+
}): ((opts: OptionalTData<TInput, TVariables>) => Promise<AxiosResponse<TOutput>>) & {
|
|
39
|
+
queryKey: (data: TInput | void) => (string | NonNullable<TInput>)[];
|
|
40
|
+
queryOptions: (opts: object extends TData<TInput, TVariables> ? ReactQueryOptions<TOutput, TInput, TVariables> | void : ReactQueryOptions<TOutput, TInput, TVariables>) => UseQueryOptions<TOutput, ZodError<TOutput> | AxiosError>;
|
|
30
41
|
mutationKey: () => string[];
|
|
31
|
-
mutationOptions: (opts: ReactMutationOptions<
|
|
42
|
+
mutationOptions: (opts: ReactMutationOptions<TOutput, TInput, TVariables> | void) => UseMutationOptions<TOutput, ZodError<TOutput> | AxiosError, OptionalTData<TInput, TVariables>>;
|
|
43
|
+
config: {
|
|
44
|
+
inputSchema: undefined extends TInputSchema ? undefined : NonNullable<TInputSchema>;
|
|
45
|
+
outputSchema: TOutputSchema;
|
|
46
|
+
method: "GET" | "POST" | "PUT" | "PATCH" | "DELETE";
|
|
47
|
+
path: string | ((data: TData<TInput, TVariables>) => string);
|
|
48
|
+
};
|
|
32
49
|
};
|
|
33
50
|
}
|
|
34
51
|
|
package/index.js
CHANGED
|
@@ -10,19 +10,24 @@ var BaseApiClient = class {
|
|
|
10
10
|
method,
|
|
11
11
|
path,
|
|
12
12
|
axiosOptions: axiosOptionsFn,
|
|
13
|
-
|
|
14
|
-
|
|
13
|
+
variablesSchema,
|
|
14
|
+
inputSchema,
|
|
15
|
+
outputSchema
|
|
15
16
|
}) {
|
|
16
17
|
const uuid = crypto.randomUUID();
|
|
17
|
-
const call = async (
|
|
18
|
-
|
|
19
|
-
|
|
18
|
+
const call = async (opts) => {
|
|
19
|
+
const data = opts ?? {};
|
|
20
|
+
if (inputSchema) {
|
|
21
|
+
inputSchema.parse(data.input);
|
|
22
|
+
}
|
|
23
|
+
if (variablesSchema) {
|
|
24
|
+
variablesSchema.parse(data.variables);
|
|
20
25
|
}
|
|
21
26
|
const axiosOptions = axiosOptionsFn?.(data);
|
|
22
27
|
const url = typeof path === "function" ? path(data) : path;
|
|
23
28
|
if (method === "GET") {
|
|
24
29
|
const response = await this.axios.get(url, axiosOptions);
|
|
25
|
-
|
|
30
|
+
outputSchema.parse(response.data);
|
|
26
31
|
return response;
|
|
27
32
|
}
|
|
28
33
|
if (method === "POST") {
|
|
@@ -31,7 +36,7 @@ var BaseApiClient = class {
|
|
|
31
36
|
axiosOptions?.data,
|
|
32
37
|
axiosOptions
|
|
33
38
|
);
|
|
34
|
-
|
|
39
|
+
outputSchema.parse(response.data);
|
|
35
40
|
return response;
|
|
36
41
|
}
|
|
37
42
|
if (method === "PUT") {
|
|
@@ -40,7 +45,7 @@ var BaseApiClient = class {
|
|
|
40
45
|
axiosOptions?.data,
|
|
41
46
|
axiosOptions
|
|
42
47
|
);
|
|
43
|
-
|
|
48
|
+
outputSchema.parse(response.data);
|
|
44
49
|
return response;
|
|
45
50
|
}
|
|
46
51
|
if (method === "PATCH") {
|
|
@@ -49,12 +54,12 @@ var BaseApiClient = class {
|
|
|
49
54
|
axiosOptions?.data,
|
|
50
55
|
axiosOptions
|
|
51
56
|
);
|
|
52
|
-
|
|
57
|
+
outputSchema.parse(response.data);
|
|
53
58
|
return response;
|
|
54
59
|
}
|
|
55
60
|
if (method === "DELETE") {
|
|
56
61
|
const response = await this.axios.delete(url, axiosOptions);
|
|
57
|
-
|
|
62
|
+
outputSchema.parse(response.data);
|
|
58
63
|
return response;
|
|
59
64
|
}
|
|
60
65
|
throw new Error(`API SDK: Unsupported method: ${method}`);
|
|
@@ -66,8 +71,13 @@ var BaseApiClient = class {
|
|
|
66
71
|
return {
|
|
67
72
|
queryFn: async () => {
|
|
68
73
|
try {
|
|
69
|
-
const response = await call(
|
|
70
|
-
|
|
74
|
+
const response = await call(
|
|
75
|
+
data
|
|
76
|
+
);
|
|
77
|
+
options.onSuccess?.(
|
|
78
|
+
response.data,
|
|
79
|
+
data
|
|
80
|
+
);
|
|
71
81
|
return response.data;
|
|
72
82
|
} catch (error) {
|
|
73
83
|
options.onError?.(
|
|
@@ -96,7 +106,13 @@ var BaseApiClient = class {
|
|
|
96
106
|
queryKey,
|
|
97
107
|
queryOptions,
|
|
98
108
|
mutationKey,
|
|
99
|
-
mutationOptions
|
|
109
|
+
mutationOptions,
|
|
110
|
+
config: {
|
|
111
|
+
inputSchema,
|
|
112
|
+
outputSchema,
|
|
113
|
+
method,
|
|
114
|
+
path
|
|
115
|
+
}
|
|
100
116
|
});
|
|
101
117
|
}
|
|
102
118
|
};
|