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

Format the files #382

Merged
merged 1 commit into from
Jan 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
673 changes: 372 additions & 301 deletions index.html

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import router from "./router/index.js";
import ui from "./ui/index.js";

ui()
router()
ui();
router();
2 changes: 1 addition & 1 deletion src/js/api/apiBase.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
export const apiBase = "https://whatver.com/api/posts";
export const url = new URL(apiBase);
export const url = new URL(apiBase);
8 changes: 4 additions & 4 deletions src/js/api/auth/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export * from "./login.js"
export * from "./logout.js"
export * from "./register.js"
export * from "./state.js"
export * from "./login.js";
export * from "./logout.js";
export * from "./register.js";
export * from "./state.js";
30 changes: 15 additions & 15 deletions src/js/api/auth/login.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
import { apiPath } from "../constants.js";
import { headers } from "../headers.js";
import { save } from '../../storage/index.js'
import { save } from "../../storage/index.js";

export async function login(email, password) {
const response = await fetch(`${apiPath}/social/auth/login`, {
method: "post",
body: JSON.stringify({ email, password }),
headers: headers("application/json")
})
const response = await fetch(`${apiPath}/social/auth/login`, {
method: "post",
body: JSON.stringify({ email, password }),
headers: headers("application/json"),
});

if (response.ok) {
const profile = await response.json()
save("token", profile.accessToken)
delete profile.accessToken
save("profile", profile)
return profile
}
if (response.ok) {
const profile = await response.json();
save("token", profile.accessToken);
delete profile.accessToken;
save("profile", profile);
return profile;
}

throw new Error(response.statusText)
}
throw new Error(response.statusText);
}
6 changes: 3 additions & 3 deletions src/js/api/auth/logout.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { remove } from "../../storage/index.js";

export function logout() {
remove("token")
remove("profile")
}
remove("token");
remove("profile");
}
20 changes: 10 additions & 10 deletions src/js/api/auth/register.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@ import { apiPath } from "../constants.js";
import { headers } from "../headers.js";

export async function register(name, email, password, avatar) {
const response = await fetch(`${apiPath}/social/auth/register`, {
method: "post",
body: JSON.stringify({ name, email, password, avatar }),
headers: headers("application/json")
})
const response = await fetch(`${apiPath}/social/auth/register`, {
method: "post",
body: JSON.stringify({ name, email, password, avatar }),
headers: headers("application/json"),
});

if (response.ok) {
return await response.json()
}
if (response.ok) {
return await response.json();
}

throw new Error(response.statusText)
}
throw new Error(response.statusText);
}
2 changes: 1 addition & 1 deletion src/js/api/auth/state.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ import { load } from "../../storage/index.js";

export const isLoggedIn = () => Boolean(load("token"));

export const profile = () => load("profile");
export const profile = () => load("profile");
2 changes: 1 addition & 1 deletion src/js/api/constants.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
export const apiUrl = new URL("https://nf-api.onrender.com/api/v1");
export const apiPath = apiUrl.toString()
export const apiPath = apiUrl.toString();
24 changes: 12 additions & 12 deletions src/js/api/headers.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import * as storage from '../storage/index.js'
import * as storage from "../storage/index.js";

export const headers = (contentType) => {
const token = storage.load("token");
const headers = {}
const token = storage.load("token");
const headers = {};

if (contentType) {
headers["Content-Type"] = contentType;
}
if (contentType) {
headers["Content-Type"] = contentType;
}

if (token) {
headers.Authorization = `Bearer ${token}`;
}
return headers;
}
if (token) {
headers.Authorization = `Bearer ${token}`;
}

return headers;
};
8 changes: 4 additions & 4 deletions src/js/api/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export * from "./auth/index.js"
export * from "./constants.js"
export * from "./posts/index.js"
export * from "./headers.js"
export * from "./auth/index.js";
export * from "./constants.js";
export * from "./posts/index.js";
export * from "./headers.js";
20 changes: 10 additions & 10 deletions src/js/api/posts/comment.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@ import { apiPath } from "../constants.js";
import { headers } from "../headers.js";

export async function comment(postId, body, replyToId) {
const response = await fetch(`${apiPath}/social/posts/${postId}/comment`, {
method: "post",
body: JSON.stringify({ body, replyToId }),
headers: headers("application/json")
})
const response = await fetch(`${apiPath}/social/posts/${postId}/comment`, {
method: "post",
body: JSON.stringify({ body, replyToId }),
headers: headers("application/json"),
});

if (response.ok) {
return await response.json()
}
if (response.ok) {
return await response.json();
}

throw new Error(response.statusText)
}
throw new Error(response.statusText);
}
20 changes: 10 additions & 10 deletions src/js/api/posts/create.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@ import { apiPath } from "../constants.js";
import { headers } from "../headers.js";

export async function createPost(title, body, media, tags) {
const response = await fetch(`${apiPath}/social/posts/`, {
method: "post",
body: JSON.stringify({ title, body, media, tags }),
headers: headers("application/json")
})
const response = await fetch(`${apiPath}/social/posts/`, {
method: "post",
body: JSON.stringify({ title, body, media, tags }),
headers: headers("application/json"),
});

if (response.ok) {
return await response.json()
}
if (response.ok) {
return await response.json();
}

throw new Error(response.statusText)
}
throw new Error(response.statusText);
}
18 changes: 9 additions & 9 deletions src/js/api/posts/delete.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ import { apiPath } from "../constants.js";
import { headers } from "../headers.js";

export async function deletePost(id) {
const response = await fetch(`${apiPath}/social/posts/${id}`, {
method: "delete",
headers: headers()
})
const response = await fetch(`${apiPath}/social/posts/${id}`, {
method: "delete",
headers: headers(),
});

if (response.ok) {
return await response.json()
}
if (response.ok) {
return await response.json();
}

throw new Error(response.statusText)
}
throw new Error(response.statusText);
}
12 changes: 6 additions & 6 deletions src/js/api/posts/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
export * from "./create.js"
export * from "./read.js"
export * from "./update.js"
export * from "./delete.js"
export * from "./react.js"
export * from "./comment.js"
export * from "./create.js";
export * from "./read.js";
export * from "./update.js";
export * from "./delete.js";
export * from "./react.js";
export * from "./comment.js";
18 changes: 9 additions & 9 deletions src/js/api/posts/react.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ import { apiPath } from "../constants.js";
import { headers } from "../headers.js";

export async function react(postId, symbol) {
const response = await fetch(`${apiPath}/social/posts/${postId}/react/${symbol}`, {
headers: headers(),
method: "put"
});
const response = await fetch(`${apiPath}/social/posts/${postId}/react/${symbol}`, {
headers: headers(),
method: "put",
});

if (response.ok) {
return await response.json();
}
if (response.ok) {
return await response.json();
}

throw new Error(response.statusText);
}
throw new Error(response.statusText);
}
26 changes: 15 additions & 11 deletions src/js/api/posts/read.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,23 @@ import { apiPath } from "../constants.js";
import { headers } from "../headers.js";

export async function getPosts(limit = 20, offset = 0) {
const response = await fetch(`${apiPath}/social/posts?limit=${limit}&offset=${offset}&_reactions=true&_author=true&_comments=true`, { headers: headers() });
if (response.ok) {
return await response.json()
}
const response = await fetch(`${apiPath}/social/posts?limit=${limit}&offset=${offset}&_reactions=true&_author=true&_comments=true`, {
headers: headers(),
});

throw new Error(response.statusText);
if (response.ok) {
return await response.json();
}

throw new Error(response.statusText);
}

export async function getPost(id) {
const response = await fetch(`${apiPath}/social/posts/${id}?_reactions=true&_author=true&_comments=true`, { headers: headers() });
if (response.ok) {
return await response.json()
}
const response = await fetch(`${apiPath}/social/posts/${id}?_reactions=true&_author=true&_comments=true`, { headers: headers() });

if (response.ok) {
return await response.json();
}

throw new Error(response.statusText);
}
throw new Error(response.statusText);
}
23 changes: 12 additions & 11 deletions src/js/api/posts/update.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,17 @@ import { apiPath } from "../constants.js";
import { headers } from "../headers.js";

export async function updatePost(id, title, body, media, tags) {
const { name: owner } = profile()
const response = await fetch(`${apiPath}/social/posts/${id}`, {
method: "put",
body: JSON.stringify({ title, body, media, tags, owner }),
headers: headers("application/json")
})
const { name: owner } = profile();

if (response.ok) {
return await response.json()
}
const response = await fetch(`${apiPath}/social/posts/${id}`, {
method: "put",
body: JSON.stringify({ title, body, media, tags, owner }),
headers: headers("application/json"),
});

throw new Error(response.statusText)
}
if (response.ok) {
return await response.json();
}

throw new Error(response.statusText);
}
18 changes: 9 additions & 9 deletions src/js/api/profiles/delete.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ import { apiPath } from "../constants.js";
import { headers } from "../headers.js";

export async function deleteProfile(name) {
const response = await fetch(`${apiPath}/social/profiles/${name}`, {
method: "delete",
headers: headers()
})
const response = await fetch(`${apiPath}/social/profiles/${name}`, {
method: "delete",
headers: headers(),
});

if (response.ok) {
return await response.json()
}
if (response.ok) {
return await response.json();
}

throw new Error(response.statusText)
}
throw new Error(response.statusText);
}
17 changes: 9 additions & 8 deletions src/js/api/profiles/follow.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@ import { apiPath } from "../constants.js";
import { headers } from "../headers.js";

export async function followProfile(name) {
const response = await fetch(`${apiPath}/social/profiles/${name}/follow`, {
headers: headers(),
method: "put"
});
if (response.ok) {
return await response.json()
}
const response = await fetch(`${apiPath}/social/profiles/${name}/follow`, {
headers: headers(),
method: "put",
});

throw new Error(response.statusText);
if (response.ok) {
return await response.json();
}

throw new Error(response.statusText);
}
10 changes: 5 additions & 5 deletions src/js/api/profiles/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export * from "./read.js"
export * from "./update.js"
export * from "./delete.js"
export * from "./follow.js"
export * from "./unfollow.js"
export * from "./read.js";
export * from "./update.js";
export * from "./delete.js";
export * from "./follow.js";
export * from "./unfollow.js";
Loading
Loading