Skip to content

Commit

Permalink
добавил jsDoc
Browse files Browse the repository at this point in the history
  • Loading branch information
veglem committed Mar 7, 2024
1 parent 94336ca commit 4802271
Show file tree
Hide file tree
Showing 7 changed files with 115 additions and 1 deletion.
33 changes: 32 additions & 1 deletion public/src/modules/ajax.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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",
Expand All @@ -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",
Expand All @@ -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"
Expand All @@ -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");

Expand All @@ -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"
Expand 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);
Expand Down
8 changes: 8 additions & 0 deletions public/src/modules/dispathcer.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
15 changes: 15 additions & 0 deletions public/src/modules/eventMaker.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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;
Expand Down
6 changes: 6 additions & 0 deletions public/src/modules/utils.js
Original file line number Diff line number Diff line change
@@ -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;
}
11 changes: 11 additions & 0 deletions public/src/shared/validation.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 ||
Expand All @@ -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};
};
4 changes: 4 additions & 0 deletions public/src/stores/user/events.js
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
39 changes: 39 additions & 0 deletions public/src/stores/user/userStore.js
Original file line number Diff line number Diff line change
Expand Up @@ -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<void>}
*/
async login(credentials){
try {
const res = await AppAuthRequests.Login(credentials.login, credentials.password);
Expand All @@ -62,6 +80,10 @@ class UserStore {
}
}

/**
*
* @returns {Promise<void>}
*/
async logout() {
try {
await AppAuthRequests.Logout();
Expand All @@ -75,6 +97,11 @@ class UserStore {
}
}

/**
*
* @param credentials
* @returns {Promise<void>}
*/
async register(credentials) {
try {
const res = await AppAuthRequests.SignUp(credentials.login, credentials.password);
Expand All @@ -88,6 +115,10 @@ class UserStore {
}
}

/**
*
* @returns {Promise<void>}
*/
async checkUser(){
try {
console.log("зареган");
Expand All @@ -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",
Expand Down

0 comments on commit 4802271

Please sign in to comment.