Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ft: Add Fetch API #173

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ So far, it includes the following examples:
1. 🌐 URL API
1. 🗒️ Selection API
1. 📃 Page Visibility API
1. 〽️ Fetch API

# 🤝 Open Source

Expand Down
2 changes: 1 addition & 1 deletion public/sw.js

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

19 changes: 19 additions & 0 deletions src/modules/apis/data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -438,5 +438,24 @@ export const data: Array<Demo> = [
'https://developer.mozilla.org/en-US/docs/Web/API/EyeDropper_API',
canIUseURL: 'https://caniuse.com/mdn-api_eyedropper',
},
},
{
id: 'fetch-api',
emoji: '〽️',
title: 'Fetch API',
description:
'The Fetch API provides an interface for fetching resources (including across the network). It will seem familiar to anyone who has used XMLHttpRequest, but the new API provides a more powerful and flexible feature set. Used JSONPlaceholder a free online REST API that you can use whenever you need some fake data for testing and prototyping.',
meta: {
author: {
name: 'Shyaka Tresor',
social: {
email: '[email protected]',
github: 'shyakadev',
twitter: 'tshyaka',
},
},
apiDocURL: 'https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API',
canIUseURL: 'https://caniuse.com/fetch',
},
}, //replace item here
];
62 changes: 62 additions & 0 deletions src/modules/apis/fetch-api/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
export const hasSupport = (): boolean =>
Boolean('connection' in window.navigator);

async function get(url: string) {
const requestOptions = {
method: 'GET',
};
const response = await fetch(url, requestOptions);
return handleResponse(response);
}

async function post(url: string, body: any) {
// eslint-disable-next-line no-useless-catch
try {
const requestOptions = {
method: 'POST',
headers: { 'Content-Type': 'application/json; charset=UTF-8' },
body,
};

const response = await fetch(url, requestOptions);
return response.json();
} catch (error) {
throw error;
}
}

async function put(url: string, body: any) {
const requestOptions = {
method: 'PUT',
headers: { 'Content-Type': 'application/json' },
body: body,
};

const response = await fetch(url, requestOptions);
return response.json();
}

// prefixed with underscore because delete is a reserved word in javascript
async function _delete(url: string) {
const requestOptions = {
method: 'DELETE',
};
const response = await fetch(url, requestOptions);
return response.json();
}
const run = {
get,
post,
put,
_delete,
};

function handleResponse(response: any) {
try {
return response.json();
} catch (error: any) {
error.message;
}
}

export default run;
Loading