Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(exports): strip referenced imports of models #61

Merged
merged 1 commit into from
Apr 20, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 16 additions & 5 deletions src/createUseQuery.mts
Original file line number Diff line number Diff line change
Expand Up @@ -72,26 +72,33 @@ export const createApiResponseType = ({
};
};

/**
* Replace the import("...") surrounding the type if there is one.
* This can happen when the type is imported from another file, but
* we are already importing all the types from that file.
*/
function getShortType(type: string) {
return type.replaceAll(/import\("[a-zA-Z\/\.-]*"\)\./g, "");
}

export function getRequestParamFromMethod(method: MethodDeclaration) {
if (!method.getParameters().length) {
return null;
}

// we need to get the properties of the object

const params = method
.getParameters()
.map((param) => {
const paramNodes = extractPropertiesFromObjectParam(param);
return paramNodes.map((refParam) => ({
name: refParam.name,
typeName: refParam.type.getText(),
typeName: getShortType(refParam.type.getText()),
optional: refParam.optional,
}));
})
.flat();

const areAllOptional = params.every((param) => param.optional);
const areAllPropertiesOptional = params.every((param) => param.optional);

return ts.factory.createParameterDeclaration(
undefined,
Expand Down Expand Up @@ -122,7 +129,11 @@ export function getRequestParamFromMethod(method: MethodDeclaration) {
);
})
),
areAllOptional ? ts.factory.createObjectLiteralExpression() : undefined
// if all params are optional, we create an empty object literal
// so the hook can be called without any parameters
areAllPropertiesOptional
? ts.factory.createObjectLiteralExpression()
: undefined
);
}

Expand Down