kiota
kiota copied to clipboard
Implement constructor to accept partials in typescript to make models initialization easier
Currently models initialization looks like that, which is verbose.
const message = new Message();
message.subject = "Did you see last night's game?";
message.importance = Importance.Low;
message.body = new ItemBody();
message.body.contentType = BodyType.HTML;
message.body.content = "They were <b>awesome</b>!";
const recipient = new Recipient();
recipient.emailAddress = new EmailAddress();
recipient.emailAddress.address = "[email protected]";
message.toRecipients = [
recipient,
];
client.me.messages.post(message);
If we added a constructor accepting a partial during the generation, consumers would be able to do the following instead.
const message = new Message({
subject: "Did you see last night's game?",
importance: Importance.Low,
body: new ItemBody({
contentType: BodyType.HTML.
content: "They were <b>awesome</b>!",
}),
toRecipients: [
new Recipient({
emailAddress: new EmailAddress({
address: "[email protected]",
}),
}),
],
});
client.me.messages.post(message);
Originally posted by @baywet in https://github.com/microsoft/kiota/issues/841#issuecomment-1007394108
Blocking this for now so that we can discuss more on the related issue #1013.
In addition to the information already provided, I believe we can do away with the constructor call all together and provide an interface like syntax by:
- having all fluent API methods accept partials (for structured types)
- having models constructors accept partials
- having models properties that are structured types partial
- having the fluent API methods feed the partial they receive into the constructor (effectively encapsulating calling the constructor)
- having the constructor model feed the partial for each property that's structured and provided to the adequate constructor.
This should make the SDK a bit heavier, but be compatible with the backing store and the interface style initialization of objects requirement.
Not required anymore as we're using interfaces.