소스 검색

Use GET instead of POST

Pouria Ezzati 7 년 전
부모
커밋
a1d9796c7f
5개의 변경된 파일17개의 추가작업 그리고 12개의 파일을 삭제
  1. 3 3
      README.md
  2. 7 2
      client/actions/index.js
  3. 1 1
      client/components/Stats/Stats.js
  4. 3 3
      server/controllers/urlController.js
  5. 3 3
      server/server.js

+ 3 - 3
README.md

@@ -76,7 +76,7 @@ Include the API key as `X-API-Key` in the header of all below requests. Availabl
 
 **Get shortened URLs list:**
 ```
-POST /api/url/geturls
+GET /api/url/geturls
 ```
 
 Returns:
@@ -99,9 +99,9 @@ Returns: URL object
 **Delete a shortened URL** and **Get stats for a shortened URL:**
 ```
 POST /api/url/deleteurl
-POST /api/url/stats
+GET /api/url/stats
 ```
-Body
+Body (or query for GET request)
   * `id`: ID of the shortened URL.
   * `domain` (optional):  Required if a custom domain is used for short URL.
 

+ 7 - 2
client/actions/index.js

@@ -22,8 +22,13 @@ export const createShortUrl = params => dispatch =>
 export const getUrlsList = params => (dispatch, getState) => {
   if (params) dispatch(updateUrlList(params));
   dispatch(showTableLoading());
+  const { url } = getState();
+  const query = Object.keys(url).reduce(
+    (string, item) => (typeof url[item] !== 'object' ? `${string + item}=${url[item]}&` : string),
+    '?'
+  );
   return axios
-    .post('/api/url/geturls', getState().url, { headers: { Authorization: cookie.get('token') } })
+    .get(`/api/url/geturls${query}`, { headers: { Authorization: cookie.get('token') } })
     .then(({ data }) => dispatch(listUrls(data)));
 };
 
@@ -49,7 +54,7 @@ export const showDomainInput = () => ({ type: types.SHOW_DOMAIN_INPUT });
 
 export const getUserSettings = () => dispatch =>
   axios
-    .post('/api/auth/usersettings', null, { headers: { Authorization: cookie.get('token') } })
+    .get('/api/auth/usersettings', { headers: { Authorization: cookie.get('token') } })
     .then(({ data }) => {
       dispatch(setDomain(data.customDomain));
       dispatch(setApiKey(data.apikey));

+ 1 - 1
client/components/Stats/Stats.js

@@ -88,7 +88,7 @@ class Stats extends Component {
     const { id } = this.props;
     if (!id) return null;
     return axios
-      .post('/api/url/stats', { id }, { headers: { Authorization: cookie.get('token') } })
+      .get(`/api/url/stats?id=${id}`, { headers: { Authorization: cookie.get('token') } })
       .then(({ data }) =>
         this.setState({
           stats: data,

+ 3 - 3
server/controllers/urlController.js

@@ -144,8 +144,8 @@ exports.goToUrl = async (req, res, next) => {
   return res.redirect(url.target);
 };
 
-exports.getUrls = async ({ body, user }, res) => {
-  const urlsList = await getUrls({ options: body, user });
+exports.getUrls = async ({ query, user }, res) => {
+  const urlsList = await getUrls({ options: query, user });
   return res.json(urlsList);
 };
 
@@ -185,7 +185,7 @@ exports.deleteUrl = async ({ body: { id, domain }, user }, res) => {
   return res.status(400).json({ error: "Couldn't delete short URL." });
 };
 
-exports.getStats = async ({ body: { id, domain }, user }, res) => {
+exports.getStats = async ({ query: { id, domain }, user }, res) => {
   if (!id) return res.status(400).json({ error: 'No id has been provided.' });
   const customDomain = domain !== config.DEFAULT_DOMAIN && domain;
   const stats = await getStats({ id, domain: customDomain, user });

+ 3 - 3
server/server.js

@@ -101,7 +101,7 @@ app.prepare().then(() => {
   server.post('/api/auth/changepassword', auth.authJwt, catchErrors(auth.changePassword));
   server.post('/api/auth/generateapikey', auth.authJwt, catchErrors(auth.generateApiKey));
   server.post('/api/auth/resetpassword', catchErrors(auth.requestPasswordReset));
-  server.post('/api/auth/usersettings', auth.authJwt, auth.userSettings);
+  server.get('/api/auth/usersettings', auth.authJwt, auth.userSettings);
 
   /* URL shortener */
   server.post(
@@ -112,10 +112,10 @@ app.prepare().then(() => {
     catchErrors(url.urlShortener)
   );
   server.post('/api/url/deleteurl', auth.authApikey, auth.authJwt, catchErrors(url.deleteUrl));
-  server.post('/api/url/geturls', auth.authApikey, auth.authJwt, catchErrors(url.getUrls));
+  server.get('/api/url/geturls', auth.authApikey, auth.authJwt, catchErrors(url.getUrls));
   server.post('/api/url/customdomain', auth.authJwt, catchErrors(url.setCustomDomain));
   server.delete('/api/url/customdomain', auth.authJwt, catchErrors(url.deleteCustomDomain));
-  server.post('/api/url/stats', auth.authApikey, auth.authJwt, catchErrors(url.getStats));
+  server.get('/api/url/stats', auth.authApikey, auth.authJwt, catchErrors(url.getStats));
   server.post('/api/url/requesturl', catchErrors(url.goToUrl));
   server.get('/:id', catchErrors(url.goToUrl), (req, res) =>
     app.render(req, res, '/url-password', req.protectedUrl)