@ryneex/api-client 1.0.0-beta.3 → 1.1.0
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 +14 -12
- package/index.js +4 -1
- package/package.json +1 -1
package/index.d.ts
CHANGED
|
@@ -9,14 +9,14 @@ type ClientPayload<TInput, TVariables> = {} & (unknown extends TInput ? unknown
|
|
|
9
9
|
variables: TVariables;
|
|
10
10
|
});
|
|
11
11
|
type OptionalPayload<T> = object extends T ? void : T;
|
|
12
|
-
type ClientOptions<TOutputSchema extends z.ZodType, TInputSchema extends z.ZodType, TVariablesSchema extends z.ZodType, TOutput = z.output<TOutputSchema>,
|
|
12
|
+
type ClientOptions<TOutputSchema extends z.ZodType, TInputSchema extends z.ZodType, TVariablesSchema extends z.ZodType, TOutput = z.output<TOutputSchema>, TInputCoerced = z.output<TInputSchema>, TVariablesCoerced = z.output<TVariablesSchema>> = {
|
|
13
13
|
method: "GET" | "POST" | "PUT" | "PATCH" | "DELETE";
|
|
14
|
-
path: string | ((payload: ClientPayload<
|
|
15
|
-
axiosOptions?: (payload: ClientPayload<
|
|
14
|
+
path: string | ((payload: ClientPayload<TInputCoerced, TVariablesCoerced>) => string);
|
|
15
|
+
axiosOptions?: (payload: ClientPayload<TInputCoerced, TVariablesCoerced>) => AxiosRequestConfig;
|
|
16
16
|
variablesSchema?: TVariablesSchema;
|
|
17
17
|
inputSchema?: TInputSchema;
|
|
18
18
|
outputSchema: TOutputSchema;
|
|
19
|
-
transform?: (data: z.output<TOutputSchema>, payload: ClientPayload<
|
|
19
|
+
transform?: (data: z.output<TOutputSchema>, payload: ClientPayload<TInputCoerced, TVariablesCoerced>) => TOutput;
|
|
20
20
|
};
|
|
21
21
|
|
|
22
22
|
type ReactQueryOptions<TOutput, TInput, TVariables> = Omit<UseQueryOptions<TOutput, ClientError>, "queryFn" | "queryKey"> & {
|
|
@@ -40,9 +40,11 @@ type Err<E> = {
|
|
|
40
40
|
error: E;
|
|
41
41
|
};
|
|
42
42
|
type Result<T, E> = Ok<T> | Err<E>;
|
|
43
|
+
type InferOk<T> = T extends Ok<infer K> ? K : never;
|
|
44
|
+
type InferErr<T> = T extends Err<infer K> ? K : never;
|
|
43
45
|
|
|
44
46
|
declare function createClient(axios: AxiosInstance): {
|
|
45
|
-
create: <TOutputSchema extends z.ZodType, TInputSchema extends z.ZodType, TVariablesSchema extends z.ZodType, TOutput = z.core.output<TOutputSchema>, TInput = z.core.input<TInputSchema>, TVariables = z.core.input<TVariablesSchema>>(opts: ClientOptions<TOutputSchema, TInputSchema, TVariablesSchema, TOutput,
|
|
47
|
+
create: <TOutputSchema extends z.ZodType, TInputSchema extends z.ZodType, TVariablesSchema extends z.ZodType, TOutput = z.core.output<TOutputSchema>, TInput = z.core.input<TInputSchema>, TInputCoerced = z.core.output<TInputSchema>, TVariables = z.core.input<TVariablesSchema>, TVariablesCoerced = z.core.output<TVariablesSchema>>(opts: ClientOptions<TOutputSchema, TInputSchema, TVariablesSchema, TOutput, TInputCoerced, TVariablesCoerced>) => ((_payload: OptionalPayload<ClientPayload<TInput, TVariables>>) => Promise<Result<TOutput, ClientError>>) & {
|
|
46
48
|
call: (_payload: OptionalPayload<ClientPayload<TInput, TVariables>>) => Promise<Result<TOutput, ClientError>>;
|
|
47
49
|
queryOptions: (_opts: object extends ClientPayload<TInput, TVariables> ? ReactQueryOptions<TOutput, TInput, TVariables> | void : ReactQueryOptions<TOutput, TInput, TVariables>) => UseQueryOptions<TOutput, ClientError>;
|
|
48
50
|
mutationOptions: (_opts: ReactMutationOptions<TOutput, TInput, TVariables> | void) => UseMutationOptions<TOutput, ClientError, object extends ClientPayload<TInput, TVariables> ? void : ClientPayload<TInput, TVariables>>;
|
|
@@ -53,18 +55,18 @@ declare function createClient(axios: AxiosInstance): {
|
|
|
53
55
|
variablesSchema: undefined extends TVariablesSchema ? undefined : NonNullable<TVariablesSchema>;
|
|
54
56
|
axios: AxiosInstance;
|
|
55
57
|
method: "GET" | "POST" | "PUT" | "PATCH" | "DELETE";
|
|
56
|
-
path: string | ((payload: ClientPayload<
|
|
57
|
-
axiosOptions?: ((payload: ClientPayload<
|
|
58
|
+
path: string | ((payload: ClientPayload<TInputCoerced, TVariablesCoerced>) => string);
|
|
59
|
+
axiosOptions?: ((payload: ClientPayload<TInputCoerced, TVariablesCoerced>) => axios.AxiosRequestConfig) | undefined;
|
|
58
60
|
outputSchema: TOutputSchema;
|
|
59
|
-
transform?: ((data: z.core.output<TOutputSchema>, payload: ClientPayload<
|
|
61
|
+
transform?: ((data: z.core.output<TOutputSchema>, payload: ClientPayload<TInputCoerced, TVariablesCoerced>) => TOutput) | undefined;
|
|
60
62
|
};
|
|
61
63
|
};
|
|
62
64
|
};
|
|
63
65
|
|
|
64
|
-
declare function callApi<TOutputSchema extends z.ZodType, TInputSchema extends z.ZodType, TVariablesSchema extends z.ZodType, TOutput = z.output<TOutputSchema>, TInput = z.input<TInputSchema>, TVariables = z.input<TVariablesSchema>>(opts: ClientOptions<TOutputSchema, TInputSchema, TVariablesSchema, TOutput,
|
|
66
|
+
declare function callApi<TOutputSchema extends z.ZodType, TInputSchema extends z.ZodType, TVariablesSchema extends z.ZodType, TOutput = z.output<TOutputSchema>, TInputCoerced = z.output<TInputSchema>, TInput = z.input<TInputSchema>, TVariablesCoerced = z.output<TVariablesSchema>, TVariables = z.input<TVariablesSchema>>(opts: ClientOptions<TOutputSchema, TInputSchema, TVariablesSchema, TOutput, TInputCoerced, TVariablesCoerced> & {
|
|
65
67
|
axios: AxiosInstance;
|
|
66
|
-
},
|
|
67
|
-
declare function getResponse<TOutputSchema extends z.ZodType, TInputSchema extends z.ZodType, TVariablesSchema extends z.ZodType, TOutput = z.output<TOutputSchema>,
|
|
68
|
+
}, _data: ClientPayload<TInput, TVariables>): Promise<Result<TOutput, ClientError>>;
|
|
69
|
+
declare function getResponse<TOutputSchema extends z.ZodType, TInputSchema extends z.ZodType, TVariablesSchema extends z.ZodType, TOutput = z.output<TOutputSchema>, TInputCoerced = z.output<TInputSchema>, TVariablesCoerced = z.output<TVariablesSchema>>(request: Promise<AxiosResponse<TOutput>>, opts: ClientOptions<TOutputSchema, TInputSchema, TVariablesSchema, TOutput, TInputCoerced, TVariablesCoerced>, payload: ClientPayload<TInputCoerced, TVariablesCoerced>): Promise<Result<TOutput, ClientError>>;
|
|
68
70
|
|
|
69
71
|
declare const VALIDATION_ERROR_NAMES: {
|
|
70
72
|
readonly INPUT: "InputValidationError";
|
|
@@ -81,4 +83,4 @@ declare class ValidationError extends ZodError {
|
|
|
81
83
|
declare function ok<T>(data: T, response: AxiosResponse): Ok<T>;
|
|
82
84
|
declare function err<E>(error: E): Err<E>;
|
|
83
85
|
|
|
84
|
-
export { type ApiClientErrorProps, type ClientError, type ClientOptions, type ClientPayload, type Err, type Ok, type OptionalPayload, type ReactMutationOptions, type ReactQueryOptions, type Result, VALIDATION_ERROR_NAMES, ValidationError, callApi, createClient, err, getResponse, ok };
|
|
86
|
+
export { type ApiClientErrorProps, type ClientError, type ClientOptions, type ClientPayload, type Err, type InferErr, type InferOk, type Ok, type OptionalPayload, type ReactMutationOptions, type ReactQueryOptions, type Result, VALIDATION_ERROR_NAMES, ValidationError, callApi, createClient, err, getResponse, ok };
|
package/index.js
CHANGED
|
@@ -59,7 +59,8 @@ function createClient(axios) {
|
|
|
59
59
|
|
|
60
60
|
// src/lib/call-api.ts
|
|
61
61
|
import "zod";
|
|
62
|
-
async function callApi(opts,
|
|
62
|
+
async function callApi(opts, _data) {
|
|
63
|
+
const data = {};
|
|
63
64
|
if (typeof data !== "object")
|
|
64
65
|
throw new Error("API_CLIENT_INTERNAL_ERROR: Data must be an object");
|
|
65
66
|
if (opts.inputSchema && "input" in data) {
|
|
@@ -71,6 +72,7 @@ async function callApi(opts, data) {
|
|
|
71
72
|
issues: parsedResult.error.issues
|
|
72
73
|
})
|
|
73
74
|
);
|
|
75
|
+
data.input = parsedResult.data;
|
|
74
76
|
}
|
|
75
77
|
if (opts.variablesSchema && "variables" in data) {
|
|
76
78
|
const parsedResult = opts.variablesSchema.safeParse(data.variables);
|
|
@@ -81,6 +83,7 @@ async function callApi(opts, data) {
|
|
|
81
83
|
issues: parsedResult.error.issues
|
|
82
84
|
})
|
|
83
85
|
);
|
|
86
|
+
data.variables = parsedResult.data;
|
|
84
87
|
}
|
|
85
88
|
const axiosOptions = opts.axiosOptions?.(data);
|
|
86
89
|
const url = typeof opts.path === "function" ? opts.path(data) : opts.path;
|