@ryneex/api-client 0.0.4 → 0.0.6

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 +21 -15
  2. package/index.js +16 -10
  3. package/package.json +4 -4
package/index.d.ts CHANGED
@@ -2,33 +2,39 @@ 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<TResponse, TData = void> = Omit<UseQueryOptions<TResponse, ZodError<TResponse> | AxiosError>, "queryFn" | "queryKey"> & {
5
+ type ReactQueryOptions<TOutput, TInput = void> = Omit<UseQueryOptions<TOutput, ZodError<TOutput> | AxiosError>, "queryFn" | "queryKey"> & {
6
6
  queryKey?: unknown[];
7
- } & (TData extends void ? {
7
+ } & (TInput extends void ? {
8
8
  data?: void;
9
9
  } : {
10
- data: TData;
10
+ data: TInput;
11
11
  }) & {
12
- onSuccess?: (data: TResponse, variables: TData) => void;
13
- onError?: (error: ZodError<TResponse> | AxiosError, variables: TData) => void;
12
+ onSuccess?: (data: TOutput, variables: TInput) => void;
13
+ onError?: (error: ZodError<TOutput> | AxiosError, variables: TInput) => void;
14
14
  };
15
- type ReactMutationOptions<TResponse, TData = void> = Omit<UseMutationOptions<TResponse, ZodError<TResponse> | AxiosError, TData>, "mutationFn" | "mutationKey"> & {
15
+ type ReactMutationOptions<TOutput, TInput = void> = Omit<UseMutationOptions<TOutput, ZodError<TOutput> | AxiosError, TInput>, "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<TResponse, TData = void>({ method, path, axiosOptions: axiosOptionsFn, dataSchema, responseSchema, }: {
21
+ createEndpoint<TOutput, TInput = void>({ method, path, axiosOptions: axiosOptionsFn, inputSchema, outputSchema, }: {
22
22
  method: "GET" | "POST" | "PUT" | "PATCH" | "DELETE";
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>;
23
+ path: string | ((data: TInput) => string);
24
+ axiosOptions?: (data: TInput) => AxiosRequestConfig;
25
+ inputSchema?: z.ZodType<TInput>;
26
+ outputSchema: z.ZodType<TOutput>;
27
+ }): ((data: TInput) => Promise<AxiosResponse<TOutput>>) & {
28
+ queryKey: (data: TInput | void) => (string | NonNullable<TInput>)[];
29
+ queryOptions: (opts: TInput extends void ? ReactQueryOptions<TOutput> | void : ReactQueryOptions<TOutput, TInput>) => UseQueryOptions<TOutput, ZodError<TOutput> | AxiosError>;
30
30
  mutationKey: () => string[];
31
- mutationOptions: (opts: ReactMutationOptions<TResponse, TData> | void) => UseMutationOptions<TResponse, ZodError<TResponse> | AxiosError, TData>;
31
+ mutationOptions: (opts: ReactMutationOptions<TOutput, TInput> | void) => UseMutationOptions<TOutput, ZodError<TOutput> | AxiosError, TInput>;
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
+ };
32
38
  };
33
39
  }
34
40
 
package/index.js CHANGED
@@ -10,19 +10,19 @@ var BaseApiClient = class {
10
10
  method,
11
11
  path,
12
12
  axiosOptions: axiosOptionsFn,
13
- dataSchema,
14
- responseSchema
13
+ inputSchema,
14
+ outputSchema
15
15
  }) {
16
16
  const uuid = crypto.randomUUID();
17
17
  const call = async (data) => {
18
- if (dataSchema) {
19
- dataSchema.parse(data);
18
+ if (inputSchema) {
19
+ inputSchema.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
- responseSchema.parse(response.data);
25
+ outputSchema.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
- responseSchema.parse(response.data);
34
+ outputSchema.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
- responseSchema.parse(response.data);
43
+ outputSchema.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
- responseSchema.parse(response.data);
52
+ outputSchema.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
- responseSchema.parse(response.data);
57
+ outputSchema.parse(response.data);
58
58
  return response;
59
59
  }
60
60
  throw new Error(`API SDK: Unsupported method: ${method}`);
@@ -96,7 +96,13 @@ var BaseApiClient = class {
96
96
  queryKey,
97
97
  queryOptions,
98
98
  mutationKey,
99
- mutationOptions
99
+ mutationOptions,
100
+ config: {
101
+ inputSchema,
102
+ outputSchema,
103
+ method,
104
+ path
105
+ }
100
106
  });
101
107
  }
102
108
  };
package/package.json CHANGED
@@ -2,9 +2,9 @@
2
2
  "name": "@ryneex/api-client",
3
3
  "module": "src/index.ts",
4
4
  "type": "module",
5
- "version": "0.0.4",
5
+ "version": "0.0.6",
6
6
  "exports": {
7
- ".": "./dist/index.js"
7
+ ".": "./index.js"
8
8
  },
9
9
  "scripts": {
10
10
  "lint": "eslint .",
@@ -13,10 +13,10 @@
13
13
  "build:publish": "bun run build && cd dist && npm publish"
14
14
  },
15
15
  "peerDependencies": {
16
- "@tanstack/react-query": "^5.90.16",
16
+ "@tanstack/react-query": "^5",
17
17
  "axios": "^1.13.2",
18
18
  "typescript": "^5",
19
- "zod": "^4.2.1"
19
+ "zod": "^4"
20
20
  },
21
21
  "devDependencies": {
22
22
  "@eslint/js": "^9.39.2",