Skip to content

Commit

Permalink
Match types for User#callFunction implementation across platforms (#4840
Browse files Browse the repository at this point in the history
)

* Remove deprecated EmailPasswordAuth functions (#4842)

The positional argument functions have been removed and now
only the object arguments are accepted.

* Match types for `User#callFunction` implementation across platforms

The unifies the call signature of User#callFunction across all documentation
and platforms.
`callFunction` now has the signature:
```javascript
  user.callFunction('func', 1, 2, 3);
```

* Update lib/user.js

Co-authored-by: Kræn Hansen <kraen.hansen@mongodb.com>

Co-authored-by: Kræn Hansen <kraen.hansen@mongodb.com>
  • Loading branch information
takameyer and kraenhansen authored Sep 14, 2022
1 parent bb8ce55 commit bdc14b7
Show file tree
Hide file tree
Showing 8 changed files with 99 additions and 297 deletions.
19 changes: 18 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,23 @@
### Notes
Based on Realm JS v10.21.0: See changelog below for details on enhancements and fixes introduced between this and the previous pre release (which was based on Realm JS v10.19.5).

### Breaking change
* Removed deprecated positional arguments to Email/Password authentication functions
* The following functions now only accept object arguments:
```javascript
Realm.Auth.EmailPasswordAuth.registerUser({email, password});
Realm.Auth.EmailPasswordAuth.confirmUser({token, tokenId});
Realm.Auth.EmailPasswordAuth.resendConfirmationEmail({email});
Realm.Auth.EmailPasswordAuth.retryCustomConfirmation({email});
Realm.Auth.EmailPasswordAuth.resetPassword({token, tokenId, password});
Realm.Auth.EmailPasswordAuth.sendResetPasswordEmail({email});
Realm.Auth.EmailPasswordAuth.callResetPasswordFunction({email, password}, ...args);
* Unify the call signature documentation of `User#callFunction` ([#3733](https://github.com/realm/realm-js/issues/3733))
* Example:
```javascript
user.callFunction("sum", 1, 2, 3); // Valid
user.callFunction("sum", [1, 2, 3]); // Invalid
```
### Enhancements
* Small improvement to performance by caching JSI property String object [#4863](https://github.com/realm/realm-js/pull/4863)

Expand All @@ -28,7 +45,7 @@ Based on Realm JS v10.21.0: See changelog below for details on enhancements and
* Fixed an exception `fcntl() with F_BARRIERFSYNC failed: Inappropriate ioctl for device` when running with MacOS on an exFAT drive. ([realm/realm-core#5789](https://github.com/realm/realm-core/issues/5789), since v10.18.0)
* Syncing of a Decimal128 with big significand could result in a crash. ([realm/realm-core#5728](https://github.com/realm/realm-core/issues/5728), since v10.0.0)
* `discardLocal` client reset mode will now wait for flexible sync Realms to be fully synchronized before beginning recovery operations. ([realm/realm-core#5705](https://github.com/realm/realm-core/issues/5705), since v10.11.0)

### Compatibility
* MongoDB Realm Cloud.
* Realm Studio v11.0.0.
Expand Down
127 changes: 28 additions & 99 deletions docs/sync.js
Original file line number Diff line number Diff line change
Expand Up @@ -542,152 +542,81 @@ class EmailPasswordAuth {
* Registers a new email identity with the email/password provider,
* and sends a confirmation email to the provided address.
*
* @param {object} userDetails The new user's email and password details
* @param {string} userDetails.email - The email address of the user to register.
* @param {string} userDetails.password - The password that the user created for the new username/password identity.
* @param {object} details The new user's email and password details
* @param {string} details.email - The email address of the user to register.
* @param {string} details.password - The password that the user created for the new username/password identity.
* @returns {Promise<void>}
* @since v10.10.0
*/
registerUser(userDetails) {}

/**
* Registers a new email identity with the email/password provider,
* and sends a confirmation email to the provided address.
*
* @param {string} email - The email address of the user to register.
* @param {string} password - The password that the user created for the new username/password identity.
* @returns {Promise<void>}
* @deprecated Use `registerUser(userDetails)` instead
*/
registerUser(email, password) {}
registerUser(details) {}

/**
* Confirms an email identity with the email/password provider.
*
* @param {object} tokenDetails The received token and ID details
* @param {string} tokenDetails.token - The confirmation token that was emailed to the user.
* @param {string} tokenDetails.tokenId - The confirmation token id that was emailed to the user.
* @param {object} details The received token and ID details
* @param {string} details.token - The confirmation token that was emailed to the user.
* @param {string} details.tokenId - The confirmation token id that was emailed to the user.
* @returns {Promise<void>}
* @since v10.10.0
*/
confirmUser(tokenDetails) {}

/**
* Confirms an email identity with the email/password provider.
*
* @param {string} token - The confirmation token that was emailed to the user.
* @param {string} id - The confirmation token id that was emailed to the user.
* @returns {Promise<void>}
* @deprecated Use `confirmUser(tokenDetails)` instead
*/
confirmUser(token, id) {}
confirmUser(details) {}

/**
* Re-sends a confirmation email to a user that has registered but
* not yet confirmed their email address.
*
* @param {object} emailDetails The associated email details
* @param {string} emailDetails.email - The email address of the user to re-send a confirmation for.
* @param {object} details The associated email details
* @param {string} details.email - The email address of the user to re-send a confirmation for.
* @returns {Promise<void>}
* @since v10.10.0
*/
resendConfirmationEmail(emailDetails) {}

/**
* Re-sends a confirmation email to a user that has registered but
* not yet confirmed their email address.
*
* @param {string} email - The email address of the user to re-send a confirmation for.
* @returns {Promise<void>}
* @deprecated Use `resendConfirmationEmail(emailDetails)` instead
*/
resendConfirmationEmail(email) {}
resendConfirmationEmail(details) {}

/**
* Re-run the custom confirmation function for user that has registered but
* not yet confirmed their email address.
*
* @param {object} emailDetails The associated email details
* @param {string} emailDetails.email - The email address of the user to re-run the confirmation for.
* @param {object} details The associated email details
* @param {string} details.email - The email address of the user to re-run the confirmation for.
* @returns {Promise<void>}
* @since v10.10.0
*/
retryCustomConfirmation(emailDetails) {}

/**
* Re-run the custom confirmation function for user that has registered but
* not yet confirmed their email address.
*
* @param {string} email - The email address of the user to re-run the confirmation for.
* @returns {Promise<void>}
* @deprecated Use `retryCustomConfirmation(emailDetails)` instead
*/
retryCustomConfirmation(email) {}
retryCustomConfirmation(details) {}

/**
* Sends an email to the user for resetting the password.
*
* @param {object} emailDetails The email details to send the reset to
* @param {string} emailDetails.email - The email address of the user to re-send a confirmation for.
* @param {object} details The email details to send the reset to
* @param {string} details.email - The email address of the user to re-send a confirmation for.
* @returns {Promise<void>}
* @since v10.10.0
*/
sendResetPasswordEmail(emailDetails) {}

/**
* Sends an email to the user for resetting the password.
* @param {string} email - The email address of the user to re-send a confirmation for.
* @returns {Promise<void>}
* @deprecated Use `sendResetPasswordEmail(emailDetails)` instead
*/
sendResetPasswordEmail(email) {}
sendResetPasswordEmail(details) {}

/**
* Resets the password of an email identity using the password reset token emailed to a user.
*
* @param {object} resetDetails The token and password details for the reset
* @param {string} resetDetails.password - The desired new password.
* @param {string} resetDetails.token - The password reset token that was emailed to the user.
* @param {string} resetDetails.tokenId - The password reset token id that was emailed to the user.
* @param {object} details The token and password details for the reset
* @param {string} details.password - The desired new password.
* @param {string} details.token - The password reset token that was emailed to the user.
* @param {string} details.tokenId - The password reset token id that was emailed to the user.
* @returns {Promise<void>}
* @since v10.10.0
*/
resetPassword(resetDetails) {}

/**
* Resets the password of an email identity using the password reset token emailed to a user.
* @param {string} password - The desired new password.
* @param {string} token - The password reset token that was emailed to the user.
* @param {string} id - The password reset token id that was emailed to the user.
* @returns {Promise<void>}
* @deprecated Use `resetPassword(resetDetails)` instead
*/
resetPassword(password, token, id) {}
resetPassword(details) {}

/**
* Resets the password of an email identity using the
* password reset function set up in the application.
*
* @param resetDetails The email and password details to reset
* @param {string} resetDetails.email - The email address of the user.
* @param {string} resetDetails.password - The desired new password.
* @param details The email and password details to reset
* @param {string} details.email - The email address of the user.
* @param {string} details.password - The desired new password.
* @param {Array<BSON>} args - Arguments passed onto the function.
* @return {Promise<void>}
* @since v10.10.0
*/
callResetPasswordFunction(resetDetails, ...args) {}

/**
* Resets the password of an email identity using the
* password reset function set up in the application.
*
* @param {string} email - The email address of the user.
* @param {string} password - The desired new password.
* @param {Array<BSON>} args - Arguments passed onto the function.
* @return {Promise<void>}
* @deprecated Use `callResetPasswordFunction(resetDetails, ...args)` instead
*/
callResetPasswordFunction(email, password, ...args) {}
callResetPasswordFunction(details, ...args) {}
}

/**
Expand Down Expand Up @@ -871,10 +800,10 @@ class User {
/**
* Calls the named server function as this user.
* @param {string} name - name of the function to call
* @param {any[]} args = [] - list of arguments to pass
* @param {...*} [args] - arguments to pass to the function
* @return {Promise<any>} - resolves when the function terminates.
*/
callFunction(name, args) {}
callFunction(name, ...args) {}

/**
* Convenience wrapper around `callFunction(name, [args])`
Expand Down
50 changes: 15 additions & 35 deletions lib/email-password-auth-methods.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
const { EJSON } = require("bson");

const { promisify } = require("./utils.js");
const { handleDeprecatedPositionalArgs } = require("@realm.io/common");

// TODO for v11: change all signatures to methodName(argsObject) and remove handleDeprecatedPositionalArgs call
//
Expand All @@ -28,52 +27,33 @@ const { handleDeprecatedPositionalArgs } = require("@realm.io/common");
// return promisify((cb) => this._registerUser(argsObject, cb));
// },
const instanceMethods = {
registerUser(...args) {
const { argsObject } = handleDeprecatedPositionalArgs(args, "registerUser", ["email", "password"]);

return promisify((cb) => this._registerUser(argsObject, cb));
registerUser(details) {
return promisify((cb) => this._registerUser(details, cb));
},

confirmUser(...args) {
const { argsObject } = handleDeprecatedPositionalArgs(args, "confirmUser", ["token", "tokenId"]);

return promisify((cb) => this._confirmUser(argsObject, cb));
confirmUser(details) {
return promisify((cb) => this._confirmUser(details, cb));
},

resendConfirmationEmail(...args) {
const { argsObject } = handleDeprecatedPositionalArgs(args, "resendConfirmationEmail", ["email"]);

return promisify((cb) => this._resendConfirmationEmail(argsObject, cb));
resendConfirmationEmail(details) {
return promisify((cb) => this._resendConfirmationEmail(details, cb));
},

retryCustomConfirmation(...args) {
const { argsObject } = handleDeprecatedPositionalArgs(args, "retryCustomConfirmation", ["email"]);

return promisify((cb) => this._retryCustomConfirmation(argsObject, cb));
retryCustomConfirmation(details) {
return promisify((cb) => this._retryCustomConfirmation(details, cb));
},

sendResetPasswordEmail(...args) {
const { argsObject } = handleDeprecatedPositionalArgs(args, "sendResetPasswordEmail", ["email"]);

return promisify((cb) => this._sendResetPasswordEmail(argsObject, cb));
sendResetPasswordEmail(details) {
return promisify((cb) => this._sendResetPasswordEmail(details, cb));
},

resetPassword(...args) {
const { argsObject } = handleDeprecatedPositionalArgs(args, "resetPassword", ["password", "token", "tokenId"]);

return promisify((cb) => this._resetPassword(argsObject, cb));
resetPassword(details) {
return promisify((cb) => this._resetPassword(details, cb));
},

callResetPasswordFunction(...args) {
const { argsObject, restArgs } = handleDeprecatedPositionalArgs(
args,
"callResetPasswordFunction",
["email", "password"],
true,
);

const stringifiedArgs = EJSON.stringify(restArgs, { relaxed: false });
return promisify((cb) => this._callResetPasswordFunction(argsObject, stringifiedArgs, cb));
callResetPasswordFunction(details, ...args) {
const stringifiedArgs = EJSON.stringify(args, { relaxed: false });
return promisify((cb) => this._callResetPasswordFunction(details, stringifiedArgs, cb));
},
};

Expand Down
12 changes: 8 additions & 4 deletions lib/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,14 @@ const instanceMethods = {
return promisify((cb) => this._logOut(cb));
},

async callFunction(name, args = [], service = undefined) {
async callFunction(name, ...args) {
return this._callFunctionOnService(name, undefined, ...args);
},

async _callFunctionOnService(name, serviceName, ...args) {
const cleanedArgs = cleanArguments(args);
const stringifiedArgs = EJSON.stringify(cleanedArgs, { relaxed: false });
const result = await promisify((cb) => this._callFunction(name, stringifiedArgs, service, cb));
const result = await promisify((cb) => this._callFunction(name, stringifiedArgs, serviceName, cb));
return EJSON.parse(result);
},

Expand Down Expand Up @@ -98,15 +102,15 @@ const instanceMethods = {
},

// Internal helpers.
_functionsOnService(service) {
_functionsOnService(serviceName) {
const user = this;
return new Proxy(
{},
{
get(target, name, receiver) {
if (typeof name === "string" && name != "inspect") {
return (...args) => {
return user.callFunction(name, args, service);
return user._callFunctionOnService(name, serviceName, ...args);
};
} else {
return Reflect.get(target, name, receiver);
Expand Down
Loading

0 comments on commit bdc14b7

Please sign in to comment.