@flumens/models 0.11.3 → 0.12.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/Drupal/User.d.ts
CHANGED
|
@@ -30,6 +30,8 @@ export interface Data extends GenericModelData {
|
|
|
30
30
|
expires_in: number;
|
|
31
31
|
token_type: string;
|
|
32
32
|
};
|
|
33
|
+
/** A flag that is set when the full profile is downloaded. */
|
|
34
|
+
profileFetched?: boolean;
|
|
33
35
|
}
|
|
34
36
|
export interface Options extends GenericModelOptions {
|
|
35
37
|
/**
|
|
@@ -100,6 +102,7 @@ export default class DrupalUserModel<T extends Data = Data> extends Model<T> {
|
|
|
100
102
|
register(email: string, password: string, otherFields: any): Promise<void>;
|
|
101
103
|
resetPassword(email: string): Promise<void>;
|
|
102
104
|
delete(): Promise<void>;
|
|
105
|
+
updateRemote(values: Record<string, any>): Promise<void>;
|
|
103
106
|
/**
|
|
104
107
|
* Gets full user profile inc. fresh new tokens.
|
|
105
108
|
*/
|
package/dist/Drupal/User.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e=require("tslib"),t=require("axios"),r=require("jwt-decode"),s=require("zod"),
|
|
1
|
+
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e=require("tslib"),t=require("axios"),r=require("jwt-decode"),s=require("zod"),n=require("@flumens/utils"),i=require("../Model.js");function a(e){return e&&"object"==typeof e&&"default"in e?e:{default:e}}var o=a(t);function c(e){var t={};return Object.keys(e).forEach((function(r){if(r.match(/^field_/)){var s=r.replace("field_","").replace(/_([a-z])/g,(function(e){return e[1].toUpperCase()}));if(!e[r])return;if(Array.isArray(e[r])&&e[r].length)return void(t[s]=e[r][0].value);t[s]=e[r]}})),t}var u=function(e){var t;if(e.response&&e.response.data&&e.response.data.message){if(e.response.data.message.includes("is already taken"))throw new n.HandledError("This email is already taken.");if("The user credentials were incorrect."===e.response.data.message)throw new n.HandledError("Incorrect password or email");if("Unrecognized username or email address."===e.response.data.message)throw new n.HandledError("Unrecognized email address.");if("This account is already activated"===e.response.data.message)throw new n.HandledError("This account is already activated.");if(e.response.data.message.includes("not been activated"))throw new n.HandledError("The user has not been activated or is blocked.");throw new Error(e.response.data.message)}if(null===(t=e.message)||void 0===t?void 0:t.includes("timeout"))throw new n.HandledError("Timeout");throw e},d=6e4;var h={email:"",tokens:void 0,verified:!1,iss:void 0,indiciaUserId:void 0,profileFetched:!1},l=function(t){function n(r){var s=this,n=r.config,i=e.__rest(r,["config"]);return(s=t.call(this,e.__assign(e.__assign({},i),{data:e.__assign(e.__assign({},h),i.data)}))||this).config=n,s}return e.__extends(n,t),n.prototype.logIn=function(t,r){return e.__awaiter(this,void 0,void 0,(function(){var s;return e.__generator(this,(function(e){switch(e.label){case 0:return[4,this._exchangePasswordToTokens(t,r)];case 1:return s=e.sent(),this.data.tokens=s,[4,this.refreshProfile()];case 2:return e.sent(),[2,this.save()]}}))}))},n.prototype.register=function(t,r,s){return e.__awaiter(this,void 0,void 0,(function(){var i,a,h,l,f;return e.__generator(this,(function(_){switch(_.label){case 0:return i=JSON.stringify(e.__assign({name:[{value:t}],pass:[{value:r}],mail:[{value:t}]},s)),a={method:"post",url:"".concat(this.config.url,"/user/register-with-password?_format=json"),headers:{"content-type":"application/json"},data:i,timeout:d},[4,o.default(a).catch(u)];case 1:if(h=_.sent().data,!!n.registerSchemaBackend.safeParse(h).error)throw new Error("Invalid backend response.");return[4,this._exchangePasswordToTokens(t,r)];case 2:return l=_.sent(),this.id=h.uid[0].value,f=c(h),Object.assign(this.data,e.__assign({email:t,tokens:l,profileFetched:!0},f)),[2,this.save()]}}))}))},n.prototype.resetPassword=function(t){return e.__awaiter(this,void 0,void 0,(function(){var r,s,n;return e.__generator(this,(function(e){switch(e.label){case 0:r=JSON.stringify({mail:t}),s={method:"post",url:"".concat(this.config.url,"/user/password?_format=json"),headers:{"content-type":"application/json"},data:r},e.label=1;case 1:return e.trys.push([1,3,,6]),[4,o.default(s).catch(u)];case 2:return e.sent(),[3,6];case 3:return"The user has not been activated or is blocked."!==(n=e.sent()).message?[3,5]:[4,this._sendVerificationEmail(t)];case 4:return e.sent(),[2];case 5:return u(n),[3,6];case 6:return[2]}}))}))},n.prototype.delete=function(){return e.__awaiter(this,void 0,void 0,(function(){var t,r;return e.__generator(this,(function(e){switch(e.label){case 0:return[4,this.getAccessToken(!0)];case 1:return t=e.sent(),r={method:"delete",url:"".concat(this.config.url,"/user/").concat(this.id,"?_format=json"),headers:{Authorization:"Bearer ".concat(t)}},[4,o.default(r).catch(u)];case 2:return e.sent(),this.logOut(),[2]}}))}))},n.prototype.updateRemote=function(t){return e.__awaiter(this,void 0,void 0,(function(){var r,s;return e.__generator(this,(function(e){switch(e.label){case 0:return[4,this.getAccessToken()];case 1:return r=e.sent(),s={method:"patch",url:"".concat(this.config.url,"/user/").concat(this.id,"?_format=json"),data:t,headers:{Authorization:"Bearer ".concat(r)},timeout:d},[4,o.default(s)];case 2:return e.sent(),[4,this._refreshAccessToken()];case 3:return e.sent(),this.save(),[2]}}))}))},n.prototype.refreshProfile=function(){return e.__awaiter(this,void 0,void 0,(function(){var t=this;return e.__generator(this,(function(r){return this._refreshingProfilePromise||(this._refreshingProfilePromise=Promise.resolve().then((function(){return e.__awaiter(t,void 0,void 0,(function(){var t,r,s,n;return e.__generator(this,(function(i){switch(i.label){case 0:return[4,this.getAccessToken(!0)];case 1:return t=i.sent(),this.data.verified?(r={url:"".concat(this.config.url,"/user/").concat(this.id,"?_format=json"),headers:{Authorization:"Bearer ".concat(t)},timeout:d},[4,o.default(r).catch(u)]):[3,3];case 2:s=i.sent().data,n=c(s),Object.assign(this.data,e.__assign({email:s.mail[0].value,profileFetched:!0},n)),i.label=3;case 3:return[4,this.save()];case 4:return i.sent(),delete this._refreshingProfilePromise,[2]}}))}))})).catch((function(e){throw delete t._refreshingProfilePromise,e}))),[2,this._refreshingProfilePromise]}))}))},n.prototype.hasLogIn=function(){return console.warn("hasLogIn is deprecated, please use isLoggedIn instead."),this.isLoggedIn()},n.prototype.isLoggedIn=function(){return this.data.email,!!this.id},n.prototype.logOut=function(){return e.__awaiter(this,void 0,void 0,(function(){return e.__generator(this,(function(e){return[2,this.reset()]}))}))},n.prototype.reset=function(r){return t.prototype.reset.call(this,e.__assign(e.__assign({},h),r))},n.prototype.getAccessToken=function(t){return e.__awaiter(this,void 0,void 0,(function(){var s,n,i,a,o;return e.__generator(this,(function(e){switch(e.label){case 0:if(!t&&!this.isLoggedIn())throw new Error("User is not logged in.");if(s=(this.data.tokens||{}).access_token,n=function(e){if(!e)return!0;var t=1e3*r.jwtDecode(e).exp;return Date.now()+12e4>t}(s),!(!s||n||t))return[3,6];e.label=1;case 1:return e.trys.push([1,3,,6]),[4,this._refreshAccessToken()];case 2:return e.sent(),[3,6];case 3:return i=e.sent(),console.error(i),a="The refresh token is invalid."===i.message,o="Token has no user email."===i.message,a||o?[4,this.logOut()]:[3,5];case 4:throw e.sent(),new Error("User re-login is required.");case 5:throw i;case 6:return[2,this.data.tokens.access_token]}}))}))},n.prototype._sendVerificationEmail=function(t){return e.__awaiter(this,void 0,void 0,(function(){var r,s;return e.__generator(this,(function(e){return r=JSON.stringify({mail:[{value:t||this.data.email}]}),s={method:"post",url:"".concat(this.config.url,"/user/register-with-password?_format=json&resendVerificationEmail=true"),headers:{"content-type":"application/json"},data:r,timeout:d},[2,o.default(s).catch(u)]}))}))},n.prototype._exchangePasswordToTokens=function(t,r){return e.__awaiter(this,void 0,void 0,(function(){var s,n,i;return e.__generator(this,(function(e){switch(e.label){case 0:return(s=new FormData).append("grant_type","password"),s.append("username",t),s.append("password",r),s.append("client_id",this.config.clientId),this.config.clientPass&&s.append("client_secret",this.config.clientPass),(null===(i=this.config.scopes)||void 0===i?void 0:i.length)&&s.append("scope",this.config.scopes.join(" ")),n={method:"post",url:"".concat(this.config.url,"/oauth/token"),data:s},[4,o.default(n).catch(u)];case 1:return[2,e.sent().data]}}))}))},n.prototype._refreshAccessToken=function(){return e.__awaiter(this,void 0,void 0,(function(){var t=this;return e.__generator(this,(function(s){return this._refreshingTokenPromise||(this._refreshingTokenPromise=Promise.resolve().then((function(){return e.__awaiter(t,void 0,void 0,(function(){var t,s,n,i,a,h,l,f,_,g,p,m;return e.__generator(this,(function(v){switch(v.label){case 0:if(!(t=(this.data.tokens||{}).refresh_token))throw new Error("No user session refresh token was found");return(s=new FormData).append("grant_type","refresh_token"),s.append("refresh_token",t),s.append("client_id",this.config.clientId),this.config.clientPass&&s.append("client_secret",this.config.clientPass),n={method:"post",url:"".concat(this.config.url,"/oauth/token"),data:s,timeout:d},[4,o.default(n).catch(u)];case 1:if(i=v.sent().data,a=e.__assign(e.__assign({},this.data.tokens),i),h=r.jwtDecode(a.access_token),!(l=h.email))throw new Error("Token has no user email.");return f=h.iss,_=h.email_verified,g=h["http://indicia.org.uk/user:id"],p=h.scopes,m=c(h),this.id=h.sub,Object.assign(this.data,e.__assign({email:l,roles:p,verified:_,iss:f,indiciaUserId:g,tokens:a},m)),[4,this.save()];case 2:return v.sent(),delete this._refreshingTokenPromise,[2]}}))}))})).catch((function(e){throw delete t._refreshingTokenPromise,e}))),[2,this._refreshingTokenPromise]}))}))},n.loginSchema=s.object({email:s.z.string().email("Please fill in"),password:s.z.string().min(1,"Please fill in")}),n.loginSchemaBackend=s.object({id:s.z.number(),email:s.z.string().email()}),n.resetSchema=s.object({email:s.z.string().email("Please fill in")}),n.registerSchema=s.object({email:s.z.string().email("Please fill in"),password:s.z.string().min(1,"Please fill in")}),n.registerSchemaBackend=s.object({uid:s.z.array(s.object({value:s.z.number()}))}),n}(i.default);exports.default=l;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e=require("tslib"),t=require("mobx"),r=require("axios"),s=require("@flumens/utils/dist/errors"),
|
|
1
|
+
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e=require("tslib"),t=require("mobx"),r=require("axios"),s=require("@flumens/utils/dist/errors"),n=require("../Collection.js"),i=require("./Group.js"),o=require("./helpers.js");function a(e){return e&&"object"==typeof e&&"default"in e?e:{default:e}}var u=a(r),c=function(r){function n(s){var n=r.call(this,e.__assign({id:"groups",Model:i.default},s))||this;return n.remote=t.observable({synchronising:!1,url:s.url,getAccessToken:s.getAccessToken}),n}return e.__extends(n,r),n.prototype.fetchRemote=function(){return e.__awaiter(this,arguments,void 0,(function(t){var r,n,i,a,c,l,d,h,f,m,p,g,b,y,v,w,j=this;return void 0===t&&(t={}),e.__generator(this,(function(e){switch(e.label){case 0:console.log("📚 Collection: ".concat(this.id," collection fetching")),this.remote.synchronising=!0,e.label=1;case 1:return e.trys.push([1,8,,9]),r=t.type,n=t.form,i=t.location,[4,this.remote.getAccessToken()];case 2:return a=e.sent(),c="".concat(this.remote.url,"/index.php/services/rest/groups"),l={},r&&(l.view=r),n&&(l.page=n.join(",")),Number.isFinite(i)&&(l.indexed_location_ids=i),d={params:l,headers:{Authorization:"Bearer ".concat(a)},timeout:8e4},[4,u.default.get(c,d)];case 3:if(h=e.sent(),f=function(e){return!e.toDate||new Date(e.toDate)>new Date},m=function(e){return!e.fromDate||new Date(e.fromDate)<new Date},p=h.data.map((function(e){return e.values})).map(o.getCamelCaseObj).filter(f).filter(m),g=p.map((function(e){return j.Model.fromDTO(e,{skipStore:"joinable"===r})})),r){if("member"===r)for(b=0;b<this.data.length;b++)(y=this.data[b]).isStored&&"t"===y.data.userIsMember&&(y.destroy(),this.data.splice(b,1),b--);else if("pending"===r)for(b=0;b<this.data.length;b++)(y=this.data[b]).isStored&&"t"===y.data.userIsPending&&(y.destroy(),this.data.splice(b,1),b--);else if("joinable"===r)for(b=0;b<this.data.length;b++)(y=this.data[b]).isStored||(this.data.splice(b,1),b--)}else for(b=0;b<this.data.length;b++)(y=this.data[b]).isStored&&y.destroy(),this.data.splice(b,1),b--;return"member"!==r?[3,5]:[4,Promise.all(g.map((function(e){return e.save()})))];case 4:e.sent(),e.label=5;case 5:return"pending"!==r?[3,7]:[4,Promise.all(g.map((function(e){return e.save()})))];case 6:e.sent(),e.label=7;case 7:return(w=this.data).push.apply(w,g),console.log("📚 Collection: ".concat(this.id," collection fetching done ").concat(p.length," documents")),[3,9];case 8:if(v=e.sent(),u.default.isCancel(v))return[2];if(s.isAxiosNetworkError(v))throw new s.HandledError("Request aborted because of a network issue (timeout or similar).");if("issues"in v)throw new Error(v.issues.map((function(e){return"".concat(e.path.join(" ")," ").concat(e.message)})).join(" "));throw this.remote.synchronising=!1,v;case 9:return this.remote.synchronising=!1,[2]}}))}))},Object.defineProperty(n.prototype,"isSynchronising",{get:function(){return this.remote.synchronising},enumerable:!1,configurable:!0}),n}(n.default);exports.byGroupMembershipStatus=function(e){return function(t){var r=t.data.userIsMember||t.data.userIsPending;return"member"===e?r:!r}},exports.default=c;
|