GithubHelp home page GithubHelp logo

gustavclausen / github-api-fetcher Goto Github PK

View Code? Open in Web Editor NEW
2.0 2.0 0.0 1.61 MB

Simplified data fetching JS/TS client for GitHub's GraphQL v4 API

License: MIT License

TypeScript 100.00%
github github-api github-api-v4 graphql graphql-client typescript nodejs fetcher fetching

github-api-fetcher's Introduction

GitHub API Fetcher

npm CircleCI Coverage Status License Greenkeeper badge

GitHub API fetcher is a simplified data fetching client for GitHub's GraphQL v4 API – supporting Node.js and browsers for all types of applications: websites, scripts, data scraping, plugins etc. Works seamlessly with both JavaScript and TypeScript.

Features

  • Simple, promise-based API for common requests like user/repository/organization profiles, contributions and more. Check out the next section to get a full overview of all predefined requests.
  • Full TypeScript support.
  • Simple error handling with defined error types.
  • Adheres to GitHub's best practices – such as dealing with abuse rate limits.
  • Support for adding your own GraphQL requests.

Predefined requests

User

  • User profile
  • Organization memberships
  • Public repository ownerships
  • Contributions:
    • Years of contribution
    • Commit contributions (monthly and yearly)
    • Issue contributions (monthly and yearly)
    • Pull request contributions (monthly and yearly)
    • Pull request review contributions (monthly and yearly)

Organization

  • Organization profile

Repository

  • Repository profile

Gist

  • Gist profile

Check out the documentation website for a complete overview of the API and the included models.

Installation

npm i github-api-fetcher

Configuration

GitHub access token

It's required to have a valid access token to use the client and access the GitHub API. You've the option to use your own personal access token (see this guide), or create an OAuth app and use the authenticated user's access token (see this guide). Personal access tokens is recommended while developing since it's simple to setup, while the OAuth app setup is more complicated and should be used for public use.

The following scopes is required to use all predefined requests:

  • repo:status
  • public_repo
  • read:org
  • read:user

(Optional) Declare access token in environment variable

Instead of passing the access token as an argument when using the client, it's possible to declare the access token in an environment variable with the name: GITHUB_FETCHER_API_ACCESS_TOKEN.

Usage

Quickstart - simple user profile request

JavaScript

const { APIFetcher } = require('github-api-fetcher');

/*
 * Pass access token as argument in constructor, or load from environment
 * variable (see 'Configuration' section)
 */
const fetcher = new APIFetcher('SECRET-ACCESS-TOKEN');

(async () => {
    const userProfile = await fetcher.user.getProfile('torvalds');

    console.log(userProfile.displayName);
})();

Output:

Linus Torvalds

TypeScript

import { APIFetcher } from 'github-api-fetcher';

/*
 * Pass access token as argument in constructor, or load from environment
 * variable (see 'Configuration' section)
 */
const fetcher = new APIFetcher('SECRET-ACCESS-TOKEN');

(async (): Promise<void> => {
    const userProfile = await fetcher.user.getProfile('torvalds');
  	
    // 'getProfile' returns null for non-existing users
    if (!userProfile) {
      console.log('User does not exist');
      return;
    }

    console.log(userProfile.displayName);
})();

Output:

Linus Torvalds

Routes

You access the requests by the route properties on the client as shown in the example below:

const fetcher = new APIFetcher('<SECRET-ACCESS-TOKEN>');

fetcher.user // User route
fetcher.organization // Organization route
fetcher.repository // Repository route
fetcher.gist // Gist route

Error handling

JavaScript

const { APIFetcher, ResponseErrorType } = require("github-api-fetcher");

const fetcher = new APIFetcher('not-a-valid-token');

(async () => {
  try {
    const userProfile = await fetcher.user.getProfile('torvalds');
    console.log(userProfile);
  } catch (err) {
    console.error(ResponseErrorType[err.type]);
  }
})();

Output:

BAD_CREDENTIALS

TypeScript

import { APIFetcher, RequestError, ResponseErrorType } from "github-api-fetcher";

const fetcher = new APIFetcher('not-a-valid-token');

(async () => {
  try {
    const userProfile = await fetcher.user.getProfile('torvalds');

    // 'getProfile' returns null for non-existing users
    if (!userProfile) {
      console.log('User does not exist');
      return;
    }

    console.log(userProfile.displayName);
  } catch (err) {
    const requestError = err as RequestError;

    console.error(ResponseErrorType[requestError.type]);
  }
})();

Output:

BAD_CREDENTIALS

Modifying the project for your needs

Feel free to fork this repository to add, modify, delete requests and models to suit your needs.

Extending with your own requests

You can build your own GraphQL queries following GitHub's schemas (documentation available here: https://developer.github.com/v4/).

Examples for custom requests can be found in the examples folder. These examples illustrates how to work with single and paged requests, as well as GraphQL fragments with plain and nested fields with aliases.

Modifying models and requests

You can simply modify the base models and requests to add, modify or delete properties. Mulitple integration tests has been written to ensure all properties on the base models is being set upon parsing the response data.
You can use the NPM task, npm test, to run all tests.

Contributing

This project needs your help! 💪

Please read this document. It explains how to contribute to this project.

License

Feel free to use the source code in any way you like – it's released under the MIT License.
I would appreciate being credited, but it's most certainly not required!

Maintainers

github-api-fetcher's People

Contributors

greenkeeper[bot] avatar gustavclausen avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar

github-api-fetcher's Issues

Action required: Greenkeeper could not be activated 🚨

🚨 You need to enable Continuous Integration on Greenkeeper branches of this repository. 🚨

To enable Greenkeeper, you need to make sure that a commit status is reported on all branches. This is required by Greenkeeper because it uses your CI build statuses to figure out when to notify you about breaking changes.

Since we didn’t receive a CI status on the greenkeeper/initial branch, it’s possible that you don’t have CI set up yet.
We recommend using:

If you have already set up a CI for this repository, you might need to check how it’s configured. Make sure it is set to run on all new branches. If you don’t want it to run on absolutely every branch, you can whitelist branches starting with greenkeeper/.

Once you have installed and configured CI on this repository correctly, you’ll need to re-trigger Greenkeeper’s initial pull request. To do this, please click the 'fix repo' button on account.greenkeeper.io.

An in-range update of @typescript-eslint/parser is breaking the build 🚨

The devDependency @typescript-eslint/parser was updated from 2.19.0 to 2.19.1.

🚨 View failing branch.

This version is covered by your current version range and after updating it in your project the build failed.

@typescript-eslint/parser is a devDependency of this project. It might not break your production code or affect downstream projects, but probably breaks your build or test tools, which may prevent deploying or publishing.

Status Details
  • ci/circleci: checkout: Your tests failed on CircleCI (Details).

Release Notes for v2.19.1

2.19.1 (2020-02-10)

Bug Fixes

  • eslint-plugin: [unbound-method] blacklist a few unbound natives (#1562) (4670aab)
  • typescript-estree: ts returning wrong file with project references (#1575) (4c12dac)
Commits

The new version differs by 5 commits.

  • 1c8f0df chore: publish v2.19.1
  • 4c12dac fix(typescript-estree): ts returning wrong file with project references (#1575)
  • e9cf734 docs(eslint-plugin): fix typo in readme
  • 10d86b1 docs(eslint-plugin): [no-dupe-class-members] fix typo (#1566)
  • 4670aab fix(eslint-plugin): [unbound-method] blacklist a few unbound natives (#1562)

See the full diff

FAQ and help

There is a collection of frequently asked questions. If those don’t help, you can always ask the humans behind Greenkeeper.


Your Greenkeeper Bot 🌴

Gists

Features:

  • Get specific Gist from resource path
  • Add public Gists to user profile

An in-range update of @types/lodash is breaking the build 🚨

The devDependency @types/lodash was updated from 4.14.140 to 4.14.141.

🚨 View failing branch.

This version is covered by your current version range and after updating it in your project the build failed.

@types/lodash is a devDependency of this project. It might not break your production code or affect downstream projects, but probably breaks your build or test tools, which may prevent deploying or publishing.

Status Details
  • ci/circleci: checkout: Your tests passed on CircleCI! (Details).
  • ci/circleci: build: Your tests passed on CircleCI! (Details).
  • ci/circleci: test: Your tests failed on CircleCI (Details).

FAQ and help

There is a collection of frequently asked questions. If those don’t help, you can always ask the humans behind Greenkeeper.


Your Greenkeeper Bot 🌴

An in-range update of eslint-plugin-import is breaking the build 🚨

The devDependency eslint-plugin-import was updated from 2.18.2 to 2.19.0.

🚨 View failing branch.

This version is covered by your current version range and after updating it in your project the build failed.

eslint-plugin-import is a devDependency of this project. It might not break your production code or affect downstream projects, but probably breaks your build or test tools, which may prevent deploying or publishing.

Status Details
  • ci/circleci: checkout: Your tests passed on CircleCI! (Details).
  • ci/circleci: build: Your tests passed on CircleCI! (Details).
  • ci/circleci: test: Your tests failed on CircleCI (Details).

Commits

The new version differs by 46 commits.

  • 9b76635 Bump to v2.19.0
  • 47a232e [resolvers/webpack] v0.12.0
  • 26ad476 [resolvers/webpack] [deps] update debug, enhanced-resolve, has, interpret, lodash, resolve, semver
  • 3f0e8f3 [resolvers/node] [Deps] update resolve
  • 7190c3e bump utils to v2.5.0
  • a60e5c6 [New] no-commonjs: add allowConditionalRequire option
  • 414c923 [New] enable passing cwd as an option to eslint-import-resolver-webpack
  • 8224e51 [New] order/no-extraneous-dependencies: Alphabetize imports within groups
  • f12ae59 [New] no-duplicates: add a considerQueryString option to handle false positives when using some webpack loaders.
  • 2d3d045 [fix] importType: Accept '@example' as internal
  • 0426f16 [New] order: add pathGroups option to add support to order by paths
  • 99b3fbf [Fix] no-extraneous-dependencies: Add support for export from
  • 21bf8c6 [Fix] no-cycle: should not warn for Flow imports
  • 0cd5e43 [Fix] no-unused-modules: fix crash due to export *
  • 05085bb [flow] no-unused-modules: add flow type support

There are 46 commits in total.

See the full diff

FAQ and help

There is a collection of frequently asked questions. If those don’t help, you can always ask the humans behind Greenkeeper.


Your Greenkeeper Bot 🌴

An in-range update of @types/node is breaking the build 🚨

The devDependency @types/node was updated from 12.12.15 to 12.12.16.

🚨 View failing branch.

This version is covered by your current version range and after updating it in your project the build failed.

@types/node is a devDependency of this project. It might not break your production code or affect downstream projects, but probably breaks your build or test tools, which may prevent deploying or publishing.

Status Details
  • ci/circleci: checkout: Your tests passed on CircleCI! (Details).
  • ci/circleci: build: Your tests passed on CircleCI! (Details).
  • ci/circleci: test: Your tests failed on CircleCI (Details).

FAQ and help

There is a collection of frequently asked questions. If those don’t help, you can always ask the humans behind Greenkeeper.


Your Greenkeeper Bot 🌴

An in-range update of @typescript-eslint/eslint-plugin is breaking the build 🚨

The devDependency @typescript-eslint/eslint-plugin was updated from 1.12.0 to 1.13.0.

🚨 View failing branch.

This version is covered by your current version range and after updating it in your project the build failed.

@typescript-eslint/eslint-plugin is a devDependency of this project. It might not break your production code or affect downstream projects, but probably breaks your build or test tools, which may prevent deploying or publishing.

Status Details
  • ci/circleci: checkout: Your tests passed on CircleCI! (Details).
  • ci/circleci: build: Your tests passed on CircleCI! (Details).
  • ci/circleci: test: Your tests failed on CircleCI (Details).

Release Notes for v1.13.0

1.13.0 (2019-07-21)

Bug Fixes

  • Correct @types/json-schema dependency (#675) (a5398ce)
  • eslint-plugin: remove imports from typescript-estree (#706) (ceb2d32), closes #705
  • eslint-plugin: undo breaking changes to recommended config (93f72e3)
  • utils: move typescript from peer dep to dev dep (#712) (f949355)
  • utils: RuleTester should not require a parser (#713) (158a417)

Features

  • eslint-plugin: add new rule no-misused-promises (#612) (28a131d)
  • eslint-plugin: add new rule require-await (#674) (807bc2d)
Commits

The new version differs by 14 commits.

  • c367b34 chore: publish v1.13.0
  • 78375ce chore: add check for dirty workspace post-install (#736)
  • 93f72e3 fix(eslint-plugin): undo breaking changes to recommended config
  • 807bc2d feat(eslint-plugin): add new rule require-await (#674)
  • f949355 fix(utils): move typescript from peer dep to dev dep (#712)
  • c1e5c37 docs: fix typo in no-misused-promises related links (#719)
  • 6be0d81 docs(eslint-plugin): correct no-magic-number docs (#717)
  • 66f9741 docs(eslint-plugin): mention member-delimiter-style in semi (#715)
  • 158a417 fix(utils): RuleTester should not require a parser (#713)
  • 28a131d feat(eslint-plugin): add new rule no-misused-promises (#612)
  • ceb2d32 fix(eslint-plugin): remove imports from typescript-estree (#706)
  • 9836fb7 docs: readme typo (#701)
  • c2ad091 chore(eslint-plugin): add config validation script to CI (#649)
  • a5398ce fix: Correct @types/json-schema dependency (#675)

See the full diff

FAQ and help

There is a collection of frequently asked questions. If those don’t help, you can always ask the humans behind Greenkeeper.


Your Greenkeeper Bot 🌴

Providing a non-existing gist name throws parse error

Describe the bug
Providing a non-existing gist name throws parse error.

To Reproduce
gists.getProfile('staltz', 'introrx.md')

Expected behavior
Should return an error of NOT_FOUND type.

Version:
2.1.0

Environment (please complete the following information):
Node.js 10.15.0 using TypeScript

Additional context

  • The gist name is not the identifier of the gist – the gist id is. There is no such thing as a gist name, I found out. Thus, the API for the gist route and associated model, GistProfile, has to be changed.
  • If you provide a non-existing username, the expected NOT_FOUND error is thrown. But if the username is valid, but the gist id is not, a parse error is thrown. It is expected to throw the NOT_FOUND error.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.