diff --git a/.env b/.env new file mode 100644 index 0000000..a4a7353 --- /dev/null +++ b/.env @@ -0,0 +1,2 @@ +NEXT_PUBLIC_API_BASE_URL=http://localhost:5000 +DOTNET_SERVER_URL=http://localhost:5000 diff --git a/package-lock.json b/package-lock.json index 1e1c817..3ba8df7 100644 --- a/package-lock.json +++ b/package-lock.json @@ -22,6 +22,7 @@ "feed": "^4.2.2", "focus-visible": "^5.2.0", "next": "^13.4.12", + "nextjs": "^0.0.3", "postcss-focus-visible": "^9.0.0", "react": "18.2.0", "react-dom": "18.2.0", @@ -6853,6 +6854,15 @@ "node": "^10 || ^12 || >=14" } }, + "node_modules/nextjs": { + "version": "0.0.3", + "resolved": "https://registry.npmjs.org/nextjs/-/nextjs-0.0.3.tgz", + "integrity": "sha512-mYbDUo4/sRAZ8TqK63PCpYnFiLg7BICG/ot9+guOrUKd4/Fo71ZmEQ41IZbH6nqbQvG7SXTBuofJXAIWfNho0w==", + "license": "MIT", + "engines": { + "node": ">=0.8.21" + } + }, "node_modules/node-releases": { "version": "2.0.13", "resolved": "https://registry.npmjs.org/node-releases/-/node-releases-2.0.13.tgz", diff --git a/package.json b/package.json index a26b950..43dffa7 100644 --- a/package.json +++ b/package.json @@ -24,6 +24,7 @@ "feed": "^4.2.2", "focus-visible": "^5.2.0", "next": "^13.4.12", + "nextjs": "^0.0.3", "postcss-focus-visible": "^9.0.0", "react": "18.2.0", "react-dom": "18.2.0", diff --git a/src/api/axiosConfig.js b/src/api/axiosConfig.js index bd45cb2..3084f46 100644 --- a/src/api/axiosConfig.js +++ b/src/api/axiosConfig.js @@ -1,12 +1,12 @@ import axios from "axios" -import { isHyperlink } from '@/lib/isHyperlink' -const BASE_URL = process.env.DOTNET_SERVER_URL +const BASE_URL = process.env.NEXT_PUBLIC_API_BASE_URL || 'http://localhost:5000' -const AXIOS_BASE = axios.create({ - baseURL: BASE_URL, - }) +const API = axios.create({ + baseURL: BASE_URL, + headers: { + 'Content-Type': 'application/json', + }, +}) -const JSON_CLIENT = isHyperlink(BASE_URL) ? AXIOS_BASE : false - -export default JSON_CLIENT +export default API diff --git a/src/api/postsApi.js b/src/api/postsApi.js index 54f8964..b907035 100644 --- a/src/api/postsApi.js +++ b/src/api/postsApi.js @@ -21,3 +21,20 @@ export const getPost = (postSlug) => { return {} } } + +export const createPost = async ({ title, description }) => { + if (!API) { + throw ['API is not configured correctly.']; + } + + try { + const response = await API.post('/posts/', { title, description }); + return response.data; + } catch (error) { + if (error.response?.data?.errors) { + throw error.response.data.errors; + } + console.error(error); + throw ['An unexpected error occurred.']; + } +}; diff --git a/src/components/ArticleLayout.jsx b/src/components/ArticleLayout.jsx index 43841f7..60b7f7a 100644 --- a/src/components/ArticleLayout.jsx +++ b/src/components/ArticleLayout.jsx @@ -28,7 +28,7 @@ export function ArticleLayout({ children, meta, isRssFeed = false, previousPathn return ( <>
-