@ryneex/api-client 0.0.8 → 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.
Files changed (3) hide show
  1. package/index.d.ts +25 -14
  2. package/index.js +14 -4
  3. package/package.json +1 -1
package/index.d.ts CHANGED
@@ -2,38 +2,49 @@ 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<TOutput, TInput = void> = Omit<UseQueryOptions<TOutput, ZodError<TOutput> | AxiosError>, "queryFn" | "queryKey"> & {
5
+ type ReactQueryOptions<TOutput, TInput, TVariables> = Omit<UseQueryOptions<TOutput, ZodError<TOutput> | AxiosError>, "queryFn" | "queryKey"> & {
6
6
  queryKey?: unknown[];
7
- } & (TInput extends void ? {
8
- data?: void;
7
+ } & (object extends TData<TInput, TVariables> ? {
8
+ data?: undefined;
9
9
  } : {
10
- data: TInput;
10
+ data: TData<TInput, TVariables>;
11
11
  }) & {
12
- onSuccess?: (data: TOutput, variables: TInput) => void;
13
- onError?: (error: ZodError<TOutput> | AxiosError, variables: TInput) => void;
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<TOutput, TInput = void> = Omit<UseMutationOptions<TOutput, ZodError<TOutput> | AxiosError, TInput>, "mutationFn" | "mutationKey"> & {
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<TOutputSchema extends z.ZodType, TInputSchema extends z.ZodType | undefined, TOutput = z.infer<TOutputSchema>, TInput = undefined extends TInputSchema ? void : z.infer<TInputSchema>>({ method, path, axiosOptions: axiosOptionsFn, inputSchema, outputSchema, }: {
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: TInput) => string);
24
- axiosOptions?: (data: TInput) => AxiosRequestConfig;
33
+ path: string | ((data: TData<TInput, TVariables>) => string);
34
+ axiosOptions?: (data: TData<TInput, TVariables>) => AxiosRequestConfig;
35
+ variablesSchema?: TVariablesSchema;
25
36
  inputSchema?: TInputSchema;
26
37
  outputSchema: TOutputSchema;
27
- }): ((data: TInput) => Promise<AxiosResponse<TOutput>>) & {
38
+ }): ((opts: OptionalTData<TInput, TVariables>) => Promise<AxiosResponse<TOutput>>) & {
28
39
  queryKey: (data: TInput | void) => (string | NonNullable<TInput>)[];
29
- queryOptions: (opts: TInput extends void ? ReactQueryOptions<TOutput> | void : ReactQueryOptions<TOutput, TInput>) => UseQueryOptions<TOutput, ZodError<TOutput> | AxiosError>;
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<TOutput, TInput> | void) => UseMutationOptions<TOutput, ZodError<TOutput> | AxiosError, TInput>;
42
+ mutationOptions: (opts: ReactMutationOptions<TOutput, TInput, TVariables> | void) => UseMutationOptions<TOutput, ZodError<TOutput> | AxiosError, OptionalTData<TInput, TVariables>>;
32
43
  config: {
33
44
  inputSchema: undefined extends TInputSchema ? undefined : NonNullable<TInputSchema>;
34
45
  outputSchema: TOutputSchema;
35
46
  method: "GET" | "POST" | "PUT" | "PATCH" | "DELETE";
36
- path: string | ((data: TInput) => string);
47
+ path: string | ((data: TData<TInput, TVariables>) => string);
37
48
  };
38
49
  };
39
50
  }
package/index.js CHANGED
@@ -10,13 +10,18 @@ var BaseApiClient = class {
10
10
  method,
11
11
  path,
12
12
  axiosOptions: axiosOptionsFn,
13
+ variablesSchema,
13
14
  inputSchema,
14
15
  outputSchema
15
16
  }) {
16
17
  const uuid = crypto.randomUUID();
17
- const call = async (data) => {
18
+ const call = async (opts) => {
19
+ const data = opts ?? {};
18
20
  if (inputSchema) {
19
- inputSchema.parse(data);
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;
@@ -66,8 +71,13 @@ var BaseApiClient = class {
66
71
  return {
67
72
  queryFn: async () => {
68
73
  try {
69
- const response = await call(data);
70
- options.onSuccess?.(response.data, data);
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?.(
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": "0.0.8",
5
+ "version": "0.0.85",
6
6
  "exports": {
7
7
  ".": "./index.js"
8
8
  },