WIP Redesign to align with GitHub's conventions#41
Open
philschatz wants to merge 53 commits intomasterfrom
Open
WIP Redesign to align with GitHub's conventions#41philschatz wants to merge 53 commits intomasterfrom
philschatz wants to merge 53 commits intomasterfrom
Conversation
so browser sepia.js can use the same fixtures
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This code change leverages the URL patterns GitHub provides and converts the returned JSON into objects which allows nifty daisy-chaining.
Overview
This library closely mirrors the https://developer.github.com/v3 documentation.
For example,
GET /repos/:owner/:repobecomesocto.repos(owner, repo).fetch()andPOST /repos/:owner/:repo/issues/:number/commentsbecomesocto.repos(owner, repo).issues(number).comments.create(params).Promises and Callbacks
This library supports NodeJS-style callbacks as well as Promises.
To use a callback, just specify it as the last argument to a method.
To use a Promise, do not specify a callback and the return value will be a Promise.
Example (get information on a repo):
Chaining
You construct the URL by chaining properties and methods together and an async call is made once a verb method is called (see below).
Example:
Or, update a specific comment:
The basic structure of these methods is:
.foos.fetch({optionalStuff:...})yields a list of items (possibly paginated).foos(id).fetch(...)yields a single item (issue, repo, user).foos.contains(id)tests membership in a list (yields true/false).foos.create(...)creates a newfoo.foos(id).add()adds an existing User/Repo to the list.foos(id).remove()removes a member from a list or deletes the object and yields a boolean indicating successJSON with methods (Hypermedia)
GitHub provides URL patterns in its JSON responses. These are automatically converted into methods.
For example:
Paged Results
If a
.fetch()returns paged results thennextPage(),previousPage(),firstPage()andlastPage()are added to the returned JSON. For example:Development
The unit tests are named to illustrate examples of using the API. See travis tests or run
npm testto see them.linkedin/sepia is used to generate recorded results from GitHub and philschatz/sepia.js uses them in the browser. If you are adding tests be sure to include the updated fixtures in the Pull Request.
Overview of Features
Implemented GitHub APIs:
Examples
Here are some examples of code you can write with this library
Global
octo.zen.fetch()octo.users.fetch({since})octo.gists.fetch({since})octo.events.fetch({since})octo.notifications.fetch({...})Search
octo.search.repos.fetch({q,sort,order})octo.search.code.fetch({q,sort,order})octo.search.issues.fetch({q,sort,order})octo.search.users.fetch({q,sort,order})User
octo.users(name).fetch()octo.users(name).repos({type,sort,direction})octo.users(name).orgs.fetch()octo.users(name).gists.fetch()octo.users(name).followers.fetch()octo.users(name).following.fetch()octo.users(name).following.contains(name)octo.users(name).keys.fetch()octo.users(name).receivedEvents.fetch({onlyPublic})octo.users(name).events.fetch({onlyPublic})Me
octo.me.fetch()octo.me.update({...})octo.me.repos.fetch({type,sort,direction})octo.me.orgs.fetch()octo.me.followers.fetch()octo.me.following.fetch()octo.me.following.contains(name)octo.me.following(name).add()octo.me.following(name).remove()octo.me.emails.fetch()octo.me.emails.add(emails)octo.me.emails.remove(emails)octo.me.keys.fetch()octo.me.keys.add()octo.me.keys(id).remove()octo.me.key(id).fetch()Org
octo.orgs(name).fetch()octo.orgs(name).update({...})octo.orgs(name).teams.fetch()octo.orgs(name).teams.create({name,repoNames,permission})octo.orgs(name).members.fetch()octo.orgs(name).members.contains(name)octo.orgs(name).members(name).remove()octo.orgs(name).repos.fetch()octo.orgs(name).repos.create(name, {...})Team
octo.teams(id).fetch()octo.teams(id).update({...})octo.teams(id).remove()octo.teams(id).members.fetch()octo.teams(id).members.contains(name)octo.teams(id).members(name).add()octo.teams(id).members(name).remove()octo.teams(id).repos.fetch()octo.teams(id).repos(owner, name).add()octo.teams(id).repos(owner, name).remove()Repo
octo.repos(owner, name).fetch()octo.repos(owner, name).remove()octo.repos(owner, name).update({...})octo.repos(owner, name).readme.fetch()octo.repos(owner, name).forks.create({organization})octo.repos(owner, name).pullRequests.create({...})octo.repos(owner, name).events.fetch()octo.repos(owner, name).issues.events.fetch()octo.repos(owner, name).notifications.fetch({since,...})octo.repos(owner, name).collaborators.fetch()octo.repos(owner, name).collaborators.contains(name)octo.repos(owner, name).collaborators(name).add()octo.repos(owner, name).collaborators(name).remove()octo.repos(owner, name).hooks.fetch()octo.repos(owner, name).hooks.create({name,config,events,active})octo.repos(owner, name).hooks.remove(id)octo.repos(owner, name).hooks(id).fetch()octo.repos(owner, name).hooks(id).update({...}) # un-camelizeocto.repos(owner, name).hooks(id).tests.create()octo.repos(owner, name).languages.fetch()octo.repos(owner, name).releases.fetch()octo.repos(owner, name).contents(path, sha?).fetch()octo.repos(owner, name).contents(path, sha).remove(message, branch)octo.networks(owner, name).events.fetch()Git
octo.repos(owner, name).git.refs.create({...})octo.repos(owner, name).git.refs(ref).remove()octo.repos(owner, name).git.refs(ref).fetch()octo.repos(owner, name).git.refs.heads.create()octo.repos(owner, name).git.refs.heads.fetch()octo.repos(owner, name).git.refs.heads(name).update({sha,force})octo.repos(owner, name).git.refs.heads(name).remove()octo.repos(owner, name).git.blobs.create({...})octo.repos(owner, name).git.blobs(sha).fetch()octo.repos(owner, name).git.trees.create({...})octo.repos(owner, name).git.trees(id).fetch({...})octo.repos(owner, name).git.trees(id).update(newTrees)octo.repos(owner, name).git.commits.fetch({...})octo.repos(owner, name).git.commits.create({parents,tree,message})octo.repos(owner, name).git.commit(sha).fetch()Gists
octo.gists.fetch()octo.gists.create({...})octo.gists(id).fetch()octo.gists(id).update({...})octo.gists(id).remove()octo.gists(id).forks.create()octo.gists(id).star.contains(USERNAME)octo.gists(id).star.add()octo.gists(id).star.remove()