diff --git a/public/src/modules/ajax.js b/public/src/modules/ajax.js index 852fde37..e8ae28f7 100644 --- a/public/src/modules/ajax.js +++ b/public/src/modules/ajax.js @@ -16,6 +16,13 @@ let JWT = null; JWT = window.localStorage.getItem("Authorization"); +/** + * + * @param method {methods} + * @param url {string} + * @param data {any} + * @returns {Promise<{body: {message}, status: number}|{body: any, status: number}>} + */ const baseRequest = async (method, url, data = null) => { const options = { method: method, @@ -57,6 +64,12 @@ const baseRequest = async (method, url, data = null) => { class AuthRequests { #baseUrl = "/auth"; + /** + * + * @param username{string} + * @param password{string} + * @returns {Promise<{create_time: string, image_path: string, id: string, username: string}>} + */ Login = async (username, password) => { const {status, body} = await baseRequest( methods.POST,this.#baseUrl + "/login", @@ -79,6 +92,12 @@ class AuthRequests { throw Error(body.message); }; + /** + * + * @param username{string} + * @param password{string} + * @returns {Promise<{create_time: string, image_path: string, id: string, username: string}>} + */ SignUp = async (username, password) => { const {status, body} = await baseRequest( methods.POST,this.#baseUrl + "/signup", @@ -101,6 +120,10 @@ class AuthRequests { throw Error(body.message); }; + /** + * + * @returns {Promise<{message: string}>} + */ Logout = async () => { const {status, body} = await baseRequest( methods.DELETE,this.#baseUrl + "/logout" @@ -119,6 +142,11 @@ class AuthRequests { } }; + /** + * + * @returns {Promise<{message}|null>} + * @throws Error - not authorized + */ CheckUser = async () => { const {status, body} = await baseRequest(methods.GET, this.#baseUrl + "/check_user"); @@ -133,6 +161,10 @@ class AuthRequests { class NoteRequests { #baseUrl = "/note"; + /** + * + * @returns {Promise<{message}|{any}>} + */ GetAll = async () => { const {status, body} = await baseRequest( methods.GET,this.#baseUrl + "/get_all" @@ -144,7 +176,6 @@ class NoteRequests { const bytes = Uint8Array.from(decoded, (m) => m.codePointAt(0)); elem.data = JSON.parse(new TextDecoder().decode(bytes)); } - console.log(body); return body; } else { throw Error(body.message); diff --git a/public/src/modules/dispathcer.js b/public/src/modules/dispathcer.js index 38cb803d..c56a44fa 100644 --- a/public/src/modules/dispathcer.js +++ b/public/src/modules/dispathcer.js @@ -5,10 +5,18 @@ class Dispatcher{ this.#callbacks = []; } + /** + * + * @param callback{function} + */ register(callback){ this.#callbacks.push(callback); } + /** + * + * @param action{type: string, payload: any} + */ dispatch(action){ this.#callbacks.forEach((callback) => { callback(action); diff --git a/public/src/modules/eventMaker.js b/public/src/modules/eventMaker.js index 2a5af1ac..b56b771e 100644 --- a/public/src/modules/eventMaker.js +++ b/public/src/modules/eventMaker.js @@ -5,6 +5,11 @@ class EventMaker{ this.#eventsMap = new Map(); } + /** + * + * @param event{string} + * @param callback{function} + */ subscribe(event, callback){ if (event in this.#eventsMap){ this.#eventsMap[event].add(callback); @@ -13,10 +18,20 @@ class EventMaker{ this.#eventsMap[event] = new Set([callback]); } + /** + * + * @param event{string} + * @param callback{function} + */ unsubscribe(event, callback){ this.#eventsMap[event].delete(callback); } + /** + * + * @param event{string} + * @param args{any} + */ notify(event, ...args){ if(!(event in this.#eventsMap)){ return; diff --git a/public/src/modules/utils.js b/public/src/modules/utils.js index a2d8637b..1f5a6709 100644 --- a/public/src/modules/utils.js +++ b/public/src/modules/utils.js @@ -1,3 +1,9 @@ +/** + * + * @param str{string} + * @param n{number} + * @returns {string} + */ export function truncate(str, n){ return (str.length > n) ? str.slice(0, n-1) + "..." : str; } \ No newline at end of file diff --git a/public/src/shared/validation.js b/public/src/shared/validation.js index 2b7f5974..aae07cba 100644 --- a/public/src/shared/validation.js +++ b/public/src/shared/validation.js @@ -30,6 +30,11 @@ export const ValidatePassword = (value) => { return ValidationResult(true); }; +/** + * + * @param value{string} + * @returns {{result: boolean, message: string | null}} + */ export const ValidateLogin = (value) => { for (let index = 0; index < value.length; ++index){ if (!(value.charCodeAt(index) >= 97 && value.charCodeAt(index) <= 122 || @@ -55,6 +60,12 @@ export const ValidateLogin = (value) => { return ValidationResult(true); }; +/** + * + * @param result{boolean} + * @param message{string | null} + * @returns {{result: boolean, message: string | null}} + */ const ValidationResult = (result, message = null) => { return {result, message}; }; \ No newline at end of file diff --git a/public/src/stores/user/events.js b/public/src/stores/user/events.js index 2de63f1e..052b436a 100644 --- a/public/src/stores/user/events.js +++ b/public/src/stores/user/events.js @@ -1,3 +1,7 @@ +/** + * + * @type {{LOGOUT: string, SUCCESSFUL_LOGIN: string, CHANGE_PAGE: string, LOGIN_ALREADY_EXIST: string, INVALID_LOGIN_OR_PASSWORD: string}} + */ export const UserStoreEvents = { SUCCESSFUL_LOGIN: "SUCCESSFUL_LOGIN", LOGOUT: "LOGOUT", diff --git a/public/src/stores/user/userStore.js b/public/src/stores/user/userStore.js index 3fd488dd..cff7e7d3 100644 --- a/public/src/stores/user/userStore.js +++ b/public/src/stores/user/userStore.js @@ -35,20 +35,38 @@ class UserStore { }); } + /** + * + * @returns {string} + */ get username() { return this.#state.username; } + /** + * + * @returns {string} + */ get avatar() { return this.#state.avatarUrl; } + /** + * + * @returns {boolean} + * @constructor + */ IsAuthenticated() { console.log("IsAuthenticated") console.log(this.#state) return this.#state.isAuth; } + /** + * + * @param credentials{{login: string, password: string}} + * @returns {Promise} + */ async login(credentials){ try { const res = await AppAuthRequests.Login(credentials.login, credentials.password); @@ -62,6 +80,10 @@ class UserStore { } } + /** + * + * @returns {Promise} + */ async logout() { try { await AppAuthRequests.Logout(); @@ -75,6 +97,11 @@ class UserStore { } } + /** + * + * @param credentials + * @returns {Promise} + */ async register(credentials) { try { const res = await AppAuthRequests.SignUp(credentials.login, credentials.password); @@ -88,6 +115,10 @@ class UserStore { } } + /** + * + * @returns {Promise} + */ async checkUser(){ try { console.log("зареган"); @@ -103,8 +134,16 @@ class UserStore { } } +/** + * + * @type {UserStore} + */ export const AppUserStore = new UserStore(); +/** + * + * @type {{REGISTER: string, LOGOUT: string, CHANGE_PAGE: string, LOGIN: string, CHECK_USER: string}} + */ export const UserActions = { LOGIN: "LOGIN", REGISTER: "REGISTER",