|
| 1 | +export class BlogApi { |
| 2 | + constructor() { |
| 3 | + this.baseUrl = "https://v2.api.noroff.dev"; |
| 4 | + this.blogName = this.getBlogName(); |
| 5 | + this.blogUrl = `${this.baseUrl}/blog/posts/${this.blogName}`; |
| 6 | + this.authUrl = `${this.baseUrl}/auth`; |
| 7 | + } |
| 8 | + |
| 9 | + /** |
| 10 | + * Helper method for performing fetch requests. |
| 11 | + * @param {string} url - The endpoint URL. |
| 12 | + * @param {object} options - Fetch options. |
| 13 | + * @param {string} errorMessage - Error message for failures. |
| 14 | + * @returns {Promise<any>} Parsed JSON response. |
| 15 | + */ |
| 16 | + async _request(url, options, errorMessage) { |
| 17 | + try { |
| 18 | + const response = await fetch(url, options); |
| 19 | + if (!response.ok) { |
| 20 | + throw new Error(`${errorMessage}. Status: ${response.status}`); |
| 21 | + } |
| 22 | + return await response.json(); |
| 23 | + } catch (error) { |
| 24 | + console.error(errorMessage, error); |
| 25 | + throw error; |
| 26 | + } |
| 27 | + } |
| 28 | + |
| 29 | + /** |
| 30 | + * Retrieves the blog name from local storage. |
| 31 | + * @returns {string} The blog name. |
| 32 | + */ |
| 33 | + getBlogName() { |
| 34 | + const loggedInUserName = localStorage.getItem("name"); |
| 35 | + return loggedInUserName ? loggedInUserName : "ailin_user"; |
| 36 | + } |
| 37 | + |
| 38 | + /** |
| 39 | + * Fetches a single blog post by ID. |
| 40 | + * @param {string} postId - The ID of the blog post. |
| 41 | + * @returns {Promise<any>} The blog post data. |
| 42 | + */ |
| 43 | + async getBlogpostByID(postId) { |
| 44 | + const url = `${this.blogUrl}/${postId}`; |
| 45 | + const { data } = await this._request(url, {}, "Error fetching blogpost"); |
| 46 | + return data; |
| 47 | + } |
| 48 | + |
| 49 | + /** |
| 50 | + * Fetches all blog posts. |
| 51 | + * @returns {Promise<any>} An array of blog posts. |
| 52 | + */ |
| 53 | + async getBlogposts() { |
| 54 | + const { data } = await this._request( |
| 55 | + this.blogUrl, |
| 56 | + {}, |
| 57 | + "Error fetching blogposts" |
| 58 | + ); |
| 59 | + return data; |
| 60 | + } |
| 61 | + |
| 62 | + /** |
| 63 | + * Creates a new blog post. |
| 64 | + * @param {string} title - The title of the blog post. |
| 65 | + * @param {string} content - The body content. |
| 66 | + * @param {string} imageUrl - The URL of the media. |
| 67 | + * @param {string} [imageAlt="Default image description"] - The media description. |
| 68 | + * @returns {Promise<any>} The created blog post data. |
| 69 | + */ |
| 70 | + async createBlogpost( |
| 71 | + title, |
| 72 | + content, |
| 73 | + imageUrl, |
| 74 | + imageAlt = "Default image description" |
| 75 | + ) { |
| 76 | + const accessToken = localStorage.getItem("accessToken"); |
| 77 | + if (!accessToken) { |
| 78 | + // Navigation to login page can be handled here if needed. |
| 79 | + return; |
| 80 | + } |
| 81 | + const data = { |
| 82 | + title, |
| 83 | + body: content, |
| 84 | + media: { |
| 85 | + url: imageUrl, |
| 86 | + alt: imageAlt, |
| 87 | + }, |
| 88 | + }; |
| 89 | + |
| 90 | + const options = { |
| 91 | + method: "POST", |
| 92 | + headers: { |
| 93 | + "Content-Type": "application/json", |
| 94 | + Authorization: `Bearer ${accessToken}`, |
| 95 | + }, |
| 96 | + body: JSON.stringify(data), |
| 97 | + }; |
| 98 | + |
| 99 | + return await this._request( |
| 100 | + this.blogUrl, |
| 101 | + options, |
| 102 | + "Error creating blog post" |
| 103 | + ); |
| 104 | + } |
| 105 | + |
| 106 | + /** |
| 107 | + * Updates a blog post by ID. |
| 108 | + * @param {string} postId - The ID of the blog post. |
| 109 | + * @param {string} title - The title of the blog post. |
| 110 | + * @param {string} content - The body content. |
| 111 | + * @param {string} imageUrl - The URL of the media. |
| 112 | + * @param {string} [imageAlt="Default image description"] - The media description. |
| 113 | + * @returns {Promise<any>} The updated blog post data. |
| 114 | + */ |
| 115 | + async updateBlogpost( |
| 116 | + postId, |
| 117 | + title, |
| 118 | + content, |
| 119 | + imageUrl, |
| 120 | + imageAlt = "Default image description" |
| 121 | + ) { |
| 122 | + const accessToken = localStorage.getItem("accessToken"); |
| 123 | + if (!accessToken) { |
| 124 | + return; |
| 125 | + } |
| 126 | + const data = { |
| 127 | + title, |
| 128 | + body: content, |
| 129 | + media: { |
| 130 | + url: imageUrl, |
| 131 | + alt: imageAlt, |
| 132 | + }, |
| 133 | + }; |
| 134 | + |
| 135 | + const options = { |
| 136 | + method: "PUT", |
| 137 | + headers: { |
| 138 | + "Content-Type": "application/json", |
| 139 | + Authorization: `Bearer ${accessToken}`, |
| 140 | + }, |
| 141 | + body: JSON.stringify(data), |
| 142 | + }; |
| 143 | + |
| 144 | + return await this._request( |
| 145 | + `${this.blogUrl}/${postId}`, |
| 146 | + options, |
| 147 | + "Error updating blog post" |
| 148 | + ); |
| 149 | + } |
| 150 | + |
| 151 | + /** |
| 152 | + * Deletes a blog post by ID. |
| 153 | + * @param {string} postId - The ID of the blog post. |
| 154 | + * @returns {Promise<void>} |
| 155 | + */ |
| 156 | + async deleteBlogpost(postId) { |
| 157 | + const accessToken = localStorage.getItem("accessToken"); |
| 158 | + if (!accessToken) { |
| 159 | + return; |
| 160 | + } |
| 161 | + const options = { |
| 162 | + method: "DELETE", |
| 163 | + headers: { |
| 164 | + Authorization: `Bearer ${accessToken}`, |
| 165 | + }, |
| 166 | + }; |
| 167 | + |
| 168 | + await this._request( |
| 169 | + `${this.blogUrl}/${postId}`, |
| 170 | + options, |
| 171 | + "Error deleting blog post" |
| 172 | + ); |
| 173 | + console.log("Blog post deleted:", postId); |
| 174 | + } |
| 175 | + |
| 176 | + /** |
| 177 | + * Logs in a user. |
| 178 | + * @param {string} email - The user's email. |
| 179 | + * @param {string} password - The user's password. |
| 180 | + * @returns {Promise<any>} User data. |
| 181 | + */ |
| 182 | + async login(email, password) { |
| 183 | + const body = { email, password }; |
| 184 | + const options = { |
| 185 | + method: "POST", |
| 186 | + headers: { |
| 187 | + "Content-Type": "application/json", |
| 188 | + accept: "application/json", |
| 189 | + }, |
| 190 | + body: JSON.stringify(body), |
| 191 | + }; |
| 192 | + |
| 193 | + const { data } = await this._request( |
| 194 | + `${this.authUrl}/login`, |
| 195 | + options, |
| 196 | + "Login failed" |
| 197 | + ); |
| 198 | + return data; |
| 199 | + } |
| 200 | + |
| 201 | + /** |
| 202 | + * Registers a new user. |
| 203 | + * @param {string} name - The user's name. |
| 204 | + * @param {string} email - The user's email. |
| 205 | + * @param {string} password - The user's password. |
| 206 | + * @returns {Promise<any>} Registration data. |
| 207 | + */ |
| 208 | + async register(name, email, password) { |
| 209 | + const body = { name, email, password }; |
| 210 | + const options = { |
| 211 | + method: "POST", |
| 212 | + headers: { |
| 213 | + "Content-Type": "application/json", |
| 214 | + accept: "application/json", |
| 215 | + }, |
| 216 | + body: JSON.stringify(body), |
| 217 | + }; |
| 218 | + |
| 219 | + const { data } = await this._request( |
| 220 | + `${this.authUrl}/register`, |
| 221 | + options, |
| 222 | + "Registration failed" |
| 223 | + ); |
| 224 | + return data; |
| 225 | + } |
| 226 | +} |
0 commit comments