@povio/openapi-codegen-cli 4.0.0-rc.3 → 4.0.0-rc.5
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/README.md +38 -11
- package/dist/{config-CiLFKLAY.d.mts → config-D8ANLlqk.d.mts} +1 -1
- package/dist/config.d.mts +2 -2
- package/dist/{generate.runner-Sh0Ps6AW.mjs → generate.runner-LDL6Lbgl.mjs} +8 -110
- package/dist/{generate.utils-BJxdEik8.cjs → generate.utils-BFeGvk98.cjs} +4 -269
- package/dist/{generateCodeFromOpenAPIDoc-B8FAsxWE.mjs → generateCodeFromOpenAPIDoc-B7VWwIbb.mjs} +198 -502
- package/dist/{generateCodeFromOpenAPIDoc-Bbi8fYi2.cjs → generateCodeFromOpenAPIDoc-CpAdx6I3.cjs} +241 -528
- package/dist/generator.d.mts +1 -1
- package/dist/generator.mjs +3 -3
- package/dist/{getDataFromOpenAPIDoc-CXYNMMMt.mjs → getDataFromOpenAPIDoc-C0iehdg8.mjs} +19 -2
- package/dist/index.d.mts +4 -4
- package/dist/metro.cjs +4 -3
- package/dist/metro.d.mts +3 -3
- package/dist/metro.mjs +1 -1
- package/dist/openapi-codegen-native-darwin-arm64.node +0 -0
- package/dist/openapi-codegen-native-linux-x64.node +0 -0
- package/dist/openapi-codegen-native-win32-x64.node +0 -0
- package/dist/{openapi-codegen.runner-bJTFHTdn.mjs → openapi-codegen.runner-CmW9oZdT.mjs} +1 -1
- package/dist/{openapi-source.runner-DIOvAX_R.d.mts → openapi-source.runner-DES1kzvS.d.mts} +1 -1
- package/dist/{options-X0zju7gw.d.mts → options-B7-eMFcv.d.mts} +4 -10
- package/dist/query.d.mts +3 -3
- package/dist/queryConfig.context-DwVQWbtL.d.mts +71 -0
- package/dist/sh.mjs +8 -8
- package/dist/tiny.d.mts +1 -1
- package/dist/{useMutationEffects-CJJuL9Ak.d.mts → useMutationEffects-I4y5WUkj.d.mts} +1 -1
- package/dist/vite.d.mts +3 -3
- package/dist/vite.mjs +1 -1
- package/package.json +4 -4
- package/dist/queryConfig.context-DjhzewdL.d.mts +0 -56
package/dist/{generateCodeFromOpenAPIDoc-Bbi8fYi2.cjs → generateCodeFromOpenAPIDoc-CpAdx6I3.cjs}
RENAMED
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
const require_generate_utils = require("./generate.utils-
|
|
1
|
+
const require_generate_utils = require("./generate.utils-BFeGvk98.cjs");
|
|
2
|
+
let openapi_types = require("openapi-types");
|
|
2
3
|
//#region src/generators/const/acl.const.ts
|
|
3
4
|
const ACL_APP_ABILITY_FILE = {
|
|
4
5
|
fileName: "acl/app.ability",
|
|
@@ -24,6 +25,22 @@ const CASL_ABILITY_IMPORT = {
|
|
|
24
25
|
from: "@casl/ability"
|
|
25
26
|
};
|
|
26
27
|
//#endregion
|
|
28
|
+
//#region src/generators/utils/endpoint.utils.ts
|
|
29
|
+
const isGetEndpoint = (endpoint) => endpoint.method === openapi_types.OpenAPIV3.HttpMethods.GET;
|
|
30
|
+
const isPaginatedGetEndpoint = (endpoint, options) => isGetEndpoint(endpoint) && Object.values(options.infiniteQueryParamNames).every((infiniteQueryParam) => endpoint.parameters.some((param) => param.name === infiniteQueryParam && param.type === "Query"));
|
|
31
|
+
//#endregion
|
|
32
|
+
//#region src/generators/utils/query.utils.ts
|
|
33
|
+
const isQuery = (endpoint) => isGetEndpoint(endpoint);
|
|
34
|
+
const isMutation = (endpoint) => !isGetEndpoint(endpoint) || !!endpoint.mediaDownload;
|
|
35
|
+
const isInfiniteQuery = (endpoint, options) => isPaginatedGetEndpoint(endpoint, options);
|
|
36
|
+
const getDestructuredVariables = (resolver, endpoint, updateQueryEndpoints) => {
|
|
37
|
+
const requiredUpdateQueryParams = updateQueryEndpoints.reduce((acc, updateEndpoint) => [...acc, ...require_generate_utils.mapEndpointParamsToFunctionParams(resolver, updateEndpoint, { includeOnlyRequiredParams: true }).map((param) => param.name)], []);
|
|
38
|
+
return require_generate_utils.mapEndpointParamsToFunctionParams(resolver, endpoint, {
|
|
39
|
+
includeOnlyRequiredParams: true,
|
|
40
|
+
excludeBodyParam: true
|
|
41
|
+
}).filter((param) => requiredUpdateQueryParams.includes(param.name)).map((param) => param.name);
|
|
42
|
+
};
|
|
43
|
+
//#endregion
|
|
27
44
|
//#region src/generators/core/zod/getZodChain.ts
|
|
28
45
|
const zodChainCache = /* @__PURE__ */ new WeakMap();
|
|
29
46
|
function getZodChain({ schema, meta, options }) {
|
|
@@ -758,7 +775,7 @@ function getEndpointAclConditionPropertyType({ resolver, endpoint, acl, name })
|
|
|
758
775
|
if (matchingMediaType) {
|
|
759
776
|
schema = bodyParameter?.bodyObject?.content?.[matchingMediaType]?.schema;
|
|
760
777
|
required = bodyParameter?.bodyObject?.required;
|
|
761
|
-
info = `${
|
|
778
|
+
info = `${isQuery(endpoint) ? "query" : "mutation"} data`;
|
|
762
779
|
if (pathSplits[index]?.startsWith("$")) index++;
|
|
763
780
|
}
|
|
764
781
|
}
|
|
@@ -1144,7 +1161,7 @@ function isOperationExcluded(operation, options) {
|
|
|
1144
1161
|
const isIncluded = require_generate_utils.isTagIncluded(tag, options);
|
|
1145
1162
|
return isDeprecated || !isIncluded;
|
|
1146
1163
|
}
|
|
1147
|
-
function getOperationName({ path, method, operation, options, tag, keepOperationTag, keepOperationPrefix }) {
|
|
1164
|
+
function getOperationName$1({ path, method, operation, options, tag, keepOperationTag, keepOperationPrefix }) {
|
|
1148
1165
|
return resolveOperationName({
|
|
1149
1166
|
path,
|
|
1150
1167
|
method,
|
|
@@ -1210,13 +1227,13 @@ function getUniqueOperationName({ operationsByTag, nameIndexes, ...params }) {
|
|
|
1210
1227
|
const { operation, options } = params;
|
|
1211
1228
|
const tag = options.splitByTags ? require_generate_utils.getOperationTag(operation, options) : options.defaultTag;
|
|
1212
1229
|
const operationName = (keepOperationTag, nameIndex) => {
|
|
1213
|
-
const name = getOperationName({
|
|
1230
|
+
const name = getOperationName$1({
|
|
1214
1231
|
...params,
|
|
1215
1232
|
tag,
|
|
1216
1233
|
keepOperationTag
|
|
1217
1234
|
});
|
|
1218
1235
|
if (nameIndex) {
|
|
1219
|
-
const pathSourceMatches = nameIndex.pathSourceCount > 0 && getOperationName({
|
|
1236
|
+
const pathSourceMatches = nameIndex.pathSourceCount > 0 && getOperationName$1({
|
|
1220
1237
|
...params,
|
|
1221
1238
|
operation: {},
|
|
1222
1239
|
tag,
|
|
@@ -1226,7 +1243,7 @@ function getUniqueOperationName({ operationsByTag, nameIndexes, ...params }) {
|
|
|
1226
1243
|
return (nameIndex.stableCounts.get(name) ?? 0) + (pathSourceMatches ? nameIndex.pathSourceCount : 0) + (pathFallbackMatches ? nameIndex.reservedFallbackCount : 0) === 1 ? name : void 0;
|
|
1227
1244
|
}
|
|
1228
1245
|
let matchingOperations = 0;
|
|
1229
|
-
for (const candidate of operationsByTag[tag]) if (getOperationName({
|
|
1246
|
+
for (const candidate of operationsByTag[tag]) if (getOperationName$1({
|
|
1230
1247
|
...params,
|
|
1231
1248
|
operation: candidate,
|
|
1232
1249
|
tag,
|
|
@@ -1237,7 +1254,7 @@ function getUniqueOperationName({ operationsByTag, nameIndexes, ...params }) {
|
|
|
1237
1254
|
}
|
|
1238
1255
|
if (matchingOperations === 1) return name;
|
|
1239
1256
|
};
|
|
1240
|
-
return operationName(void 0, nameIndexes?.[0]) ?? operationName(true, nameIndexes?.[1]) ?? getOperationName({
|
|
1257
|
+
return operationName(void 0, nameIndexes?.[0]) ?? operationName(true, nameIndexes?.[1]) ?? getOperationName$1({
|
|
1241
1258
|
...params,
|
|
1242
1259
|
tag,
|
|
1243
1260
|
keepOperationTag: true,
|
|
@@ -2021,6 +2038,35 @@ function getTagElement$1(tag, data) {
|
|
|
2021
2038
|
return data.get(tag);
|
|
2022
2039
|
}
|
|
2023
2040
|
//#endregion
|
|
2041
|
+
//#region src/generators/utils/generate/generate.query.utils.ts
|
|
2042
|
+
const operationNameByEndpoint = /* @__PURE__ */ new WeakMap();
|
|
2043
|
+
const capitalizedOperationNameByEndpoint = /* @__PURE__ */ new WeakMap();
|
|
2044
|
+
function getOperationName(endpoint) {
|
|
2045
|
+
let name = operationNameByEndpoint.get(endpoint);
|
|
2046
|
+
if (name === void 0) {
|
|
2047
|
+
name = require_generate_utils.snakeToCamel(endpoint.operationName);
|
|
2048
|
+
operationNameByEndpoint.set(endpoint, name);
|
|
2049
|
+
}
|
|
2050
|
+
return name;
|
|
2051
|
+
}
|
|
2052
|
+
function getCapitalizedOperationName(endpoint) {
|
|
2053
|
+
let name = capitalizedOperationNameByEndpoint.get(endpoint);
|
|
2054
|
+
if (name === void 0) {
|
|
2055
|
+
name = require_generate_utils.capitalize(getOperationName(endpoint));
|
|
2056
|
+
capitalizedOperationNameByEndpoint.set(endpoint, name);
|
|
2057
|
+
}
|
|
2058
|
+
return name;
|
|
2059
|
+
}
|
|
2060
|
+
const getQueryName = (endpoint, mutation) => {
|
|
2061
|
+
const addMutationSuffix = isQuery(endpoint) && isMutation(endpoint) && mutation;
|
|
2062
|
+
return `use${getCapitalizedOperationName(endpoint)}${addMutationSuffix ? "Mutation" : ""}`;
|
|
2063
|
+
};
|
|
2064
|
+
const getInfiniteQueryName = (endpoint) => `use${getCapitalizedOperationName(endpoint)}Infinite`;
|
|
2065
|
+
const getQueryOptionsName = (endpoint) => `${getOperationName(endpoint)}QueryOptions`;
|
|
2066
|
+
const getInfiniteQueryOptionsName = (endpoint) => `${getOperationName(endpoint)}InfiniteQueryOptions`;
|
|
2067
|
+
const getPrefetchQueryName = (endpoint) => `prefetch${getCapitalizedOperationName(endpoint)}`;
|
|
2068
|
+
const getPrefetchInfiniteQueryName = (endpoint) => `prefetch${getCapitalizedOperationName(endpoint)}Infinite`;
|
|
2069
|
+
//#endregion
|
|
2024
2070
|
//#region src/generators/generate/generateAcl.ts
|
|
2025
2071
|
function generateAcl({ resolver, data, tag }) {
|
|
2026
2072
|
const nativeContent = resolver.getNativeRenderedAcl?.(tag);
|
|
@@ -2043,9 +2089,9 @@ function generateAcl({ resolver, data, tag }) {
|
|
|
2043
2089
|
from: require_generate_utils.CONFIG_PACKAGE_IMPORT_PATH
|
|
2044
2090
|
};
|
|
2045
2091
|
const lines = [];
|
|
2046
|
-
lines.push(renderImport$
|
|
2047
|
-
if (hasWorkspaceContext) lines.push(renderImport$
|
|
2048
|
-
for (const modelsImport of modelsImports) lines.push(renderImport$
|
|
2092
|
+
lines.push(renderImport$3(caslAbilityTupleImport));
|
|
2093
|
+
if (hasWorkspaceContext) lines.push(renderImport$3(workspaceContextImport));
|
|
2094
|
+
for (const modelsImport of modelsImports) lines.push(renderImport$3(modelsImport));
|
|
2049
2095
|
lines.push("");
|
|
2050
2096
|
if (resolver.options.tsNamespaces) lines.push(`export namespace ${require_generate_utils.getNamespaceName({
|
|
2051
2097
|
type: "acl",
|
|
@@ -2081,8 +2127,8 @@ function generateAppAcl({ resolver, data }) {
|
|
|
2081
2127
|
typeOnly: true
|
|
2082
2128
|
};
|
|
2083
2129
|
const lines = [];
|
|
2084
|
-
lines.push(renderImport$
|
|
2085
|
-
for (const modelsImport of modelsImports) lines.push(renderImport$
|
|
2130
|
+
lines.push(renderImport$3(caslAbilityTupleImport));
|
|
2131
|
+
for (const modelsImport of modelsImports) lines.push(renderImport$3(modelsImport));
|
|
2086
2132
|
lines.push("");
|
|
2087
2133
|
if (appAbilitiesType) {
|
|
2088
2134
|
lines.push(`export type ${ACL_APP_ABILITIES} = `);
|
|
@@ -2092,7 +2138,7 @@ function generateAppAcl({ resolver, data }) {
|
|
|
2092
2138
|
lines.push(`export type AppAbility = PureAbility<${ACL_APP_ABILITIES}>;`);
|
|
2093
2139
|
return lines.join("\n");
|
|
2094
2140
|
}
|
|
2095
|
-
function renderImport$
|
|
2141
|
+
function renderImport$3(importData) {
|
|
2096
2142
|
const namedImports = [...importData.bindings, ...(importData.typeBindings ?? []).map((binding) => importData.typeOnly ? binding : `type ${binding}`)];
|
|
2097
2143
|
const names = [...importData.defaultImport ? [importData.defaultImport] : [], ...namedImports.length > 0 ? [`{ ${namedImports.join(", ")} }`] : []].join(", ");
|
|
2098
2144
|
return `import${importData.typeOnly ? " type" : ""} ${names} from "${importData.from}";`;
|
|
@@ -2136,7 +2182,7 @@ function renderAbilityFunction({ resolver, endpoint }) {
|
|
|
2136
2182
|
const abilityConditionsTypes = require_generate_utils.getAbilityConditionsTypes(endpoint) ?? [];
|
|
2137
2183
|
const hasConditions = require_generate_utils.hasAbilityConditions(endpoint);
|
|
2138
2184
|
const lines = [];
|
|
2139
|
-
const abilityQuery = endpoint.method === "get" ? endpoint.mediaDownload ? `\`${
|
|
2185
|
+
const abilityQuery = endpoint.method === "get" ? endpoint.mediaDownload ? `\`${getQueryName(endpoint)}\` query or \`${getQueryName(endpoint, true)}\` mutation` : `\`${getQueryName(endpoint)}\` query` : `\`${getQueryName(endpoint)}\` mutation`;
|
|
2140
2186
|
lines.push("/**");
|
|
2141
2187
|
lines.push(` * Use for ${abilityQuery} ability. ${hasConditions ? "For global ability, omit the object parameter." : ""}${require_generate_utils.getAbilityDescription(endpoint) ? "" : ""}`);
|
|
2142
2188
|
if (require_generate_utils.getAbilityDescription(endpoint)) lines.push(` * @description ${require_generate_utils.getAbilityDescription(endpoint)}`);
|
|
@@ -2163,495 +2209,6 @@ function renderConditionType(resolver, endpoint, propertyType) {
|
|
|
2163
2209
|
return require_generate_utils.getAbilityConditionType(propertyType, resolver, require_generate_utils.getEndpointTag(endpoint, resolver.options));
|
|
2164
2210
|
}
|
|
2165
2211
|
//#endregion
|
|
2166
|
-
//#region src/generators/const/buildConfigs.const.ts
|
|
2167
|
-
const BUILD_CONFIG_SUFFIX = "config";
|
|
2168
|
-
//#endregion
|
|
2169
|
-
//#region src/generators/utils/generate/generate.configs.utils.ts
|
|
2170
|
-
function getBuilderConfigs({ data, tag, resolver }) {
|
|
2171
|
-
const endpoints = data.get(tag)?.endpoints;
|
|
2172
|
-
if (!endpoints || endpoints.length === 0) return {
|
|
2173
|
-
configs: [],
|
|
2174
|
-
hasZodImport: false,
|
|
2175
|
-
modelsImports: [],
|
|
2176
|
-
queriesImports: [],
|
|
2177
|
-
aclImports: []
|
|
2178
|
-
};
|
|
2179
|
-
const extendedEndpoints = endpoints.map((endpoint) => ({
|
|
2180
|
-
...endpoint,
|
|
2181
|
-
pathSegments: require_generate_utils.getPathSegments(endpoint.path)
|
|
2182
|
-
}));
|
|
2183
|
-
const namedReadAllEndpoints = resolveBuilderConfigNames(extendedEndpoints.filter((endpoint) => require_generate_utils.isReadAllEndpoint(endpoint, resolver.options)));
|
|
2184
|
-
let hasZodImport = false;
|
|
2185
|
-
const importedZodSchemas = [];
|
|
2186
|
-
const importedEndpoints = [];
|
|
2187
|
-
const importedInfiniteEndpoints = [];
|
|
2188
|
-
const configs = Object.entries(namedReadAllEndpoints).map(([name, readAllEndpoint]) => {
|
|
2189
|
-
const columnsConfig = getColumnsConfig(resolver, readAllEndpoint);
|
|
2190
|
-
if (!columnsConfig) return;
|
|
2191
|
-
importedEndpoints.push(readAllEndpoint);
|
|
2192
|
-
importedInfiniteEndpoints.push(readAllEndpoint);
|
|
2193
|
-
if (columnsConfig.zodSchema) {
|
|
2194
|
-
if (require_generate_utils.isNamedZodSchema(columnsConfig.zodSchema)) importedZodSchemas.push(columnsConfig.zodSchema);
|
|
2195
|
-
else hasZodImport = true;
|
|
2196
|
-
}
|
|
2197
|
-
if (columnsConfig.sortableEnumSchemaName) importedZodSchemas.push(columnsConfig.sortableEnumSchemaName);
|
|
2198
|
-
const filter = readAllEndpoint.parameters.find((param) => param.name === resolver.options.filterParamName);
|
|
2199
|
-
if (filter) importedZodSchemas.push(filter.zodSchema);
|
|
2200
|
-
const config = {
|
|
2201
|
-
name,
|
|
2202
|
-
title: require_generate_utils.capitalize(require_generate_utils.camelToSpaceSeparated(name.replace(/config$/i, ""))),
|
|
2203
|
-
readAll: {
|
|
2204
|
-
acl: getAclConfig(readAllEndpoint, resolver.options),
|
|
2205
|
-
paginated: require_generate_utils.getImportedQueryName(readAllEndpoint, resolver.options),
|
|
2206
|
-
infinite: resolver.options.infiniteQueries ? require_generate_utils.getImportedInfiniteQueryName(readAllEndpoint, resolver.options) : void 0,
|
|
2207
|
-
filters: getInputsConfig(resolver, filter),
|
|
2208
|
-
columns: columnsConfig.columns
|
|
2209
|
-
}
|
|
2210
|
-
};
|
|
2211
|
-
const readEndpoint = extendedEndpoints.find((endpoint) => require_generate_utils.isReadEndpoint(endpoint, readAllEndpoint));
|
|
2212
|
-
if (readEndpoint) {
|
|
2213
|
-
importedEndpoints.push(readEndpoint);
|
|
2214
|
-
let responseSchema = readEndpoint.response;
|
|
2215
|
-
if (require_generate_utils.isNamedZodSchema(responseSchema)) {
|
|
2216
|
-
importedZodSchemas.push(responseSchema);
|
|
2217
|
-
responseSchema = require_generate_utils.getImportedZodSchemaName(resolver, responseSchema);
|
|
2218
|
-
} else hasZodImport = true;
|
|
2219
|
-
config.read = {
|
|
2220
|
-
acl: getAclConfig(readEndpoint, resolver.options),
|
|
2221
|
-
schema: responseSchema,
|
|
2222
|
-
query: require_generate_utils.getImportedQueryName(readEndpoint, resolver.options)
|
|
2223
|
-
};
|
|
2224
|
-
}
|
|
2225
|
-
const createEndpoint = extendedEndpoints.find((endpoint) => require_generate_utils.isCreateEndpoint(endpoint, readAllEndpoint));
|
|
2226
|
-
if (createEndpoint) {
|
|
2227
|
-
importedEndpoints.push(createEndpoint);
|
|
2228
|
-
const body = require_generate_utils.getEndpointBody(createEndpoint);
|
|
2229
|
-
if (body) {
|
|
2230
|
-
importedZodSchemas.push(body.zodSchema);
|
|
2231
|
-
config.create = {
|
|
2232
|
-
acl: getAclConfig(createEndpoint, resolver.options),
|
|
2233
|
-
schema: require_generate_utils.getImportedZodSchemaName(resolver, body.zodSchema),
|
|
2234
|
-
mutation: createEndpoint,
|
|
2235
|
-
inputDefs: getInputsConfig(resolver, body)
|
|
2236
|
-
};
|
|
2237
|
-
}
|
|
2238
|
-
}
|
|
2239
|
-
const updateEndpoint = extendedEndpoints.find((endpoint) => require_generate_utils.isUpdateEndpoint(endpoint, readAllEndpoint));
|
|
2240
|
-
if (updateEndpoint) {
|
|
2241
|
-
importedEndpoints.push(updateEndpoint);
|
|
2242
|
-
const body = require_generate_utils.getEndpointBody(updateEndpoint);
|
|
2243
|
-
if (body) {
|
|
2244
|
-
importedZodSchemas.push(body.zodSchema);
|
|
2245
|
-
config.update = {
|
|
2246
|
-
acl: getAclConfig(updateEndpoint, resolver.options),
|
|
2247
|
-
schema: require_generate_utils.getImportedZodSchemaName(resolver, body.zodSchema),
|
|
2248
|
-
mutation: updateEndpoint,
|
|
2249
|
-
inputDefs: getInputsConfig(resolver, body)
|
|
2250
|
-
};
|
|
2251
|
-
}
|
|
2252
|
-
}
|
|
2253
|
-
const deleteEndpoint = extendedEndpoints.find((endpoint) => require_generate_utils.isDeleteEndpoint(endpoint, readAllEndpoint));
|
|
2254
|
-
if (deleteEndpoint) {
|
|
2255
|
-
importedEndpoints.push(deleteEndpoint);
|
|
2256
|
-
const body = require_generate_utils.getEndpointBody(deleteEndpoint);
|
|
2257
|
-
if (body) {
|
|
2258
|
-
importedZodSchemas.push(body.zodSchema);
|
|
2259
|
-
config.delete = {
|
|
2260
|
-
acl: getAclConfig(deleteEndpoint, resolver.options),
|
|
2261
|
-
mutation: deleteEndpoint
|
|
2262
|
-
};
|
|
2263
|
-
}
|
|
2264
|
-
}
|
|
2265
|
-
const bulkDeleteEndpoint = extendedEndpoints.find((endpoint) => require_generate_utils.isBulkDeleteEndpoint(endpoint, readAllEndpoint));
|
|
2266
|
-
if (bulkDeleteEndpoint) {
|
|
2267
|
-
importedEndpoints.push(bulkDeleteEndpoint);
|
|
2268
|
-
const body = require_generate_utils.getEndpointBody(bulkDeleteEndpoint);
|
|
2269
|
-
if (body) {
|
|
2270
|
-
importedZodSchemas.push(body.zodSchema);
|
|
2271
|
-
config.bulkDelete = {
|
|
2272
|
-
acl: getAclConfig(bulkDeleteEndpoint, resolver.options),
|
|
2273
|
-
schema: require_generate_utils.getImportedZodSchemaName(resolver, body.zodSchema),
|
|
2274
|
-
mutation: bulkDeleteEndpoint,
|
|
2275
|
-
inputDefs: getInputsConfig(resolver, body)
|
|
2276
|
-
};
|
|
2277
|
-
}
|
|
2278
|
-
}
|
|
2279
|
-
return config;
|
|
2280
|
-
});
|
|
2281
|
-
const modelsImports = require_generate_utils.getModelsImports({
|
|
2282
|
-
resolver,
|
|
2283
|
-
tag,
|
|
2284
|
-
zodSchemas: importedZodSchemas
|
|
2285
|
-
});
|
|
2286
|
-
const queriesImports = require_generate_utils.getQueriesImports({
|
|
2287
|
-
tag,
|
|
2288
|
-
endpoints: importedEndpoints,
|
|
2289
|
-
options: resolver.options
|
|
2290
|
-
});
|
|
2291
|
-
const infiniteQueriesImports = resolver.options.infiniteQueries ? require_generate_utils.getInfiniteQueriesImports({
|
|
2292
|
-
tag,
|
|
2293
|
-
endpoints: importedInfiniteEndpoints,
|
|
2294
|
-
options: resolver.options
|
|
2295
|
-
}) : [];
|
|
2296
|
-
const aclImports = require_generate_utils.getAclImports({
|
|
2297
|
-
tag,
|
|
2298
|
-
endpoints: importedEndpoints.filter((endpoint) => endpoint.acl),
|
|
2299
|
-
options: resolver.options
|
|
2300
|
-
});
|
|
2301
|
-
return {
|
|
2302
|
-
configs: configs.filter(Boolean),
|
|
2303
|
-
hasZodImport,
|
|
2304
|
-
modelsImports,
|
|
2305
|
-
queriesImports: require_generate_utils.mergeImports(resolver.options, queriesImports, infiniteQueriesImports),
|
|
2306
|
-
aclImports
|
|
2307
|
-
};
|
|
2308
|
-
}
|
|
2309
|
-
function resolveBuilderConfigNames(endpoints) {
|
|
2310
|
-
const sortedEndpoints = endpoints.map((endpoint) => ({
|
|
2311
|
-
...endpoint,
|
|
2312
|
-
namePathSegments: endpoint.pathSegments.filter((segment) => !require_generate_utils.isPathSegmentParam(segment))
|
|
2313
|
-
})).toSorted((a, b) => a.namePathSegments.length - b.namePathSegments.length);
|
|
2314
|
-
const namedEndpoints = {};
|
|
2315
|
-
for (const endpoint of sortedEndpoints) {
|
|
2316
|
-
const namePathSegments = endpoint.namePathSegments;
|
|
2317
|
-
let name = namePathSegments.length > 0 ? `${require_generate_utils.kebabToCamel(namePathSegments.pop())}${require_generate_utils.capitalize(BUILD_CONFIG_SUFFIX)}` : BUILD_CONFIG_SUFFIX;
|
|
2318
|
-
while (namedEndpoints[name]) {
|
|
2319
|
-
if (namePathSegments.length === 0) throw new Error(`Can't uniquely resolve builder config name: ${name}`);
|
|
2320
|
-
name = `${require_generate_utils.kebabToCamel(namePathSegments.pop())}${require_generate_utils.capitalize(name)}`;
|
|
2321
|
-
}
|
|
2322
|
-
namedEndpoints[name] = endpoint;
|
|
2323
|
-
}
|
|
2324
|
-
return namedEndpoints;
|
|
2325
|
-
}
|
|
2326
|
-
function getAclConfig(endpoint, options) {
|
|
2327
|
-
return options.acl && endpoint.acl ? require_generate_utils.getImportedAbilityFunctionName(endpoint, options) : void 0;
|
|
2328
|
-
}
|
|
2329
|
-
function getInputsConfig(resolver, endpointParameter) {
|
|
2330
|
-
if (!endpointParameter) return;
|
|
2331
|
-
let schema;
|
|
2332
|
-
if (endpointParameter.type === "Body") schema = endpointParameter.bodyObject?.content?.[require_generate_utils.JSON_APPLICATION_FORMAT]?.schema;
|
|
2333
|
-
else schema = endpointParameter.parameterObject?.schema;
|
|
2334
|
-
if (!schema) return;
|
|
2335
|
-
const schemaObj = resolver.resolveObject(schema);
|
|
2336
|
-
const inputs = Object.keys(schemaObj?.properties ?? {}).reduce((acc, key) => ({
|
|
2337
|
-
...acc,
|
|
2338
|
-
[key]: true
|
|
2339
|
-
}), {});
|
|
2340
|
-
return {
|
|
2341
|
-
schema: require_generate_utils.getImportedZodSchemaName(resolver, endpointParameter.zodSchema),
|
|
2342
|
-
options: { inputs }
|
|
2343
|
-
};
|
|
2344
|
-
}
|
|
2345
|
-
function resolveReadAllItemZodSchema(resolver, endpoint, itemSchema, itemsPropertyKey) {
|
|
2346
|
-
if (require_generate_utils.isReferenceObject(itemSchema)) return resolver.getZodSchemaNameByRef(itemSchema.$ref);
|
|
2347
|
-
if (require_generate_utils.isNamedZodSchema(endpoint.response)) return `${require_generate_utils.getImportedZodSchemaName(resolver, endpoint.response)}.shape.${itemsPropertyKey}.element`;
|
|
2348
|
-
return require_generate_utils.ANY_SCHEMA;
|
|
2349
|
-
}
|
|
2350
|
-
function getColumnsConfig(resolver, endpoint) {
|
|
2351
|
-
const endpointResponse = endpoint.responseObject;
|
|
2352
|
-
if (!endpointResponse) return;
|
|
2353
|
-
const schema = endpointResponse.content?.[require_generate_utils.JSON_APPLICATION_FORMAT]?.schema;
|
|
2354
|
-
if (!schema) return;
|
|
2355
|
-
const properties = getSchemaProperties(resolver, schema);
|
|
2356
|
-
const arrayPropertyKeys = Object.keys(properties).filter((key) => resolver.resolveObject(properties[key]).type === "array");
|
|
2357
|
-
arrayPropertyKeys.sort((a, b) => {
|
|
2358
|
-
const aIndex = resolver.options.dataResponseParamNames.indexOf(a);
|
|
2359
|
-
const bIndex = resolver.options.dataResponseParamNames.indexOf(b);
|
|
2360
|
-
if (aIndex !== -1 && bIndex !== -1) return aIndex - bIndex;
|
|
2361
|
-
if (aIndex !== -1) return -1;
|
|
2362
|
-
if (bIndex !== -1) return 1;
|
|
2363
|
-
return a.localeCompare(b);
|
|
2364
|
-
});
|
|
2365
|
-
const propertyKey = arrayPropertyKeys[0];
|
|
2366
|
-
if (!propertyKey) return;
|
|
2367
|
-
const dataSchemaObj = resolver.resolveObject(properties[propertyKey]);
|
|
2368
|
-
if (!dataSchemaObj || dataSchemaObj.type !== "array") return;
|
|
2369
|
-
const itemSchema = dataSchemaObj.items;
|
|
2370
|
-
const itemSchemaObj = resolver.resolveObject(itemSchema);
|
|
2371
|
-
if (!itemSchemaObj) return;
|
|
2372
|
-
const zodSchema = resolveReadAllItemZodSchema(resolver, endpoint, itemSchema, propertyKey);
|
|
2373
|
-
const columns = Object.keys(itemSchemaObj?.properties ?? {}).reduce((acc, key) => ({
|
|
2374
|
-
...acc,
|
|
2375
|
-
[key]: true
|
|
2376
|
-
}), {});
|
|
2377
|
-
const sortableEnumSchemaName = endpoint.parameters.find((param) => param.parameterSortingEnumSchemaName)?.parameterSortingEnumSchemaName;
|
|
2378
|
-
return {
|
|
2379
|
-
columns: {
|
|
2380
|
-
schema: require_generate_utils.getImportedZodSchemaName(resolver, zodSchema),
|
|
2381
|
-
options: {
|
|
2382
|
-
columns,
|
|
2383
|
-
sortable: sortableEnumSchemaName ? require_generate_utils.getImportedZodSchemaName(resolver, sortableEnumSchemaName) : void 0
|
|
2384
|
-
}
|
|
2385
|
-
},
|
|
2386
|
-
zodSchema,
|
|
2387
|
-
sortableEnumSchemaName
|
|
2388
|
-
};
|
|
2389
|
-
}
|
|
2390
|
-
function getSchemaProperties(resolver, schema) {
|
|
2391
|
-
const schemaObj = resolver.resolveObject(schema);
|
|
2392
|
-
if (schemaObj.properties) return schemaObj.properties;
|
|
2393
|
-
else if ("allOf" in schemaObj && schemaObj.allOf) return schemaObj.allOf.reduce((acc, cur) => ({
|
|
2394
|
-
...acc,
|
|
2395
|
-
...getSchemaProperties(resolver, cur)
|
|
2396
|
-
}), {});
|
|
2397
|
-
return {};
|
|
2398
|
-
}
|
|
2399
|
-
//#endregion
|
|
2400
|
-
//#region src/generators/const/queries.const.ts
|
|
2401
|
-
const QUERY_HOOKS = {
|
|
2402
|
-
query: "useQuery",
|
|
2403
|
-
infiniteQuery: "useInfiniteQuery",
|
|
2404
|
-
mutation: "useMutation"
|
|
2405
|
-
};
|
|
2406
|
-
const QUERY_IMPORT = {
|
|
2407
|
-
bindings: [
|
|
2408
|
-
QUERY_HOOKS.query,
|
|
2409
|
-
QUERY_HOOKS.infiniteQuery,
|
|
2410
|
-
QUERY_HOOKS.mutation
|
|
2411
|
-
],
|
|
2412
|
-
from: "@tanstack/react-query"
|
|
2413
|
-
};
|
|
2414
|
-
const QUERIES_MODULE_NAME = "moduleName";
|
|
2415
|
-
//#endregion
|
|
2416
|
-
//#region src/generators/generate/generateConfigs.ts
|
|
2417
|
-
function generateConfigs(generateTypeParams) {
|
|
2418
|
-
const { configs, hasZodImport, modelsImports, queriesImports, aclImports } = getBuilderConfigs(generateTypeParams);
|
|
2419
|
-
if (configs.length === 0) return;
|
|
2420
|
-
const { resolver, tag } = generateTypeParams;
|
|
2421
|
-
const endpoints = configs.flatMap((config) => [
|
|
2422
|
-
config.create?.mutation,
|
|
2423
|
-
config.update?.mutation,
|
|
2424
|
-
config.delete?.mutation,
|
|
2425
|
-
config.bulkDelete?.mutation
|
|
2426
|
-
]).filter((m) => typeof m !== "string" && m !== void 0);
|
|
2427
|
-
const hasMutation = endpoints.length > 0;
|
|
2428
|
-
const hasAclCheck = resolver.options.checkAcl && endpoints.some((e) => e.acl);
|
|
2429
|
-
const hasMutationEffects = resolver.options.mutationEffects && hasMutation;
|
|
2430
|
-
const hasMutationDefaultOnError = resolver.options.mutationDefaultOnError && hasMutation;
|
|
2431
|
-
const hasAxiosRequestConfig = resolver.options.axiosRequestConfig;
|
|
2432
|
-
const nativeClient = resolver.options.restClient === "native";
|
|
2433
|
-
const hasAxiosDefaultImport = !nativeClient && endpoints.some((e) => e.mediaUpload);
|
|
2434
|
-
const hasAxiosImport = !nativeClient && (hasAxiosRequestConfig || hasAxiosDefaultImport);
|
|
2435
|
-
const axiosImport = {
|
|
2436
|
-
defaultImport: hasAxiosDefaultImport ? require_generate_utils.AXIOS_DEFAULT_IMPORT_NAME : void 0,
|
|
2437
|
-
bindings: [],
|
|
2438
|
-
typeBindings: hasAxiosImport ? [require_generate_utils.AXIOS_REQUEST_CONFIG_TYPE] : [],
|
|
2439
|
-
from: "axios"
|
|
2440
|
-
};
|
|
2441
|
-
const nativeImport = {
|
|
2442
|
-
bindings: [],
|
|
2443
|
-
typeBindings: nativeClient && hasAxiosRequestConfig ? [require_generate_utils.getRequestConfigTypeName("native")] : [],
|
|
2444
|
-
from: require_generate_utils.REST_PACKAGE_IMPORT_PATH
|
|
2445
|
-
};
|
|
2446
|
-
const appRestClientImport = {
|
|
2447
|
-
bindings: [require_generate_utils.APP_REST_CLIENT_NAME],
|
|
2448
|
-
from: require_generate_utils.getAppRestClientImportPath(resolver.options)
|
|
2449
|
-
};
|
|
2450
|
-
const endpointsImports = require_generate_utils.getEndpointsImports({
|
|
2451
|
-
tag,
|
|
2452
|
-
endpoints,
|
|
2453
|
-
options: resolver.options
|
|
2454
|
-
});
|
|
2455
|
-
const queryImport = {
|
|
2456
|
-
bindings: [QUERY_HOOKS.mutation],
|
|
2457
|
-
from: "@tanstack/react-query"
|
|
2458
|
-
};
|
|
2459
|
-
const queryTypesImport = {
|
|
2460
|
-
bindings: [...hasMutationDefaultOnError ? ["OpenApiQueryConfig"] : []],
|
|
2461
|
-
typeBindings: [require_generate_utils.QUERY_OPTIONS_TYPES.mutation],
|
|
2462
|
-
from: require_generate_utils.getQueryTypesImportPath(resolver.options)
|
|
2463
|
-
};
|
|
2464
|
-
const mutationEffectsImport = {
|
|
2465
|
-
bindings: [require_generate_utils.MUTATION_EFFECTS.hookName],
|
|
2466
|
-
typeBindings: [require_generate_utils.MUTATION_EFFECTS.optionsType],
|
|
2467
|
-
from: resolver.options.mutationEffectsImportPath
|
|
2468
|
-
};
|
|
2469
|
-
const queryModulesImport = {
|
|
2470
|
-
bindings: [require_generate_utils.QUERY_MODULE_ENUM],
|
|
2471
|
-
from: require_generate_utils.getQueryModulesImportPath(resolver.options)
|
|
2472
|
-
};
|
|
2473
|
-
const aclCheckImport = {
|
|
2474
|
-
bindings: [ACL_CHECK_HOOK],
|
|
2475
|
-
from: resolver.options.aclCheckImportPath
|
|
2476
|
-
};
|
|
2477
|
-
const hasDynamicInputsImport = configs.some((config) => config.readAll.filters || config.create?.inputDefs || config.update?.inputDefs);
|
|
2478
|
-
const dynamicInputsImport = {
|
|
2479
|
-
bindings: [require_generate_utils.BUILDERS_UTILS.dynamicInputs],
|
|
2480
|
-
from: resolver.options.dynamicInputsImportPath
|
|
2481
|
-
};
|
|
2482
|
-
const hasDynamicColumnsImport = configs.some((config) => config.readAll.columns);
|
|
2483
|
-
const dynamicColumnsImport = {
|
|
2484
|
-
bindings: [require_generate_utils.BUILDERS_UTILS.dynamicColumns],
|
|
2485
|
-
from: resolver.options.dynamicColumnsImportPath
|
|
2486
|
-
};
|
|
2487
|
-
const lines = [];
|
|
2488
|
-
if (hasAxiosImport) lines.push(renderImport$3(axiosImport));
|
|
2489
|
-
if (nativeImport.typeBindings?.length) lines.push(renderImport$3(nativeImport));
|
|
2490
|
-
if (nativeClient && endpoints.some((endpoint) => endpoint.mediaUpload)) lines.push(renderImport$3(appRestClientImport));
|
|
2491
|
-
if (hasZodImport) lines.push(renderImport$3(require_generate_utils.ZOD_IMPORT));
|
|
2492
|
-
if (hasDynamicInputsImport) lines.push(renderImport$3(dynamicInputsImport));
|
|
2493
|
-
if (hasDynamicColumnsImport) lines.push(renderImport$3(dynamicColumnsImport));
|
|
2494
|
-
for (const modelsImport of modelsImports) lines.push(renderImport$3(modelsImport));
|
|
2495
|
-
for (const queriesImport of queriesImports) lines.push(renderImport$3(queriesImport));
|
|
2496
|
-
for (const endpointsImport of endpointsImports) lines.push(renderImport$3(endpointsImport));
|
|
2497
|
-
if (hasMutation) {
|
|
2498
|
-
lines.push(renderImport$3(queryImport));
|
|
2499
|
-
lines.push(renderImport$3(queryTypesImport));
|
|
2500
|
-
if (hasMutationEffects) {
|
|
2501
|
-
lines.push(renderImport$3(queryModulesImport));
|
|
2502
|
-
lines.push(renderImport$3(mutationEffectsImport));
|
|
2503
|
-
}
|
|
2504
|
-
if (hasAclCheck) lines.push(renderImport$3(aclCheckImport));
|
|
2505
|
-
}
|
|
2506
|
-
for (const aclImport of aclImports) lines.push(renderImport$3(aclImport));
|
|
2507
|
-
lines.push("");
|
|
2508
|
-
if (resolver.options.tsNamespaces) lines.push(`export namespace ${require_generate_utils.getNamespaceName({
|
|
2509
|
-
type: "configs",
|
|
2510
|
-
tag,
|
|
2511
|
-
options: resolver.options
|
|
2512
|
-
})} {`);
|
|
2513
|
-
for (const config of configs) {
|
|
2514
|
-
lines.push(renderBuilderConfig(config, generateTypeParams));
|
|
2515
|
-
lines.push("");
|
|
2516
|
-
}
|
|
2517
|
-
if (resolver.options.tsNamespaces) lines.push("}");
|
|
2518
|
-
return lines.join("\n").trimEnd() + "\n";
|
|
2519
|
-
}
|
|
2520
|
-
function renderImport$3(importData) {
|
|
2521
|
-
const namedImports = [...importData.bindings, ...(importData.typeBindings ?? []).map((binding) => importData.typeOnly ? binding : `type ${binding}`)];
|
|
2522
|
-
const names = [...importData.defaultImport ? [importData.defaultImport] : [], ...namedImports.length > 0 ? [`{ ${namedImports.join(", ")} }`] : []].join(", ");
|
|
2523
|
-
return `import${importData.typeOnly ? " type" : ""} ${names} from "${importData.from}";`;
|
|
2524
|
-
}
|
|
2525
|
-
function renderInputsConfig(inputsConfig) {
|
|
2526
|
-
const lines = [];
|
|
2527
|
-
lines.push("{");
|
|
2528
|
-
lines.push(` schema: ${inputsConfig.schema},`);
|
|
2529
|
-
lines.push(" options: {");
|
|
2530
|
-
lines.push(" inputs: {");
|
|
2531
|
-
for (const key of Object.keys(inputsConfig.options.inputs)) lines.push(` ${key}: true,`);
|
|
2532
|
-
lines.push(" },");
|
|
2533
|
-
lines.push(" },");
|
|
2534
|
-
lines.push("}");
|
|
2535
|
-
return lines.join("\n");
|
|
2536
|
-
}
|
|
2537
|
-
function renderColumnsConfig(columnsConfig) {
|
|
2538
|
-
const lines = [];
|
|
2539
|
-
lines.push("{");
|
|
2540
|
-
lines.push(` schema: ${columnsConfig.schema},`);
|
|
2541
|
-
lines.push(" options: {");
|
|
2542
|
-
lines.push(" columns: {");
|
|
2543
|
-
for (const key of Object.keys(columnsConfig.options.columns)) lines.push(` ${key}: true,`);
|
|
2544
|
-
lines.push(" },");
|
|
2545
|
-
if (columnsConfig.options.sortable) lines.push(` sortable: ${columnsConfig.options.sortable},`);
|
|
2546
|
-
lines.push(" },");
|
|
2547
|
-
lines.push("}");
|
|
2548
|
-
return lines.join("\n");
|
|
2549
|
-
}
|
|
2550
|
-
function renderMutationContent(resolver, endpoint, tag) {
|
|
2551
|
-
const hasAclCheck = resolver.options.checkAcl && endpoint.acl;
|
|
2552
|
-
const hasMutationEffects = resolver.options.mutationEffects;
|
|
2553
|
-
const hasMutationDefaultOnError = resolver.options.mutationDefaultOnError;
|
|
2554
|
-
const hasAxiosRequestConfig = resolver.options.axiosRequestConfig;
|
|
2555
|
-
const endpointTag = require_generate_utils.getEndpointTag(endpoint, resolver.options);
|
|
2556
|
-
const endpointParams = require_generate_utils.mapEndpointParamsToFunctionParams(resolver, endpoint, {
|
|
2557
|
-
includeFileParam: true,
|
|
2558
|
-
modelNamespaceTag: endpointTag
|
|
2559
|
-
});
|
|
2560
|
-
const endpointParamsStr = endpointParams.map((p) => `${p.name}${p.required ? "" : "?"}: ${p.type}`).join("; ");
|
|
2561
|
-
const destructuredMutationArgs = endpointParams.map((p) => p.name).join(", ");
|
|
2562
|
-
const resolvedEndpointArgs = require_generate_utils.mapEndpointParamsToFunctionParams(resolver, endpoint, { modelNamespaceTag: endpointTag }).map((p) => p.name).join(", ");
|
|
2563
|
-
const endpointFunction = require_generate_utils.getImportedEndpointName(endpoint, resolver.options);
|
|
2564
|
-
const mutationVariablesType = endpoint.mediaUpload ? `{ ${endpointParamsStr}${endpointParamsStr ? "; " : ""}abortController?: AbortController; onUploadProgress?: (progress: { loaded: number; total: number }) => void }` : `{ ${endpointParamsStr} }`;
|
|
2565
|
-
const lines = [];
|
|
2566
|
-
lines.push(`(options?: AppMutationOptions<typeof ${endpointFunction}, ${mutationVariablesType}>${hasMutationEffects ? ` & ${require_generate_utils.MUTATION_EFFECTS.optionsType}` : ""}${hasAxiosRequestConfig ? `, config?: ${require_generate_utils.getRequestConfigTypeName(resolver.options.restClient)}` : ""}) => {`);
|
|
2567
|
-
if (hasMutationDefaultOnError) lines.push(" const queryConfig = OpenApiQueryConfig.useConfig();");
|
|
2568
|
-
if (hasMutationEffects) lines.push(` const { runMutationEffects } = useMutationEffects<${require_generate_utils.QUERY_MODULE_ENUM}.${endpointTag}>({ currentModule: ${require_generate_utils.QUERY_MODULE_ENUM}.${tag} });`);
|
|
2569
|
-
if (hasAclCheck) lines.push(` const { checkAcl } = ${ACL_CHECK_HOOK}();`);
|
|
2570
|
-
lines.push("");
|
|
2571
|
-
lines.push(` return ${QUERY_HOOKS.mutation}({`);
|
|
2572
|
-
const mutationFnArg = destructuredMutationArgs ? `{ ${destructuredMutationArgs}${endpoint.mediaUpload ? `${destructuredMutationArgs ? ", " : ""}abortController, onUploadProgress` : ""} }` : "";
|
|
2573
|
-
lines.push(` mutationFn: ${endpoint.mediaUpload ? "async " : ""}(${mutationFnArg}) => {`);
|
|
2574
|
-
if (hasAclCheck) lines.push(require_generate_utils.renderAclCheckCall(resolver, endpoint, void 0, " "));
|
|
2575
|
-
if (endpoint.mediaUpload) lines.push(...require_generate_utils.renderMediaUploadMutationBody({
|
|
2576
|
-
resolver,
|
|
2577
|
-
endpointFunction,
|
|
2578
|
-
resolvedEndpointArgs
|
|
2579
|
-
}).map((line) => ` ${line}`));
|
|
2580
|
-
else lines.push(` return ${endpointFunction}(${destructuredMutationArgs}${hasAxiosRequestConfig ? `${destructuredMutationArgs ? ", " : ""}config` : ""});`);
|
|
2581
|
-
lines.push(" },");
|
|
2582
|
-
lines.push(" ...options,");
|
|
2583
|
-
if (hasMutationDefaultOnError) lines.push(" onError: options?.onError ?? queryConfig.onError,");
|
|
2584
|
-
if (hasMutationEffects) {
|
|
2585
|
-
lines.push(" onSuccess: async (resData, variables, onMutateResult, context) => {");
|
|
2586
|
-
lines.push(" await runMutationEffects(resData, variables, options);");
|
|
2587
|
-
lines.push(" options?.onSuccess?.(resData, variables, onMutateResult, context);");
|
|
2588
|
-
lines.push(" },");
|
|
2589
|
-
}
|
|
2590
|
-
lines.push(" });");
|
|
2591
|
-
lines.push("}");
|
|
2592
|
-
return lines.map((line) => " " + line).join("\n").trimStart();
|
|
2593
|
-
}
|
|
2594
|
-
function renderBuilderConfig(config, params) {
|
|
2595
|
-
const { resolver, tag } = params;
|
|
2596
|
-
const lines = [];
|
|
2597
|
-
lines.push(`export const ${config.name} = {`);
|
|
2598
|
-
lines.push(" meta: {");
|
|
2599
|
-
lines.push(` title: "${config.title}",`);
|
|
2600
|
-
lines.push(" },");
|
|
2601
|
-
lines.push(" readAll: {");
|
|
2602
|
-
if (config.readAll.acl) lines.push(` acl: ${config.readAll.acl},`);
|
|
2603
|
-
lines.push(` schema: ${config.readAll.columns.schema},`);
|
|
2604
|
-
lines.push(` paginated: ${config.readAll.paginated},`);
|
|
2605
|
-
if (config.readAll.infinite) lines.push(` infinite: ${config.readAll.infinite},`);
|
|
2606
|
-
if (config.readAll.filters) {
|
|
2607
|
-
lines.push(" filters: {");
|
|
2608
|
-
lines.push(` schema: ${config.readAll.filters.schema},`);
|
|
2609
|
-
lines.push(` filterDefs: ${require_generate_utils.BUILDERS_UTILS.dynamicInputs}(${renderInputsConfig(config.readAll.filters)})`);
|
|
2610
|
-
lines.push(" },");
|
|
2611
|
-
}
|
|
2612
|
-
lines.push(` columns: ${require_generate_utils.BUILDERS_UTILS.dynamicColumns}(${renderColumnsConfig(config.readAll.columns)}),`);
|
|
2613
|
-
lines.push(" },");
|
|
2614
|
-
if (config.read) {
|
|
2615
|
-
lines.push(" read: {");
|
|
2616
|
-
if (config.read.acl) lines.push(` acl: ${config.read.acl},`);
|
|
2617
|
-
lines.push(` schema: ${config.read.schema},`);
|
|
2618
|
-
lines.push(` query: ${config.read.query},`);
|
|
2619
|
-
lines.push(" },");
|
|
2620
|
-
}
|
|
2621
|
-
if (config.create) {
|
|
2622
|
-
lines.push(" create: {");
|
|
2623
|
-
if (config.create.acl) lines.push(` acl: ${config.create.acl},`);
|
|
2624
|
-
if (config.create.schema) lines.push(` schema: ${config.create.schema},`);
|
|
2625
|
-
lines.push(` mutation: ${typeof config.create.mutation === "string" ? config.create.mutation : renderMutationContent(resolver, config.create.mutation, tag)},`);
|
|
2626
|
-
if (config.create.inputDefs) lines.push(` inputDefs: ${require_generate_utils.BUILDERS_UTILS.dynamicInputs}(${renderInputsConfig(config.create.inputDefs)})`);
|
|
2627
|
-
lines.push(" },");
|
|
2628
|
-
}
|
|
2629
|
-
if (config.update) {
|
|
2630
|
-
lines.push(" update: {");
|
|
2631
|
-
if (config.update.acl) lines.push(` acl: ${config.update.acl},`);
|
|
2632
|
-
if (config.update.schema) lines.push(` schema: ${config.update.schema},`);
|
|
2633
|
-
lines.push(` mutation: ${typeof config.update.mutation === "string" ? config.update.mutation : renderMutationContent(resolver, config.update.mutation, tag)},`);
|
|
2634
|
-
if (config.update.inputDefs) lines.push(` inputDefs: ${require_generate_utils.BUILDERS_UTILS.dynamicInputs}(${renderInputsConfig(config.update.inputDefs)})`);
|
|
2635
|
-
lines.push(" },");
|
|
2636
|
-
}
|
|
2637
|
-
if (config.delete) {
|
|
2638
|
-
lines.push(" delete: {");
|
|
2639
|
-
if (config.delete.acl) lines.push(` acl: ${config.delete.acl},`);
|
|
2640
|
-
lines.push(` mutation: ${typeof config.delete.mutation === "string" ? config.delete.mutation : renderMutationContent(resolver, config.delete.mutation, tag)},`);
|
|
2641
|
-
lines.push(" },");
|
|
2642
|
-
}
|
|
2643
|
-
if (config.bulkDelete) {
|
|
2644
|
-
lines.push(" bulkDelete: {");
|
|
2645
|
-
if (config.bulkDelete.acl) lines.push(` acl: ${config.bulkDelete.acl},`);
|
|
2646
|
-
if (config.bulkDelete.schema) lines.push(` schema: ${config.bulkDelete.schema},`);
|
|
2647
|
-
lines.push(` mutation: ${typeof config.bulkDelete.mutation === "string" ? config.bulkDelete.mutation : renderMutationContent(resolver, config.bulkDelete.mutation, tag)},`);
|
|
2648
|
-
if (config.bulkDelete.inputDefs) lines.push(` inputDefs: ${require_generate_utils.BUILDERS_UTILS.dynamicInputs}(${renderInputsConfig(config.bulkDelete.inputDefs)})`);
|
|
2649
|
-
lines.push(" },");
|
|
2650
|
-
}
|
|
2651
|
-
lines.push("};");
|
|
2652
|
-
return lines.join("\n");
|
|
2653
|
-
}
|
|
2654
|
-
//#endregion
|
|
2655
2212
|
//#region src/generators/generate/generateEndpoints.ts
|
|
2656
2213
|
function generateEndpoints({ resolver, data, tag }) {
|
|
2657
2214
|
const nativeContent = resolver.getNativeRenderedEndpoints?.(tag);
|
|
@@ -2784,6 +2341,83 @@ function renderEndpointConfig(resolver, endpoint, modelNamespaceTag) {
|
|
|
2784
2341
|
return lines.join("\n ");
|
|
2785
2342
|
}
|
|
2786
2343
|
//#endregion
|
|
2344
|
+
//#region src/generators/utils/generate/generate.lazy.utils.ts
|
|
2345
|
+
const ONCE_HELPER_LINES = [
|
|
2346
|
+
"const once = <T>(create: () => T) => {",
|
|
2347
|
+
" let value: T | undefined;",
|
|
2348
|
+
" return () => (value ??= create());",
|
|
2349
|
+
"};"
|
|
2350
|
+
];
|
|
2351
|
+
const isIdentifierChar = (char) => /[A-Za-z0-9_$]/.test(char);
|
|
2352
|
+
const findLiteralEnd = (code, start, quote) => {
|
|
2353
|
+
let index = start + 1;
|
|
2354
|
+
while (index < code.length) {
|
|
2355
|
+
const char = code[index];
|
|
2356
|
+
if (char === "\\") {
|
|
2357
|
+
index += 2;
|
|
2358
|
+
continue;
|
|
2359
|
+
}
|
|
2360
|
+
index += 1;
|
|
2361
|
+
if (char === quote) break;
|
|
2362
|
+
}
|
|
2363
|
+
return Math.min(index, code.length);
|
|
2364
|
+
};
|
|
2365
|
+
const findRegexEnd = (code, start) => {
|
|
2366
|
+
let escaped = false;
|
|
2367
|
+
let characterClass = false;
|
|
2368
|
+
for (let index = start + 1; index < code.length; index += 1) {
|
|
2369
|
+
const char = code[index];
|
|
2370
|
+
if (escaped) {
|
|
2371
|
+
escaped = false;
|
|
2372
|
+
continue;
|
|
2373
|
+
}
|
|
2374
|
+
if (char === "\\") {
|
|
2375
|
+
escaped = true;
|
|
2376
|
+
continue;
|
|
2377
|
+
}
|
|
2378
|
+
if (char === "[") characterClass = true;
|
|
2379
|
+
if (char === "]") characterClass = false;
|
|
2380
|
+
if (char === "/" && !characterClass) return index + 1;
|
|
2381
|
+
}
|
|
2382
|
+
return code.length;
|
|
2383
|
+
};
|
|
2384
|
+
/**
|
|
2385
|
+
* Rewrites bare references to sibling schemas (`PetSchema`) into factory calls (`PetSchema()`).
|
|
2386
|
+
* Member accesses (`CommonModels.PetSchema`, `.enum`), object keys and literal values are left alone.
|
|
2387
|
+
*/
|
|
2388
|
+
function callLazySchemaReferences(code, schemaNames) {
|
|
2389
|
+
let output = "";
|
|
2390
|
+
let index = 0;
|
|
2391
|
+
while (index < code.length) {
|
|
2392
|
+
const char = code[index];
|
|
2393
|
+
if (char === "\"" || char === "'" || char === "`") {
|
|
2394
|
+
const end = findLiteralEnd(code, index, char);
|
|
2395
|
+
output += code.slice(index, end);
|
|
2396
|
+
index = end;
|
|
2397
|
+
continue;
|
|
2398
|
+
}
|
|
2399
|
+
if (char === "/" && output.trimEnd().endsWith(".regex(")) {
|
|
2400
|
+
const end = findRegexEnd(code, index);
|
|
2401
|
+
output += code.slice(index, end);
|
|
2402
|
+
index = end;
|
|
2403
|
+
continue;
|
|
2404
|
+
}
|
|
2405
|
+
if (isIdentifierChar(char)) {
|
|
2406
|
+
let end = index + 1;
|
|
2407
|
+
while (end < code.length && isIdentifierChar(code[end])) end += 1;
|
|
2408
|
+
const identifier = code.slice(index, end);
|
|
2409
|
+
const isMemberAccess = output.endsWith(".");
|
|
2410
|
+
const isObjectKey = code.slice(end).trimStart().startsWith(":");
|
|
2411
|
+
output += schemaNames.has(identifier) && !isMemberAccess && !isObjectKey ? `${identifier}()` : identifier;
|
|
2412
|
+
index = end;
|
|
2413
|
+
continue;
|
|
2414
|
+
}
|
|
2415
|
+
output += char;
|
|
2416
|
+
index += 1;
|
|
2417
|
+
}
|
|
2418
|
+
return output;
|
|
2419
|
+
}
|
|
2420
|
+
//#endregion
|
|
2787
2421
|
//#region src/generators/generate/generateModels.ts
|
|
2788
2422
|
function generateModels({ resolver, data, tag }) {
|
|
2789
2423
|
const nativeContent = resolver.getNativeRenderedModels?.(tag);
|
|
@@ -2819,6 +2453,12 @@ function generateModels({ resolver, data, tag }) {
|
|
|
2819
2453
|
lines.push(renderImport$1(require_generate_utils.ZOD_IMPORT));
|
|
2820
2454
|
for (const modelsImport of modelsImports) lines.push(renderImport$1(modelsImport));
|
|
2821
2455
|
lines.push("");
|
|
2456
|
+
if (resolver.options.lazySchemas && resolver.options.tsNamespaces) return renderLazyModels({
|
|
2457
|
+
resolver,
|
|
2458
|
+
tag,
|
|
2459
|
+
zodSchemasData,
|
|
2460
|
+
lines
|
|
2461
|
+
});
|
|
2822
2462
|
if (resolver.options.tsNamespaces) lines.push(`export namespace ${require_generate_utils.getNamespaceName({
|
|
2823
2463
|
type: "models",
|
|
2824
2464
|
tag,
|
|
@@ -2843,6 +2483,50 @@ function generateModels({ resolver, data, tag }) {
|
|
|
2843
2483
|
if (resolver.options.tsNamespaces) lines.push("}");
|
|
2844
2484
|
return lines.join("\n").trimEnd() + "\n";
|
|
2845
2485
|
}
|
|
2486
|
+
function renderLazyModels({ resolver, tag, zodSchemasData, lines }) {
|
|
2487
|
+
const namespace = require_generate_utils.getNamespaceName({
|
|
2488
|
+
type: "models",
|
|
2489
|
+
tag,
|
|
2490
|
+
options: resolver.options
|
|
2491
|
+
});
|
|
2492
|
+
const schemaNames = new Set(Object.keys(zodSchemasData));
|
|
2493
|
+
const members = [];
|
|
2494
|
+
const types = [];
|
|
2495
|
+
lines.push(...ONCE_HELPER_LINES, "");
|
|
2496
|
+
for (const [name, zodSchema] of Object.entries(zodSchemasData)) {
|
|
2497
|
+
const inferredTypeName = require_generate_utils.getZodSchemaInferedTypeName(name, resolver.options);
|
|
2498
|
+
const typeAnnotation = zodSchema.isCircular ? `: () => z.ZodObject<z.ZodRawShape> & z.ZodType<${namespace}.${inferredTypeName}>` : "";
|
|
2499
|
+
const schemaCode = resolver.options.compileSchemas && !zodSchema.isCircular ? `z.compile(${zodSchema.code})` : zodSchema.code;
|
|
2500
|
+
lines.push(`const ${name}${typeAnnotation} = once(() => ${callLazySchemaReferences(schemaCode, schemaNames)});`);
|
|
2501
|
+
members.push(...indent(renderModelJsDocs({
|
|
2502
|
+
name,
|
|
2503
|
+
zodSchema,
|
|
2504
|
+
tag,
|
|
2505
|
+
resolver
|
|
2506
|
+
}).split("\n")));
|
|
2507
|
+
members.push(...renderLazyMember(name, `${name}()`));
|
|
2508
|
+
if (zodSchema.isCircular && zodSchema.schemaObj) types.push(` export type ${inferredTypeName} = ${renderSchemaType(zodSchema.schemaObj, resolver)};`);
|
|
2509
|
+
else types.push(` export type ${inferredTypeName} = z.infer<typeof ${namespace}.${name}>;`);
|
|
2510
|
+
if (zodSchema.isEnum) {
|
|
2511
|
+
lines.push(`const ${inferredTypeName} = once(() => ${name}().enum);`);
|
|
2512
|
+
members.push(...renderLazyMember(inferredTypeName, `${inferredTypeName}()`));
|
|
2513
|
+
}
|
|
2514
|
+
}
|
|
2515
|
+
lines.push("");
|
|
2516
|
+
lines.push(`export const ${namespace} = {`, ...members, "};");
|
|
2517
|
+
lines.push(`export declare namespace ${namespace} {`, ...types, "}");
|
|
2518
|
+
return lines.join("\n") + "\n";
|
|
2519
|
+
}
|
|
2520
|
+
function renderLazyMember(name, value) {
|
|
2521
|
+
return [
|
|
2522
|
+
` get ${name}() {`,
|
|
2523
|
+
` return ${value};`,
|
|
2524
|
+
` },`
|
|
2525
|
+
];
|
|
2526
|
+
}
|
|
2527
|
+
function indent(lines) {
|
|
2528
|
+
return lines.map((line) => ` ${line}`);
|
|
2529
|
+
}
|
|
2846
2530
|
function renderSchemaType(schema, resolver) {
|
|
2847
2531
|
if (require_generate_utils.isReferenceObject(schema)) {
|
|
2848
2532
|
const schemaName = resolver.getZodSchemaNameByRef(schema.$ref);
|
|
@@ -2901,6 +2585,20 @@ function renderModelsProxy({ resolver, data, tag }) {
|
|
|
2901
2585
|
})}`;
|
|
2902
2586
|
const inferredTypeNames = schemaNames.map((schemaName) => require_generate_utils.getZodSchemaInferedTypeName(schemaName, resolver.options));
|
|
2903
2587
|
const enumInferredTypeNames = schemaNames.filter((schemaName) => require_generate_utils.isEnumZodSchema(commonZodSchemas[schemaName] ?? "")).map((schemaName) => require_generate_utils.getZodSchemaInferedTypeName(schemaName, resolver.options));
|
|
2588
|
+
if (resolver.options.tsNamespaces && resolver.options.lazySchemas) {
|
|
2589
|
+
const lines = [];
|
|
2590
|
+
lines.push(`import { ${commonNamespace} } from "${commonModelsPath}";`);
|
|
2591
|
+
lines.push("");
|
|
2592
|
+
lines.push(`export const ${modelsNamespace} = {`);
|
|
2593
|
+
for (const schemaName of schemaNames) lines.push(...renderLazyMember(schemaName, `${commonNamespace}.${schemaName}`));
|
|
2594
|
+
for (const enumName of enumInferredTypeNames) lines.push(...renderLazyMember(enumName, `${commonNamespace}.${enumName}`));
|
|
2595
|
+
lines.push("};");
|
|
2596
|
+
lines.push(`export declare namespace ${modelsNamespace} {`);
|
|
2597
|
+
for (const typeName of inferredTypeNames) lines.push(` export type ${typeName} = ${commonNamespace}.${typeName};`);
|
|
2598
|
+
lines.push("}");
|
|
2599
|
+
lines.push("");
|
|
2600
|
+
return lines.join("\n");
|
|
2601
|
+
}
|
|
2904
2602
|
if (resolver.options.tsNamespaces) {
|
|
2905
2603
|
const lines = [];
|
|
2906
2604
|
lines.push(`import { ${commonNamespace} } from "${commonModelsPath}";`);
|
|
@@ -2963,6 +2661,22 @@ function renderModelJsDocs({ name, zodSchema, tag, resolver }) {
|
|
|
2963
2661
|
return lines.join("\n");
|
|
2964
2662
|
}
|
|
2965
2663
|
//#endregion
|
|
2664
|
+
//#region src/generators/const/queries.const.ts
|
|
2665
|
+
const QUERY_HOOKS = {
|
|
2666
|
+
query: "useQuery",
|
|
2667
|
+
infiniteQuery: "useInfiniteQuery",
|
|
2668
|
+
mutation: "useMutation"
|
|
2669
|
+
};
|
|
2670
|
+
const QUERY_IMPORT = {
|
|
2671
|
+
bindings: [
|
|
2672
|
+
QUERY_HOOKS.query,
|
|
2673
|
+
QUERY_HOOKS.infiniteQuery,
|
|
2674
|
+
QUERY_HOOKS.mutation
|
|
2675
|
+
],
|
|
2676
|
+
from: "@tanstack/react-query"
|
|
2677
|
+
};
|
|
2678
|
+
const QUERIES_MODULE_NAME = "moduleName";
|
|
2679
|
+
//#endregion
|
|
2966
2680
|
//#region src/generators/generate/generateQueries.ts
|
|
2967
2681
|
const endpointParamMappingCache = /* @__PURE__ */ new WeakMap();
|
|
2968
2682
|
const workspaceParamNamesCache = /* @__PURE__ */ new WeakMap();
|
|
@@ -3294,9 +3008,9 @@ function addAsteriskAfterNewLine(str) {
|
|
|
3294
3008
|
}
|
|
3295
3009
|
function renderQueryJsDocs({ resolver, endpoint, mode, tag }) {
|
|
3296
3010
|
const lines = ["/** "];
|
|
3297
|
-
if (mode === "infiniteQuery") lines.push(` * Infinite query \`${
|
|
3298
|
-
else if (mode === "query") lines.push(` * Query \`${
|
|
3299
|
-
else lines.push(` * Mutation \`${
|
|
3011
|
+
if (mode === "infiniteQuery") lines.push(` * Infinite query \`${getInfiniteQueryName(endpoint)}${endpoint.summary ? "" : ""}`);
|
|
3012
|
+
else if (mode === "query") lines.push(` * Query \`${getQueryName(endpoint)}\`${endpoint.summary && endpoint.mediaDownload ? " - recommended when file should be cached" : ""}`);
|
|
3013
|
+
else lines.push(` * Mutation \`${getQueryName(endpoint, true)}\`${endpoint.summary && endpoint.mediaDownload ? " - recommended when file should not be cached" : ""}`);
|
|
3300
3014
|
if (endpoint.summary) lines.push(` * @summary ${addAsteriskAfterNewLine(endpoint.summary)}`);
|
|
3301
3015
|
if (endpoint.description) lines.push(` * @description ${addAsteriskAfterNewLine(endpoint.description)}`);
|
|
3302
3016
|
if (endpoint.acl) lines.push(` * @permission Requires \`${require_generate_utils.getAbilityFunctionName(endpoint)}\` ability `);
|
|
@@ -3328,7 +3042,7 @@ function renderQueryKeys({ resolver, queryEndpoints, tag }) {
|
|
|
3328
3042
|
pathParamsRequiredOnly: true,
|
|
3329
3043
|
modelNamespaceTag: tag
|
|
3330
3044
|
})}) => [...keys.all, "${endpoint.path}", ${renderEndpointArgs(resolver, endpoint, {})}] as const,`);
|
|
3331
|
-
if (resolver.options.infiniteQueries &&
|
|
3045
|
+
if (resolver.options.infiniteQueries && isInfiniteQuery(endpoint, resolver.options)) lines.push(` ${require_generate_utils.getEndpointName(endpoint)}Infinite: (${renderEndpointParams(resolver, endpoint, {
|
|
3332
3046
|
excludePageParam: true,
|
|
3333
3047
|
pathParamsRequiredOnly: true,
|
|
3334
3048
|
modelNamespaceTag: tag
|
|
@@ -3411,7 +3125,7 @@ function renderQueryOptions({ resolver, endpoint, inlineEndpoints }) {
|
|
|
3411
3125
|
const endpointArgs = renderEndpointArgs(resolver, endpoint, {});
|
|
3412
3126
|
const endpointFunction = inlineEndpoints ? require_generate_utils.getEndpointName(endpoint) : require_generate_utils.getImportedEndpointName(endpoint, resolver.options);
|
|
3413
3127
|
const lines = [];
|
|
3414
|
-
lines.push(`const ${
|
|
3128
|
+
lines.push(`const ${getQueryOptionsName(endpoint)} = (${endpointParams ? `{ ${endpointArgs} }: { ${endpointParams} }` : ""}${hasRequestConfigParam ? `${endpointParams ? ", " : ""}${require_generate_utils.AXIOS_REQUEST_CONFIG_NAME}?: ${getRequestConfigType(resolver)}` : ""}) => ({`);
|
|
3415
3129
|
lines.push(` queryKey: keys.${require_generate_utils.getEndpointName(endpoint)}(${endpointArgs}),`);
|
|
3416
3130
|
const requestConfigWithSignal = renderRequestConfigWithSignal(hasRequestConfigParam);
|
|
3417
3131
|
lines.push(` queryFn: ({ signal }: { signal: AbortSignal }) => ${endpointFunction}(${endpointArgs}${hasRequestConfigParam ? `${endpointArgs ? ", " : ""}${requestConfigWithSignal}` : ""}),`);
|
|
@@ -3428,7 +3142,7 @@ function renderInfiniteQueryOptions({ resolver, endpoint, inlineEndpoints }) {
|
|
|
3428
3142
|
const endpointArgsWithPage = renderEndpointArgs(resolver, endpoint, { replacePageParam: true });
|
|
3429
3143
|
const endpointFunction = inlineEndpoints ? require_generate_utils.getEndpointName(endpoint) : require_generate_utils.getImportedEndpointName(endpoint, resolver.options);
|
|
3430
3144
|
const lines = [];
|
|
3431
|
-
lines.push(`const ${
|
|
3145
|
+
lines.push(`const ${getInfiniteQueryOptionsName(endpoint)} = (${endpointParams ? `{ ${endpointArgsWithoutPage} }: { ${endpointParams} }` : ""}${hasRequestConfigParam ? `${endpointParams ? ", " : ""}${require_generate_utils.AXIOS_REQUEST_CONFIG_NAME}?: ${getRequestConfigType(resolver)}` : ""}) => ({`);
|
|
3432
3146
|
lines.push(` queryKey: keys.${require_generate_utils.getEndpointName(endpoint)}Infinite(${endpointArgsWithoutPage}),`);
|
|
3433
3147
|
const requestConfigWithSignal = renderRequestConfigWithSignal(hasRequestConfigParam);
|
|
3434
3148
|
lines.push(` queryFn: ({ pageParam, signal }: { pageParam: number; signal: AbortSignal }) => ${endpointFunction}(${endpointArgsWithPage}${hasRequestConfigParam ? `, ${requestConfigWithSignal}` : ""}),`);
|
|
@@ -3446,8 +3160,8 @@ function renderPrefetchQuery({ resolver, endpoint }) {
|
|
|
3446
3160
|
const endpointParams = renderEndpointParams(resolver, endpoint, { modelNamespaceTag: require_generate_utils.getEndpointTag(endpoint, resolver.options) });
|
|
3447
3161
|
const endpointArgs = renderEndpointArgs(resolver, endpoint, {});
|
|
3448
3162
|
const lines = [];
|
|
3449
|
-
lines.push(`export const ${
|
|
3450
|
-
lines.push(` const queryOptions = { ...${
|
|
3163
|
+
lines.push(`export const ${getPrefetchQueryName(endpoint)} = (queryClient: QueryClient, ${endpointParams ? `{ ${endpointArgs} }: { ${endpointParams} }, ` : ""}options?: Omit<Parameters<QueryClient["prefetchQuery"]>[0], "queryKey" | "queryFn">, ${hasRequestConfigParam ? `${require_generate_utils.AXIOS_REQUEST_CONFIG_NAME}?: ${require_generate_utils.getRequestConfigTypeName(resolver.options.restClient)}, ` : ""}throwOnError = false) => {`);
|
|
3164
|
+
lines.push(` const queryOptions = { ...${getQueryOptionsName(endpoint)}(${endpointParams ? `{ ${endpointArgs} }` : ""}${hasRequestConfigParam ? `${endpointParams ? ", " : ""}${require_generate_utils.AXIOS_REQUEST_CONFIG_NAME}` : ""}), ...options };`);
|
|
3451
3165
|
lines.push(` return throwOnError ? queryClient.fetchQuery(queryOptions) : queryClient.prefetchQuery(queryOptions);`);
|
|
3452
3166
|
lines.push("};");
|
|
3453
3167
|
return lines.join("\n");
|
|
@@ -3461,8 +3175,8 @@ function renderPrefetchInfiniteQuery({ resolver, endpoint }) {
|
|
|
3461
3175
|
const endpointArgs = renderEndpointArgs(resolver, endpoint, { excludePageParam: true });
|
|
3462
3176
|
const optionsArgs = `${endpointParams ? `{ ${endpointArgs} }` : ""}${hasRequestConfigParam ? `${endpointParams ? ", " : ""}${require_generate_utils.AXIOS_REQUEST_CONFIG_NAME}` : ""}`;
|
|
3463
3177
|
const lines = [];
|
|
3464
|
-
lines.push(`export const ${
|
|
3465
|
-
lines.push(` const queryOptions = { ...${
|
|
3178
|
+
lines.push(`export const ${getPrefetchInfiniteQueryName(endpoint)} = (queryClient: QueryClient, ${endpointParams ? `{ ${endpointArgs} }: { ${endpointParams} }, ` : ""}options?: Omit<Parameters<QueryClient["prefetchInfiniteQuery"]>[0], "queryKey" | "queryFn" | "initialPageParam" | "getNextPageParam">, ${hasRequestConfigParam ? `${require_generate_utils.AXIOS_REQUEST_CONFIG_NAME}?: ${require_generate_utils.getRequestConfigTypeName(resolver.options.restClient)}, ` : ""}throwOnError = false) => {`);
|
|
3179
|
+
lines.push(` const queryOptions = { ...${getInfiniteQueryOptionsName(endpoint)}(${optionsArgs}), ...(options as {}) };`);
|
|
3466
3180
|
lines.push(` return throwOnError ? queryClient.fetchInfiniteQuery(queryOptions) : queryClient.prefetchInfiniteQuery(queryOptions);`);
|
|
3467
3181
|
lines.push("};");
|
|
3468
3182
|
return lines.join("\n");
|
|
@@ -3479,7 +3193,7 @@ function renderQuery({ resolver, endpoint, inlineEndpoints }) {
|
|
|
3479
3193
|
optionalPathParams: resolver.options.workspaceContext,
|
|
3480
3194
|
modelNamespaceTag: tag
|
|
3481
3195
|
});
|
|
3482
|
-
const queryOptionsName =
|
|
3196
|
+
const queryOptionsName = getQueryOptionsName(endpoint);
|
|
3483
3197
|
const hasQueryFnOverride = hasAclCheck;
|
|
3484
3198
|
const requestConfig = `{ ${hasAxiosRequestConfig ? `...${require_generate_utils.AXIOS_REQUEST_CONFIG_NAME}, ` : ""}allowInvalidResponseData: queryConfig.allowInvalidResponseData }`;
|
|
3485
3199
|
const queryOptionsArgs = `${resolvedEndpointArgs ? `{ ${resolvedEndpointArgs} }, ` : ""}${requestConfig}`;
|
|
@@ -3490,7 +3204,7 @@ function renderQuery({ resolver, endpoint, inlineEndpoints }) {
|
|
|
3490
3204
|
mode: "query",
|
|
3491
3205
|
tag
|
|
3492
3206
|
}));
|
|
3493
|
-
lines.push(`export const ${
|
|
3207
|
+
lines.push(`export const ${getQueryName(endpoint)} = <TData>(${endpointParams ? `{ ${endpointArgs} }: { ${endpointParams} }, ` : ""}options?: AppQueryOptions<typeof ${inlineEndpoints ? require_generate_utils.getEndpointName(endpoint) : require_generate_utils.getImportedEndpointName(endpoint, resolver.options)}, TData>${hasAxiosRequestConfig ? `, ${require_generate_utils.AXIOS_REQUEST_CONFIG_NAME}?: ${require_generate_utils.getRequestConfigTypeName(resolver.options.restClient)}` : ""}) => {`);
|
|
3494
3208
|
lines.push(" const queryConfig = OpenApiQueryConfig.useConfig();");
|
|
3495
3209
|
if (hasAclCheck) lines.push(` const { checkAcl } = ${ACL_CHECK_HOOK}();`);
|
|
3496
3210
|
lines.push(...renderWorkspaceParamResolutions({
|
|
@@ -3529,7 +3243,7 @@ function renderMutation({ resolver, endpoint, inlineEndpoints, precomputed }) {
|
|
|
3529
3243
|
const destructuredMutationArgs = renderEndpointArgs(resolver, endpoint, { includeFileParam: true });
|
|
3530
3244
|
const endpointFunction = inlineEndpoints ? require_generate_utils.getEndpointName(endpoint) : require_generate_utils.getImportedEndpointName(endpoint, resolver.options);
|
|
3531
3245
|
const updateQueryEndpoints = precomputed?.updateQueryEndpoints ?? [];
|
|
3532
|
-
const destructuredVariables = precomputed?.destructuredVariables ??
|
|
3246
|
+
const destructuredVariables = precomputed?.destructuredVariables ?? getDestructuredVariables(resolver, endpoint, updateQueryEndpoints);
|
|
3533
3247
|
const hasMutationFnBody = endpoint.mediaUpload || hasAclCheck || Object.keys(workspaceParamReplacements).length > 0;
|
|
3534
3248
|
const mutationVariablesType = endpoint.mediaUpload ? `${endpointParams}${endpointParams ? "; " : ""}abortController?: AbortController; onUploadProgress?: (progress: { loaded: number; total: number }) => void` : endpointParams;
|
|
3535
3249
|
const isPost = endpoint.method === "post";
|
|
@@ -3560,7 +3274,7 @@ function renderMutation({ resolver, endpoint, inlineEndpoints, precomputed }) {
|
|
|
3560
3274
|
mode: "mutation",
|
|
3561
3275
|
tag
|
|
3562
3276
|
}));
|
|
3563
|
-
lines.push(`export const ${
|
|
3277
|
+
lines.push(`export const ${getQueryName(endpoint, true)} = (${pathParamFirstArg}options?: AppMutationOptions<typeof ${endpointFunction}${mutationOptionsTypeArg}>${hasMutationEffects ? ` & ${require_generate_utils.MUTATION_EFFECTS.optionsType}` : ""}${hasAxiosRequestConfig ? `, ${require_generate_utils.AXIOS_REQUEST_CONFIG_NAME}?: ${require_generate_utils.getRequestConfigTypeName(resolver.options.restClient)}` : ""}) => {`);
|
|
3564
3278
|
if (hasMutationDefaultOnError) lines.push(" const queryConfig = OpenApiQueryConfig.useConfig();");
|
|
3565
3279
|
if (hasAclCheck) lines.push(` const { checkAcl } = ${ACL_CHECK_HOOK}();`);
|
|
3566
3280
|
lines.push(...renderWorkspaceContextDestructure({
|
|
@@ -3626,9 +3340,9 @@ function groupEndpoints(endpoints, resolver) {
|
|
|
3626
3340
|
const infoByEndpoint = /* @__PURE__ */ new Map();
|
|
3627
3341
|
const mutationDataByEndpoint = /* @__PURE__ */ new Map();
|
|
3628
3342
|
for (const endpoint of endpoints) {
|
|
3629
|
-
const query =
|
|
3630
|
-
const mutation =
|
|
3631
|
-
const infiniteQuery = Boolean(resolver.options.infiniteQueries && query &&
|
|
3343
|
+
const query = isQuery(endpoint);
|
|
3344
|
+
const mutation = isMutation(endpoint);
|
|
3345
|
+
const infiniteQuery = Boolean(resolver.options.infiniteQueries && query && isInfiniteQuery(endpoint, resolver.options));
|
|
3632
3346
|
if (query) queryEndpoints.push(endpoint);
|
|
3633
3347
|
if (mutation) mutationEndpoints.push(endpoint);
|
|
3634
3348
|
if (infiniteQuery) infiniteQueryEndpoints.push(endpoint);
|
|
@@ -3643,7 +3357,7 @@ function groupEndpoints(endpoints, resolver) {
|
|
|
3643
3357
|
const updateQueryEndpoints = queryEndpoints.filter((queryEndpoint) => queryEndpoint.parameters.filter((param) => param.parameterObject?.required).every((pathParam) => endpoint.parameters.some((param) => param.name === pathParam.name)) && queryEndpoint.response === endpoint.response);
|
|
3644
3358
|
mutationDataByEndpoint.set(endpoint, {
|
|
3645
3359
|
updateQueryEndpoints,
|
|
3646
|
-
destructuredVariables:
|
|
3360
|
+
destructuredVariables: getDestructuredVariables(resolver, endpoint, updateQueryEndpoints)
|
|
3647
3361
|
});
|
|
3648
3362
|
}
|
|
3649
3363
|
return {
|
|
@@ -3668,7 +3382,7 @@ function renderInfiniteQuery({ resolver, endpoint, inlineEndpoints }) {
|
|
|
3668
3382
|
});
|
|
3669
3383
|
const endpointArgsWithoutPage = renderEndpointArgs(resolver, endpoint, { excludePageParam: true });
|
|
3670
3384
|
const resolvedEndpointArgsWithoutPage = renderEndpointObjectArgs(resolver, endpoint, { excludePageParam: true }, workspaceParamReplacements);
|
|
3671
|
-
const queryOptionsName =
|
|
3385
|
+
const queryOptionsName = getInfiniteQueryOptionsName(endpoint);
|
|
3672
3386
|
const requestConfig = `{ ${hasAxiosRequestConfig ? `...${require_generate_utils.AXIOS_REQUEST_CONFIG_NAME}, ` : ""}allowInvalidResponseData: queryConfig.allowInvalidResponseData }`;
|
|
3673
3387
|
const queryOptionsArgs = `${resolvedEndpointArgsWithoutPage ? `{ ${resolvedEndpointArgsWithoutPage} }, ` : ""}${requestConfig}`;
|
|
3674
3388
|
const hasQueryFnOverride = hasAclCheck;
|
|
@@ -3679,7 +3393,7 @@ function renderInfiniteQuery({ resolver, endpoint, inlineEndpoints }) {
|
|
|
3679
3393
|
mode: "infiniteQuery",
|
|
3680
3394
|
tag
|
|
3681
3395
|
}));
|
|
3682
|
-
lines.push(`export const ${
|
|
3396
|
+
lines.push(`export const ${getInfiniteQueryName(endpoint)} = <TData>(${endpointParams ? `{ ${endpointArgsWithoutPage} }: { ${endpointParams} }, ` : ""}options?: AppInfiniteQueryOptions<typeof ${inlineEndpoints ? require_generate_utils.getEndpointName(endpoint) : require_generate_utils.getImportedEndpointName(endpoint, resolver.options)}, TData>${hasAxiosRequestConfig ? `, ${require_generate_utils.AXIOS_REQUEST_CONFIG_NAME}?: ${require_generate_utils.getRequestConfigTypeName(resolver.options.restClient)}` : ""}) => {`);
|
|
3683
3397
|
lines.push(" const queryConfig = OpenApiQueryConfig.useConfig();");
|
|
3684
3398
|
if (hasAclCheck) lines.push(` const { checkAcl } = ${ACL_CHECK_HOOK}();`);
|
|
3685
3399
|
lines.push(...renderWorkspaceParamResolutions({
|
|
@@ -3771,7 +3485,7 @@ function generateQueryModules({ resolver, data }) {
|
|
|
3771
3485
|
tag,
|
|
3772
3486
|
options: resolver.options
|
|
3773
3487
|
}),
|
|
3774
|
-
mutationHooks: endpoints.filter(
|
|
3488
|
+
mutationHooks: endpoints.filter(isMutation).map((endpoint) => getQueryName(endpoint, true))
|
|
3775
3489
|
});
|
|
3776
3490
|
});
|
|
3777
3491
|
const mutationModules = modules.filter((module) => module.mutationHooks.length > 0);
|
|
@@ -3800,6 +3514,7 @@ function generateQueryModules({ resolver, data }) {
|
|
|
3800
3514
|
lines.push("}");
|
|
3801
3515
|
lines.push("");
|
|
3802
3516
|
lines.push("declare global {");
|
|
3517
|
+
lines.push(" interface OpenApiCodegenQueryModules { queryModule: QueryModule }");
|
|
3803
3518
|
lines.push(" interface OpenApiCodegenQueryModuleMutations extends QueryModuleMutations {}");
|
|
3804
3519
|
lines.push("}");
|
|
3805
3520
|
return lines.join("\n");
|
|
@@ -4067,15 +3782,13 @@ function generateCodeFromOpenAPIDoc(openApiDoc, options, profiler, nativeSource)
|
|
|
4067
3782
|
"models",
|
|
4068
3783
|
...shouldGenerateEndpoints ? ["endpoints"] : [],
|
|
4069
3784
|
"queries",
|
|
4070
|
-
...resolver.options.acl ? ["acl"] : []
|
|
4071
|
-
...resolver.options.builderConfigs ? ["configs"] : []
|
|
3785
|
+
...resolver.options.acl ? ["acl"] : []
|
|
4072
3786
|
];
|
|
4073
3787
|
const generateFunctions = {
|
|
4074
3788
|
["models"]: generateModels,
|
|
4075
3789
|
["endpoints"]: generateEndpoints,
|
|
4076
3790
|
["queries"]: generateQueries,
|
|
4077
|
-
["acl"]: generateAcl
|
|
4078
|
-
["configs"]: generateConfigs
|
|
3791
|
+
["acl"]: generateAcl
|
|
4079
3792
|
};
|
|
4080
3793
|
data.forEach((_, tag) => {
|
|
4081
3794
|
generateTypes.forEach((type) => {
|