Skip to content

[63533] Improve Node SDK typing#274

Merged
mrashed-dev merged 14 commits into
mostafarashed/ch66699/release-node-sdk-v6-0-0from
mostafarashed/ch63533/fix-node-sdk-typing
Sep 17, 2021
Merged

[63533] Improve Node SDK typing#274
mrashed-dev merged 14 commits into
mostafarashed/ch66699/release-node-sdk-v6-0-0from
mostafarashed/ch63533/fix-node-sdk-typing

Conversation

@mrashed-dev
Copy link
Copy Markdown
Contributor

@mrashed-dev mrashed-dev commented Sep 14, 2021

Description

This PR is to address the Node SDK typing issues our customers have been experiencing. The biggest change we've made here is that all of the SDK methods that are meant to be used by the user for Nylas operations all have a non-any return type when possible.

In addition to this, the PR improves on the previous work of the refactor to ensure that we are utilizing the use of models, interfaces, and proper deserialization. With this, our support for Calendar availability has improved with the introduction of classes and types for API objects like free-busy and availability. Our support for Native Authentication and Virtual Calendars have also seen improvements as we have models to represent these objects and introduced enums to provide the user an easier time with providing things like providers and scopes without needing to refer to the API docs to see what scopes/providers are supported and to reduce user typing errors.

License

I confirm that this contribution is made under the terms of the MIT license and that I have the authority necessary to make this contribution on behalf of its copyright owner.

@shortcut-integration
Copy link
Copy Markdown

This pull request has been linked to Shortcut Story #63533: Fix Node SDK Typing.

Comment thread src/models/event.ts Outdated

deleteRequestQueryString(params: { [key: string]: any } = {}) {
deleteRequestQueryString(
params: { [key: string]: any } = {}
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mrashed-dev for such instances like { [key: string]: any } - I think it might be better to opt to use the Record<keyType, valueType> built-in type.

For example params: Recod<string,any>.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Separately, is there no way to avoid the use of any? It seems to defeat the purpose of typing :'(

If we do have to use any, could maybe unknown be of better use? (see article for more info)

Copy link
Copy Markdown
Contributor Author

@mrashed-dev mrashed-dev Sep 15, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@AaronDDM thanks for bringing Record and unknown to my attention! Let me play with it and see how it works with the different methods we have.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@AaronDDM I've updated the PR to include both changes!

Comment thread src/models/model.ts Outdated
}

fromJSON(json?: any): this {
fromJSON(json: { [key: string]: any }): any {
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Previously we returned this, but changing it to any would break typing for chaining, would it not?

e.g.

model.fromJSON({ ... }).toJSON()
                         ^ typing would be broken and unable determine if `toJSON()` is valid

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@AaronDDM good catch! Not sure why I reverted it. In any case, it's back in now.

Comment thread __tests__/connect-spec.js
@@ -78,7 +95,7 @@ describe('Connect', () => {
email_address: exchangeEmail,
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some places we have emailAddress and some places email_address, is it possible to have this consistent throughout?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This assertion (toHaveBeenCalledWith) checks the outgoing POST call. In the TS code we use emailAddress (camelCase) as the property name but when it gets serialized to JSON it becomes email_address (snake_case) to conform with what the API expects.

Comment thread src/models/message.ts
Comment on lines +78 to 80
fileIds(): (string | undefined)[] {
return this.files ? this.files.map(file => file.id) : [];
}
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mrashed-dev should this really be an array of string | undefined or can we filter out undefined ids? Not sure if there is a requirement to get the same # of array elements even if we don't have valid IDs.

Suggested change
fileIds(): (string | undefined)[] {
return this.files ? this.files.map(file => file.id) : [];
}
fileIds(): (string | undefined)[] {
return this.files ? this.files.filter(file => file.id).map(file => file.id) : [];
}

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@AaronDDM I think that it will always return string | undefined because this.files is not guaranteed to be populated, which I think means it yields an undefined array type.

- used unknown instead of any when possible
- used this instead of any when possible
- used types instead of interfaces to allow for stronger typing
@mrashed-dev mrashed-dev merged commit c4b53c6 into mostafarashed/ch66699/release-node-sdk-v6-0-0 Sep 17, 2021
@mrashed-dev mrashed-dev deleted the mostafarashed/ch63533/fix-node-sdk-typing branch September 17, 2021 14:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants