create gistlib in javascript

To create a Gist library in JavaScript, you can make use of the GitHub API to create and manipulate gists programmatically.

Here's an example implementation using Node.js:

index.tsx
const request = require('request');
const fs = require('fs');

const createGist = (filename, content) => {
  const options = {
    url: 'https://api.github.com/gists',
    headers: {
      'User-Agent': 'gistlib',
      'Authorization': `token <your_personal_access_token>`
    },
    json: true,
    body: {
      files: {
        [filename]: { content }
      }
    }
  };

  return new Promise((resolve, reject) => {
    request.post(options, (err, response, body) => {
      if (err) return reject(err);
      const { html_url } = body;
      return resolve(html_url);
    });
  });
};

const updateGist = (gistId, filename, content) => {
  const options = {
    url: `https://api.github.com/gists/${gistId}`,
    headers: {
      'User-Agent': 'gistlib',
      'Authorization': `token <your_personal_access_token>`
    },
    json: true,
    body: {
      files: {
        [filename]: { content }
      }
    }
  };

  return new Promise((resolve, reject) => {
    request.patch(options, (err, response, body) => {
      if (err) return reject(err);
      const { html_url } = body;
      return resolve(html_url);
    });
  });
};

const deleteGist = (gistId) => {
  const options = {
    url: `https://api.github.com/gists/${gistId}`,
    headers: {
      'User-Agent': 'gistlib',
      'Authorization': `token <your_personal_access_token>`
    }
  };

  return new Promise((resolve, reject) => {
    request.delete(options, (err, response, body) => {
      if (err) return reject(err);
      return resolve(response.statusCode === 204); // Returns true if successful
    });
  });
};

const downloadGistFile = (gistUrl, filename) => {
  const options = {
    url: `${gistUrl}/raw/${filename}`,
    headers: {
      'User-Agent': 'gistlib'
    }
  };

  return new Promise((resolve, reject) => {
    request.get(options, (err, response, body) => {
      if (err) return reject(err);
      fs.writeFile(filename, body, (err) => {
        if (err) return reject(err);
        return resolve(true);
      });
    });
  });
};

module.exports = {
  createGist,
  updateGist,
  deleteGist,
  downloadGistFile
};
2114 chars
94 lines

The above code exposes four functions:

  • createGist(filename, content) - This function creates a new Gist with the specified filename and content.
  • updateGist(gistId, filename, content) - This function updates an existing Gist with the specified Gist ID, filename, and content.
  • deleteGist(gistId) - This function deletes an existing Gist with the specified Gist ID.
  • downloadGistFile(gistUrl, filename) - This function downloads a file from the specified Gist URL and saves it with the specified filename.

Each of these functions uses the request module to make requests to the GitHub API with your Personal Access Token for authentication.

gistlibby LogSnag