Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merge Realm JS and Realm Web types #2950

Merged
merged 32 commits into from
Jul 10, 2020
Merged

Merge Realm JS and Realm Web types #2950

merged 32 commits into from
Jul 10, 2020

Conversation

kraenhansen
Copy link
Member

@kraenhansen kraenhansen commented Jun 9, 2020

What, How & Why?

The purpose of this PR is to merge duplicated type definitions (such as App, User, UserProfile) across ´types/index.d.ts` and the other files in that directory.

I propose the following changes to the Realm JS types:

  • User related changes:
    • Rename identity on User to id (to align with other SDKs and avoid confusion from authentication provider identities returned when fetching the user profile).
    • Rename token to accessToken (to make it more obvious what token this is) and add the refreshToken as a property (this is up for debate - but it makes it easier to test the User implementation).
    • Change customData to use a generic type defined by User and the App (like we do for FunctionsFactoryType) to allow users of the API to define types for this custom data, once (when creating the app instance).
    • Change the return type of logOut to Promise<void> | void, since it sends a request to invalidate the refresh token.
  • App related changes:
    • Change the allUsers from a method to a getter-property returning a list instead of a UserMap.
    • Change the currentUser from a method to a getter-property.
    • Have removeUser return a Promise<void> instead of Promise<User>, since that user would have limited use anyway.
  • Auth related changes:
    • Rename from API* to Api.
    • Prepend Auth to the class names (Realm.Auth.ApiKeyAuth and Realm.Auth.EmailPasswordAuth).
    • A lot of changes to the naming of methods in the provider clients (see commits for details).
    • Added a callResetPasswordFunction which was part of the existing Stitch SDK.

I propose the following changest to the Realm Web Types:

  • App related changes:
    • Remove string as a valid argument on methods called with a user parameter (it was ment to enable users to pass in a user ID instead of a user object).
  • UserProfile related changes:
    • Rename pictureURL to pictureUrl.
  • Auth related changes:
    • Rename ApiKeyProvider to ApiKeyAuth.
    • Rename EmailPasswordProvider to EmailPasswordAuth.
    • Rename get and list to fetch and fetchAll respectively, on the ApiKeyProvider.
    • Rename resendConfirmation to resendConfirmationEmail on the EmailPasswordProvider.

These changes lead to some simplifications of the way user state is managed by the app, which also found its way into this PR.

@kraenhansen kraenhansen self-assigned this Jun 9, 2020
@kraenhansen kraenhansen changed the title Updating package locks from release Merge Realm JS and Realm Web types Jun 10, 2020
@kraenhansen kraenhansen force-pushed the kh/v10-merged-types branch 3 times, most recently from e8b2d7a to a559969 Compare June 10, 2020 23:17
@kraenhansen kraenhansen marked this pull request as ready for review June 17, 2020 10:55
}

public async refreshCustomData() {
await this.refreshAccessToken();
Copy link
Contributor

Choose a reason for hiding this comment

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

So in reality, an exception is thrown, right?

Copy link
Member Author

@kraenhansen kraenhansen Jun 18, 2020

Choose a reason for hiding this comment

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

Yeah, for now :) Until #2967 gets fixed.

types/app.d.ts Outdated Show resolved Hide resolved
@blagoev
Copy link
Contributor

blagoev commented Jun 18, 2020

what does this means

Change the return type of logOut to Promise | void, since it sends a request to invalidate the refresh token.

I find it problematic to have the return type differ. Sometimes returning Promise and sometimes void. When would this return void. Or I am not reading this right?

@kraenhansen
Copy link
Member Author

I find it problematic to have the return type differ. Sometimes returning Promise and sometimes void. When would this return void. Or I am not reading this right?

Ideally this would just be Promise<void> and you're right, it's confusing.

It's because Realm Web sends a request (like the legacy SDKs) but that doesn't seem to be implemented in the way Object Store logs out a user - or it's implemented in a way that allows it to fail silently.

I propose we change it to make Realm JS return a resolved Promise<void> (even though it's just a synchronous call underneath) and create an issue in object store tracking that it should send the request to invalidate the refresh token. Would that be better?

@blagoev
Copy link
Contributor

blagoev commented Jun 19, 2020

exactly what I was thinking. if its a synchronous call in some cases this should return a resolved Promise.

@kraenhansen kraenhansen requested a review from blagoev June 19, 2020 12:34
@kraenhansen kraenhansen mentioned this pull request Jun 22, 2020
5 tasks
// Add the user at the top of the stack
this.users.splice(0, 0, handle);
this.users.splice(0, 0, user);
Copy link
Contributor

Choose a reason for hiding this comment

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

unshift would just take the user.

@@ -153,13 +114,7 @@ export class User implements Realm.User {
private _state: Realm.UserState;
private transport: AuthenticatedTransport;
Copy link
Contributor

Choose a reason for hiding this comment

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

A bit inconsistency in prefixing props marked as private with _.
Have we considered to switching away from TS public/private keyword (which doesn't translate ti JS), and adopt JS private class fields.
I'm pretty sure latest versions of TS will emulates the runtime behaviour of private fields the best it can.

Copy link
Member Author

Choose a reason for hiding this comment

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

The private fields with an _ prefix is only because the class also has a public getter overloading the name. It could be called internalState or something else. But I actually think this specific fields goes away in one of the later PRs.

With regards to JS private fields. It's valuable to keep an eye on, but I don't think the adoption in browsers are good enough for us to rely on. And I don't know the state of polyfils for this?

Copy link
Contributor

Choose a reason for hiding this comment

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

I think TS handles that, but I'm not sure (as in I haven't tested it) microsoft/TypeScript#30829

Copy link
Member Author

Choose a reason for hiding this comment

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

Oh ...

@kraenhansen kraenhansen force-pushed the kh/v10-merged-types branch 2 times, most recently from 5c68a02 to e88cbd5 Compare June 29, 2020 08:11
@kraenhansen kraenhansen force-pushed the kh/v10-merged-types branch 2 times, most recently from 40bd1e7 to b7476c1 Compare July 1, 2020 10:51
}
public switchUser(nextUser: User<FunctionsFactoryType, CustomDataType>) {
const index = this.users.findIndex(u => u === nextUser);
if (index >= 0) {
Copy link
Contributor

Choose a reason for hiding this comment

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

index !== -1 might be more precise

// Remove the user from the list of users
const index = this.users.findIndex(({ user: u }) => u === user);
const index = this.users.findIndex(u => u === user);
Copy link
Contributor

Choose a reason for hiding this comment

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

Will we always find a user?

Copy link
Member Author

@kraenhansen kraenhansen Jul 10, 2020

Choose a reason for hiding this comment

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

If not, findIndex will return -1, which would result in a bug, since the first argument of splice states:

An integer that specifies at what position to add/remove items, Use negative values to specify the position from the end of the array

types/app.d.ts Outdated
logOut(): Promise<void>;

/**
* Link the user with a new identity represented by another set of credentials.
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
* Link the user with a new identity represented by another set of credentials.
* Link the user with an identity represented by another set of credentials.

types/app.d.ts Outdated
linkCredentials(credentials: Credentials): Promise<void>;

/**
* Call a remote MongoDB Realm function by it's name.
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
* Call a remote MongoDB Realm function by it's name.
* Call a remote MongoDB Realm function by its name.

kraenhansen and others added 3 commits July 10, 2020 09:49
* Renamed UserAPIKeyProvider to ApiKeyAuth

* Renaming instance methods

* Moved from user.auth.apiKeys to user.apiKeys

* Renamed js_user_apikey_provider.hpp to js_api_key_auth.hpp

* Updated the jsdocs

* Adding apiKeys to the browser proxy

* Adding a note in the changelog
Kræn Hansen and others added 6 commits July 10, 2020 12:36
    * Renamed to js_email_password_auth.hpp, email-password-auth-methods.js and api-key-auth-methods.js
    * Adding a note in the changelog
    * Updated docs
    * Renamed to EmailPasswordAuth
* registerEmail -> registerUser

* Realm.User.identity -> Realm.User.id. Realm.User.token -> Realm.User.accessToken. Add Realm.User.refreshToken.

* Turn Realm.App.{currentUser, allUsers} to an instance property

* Add Realm.Auth.EmailPassword.callResetPasswordFunction

* Wrongly merged
@kraenhansen kraenhansen merged commit fb9f1d1 into v10 Jul 10, 2020
@kraenhansen kraenhansen deleted the kh/v10-merged-types branch July 10, 2020 13:18
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Mar 16, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants