@ryneex/api-client 1.0.0-beta.4 → 1.1.1

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.
Files changed (3) hide show
  1. package/index.d.ts +11 -11
  2. package/index.js +9 -6
  3. 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>, TInput = z.input<TInputSchema>, TVariables = z.input<TVariablesSchema>> = {
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<TInput, TVariables>) => string);
15
- axiosOptions?: (payload: ClientPayload<TInput, TVariables>) => AxiosRequestConfig;
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<TInput, TVariables>) => TOutput;
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"> & {
@@ -44,7 +44,7 @@ type InferOk<T> = T extends Ok<infer K> ? K : never;
44
44
  type InferErr<T> = T extends Err<infer K> ? K : never;
45
45
 
46
46
  declare function createClient(axios: AxiosInstance): {
47
- 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, TInput, TVariables>) => ((_payload: OptionalPayload<ClientPayload<TInput, TVariables>>) => Promise<Result<TOutput, ClientError>>) & {
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>>) & {
48
48
  call: (_payload: OptionalPayload<ClientPayload<TInput, TVariables>>) => Promise<Result<TOutput, ClientError>>;
49
49
  queryOptions: (_opts: object extends ClientPayload<TInput, TVariables> ? ReactQueryOptions<TOutput, TInput, TVariables> | void : ReactQueryOptions<TOutput, TInput, TVariables>) => UseQueryOptions<TOutput, ClientError>;
50
50
  mutationOptions: (_opts: ReactMutationOptions<TOutput, TInput, TVariables> | void) => UseMutationOptions<TOutput, ClientError, object extends ClientPayload<TInput, TVariables> ? void : ClientPayload<TInput, TVariables>>;
@@ -55,18 +55,18 @@ declare function createClient(axios: AxiosInstance): {
55
55
  variablesSchema: undefined extends TVariablesSchema ? undefined : NonNullable<TVariablesSchema>;
56
56
  axios: AxiosInstance;
57
57
  method: "GET" | "POST" | "PUT" | "PATCH" | "DELETE";
58
- path: string | ((payload: ClientPayload<TInput, TVariables>) => string);
59
- axiosOptions?: ((payload: ClientPayload<TInput, TVariables>) => axios.AxiosRequestConfig) | undefined;
58
+ path: string | ((payload: ClientPayload<TInputCoerced, TVariablesCoerced>) => string);
59
+ axiosOptions?: ((payload: ClientPayload<TInputCoerced, TVariablesCoerced>) => axios.AxiosRequestConfig) | undefined;
60
60
  outputSchema: TOutputSchema;
61
- transform?: ((data: z.core.output<TOutputSchema>, payload: ClientPayload<TInput, TVariables>) => TOutput) | undefined;
61
+ transform?: ((data: z.core.output<TOutputSchema>, payload: ClientPayload<TInputCoerced, TVariablesCoerced>) => TOutput) | undefined;
62
62
  };
63
63
  };
64
64
  };
65
65
 
66
- 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, TInput, TVariables> & {
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> & {
67
67
  axios: AxiosInstance;
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>, TInput = z.input<TInputSchema>, TVariables = z.input<TVariablesSchema>>(request: Promise<AxiosResponse<TOutput>>, opts: ClientOptions<TOutputSchema, TInputSchema, TVariablesSchema, TOutput, TInput, TVariables>, payload: ClientPayload<TInput, TVariables>): Promise<Result<TOutput, ClientError>>;
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>>;
70
70
 
71
71
  declare const VALIDATION_ERROR_NAMES: {
72
72
  readonly INPUT: "InputValidationError";
package/index.js CHANGED
@@ -59,11 +59,12 @@ function createClient(axios) {
59
59
 
60
60
  // src/lib/call-api.ts
61
61
  import "zod";
62
- async function callApi(opts, data) {
63
- if (typeof data !== "object")
62
+ async function callApi(opts, _data) {
63
+ const data = {};
64
+ if (typeof _data !== "object")
64
65
  throw new Error("API_CLIENT_INTERNAL_ERROR: Data must be an object");
65
- if (opts.inputSchema && "input" in data) {
66
- const parsedResult = opts.inputSchema.safeParse(data.input);
66
+ if (opts.inputSchema && "input" in _data) {
67
+ const parsedResult = opts.inputSchema.safeParse(_data.input);
67
68
  if (!parsedResult.success)
68
69
  return err(
69
70
  new ValidationError({
@@ -71,9 +72,10 @@ async function callApi(opts, data) {
71
72
  issues: parsedResult.error.issues
72
73
  })
73
74
  );
75
+ data.input = parsedResult.data;
74
76
  }
75
- if (opts.variablesSchema && "variables" in data) {
76
- const parsedResult = opts.variablesSchema.safeParse(data.variables);
77
+ if (opts.variablesSchema && "variables" in _data) {
78
+ const parsedResult = opts.variablesSchema.safeParse(_data.variables);
77
79
  if (!parsedResult.success)
78
80
  return err(
79
81
  new ValidationError({
@@ -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;
package/package.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "name": "@ryneex/api-client",
3
3
  "module": "src/index.ts",
4
4
  "type": "module",
5
- "version": "1.0.0-beta.4",
5
+ "version": "1.1.1",
6
6
  "exports": {
7
7
  ".": "./index.js"
8
8
  },