kiota icon indicating copy to clipboard operation
kiota copied to clipboard

Implement constructor to accept partials in typescript to make models initialization easier

Open baywet opened this issue 2 years ago • 2 comments

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

baywet avatar Jan 07 '22 14:01 baywet

Blocking this for now so that we can discuss more on the related issue #1013.

nikithauc avatar Jan 10 '22 20:01 nikithauc

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:

  1. having all fluent API methods accept partials (for structured types)
  2. having models constructors accept partials
  3. having models properties that are structured types partial
  4. having the fluent API methods feed the partial they receive into the constructor (effectively encapsulating calling the constructor)
  5. 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.

baywet avatar Jan 20 '22 13:01 baywet

Not required anymore as we're using interfaces.

sebastienlevert avatar Mar 08 '23 16:03 sebastienlevert