Skip to content

Commit

Permalink
Correctly emit arrays
Browse files Browse the repository at this point in the history
  • Loading branch information
skonves committed Dec 2, 2024
1 parent 629a825 commit 916474a
Showing 1 changed file with 41 additions and 53 deletions.
94 changes: 41 additions & 53 deletions src/hook-file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,40 +107,13 @@ export class HookFile extends ModuleBuilder {
const queryOptionsName = getQueryOptionsName(method);
const paramsCallsite = method.parameters.length ? 'params' : '';

const returnType = getTypeByName(
this.service,
method.returnType?.typeName.value,
);
const dataType = getTypeByName(
this.service,
returnType?.properties.find((p) => p.name.value === 'data')?.typeName
.value,
);

const skipSelect =
returnType &&
returnType.properties.some(
(prop) =>
prop.name.value !== 'data' && prop.name.value !== 'errors',
);

const returnTypeName = returnType ? buildTypeName(returnType) : 'void';
let dataTypeName: string;
if (skipSelect) {
dataTypeName = returnTypeName;
} else {
dataTypeName = dataType ? buildTypeName(dataType) : 'void';
}

const queryParams = httpMethod?.parameters.filter((p) =>
isCacheParam(p, true),
);
const { dataTypeName, returnTypeName, array } = this.xxxx(method);

const optionsExpression = `options?: Omit<${UndefinedInitialDataOptions()}<${type(
returnTypeName,
)}, ThrowableError, ${type(
dataTypeName,
)} | undefined>,'queryKey' | 'queryFn' | 'select'>`;
)}${array} | undefined>,'queryKey' | 'queryFn' | 'select'>`;

yield* buildDescription(
method.description,
Expand Down Expand Up @@ -284,6 +257,42 @@ export class HookFile extends ModuleBuilder {
}
}

private xxxx(method: Method) {
const returnType = getTypeByName(
this.service,
method.returnType?.typeName.value,
);

const dataProp = returnType?.properties.find(
(prop) => prop.name.value === 'data',
);

const dataType = getTypeByName(this.service, dataProp?.typeName.value);

const skipSelect =
returnType &&
returnType.properties.some(
(prop) => prop.name.value !== 'data' && prop.name.value !== 'errors',
);

const returnTypeName = returnType ? buildTypeName(returnType) : 'void';
let dataTypeName: string;
if (skipSelect) {
dataTypeName = returnTypeName;
} else {
dataTypeName = dataProp && dataType ? buildTypeName(dataType) : 'void';
}

const dataTypeArray = (!skipSelect && dataProp?.isArray) ?? false;

return {
returnTypeName,
dataTypeName,
array: dataTypeArray ? '[]' : '',
skipSelect,
};
}

private *buildInfiniteSelectFn(method: Method): Iterable<string> {
const InfiniteData = () => this.tanstack.type('InfiniteData');
const type = (t: string) => this.types.type(t);
Expand Down Expand Up @@ -324,37 +333,16 @@ export class HookFile extends ModuleBuilder {
: '';
const paramsCallsite = method.parameters.length ? 'params' : '';

const returnType = getTypeByName(
this.service,
method.returnType?.typeName.value,
);

const dataType = getTypeByName(
this.service,
returnType?.properties.find((p) => p.name.value === 'data')?.typeName
.value,
);

const returnTypeName = returnType ? buildTypeName(returnType) : 'void';
const skipSelect =
returnType &&
returnType.properties.some(
(prop) => prop.name.value !== 'data' && prop.name.value !== 'errors',
);
let dataTypeName: string;
if (skipSelect) {
dataTypeName = returnTypeName;
} else {
dataTypeName = dataType ? buildTypeName(dataType) : 'void';
}
const { dataTypeName, returnTypeName, array, skipSelect } =
this.xxxx(method);

const guard = () => this.runtime.fn('guard');

yield `const ${name} = (${paramsExpression}) => {`;
yield ` const ${serviceName} = ${this.context.fn(serviceHookName)}()`;
yield ` return ${queryOptions()}<${type(
returnTypeName,
)}, ThrowableError, ${type(dataTypeName)} | undefined>({`;
)}, ThrowableError, ${type(dataTypeName)}${array} | undefined>({`;
yield ` queryKey: ${this.buildQueryKey(httpPath, method, {
includeRelayParams: true,
})},`;
Expand Down

0 comments on commit 916474a

Please sign in to comment.