Skip to content

Commit

Permalink
refactor: add fetchJson function
Browse files Browse the repository at this point in the history
  • Loading branch information
davidyuk committed Jul 7, 2021
1 parent 33a9d8d commit 749858e
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 7 deletions.
9 changes: 7 additions & 2 deletions src/components/layout/sendTip/GiphySearch.vue
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
</template>

<script>
import { fetchJson } from '../../../utils';
import Loading from '../../Loading.vue';
import SearchInput from '../SearchInput.vue';
Expand Down Expand Up @@ -68,8 +69,12 @@ export default {
},
async search() {
try {
const results = await fetch(`https://api.giphy.com/v1/gifs/${this.query ? `search?q=${this.query}&` : 'trending?'}api_key=${process.env.VUE_APP_GIPHY_API_KEY}&limit=10&offset=${this.offset}`);
const { data, pagination } = await results.json();
const url = new URL(`https://api.giphy.com/v1/gifs/${this.query ? 'search' : 'trending'}`);
if (this.query) url.searchParams.set('q', this.query);
url.searchParams.set('limit', 10);
url.searchParams.set('offset', this.offset);
url.searchParams.set('api_key', process.env.VUE_APP_GIPHY_API_KEY);
const { data, pagination } = await fetchJson(url);
this.resultsCount = `${pagination.total_count.toLocaleString()} Results`;
this.results.push(...data.map(({ images }) => ({
still: images.fixed_width_still.url,
Expand Down
4 changes: 2 additions & 2 deletions src/components/layout/sendTip/SendPost.vue
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@

<script>
import { mapState } from 'vuex';
import { fetchJson } from '../../../utils';
import { EventBus } from '../../../utils/eventBus';
import Backend from '../../../utils/backend';
import MessageInput from '../../MessageInput.vue';
Expand Down Expand Up @@ -149,14 +150,13 @@ export default {
const formData = new FormData();
formData.append('image', event.target.files[0]);
try {
const result = await fetch('https://api.imgur.com/3/image.json', {
const { data } = await fetchJson('https://api.imgur.com/3/image.json', {
method: 'post',
body: formData,
headers: {
Authorization: `Client-ID ${process.env.VUE_APP_IMGUR_API_CLIENT_ID}`,
},
});
const { data } = await result.json();
this.media.push({ link: data.link, deletehash: data.deletehash });
} finally { this.uploadingMedia = false; }
},
Expand Down
5 changes: 2 additions & 3 deletions src/store/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import modals from './plugins/modals';
import backend from './modules/backend';
import aeternity from './modules/aeternity';
import Backend from '../utils/backend';
import { handleUnknownError } from '../utils';
import { handleUnknownError, fetchJson } from '../utils';

Vue.use(Vuex);

Expand Down Expand Up @@ -75,8 +75,7 @@ export default new Vuex.Store({
},
async initMiddleware({ commit }) {
const specUrl = `${process.env.VUE_APP_MIDDLEWARE_URL}/swagger/swagger.json`;
const res = await fetch(specUrl);
const spec = await res.json();
const spec = await fetchJson(specUrl);

spec.paths = {
...spec.paths,
Expand Down
6 changes: 6 additions & 0 deletions src/utils/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,3 +64,9 @@ export const blockToDate = (goalBlock, height) => {
const diff = goalBlock - height;
return new Date(diff * 180000 + Date.now());
};

export const fetchJson = async (...args) => {
const response = await fetch(...args);
if (!response.ok) throw new Error(`Request failed with ${response.status}`);
return response.json();
};

0 comments on commit 749858e

Please sign in to comment.