4 |
5 |
6 |
7 |
8 | ## What’s In This Document
9 |
10 | - [About This Tool](#-about-this-tool)
11 | - [How to Setup Locally](#-how-to-setup-locally)
12 | - [How to Contribute](#-how-to-contribute)
13 | - [License](#license)
14 |
15 |
16 |
17 | ## 📖 About This Tool
18 |
19 | I was re-designing my portfolio website and my main task was this where I'll host my basic data from where I can manage the content easily and in a simple way. But I couldn't find are no solution. So, I get the idea about GitHub API and using GitHub APIs I build this tool. So, my website(https://mohddanish.me) content is managed by this tool. So, I published this on ProductHunt(https://www.producthunt.com/posts/apis-with-github) and when people ask me that it's open-source? And then I say No. So, I quickly make this tool open Source. Because I love when people want to contribute to your projects. check this thread (https://twitter.com/mddanishyusuf/status/1124261537537974278)
20 |
21 |
22 |
23 |
24 | ## ✍ How to Setup Locally
25 |
26 | This tool is build with React(CRA), GitHub APIs, JSON Editor(https://github.com/josdejong/jsoneditor) and Bootstrap for Grid System. And Hosted on Netlify.
27 |
28 | ## Steps
29 |
30 | 1. `git clone https://github.com/mddanishyusuf/json-apis-with-github`
31 | 2. `cd json-apis-with-github`
32 | 3. `yarn install`
33 | 4. `npm run start`
34 |
35 |
36 |
37 | ### Tool Build With
38 |
39 | - **ReactJS**- Create React APP(https://github.com/facebook/create-react-app)
40 | - **GitHub APIs:** https://developer.github.com/v3/
41 | - **Netlify:** To host the tool.
42 |
43 |
44 |
45 | ## License
46 |
47 | [](https://creativecommons.org/publicdomain/zero/1.0/)
48 |
--------------------------------------------------------------------------------
/src/assets/images/cofee.svg:
--------------------------------------------------------------------------------
1 |
--------------------------------------------------------------------------------
/src/components/JSONEditorReact.js:
--------------------------------------------------------------------------------
1 | import React, { Component } from 'react';
2 | import isEqual from 'lodash/isEqual';
3 | import cloneDeep from 'lodash/cloneDeep';
4 |
5 | import JSONEditor from 'jsoneditor';
6 | import 'jsoneditor/dist/jsoneditor.css';
7 |
8 | import './JSONEditorReact.css';
9 |
10 | export default class JSONEditorReact extends Component {
11 | componentDidMount() {
12 | // copy all properties into options for the editor
13 | // (except the properties for the JSONEditorReact component itself)
14 | const options = Object.assign({}, this.props);
15 | delete options.json;
16 | delete options.text;
17 |
18 | this.jsoneditor = new JSONEditor(this.container, options);
19 |
20 | if ('json' in this.props) {
21 | this.jsoneditor.set(this.props.json);
22 | }
23 | if ('text' in this.props) {
24 | this.jsoneditor.setText(this.props.text);
25 | }
26 | this.schema = cloneDeep(this.props.schema);
27 | this.schemaRefs = cloneDeep(this.props.schemaRefs);
28 | }
29 |
30 | componentWillUpdate(nextProps, nextState) {
31 | if ('json' in nextProps) {
32 | this.jsoneditor.update(nextProps.json);
33 | }
34 |
35 | if ('text' in nextProps) {
36 | this.jsoneditor.updateText(nextProps.text);
37 | }
38 |
39 | if ('mode' in nextProps) {
40 | this.jsoneditor.setMode(nextProps.mode);
41 | }
42 |
43 | // store a clone of the schema to keep track on when it actually changes.
44 | // (When using a PureComponent all of this would be redundant)
45 | const schemaChanged = !isEqual(nextProps.schema, this.schema);
46 | const schemaRefsChanged = !isEqual(nextProps.schemaRefs, this.schemaRefs);
47 | if (schemaChanged || schemaRefsChanged) {
48 | this.schema = cloneDeep(nextProps.schema);
49 | this.schemaRefs = cloneDeep(nextProps.schemaRefs);
50 | this.jsoneditor.setSchema(nextProps.schema, nextProps.schemaRefs);
51 | }
52 | }
53 |
54 | componentWillUnmount() {
55 | if (this.jsoneditor) {
56 | this.jsoneditor.destroy();
57 | }
58 | }
59 |
60 | render() {
61 | return
(this.container = elem)} />;
62 | }
63 | }
64 |
--------------------------------------------------------------------------------
/src/config/api.js:
--------------------------------------------------------------------------------
1 | import axios from 'axios';
2 |
3 | export function sortContentArray(array) {
4 | if (array.length > 0) {
5 | array.sort(function(x, y) {
6 | // true values first
7 | return x.type === y.type ? 0 : x.type === 'dir' ? -1 : 1;
8 | // false values first
9 | // return (x === y)? 0 : x? 1 : -1;
10 | });
11 | }
12 | return array;
13 | }
14 |
15 | export function checkFileType(content) {
16 | const fileNameIntoArray = content.name.split('.');
17 | const fileExt = fileNameIntoArray[1] === 'json' || fileNameIntoArray[1] === undefined;
18 | return fileExt;
19 | }
20 |
21 | export async function getData(location) {
22 | const param = location.pathname.split('/editor/')[1];
23 | const accessToken = localStorage.getItem('token');
24 | try {
25 | const response = await axios.get(`https://api.github.com/repos/${param}/contents?access_token=${accessToken}`, {
26 | headers: { 'If-None-Match': '' },
27 | });
28 | return response;
29 | } catch (error) {
30 | return error.response;
31 | }
32 | }
33 |
34 | export async function UpdateFile(path, param, location) {
35 | const accessToken = localStorage.getItem('token');
36 | const pathName = location.pathname.split('/editor/')[1];
37 | try {
38 | const response = await axios.put(
39 | `https://api.github.com/repos/${pathName}/contents/${path}?access_token=${accessToken}`,
40 | param
41 | );
42 | return response;
43 | } catch (error) {
44 | return error;
45 | }
46 | }
47 |
48 | export async function deleteFile(param, location, path) {
49 | const accessToken = localStorage.getItem('token');
50 | const pathName = location.pathname.split('/editor/')[1];
51 | try {
52 | const response = await axios.delete(
53 | `https://api.github.com/repos/${pathName}/contents/${path}?access_token=${accessToken}`,
54 | { data: param }
55 | );
56 | return response;
57 | } catch (error) {
58 | return error;
59 | }
60 | }
61 |
62 | export async function CrateANewFile(path, param, location) {
63 | const accessToken = localStorage.getItem('token');
64 | const pathName = location.pathname.split('/editor/')[1];
65 | try {
66 | const response = await axios.put(
67 | `https://api.github.com/repos/${pathName}/contents/${path}?access_token=${accessToken}`,
68 | param
69 | );
70 | return response;
71 | } catch (error) {
72 | return error;
73 | }
74 | }
75 |
--------------------------------------------------------------------------------
/src/logo.svg:
--------------------------------------------------------------------------------
1 |
8 |
--------------------------------------------------------------------------------
/public/index.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
33 |
34 |
43 | API with GitHub | Small and Tiny APIs with GitHub for quick use
44 |
45 |
46 |
47 |
54 |
55 |
56 |
57 |
58 |
59 |
60 |
70 |
71 |
72 |
--------------------------------------------------------------------------------
/src/serviceWorker.js:
--------------------------------------------------------------------------------
1 | // This optional code is used to register a service worker.
2 | // register() is not called by default.
3 |
4 | // This lets the app load faster on subsequent visits in production, and gives
5 | // it offline capabilities. However, it also means that developers (and users)
6 | // will only see deployed updates on subsequent visits to a page, after all the
7 | // existing tabs open on the page have been closed, since previously cached
8 | // resources are updated in the background.
9 |
10 | // To learn more about the benefits of this model and instructions on how to
11 | // opt-in, read https://bit.ly/CRA-PWA
12 |
13 | const isLocalhost = Boolean(
14 | window.location.hostname === 'localhost' ||
15 | // [::1] is the IPv6 localhost address.
16 | window.location.hostname === '[::1]' ||
17 | // 127.0.0.1/8 is considered localhost for IPv4.
18 | window.location.hostname.match(
19 | /^127(?:\.(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)){3}$/
20 | )
21 | );
22 |
23 | export function register(config) {
24 | if (process.env.NODE_ENV === 'production' && 'serviceWorker' in navigator) {
25 | // The URL constructor is available in all browsers that support SW.
26 | const publicUrl = new URL(process.env.PUBLIC_URL, window.location.href);
27 | if (publicUrl.origin !== window.location.origin) {
28 | // Our service worker won't work if PUBLIC_URL is on a different origin
29 | // from what our page is served on. This might happen if a CDN is used to
30 | // serve assets; see https://github.com/facebook/create-react-app/issues/2374
31 | return;
32 | }
33 |
34 | window.addEventListener('load', () => {
35 | const swUrl = `${process.env.PUBLIC_URL}/service-worker.js`;
36 |
37 | if (isLocalhost) {
38 | // This is running on localhost. Let's check if a service worker still exists or not.
39 | checkValidServiceWorker(swUrl, config);
40 |
41 | // Add some additional logging to localhost, pointing developers to the
42 | // service worker/PWA documentation.
43 | navigator.serviceWorker.ready.then(() => {
44 | console.log(
45 | 'This web app is being served cache-first by a service ' +
46 | 'worker. To learn more, visit https://bit.ly/CRA-PWA'
47 | );
48 | });
49 | } else {
50 | // Is not localhost. Just register service worker
51 | registerValidSW(swUrl, config);
52 | }
53 | });
54 | }
55 | }
56 |
57 | function registerValidSW(swUrl, config) {
58 | navigator.serviceWorker
59 | .register(swUrl)
60 | .then(registration => {
61 | registration.onupdatefound = () => {
62 | const installingWorker = registration.installing;
63 | if (installingWorker == null) {
64 | return;
65 | }
66 | installingWorker.onstatechange = () => {
67 | if (installingWorker.state === 'installed') {
68 | if (navigator.serviceWorker.controller) {
69 | // At this point, the updated precached content has been fetched,
70 | // but the previous service worker will still serve the older
71 | // content until all client tabs are closed.
72 | console.log(
73 | 'New content is available and will be used when all ' +
74 | 'tabs for this page are closed. See https://bit.ly/CRA-PWA.'
75 | );
76 |
77 | // Execute callback
78 | if (config && config.onUpdate) {
79 | config.onUpdate(registration);
80 | }
81 | } else {
82 | // At this point, everything has been precached.
83 | // It's the perfect time to display a
84 | // "Content is cached for offline use." message.
85 | console.log('Content is cached for offline use.');
86 |
87 | // Execute callback
88 | if (config && config.onSuccess) {
89 | config.onSuccess(registration);
90 | }
91 | }
92 | }
93 | };
94 | };
95 | })
96 | .catch(error => {
97 | console.error('Error during service worker registration:', error);
98 | });
99 | }
100 |
101 | function checkValidServiceWorker(swUrl, config) {
102 | // Check if the service worker can be found. If it can't reload the page.
103 | fetch(swUrl)
104 | .then(response => {
105 | // Ensure service worker exists, and that we really are getting a JS file.
106 | const contentType = response.headers.get('content-type');
107 | if (
108 | response.status === 404 ||
109 | (contentType != null && contentType.indexOf('javascript') === -1)
110 | ) {
111 | // No service worker found. Probably a different app. Reload the page.
112 | navigator.serviceWorker.ready.then(registration => {
113 | registration.unregister().then(() => {
114 | window.location.reload();
115 | });
116 | });
117 | } else {
118 | // Service worker found. Proceed as normal.
119 | registerValidSW(swUrl, config);
120 | }
121 | })
122 | .catch(() => {
123 | console.log(
124 | 'No internet connection found. App is running in offline mode.'
125 | );
126 | });
127 | }
128 |
129 | export function unregister() {
130 | if ('serviceWorker' in navigator) {
131 | navigator.serviceWorker.ready.then(registration => {
132 | registration.unregister();
133 | });
134 | }
135 | }
136 |
--------------------------------------------------------------------------------
/src/components/LandingPage.js:
--------------------------------------------------------------------------------
1 | import React from 'react';
2 | import { Player, ControlBar } from 'video-react';
3 | import { auth, firebase } from '../config/firebase';
4 |
5 | function LandingPage({ history }) {
6 | const loginWithGitHub = () => {
7 | const providerOAuth = new auth.GithubOAuth();
8 |
9 | firebase
10 | .auth()
11 | .signInWithPopup(providerOAuth)
12 | .then(result => {
13 | localStorage.setItem('token', result.credential.accessToken);
14 | history.push('/database');
15 | })
16 | .catch(err => console.error(err));
17 | };
18 |
19 | return (
20 |
21 |
22 |
23 |
24 |
25 |
API with GitHub
26 |
27 | Now build simple API quickly with JSON and store on GitHub Repository. Seems cool?
28 |
29 |
32 |
33 |
34 |
35 |
41 |
42 |
43 |
44 |
45 |
46 |
47 |
48 |
49 |
50 |
51 |
How it works
52 |
53 |
54 |
1. Setup GitHub APP
55 |
56 | Install Our GitHub App on your GitHub Account and give the access to new repository you
57 | make to build APIs. We are not storing any data on our server.
58 |
59 |
60 |
61 |
65 |
66 |
67 |
68 |
69 |
70 |
74 |
75 |
76 |
77 |
2. Create Repository
78 |
79 | Make new public GitHub repository with README file. So, we using repository as a JSON
80 | files Storage.
81 |
82 |
83 |
84 |
85 |
86 |
3. Make API & Save
87 |
88 | Make new JSON files with our editor and save. So, when you save the file the changes
89 | will reflect on your GitHub repository.
90 |
125 | There is only one possibility that we don;t have access to that Repository. So, No problem
126 | Below is the GIF image is showing how to give access.
127 |