react-query-lightbase-codegen 2.0.8 → 2.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.
@@ -89,6 +89,7 @@ function generateAxiosMethod(operation, spec) {
89
89
  const responseType = successResponse ? `T.${`${namedType}Response${successResponse[0]}`}` : "any";
90
90
  const urlWithParams = urlParams.length > 0 ? `\`${path.replace(/{(\w+)}/g, "${data.$1}")}\`` : `"${path}"`;
91
91
  const methodBody = [
92
+ `${hasData ? "const { axiosConfig, ...data } = props || {};" : "const { axiosConfig } = props || {};"}`,
92
93
  "const apiClient = getApiClient();",
93
94
  `const url = ${urlWithParams};`,
94
95
  queryParams.length > 0
@@ -120,7 +121,7 @@ function generateAxiosMethod(operation, spec) {
120
121
  `const res = await apiClient.${method}<${responseType}>(url, {
121
122
  ${queryParams.length > 0 ? "params: queryData," : ""}
122
123
  ${requestBody ? `data: ${isFormData ? "formData" : "bodyData"},` : ""}
123
- ${isFormData ? `config: { headers: { 'Content-Type': 'multipart/form-data', ...config?.headers }, ...config },` : "...config"}
124
+ ${isFormData ? `config: { headers: { 'Content-Type': 'multipart/form-data', ...axiosConfig?.headers }, ...axiosConfig },` : "...axiosConfig"}
124
125
  });
125
126
  return res.data;`,
126
127
  ]
@@ -128,7 +129,7 @@ function generateAxiosMethod(operation, spec) {
128
129
  .join("\n ");
129
130
  return `
130
131
  ${jsDocLines.join("\n ")}
131
- export async function ${(0, utils_1.camelCase)(operationId)}(data${hasData ? `: ${dataType}` : "?: undefined"}, config?: AxiosRequestConfig): Promise<${responseType}> {
132
+ export async function ${(0, utils_1.camelCase)(operationId)}(props: ${hasData ? `${dataType} & { axiosConfig?: AxiosRequestConfig; }` : "{ axiosConfig?: AxiosRequestConfig }"} ): Promise<${responseType}> {
132
133
  ${methodBody}
133
134
  }`;
134
135
  }
@@ -31,16 +31,14 @@ function generateQueryOptions(operation, spec) {
31
31
  const namedQueryOptions = `get${operationId}QueryOptions`;
32
32
  const namedQuery = (0, utils_1.camelCase)(`${operationId}`);
33
33
  return `
34
- export const ${namedQueryOptions} = (
35
- ${hasData ? `params: Partial<Parameters<typeof apiClient.${namedQuery}>[0]>, config?: Partial<Parameters<typeof apiClient.${namedQuery}>[1]>` : `_?: undefined, config?: Partial<Parameters<typeof apiClient.${namedQuery}>[1]>`}
34
+ export const ${namedQueryOptions} = (
35
+ ${hasData ? `props: Partial<Parameters<typeof apiClient.${namedQuery}>[0]>` : `props?: Partial<Parameters<typeof apiClient.${namedQuery}>[0]>`}
36
36
  ) => {
37
+ ${hasData ? "const { axiosConfig, ...params } = props || {};" : "const { axiosConfig } = props || {};"}
37
38
  const enabled = ${hasData ? `hasDefinedProps(params, ${requiredParams.join(", ")})` : "true"};
38
39
  return queryOptions({
39
- queryKey: ['${(0, utils_1.camelCase)(operationId)}', ${hasData ? "params" : "undefined"}],
40
- queryFn: enabled ? async () => {
41
- const response = await apiClient.${namedQuery}(${hasData ? "params" : "undefined"}, config);
42
- return response.data;
43
- } : skipToken,
40
+ queryKey: ['${(0, utils_1.camelCase)(operationId)}', ${hasData ? "params" : ""}],
41
+ queryFn: enabled ? () => apiClient.${namedQuery}(${hasData ? "{...params, axiosConfig}" : "{axiosConfig}"}) : skipToken,
44
42
  });
45
43
  };`;
46
44
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "react-query-lightbase-codegen",
3
- "version": "2.0.8",
3
+ "version": "2.1.0",
4
4
  "license": "MIT",
5
5
  "description": "Generate Axios API clients and React Query options from OpenAPI specifications",
6
6
  "exports": "./dist/index.js",
@@ -115,6 +115,7 @@ function generateAxiosMethod(operation: OperationInfo, spec: OpenAPIV3.Document)
115
115
  const urlWithParams = urlParams.length > 0 ? `\`${path.replace(/{(\w+)}/g, "${data.$1}")}\`` : `"${path}"`;
116
116
 
117
117
  const methodBody = [
118
+ `${hasData ? "const { axiosConfig, ...data } = props || {};" : "const { axiosConfig } = props || {};"}`,
118
119
  "const apiClient = getApiClient();",
119
120
  `const url = ${urlWithParams};`,
120
121
  queryParams.length > 0
@@ -146,7 +147,7 @@ function generateAxiosMethod(operation: OperationInfo, spec: OpenAPIV3.Document)
146
147
  `const res = await apiClient.${method}<${responseType}>(url, {
147
148
  ${queryParams.length > 0 ? "params: queryData," : ""}
148
149
  ${requestBody ? `data: ${isFormData ? "formData" : "bodyData"},` : ""}
149
- ${isFormData ? `config: { headers: { 'Content-Type': 'multipart/form-data', ...config?.headers }, ...config },` : "...config"}
150
+ ${isFormData ? `config: { headers: { 'Content-Type': 'multipart/form-data', ...axiosConfig?.headers }, ...axiosConfig },` : "...axiosConfig"}
150
151
  });
151
152
  return res.data;`,
152
153
  ]
@@ -155,7 +156,9 @@ function generateAxiosMethod(operation: OperationInfo, spec: OpenAPIV3.Document)
155
156
 
156
157
  return `
157
158
  ${jsDocLines.join("\n ")}
158
- export async function ${camelCase(operationId)}(data${hasData ? `: ${dataType}` : "?: undefined"}, config?: AxiosRequestConfig): Promise<${responseType}> {
159
+ export async function ${camelCase(operationId)}(props: ${
160
+ hasData ? `${dataType} & { axiosConfig?: AxiosRequestConfig; }` : "{ axiosConfig?: AxiosRequestConfig }"
161
+ } ): Promise<${responseType}> {
159
162
  ${methodBody}
160
163
  }`;
161
164
  }
@@ -39,16 +39,14 @@ function generateQueryOptions(operation: OperationInfo, spec: OpenAPIV3.Document
39
39
  const namedQuery = camelCase(`${operationId}`);
40
40
 
41
41
  return `
42
- export const ${namedQueryOptions} = (
43
- ${hasData ? `params: Partial<Parameters<typeof apiClient.${namedQuery}>[0]>, config?: Partial<Parameters<typeof apiClient.${namedQuery}>[1]>` : `_?: undefined, config?: Partial<Parameters<typeof apiClient.${namedQuery}>[1]>`}
42
+ export const ${namedQueryOptions} = (
43
+ ${hasData ? `props: Partial<Parameters<typeof apiClient.${namedQuery}>[0]>` : `props?: Partial<Parameters<typeof apiClient.${namedQuery}>[0]>`}
44
44
  ) => {
45
+ ${hasData ? "const { axiosConfig, ...params } = props || {};" : "const { axiosConfig } = props || {};"}
45
46
  const enabled = ${hasData ? `hasDefinedProps(params, ${requiredParams.join(", ")})` : "true"};
46
47
  return queryOptions({
47
- queryKey: ['${camelCase(operationId)}', ${hasData ? "params" : "undefined"}],
48
- queryFn: enabled ? async () => {
49
- const response = await apiClient.${namedQuery}(${hasData ? "params" : "undefined"}, config);
50
- return response.data;
51
- } : skipToken,
48
+ queryKey: ['${camelCase(operationId)}', ${hasData ? "params" : ""}],
49
+ queryFn: enabled ? () => apiClient.${namedQuery}(${hasData ? "{...params, axiosConfig}" : "{axiosConfig}"}) : skipToken,
52
50
  });
53
51
  };`;
54
52
  }