Skip to content

Commit

Permalink
fix(exports): strips referenced imports of models (#61)
Browse files Browse the repository at this point in the history
  • Loading branch information
seriouslag committed Apr 20, 2024
1 parent 4e27e41 commit be51e19
Showing 1 changed file with 16 additions and 5 deletions.
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

0 comments on commit be51e19

Please sign in to comment.