Skip to content

Commit be51e19

Browse files
authored
fix(exports): strips referenced imports of models (#61)
1 parent 4e27e41 commit be51e19

File tree

1 file changed

+16
-5
lines changed

1 file changed

+16
-5
lines changed

src/createUseQuery.mts

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -72,26 +72,33 @@ export const createApiResponseType = ({
7272
};
7373
};
7474

75+
/**
76+
* Replace the import("...") surrounding the type if there is one.
77+
* This can happen when the type is imported from another file, but
78+
* we are already importing all the types from that file.
79+
*/
80+
function getShortType(type: string) {
81+
return type.replaceAll(/import\("[a-zA-Z\/\.-]*"\)\./g, "");
82+
}
83+
7584
export function getRequestParamFromMethod(method: MethodDeclaration) {
7685
if (!method.getParameters().length) {
7786
return null;
7887
}
7988

80-
// we need to get the properties of the object
81-
8289
const params = method
8390
.getParameters()
8491
.map((param) => {
8592
const paramNodes = extractPropertiesFromObjectParam(param);
8693
return paramNodes.map((refParam) => ({
8794
name: refParam.name,
88-
typeName: refParam.type.getText(),
95+
typeName: getShortType(refParam.type.getText()),
8996
optional: refParam.optional,
9097
}));
9198
})
9299
.flat();
93100

94-
const areAllOptional = params.every((param) => param.optional);
101+
const areAllPropertiesOptional = params.every((param) => param.optional);
95102

96103
return ts.factory.createParameterDeclaration(
97104
undefined,
@@ -122,7 +129,11 @@ export function getRequestParamFromMethod(method: MethodDeclaration) {
122129
);
123130
})
124131
),
125-
areAllOptional ? ts.factory.createObjectLiteralExpression() : undefined
132+
// if all params are optional, we create an empty object literal
133+
// so the hook can be called without any parameters
134+
areAllPropertiesOptional
135+
? ts.factory.createObjectLiteralExpression()
136+
: undefined
126137
);
127138
}
128139

0 commit comments

Comments
 (0)