Skip to content

Commit

Permalink
refactor: network requests to use unified request function
Browse files Browse the repository at this point in the history
  • Loading branch information
dewanakl committed Feb 26, 2025
1 parent e42d970 commit b4ad3c8
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 6 deletions.
3 changes: 2 additions & 1 deletion js/app/component/comment.js
Original file line number Diff line number Diff line change
Expand Up @@ -611,7 +611,8 @@ export const comment = (() => {
return;
}

fetch(`https://freeipapi.com/api/json/${c.ip}`)
request(HTTP_GET, `https://freeipapi.com/api/json/${c.ip}`)
.default()
.then((res) => res.json())
.then((res) => {
let result = res.cityName + ' - ' + res.regionName;
Expand Down
12 changes: 8 additions & 4 deletions js/app/component/gif.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { util } from '../../common/util.js';
import { storage } from '../../common/storage.js';
import { request, HTTP_GET } from '../../connection/request.js';

export const gif = (() => {

Expand Down Expand Up @@ -42,7 +43,8 @@ export const gif = (() => {
* @param {number} delay
* @returns {Promise<Blob>}
*/
const fetchPut = (c, retries = 3, delay = 1000) => fetch(url)
const fetchPut = (c, retries = 3, delay = 1000) => request(HTTP_GET, url)
.default()
.then((r) => r.blob().then((b) => c.put(url, new Response(b, { headers: r.headers })).then(() => b)))
.catch((err) => {
if (retries <= 0) {
Expand Down Expand Up @@ -174,8 +176,8 @@ export const gif = (() => {
const get = (path, params) => {
params = {
key: conf.get('key'),
media_filter: 'tinygif',
client_key: 'undangan_app',
media_filter: conf.get('media_filter'),
client_key: conf.get('client_key'),
country: conf.get('country'),
locale: conf.get('locale'),
...(params ?? {}),
Expand All @@ -186,7 +188,7 @@ export const gif = (() => {
.map((k) => `${k}=${encodeURIComponent(params[k])}`).join('&');

const url = 'https://tenor.googleapis.com/v2';
return fetch(`${url + path}?${param}`).then((r) => r.json());
return request(HTTP_GET, `${url + path}?${param}`).default().then((r) => r.json());
};

/**
Expand Down Expand Up @@ -410,6 +412,8 @@ export const gif = (() => {
conf.set('key', 'AIzaSyB-Z10TLX7MbkcMT5S_YA1iEqCmGzutV7s');
conf.set('country', 'ID');
conf.set('locale', 'id_ID');
conf.set('media_filter', 'tinygif');
conf.set('client_key', 'undangan_app');
};

return {
Expand Down
4 changes: 3 additions & 1 deletion js/app/guest/image.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { progress } from './progress.js';
import { util } from '../../common/util.js';
import { request, HTTP_GET } from '../../connection/request.js';

export const image = (() => {

Expand Down Expand Up @@ -73,7 +74,8 @@ export const image = (() => {
* @param {number} delay
* @returns {Promise<Blob>}
*/
const fetchPut = (c, retries = 3, delay = 1000) => fetch(url)
const fetchPut = (c, retries = 3, delay = 1000) => request(HTTP_GET, url)
.default()
.then((r) => r.blob())
.then((b) => window.createImageBitmap(b))
.then((i) => toWebp(i))
Expand Down
8 changes: 8 additions & 0 deletions js/connection/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,14 @@ export const request = (method, path) => {
throw new Error(err);
});
},
/**
* @param {object|null} header
* @returns {Promise<Response>}
*/
default(header = null) {
req.headers = new Headers(header ?? {});
return fetch(path, req);
},
/**
* @param {string} token
* @returns {ReturnType<typeof request>}
Expand Down

0 comments on commit b4ad3c8

Please sign in to comment.