Skip to content
Open
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
78 changes: 78 additions & 0 deletions ArticleService.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
export async function getArticleList(params = {}) {
const url = new URL('https://sprint-mission-api.vercel.app/articles')
Object.keys(params).forEach((key) =>
url.searchParams.append(key, params[key])
);

fetch(url)
.then((response) => {
if (!response.ok) {
return response.text().then(message => {throw new Error(message)});
}
return response.json();
})
.then((data) => {return data;})
.catch((error) => console.log('Error'));
}

export async function getArticle(id) {
fetch(`https://sprint-mission-api.vercel.app/articles/${id}`)
.then((response) => {
if (!response.ok) {
return response.text().then(message => {throw new Error(message)});
}
return response.json();
})
.then((data) => {return data;})
.catch((error) => console.log('Error'));
}

export async function createArticle(requestData) {
fetch('https://sprint-mission-api.vercel.app/articles', {
method: 'POST',
body: JSON.stringify(requestData),
headers: {
'Content-Type': 'application/json',
},
})
.then((response) => {
if (!response.ok) {
return response.text().then(message => {throw new Error(message)});
}
return response.json();
})
.then((data) => {return data;})
.catch((error) => console.log('Error'));
}

export async function patchArticle(id, requestData) {
fetch(`https://sprint-mission-api.vercel.app/articles/${id}`, {
method: 'PATCH',
body: JSON.stringify(requestData),
headers: {
'Content-Type': 'application/json',
},
})
.then((response) => {
if (!response.ok) {
return response.text().then(message => {throw new Error(message)});
}
return response.json();
})
.then((data) => {return data;})
.catch((error) => console.log('Error'));
}

export async function deleteArticle(id) {
fetch(`https://sprint-mission-api.vercel.app/articles/${id}`, {
method: 'DELETE',
})
.then((response) => {
if (!response.ok) {
return response.text().then(message => {throw new Error(message)});
}
return response.json();
})
.then((data) => {return data;})
.catch((error) => console.log('Error'));
}
55 changes: 55 additions & 0 deletions ProductService.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import axios from "axios";

export async function getProductList(params = {}) {
try {
const res = await axios.get(
'https://sprint-mission-api.vercel.app/products',
{ params },
);
return res.data;
} catch (error) {
console.log(error.message);
}
}

export async function getProduct(id) {
try {
const res = await axios.get(`https://sprint-mission-api.vercel.app/products/${id}`);
return res.data;
} catch (error) {
console.log(error.message);
}
}

export async function createProduct(requestData) {
try {
const res = await axios.post(
'https://sprint-mission-api.vercel.app/products',
requestData,
)
return res.data;
} catch (error) {
console.log(error.message);
}
}

export async function patchProduct(id, requestData) {
try {
const res = await axios.patch(
`https://sprint-mission-api.vercel.app/products/${id}`,
requestData,
);
return res.data;
} catch (error) {
console.log(error.message);
}
}

export async function deleteProduct(id) {
try {
const res = await axios.delete(`https://sprint-mission-api.vercel.app/products/${id}`);
return res.data;
} catch (error) {
console.log(error.message);
}
}
36 changes: 36 additions & 0 deletions main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { getArticleList, getArticle, createArticle, patchArticle, deleteArticle } from './ArticleService.js';
import { getProductList, getProduct, createProduct, patchProduct, deleteProduct } from './ProductService.js';

getArticleList({ page: 1, pageSize: 10, keyword: 'test' });

getArticle(81);

const articleData = {
title: 'title',
content: 'content',
image: 'image',
}

createArticle(articleData);

patchArticle(199, articleData);

deleteArticle(170);

getProductList({ page: 1, pageSize: 10, keyword: 'test' });

getProduct(80);

const productData = {
name: 'name',
description: 'description',
price: 1000,
tags: 'tags',
image: 'image',
};

createProduct(productData);

patchProduct(121, productData);

deleteProduct(99);
97 changes: 97 additions & 0 deletions node_modules/.package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 21 additions & 0 deletions node_modules/asynckit/LICENSE

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading