Skip to content

Commit 3f712b4

Browse files
committed
feat: add overloads to methods with array and single return types
1 parent 2bf5b9e commit 3f712b4

File tree

1 file changed

+25
-2
lines changed

1 file changed

+25
-2
lines changed

Diff for: generator/Main.ts

+25-2
Original file line numberDiff line numberDiff line change
@@ -180,8 +180,8 @@ declarationClass.addMethod(methodStructure)
180180
// setting the parameters typing and binding to the correct interface and endpoint
181181
for (const [methodName, endpoint, jsdocs] of endpoints) {
182182
const inputParam = methodName.match(/ByName$/) ? 'nameOrId' : 'id';
183-
const inputParamType = methodName.match(/ByName$/) ? 'string | number | Array<string | number>' : 'number | number[]';
184183

184+
const inputParamType = methodName.match(/ByName$/) ? 'string | number | Array<string | number>' : 'number | number[]';
185185
methodStructure = {
186186
name: methodName,
187187
isAsync: true,
@@ -191,7 +191,7 @@ for (const [methodName, endpoint, jsdocs] of endpoints) {
191191
},
192192
{
193193
name: 'callback',
194-
type: `(result: PokeAPITypes.${apiMap[endpoint]} | PokeAPITypes.${apiMap[endpoint]}[], error?: any) => any`,
194+
type: `((result: PokeAPITypes.${apiMap[endpoint]}, error?: any) => any) & ((result: PokeAPITypes.${apiMap[endpoint]}[], error?: any) => any)`,
195195
hasQuestionToken: true,
196196
}],
197197
returnType: `Promise<PokeAPITypes.${apiMap[endpoint]} | PokeAPITypes.${apiMap[endpoint]}[]>`,
@@ -230,6 +230,29 @@ for (const [methodName, endpoint, jsdocs] of endpoints) {
230230
handleError(error, callback);
231231
}`);
232232

233+
const singleParamType = methodName.match(/ByName$/) ? 'string | number' : 'number';
234+
const overloadMethodStructure = {
235+
name: methodName,
236+
isAsync: true,
237+
parameters: [{
238+
name: inputParam,
239+
type: singleParamType,
240+
},
241+
{
242+
name: 'callback',
243+
type: `(result: PokeAPITypes.${apiMap[endpoint]} | PokeAPITypes.${apiMap[endpoint]}[], error?: any) => any`,
244+
hasQuestionToken: true,
245+
}],
246+
returnType: `Promise<PokeAPITypes.${apiMap[endpoint]}>`,
247+
};
248+
generatedMethod.addOverload(overloadMethodStructure);
249+
250+
const multipleParamType = methodName.match(/ByName$/) ? 'Array<string | number>' : 'number[]';
251+
overloadMethodStructure.parameters[0].type = multipleParamType;
252+
overloadMethodStructure.parameters[1].type = `(result: PokeAPITypes.${apiMap[endpoint]}[], error?: any) => any`;
253+
overloadMethodStructure.returnType = `Promise<PokeAPITypes.${apiMap[endpoint]}[]>`;
254+
generatedMethod.addOverload(overloadMethodStructure);
255+
233256
// Add the declaration to the types file
234257
// Sanitizing the namespace and remove the async keyword
235258
methodStructure.isAsync = false;

0 commit comments

Comments
 (0)