UNPKG

7.97 kBSource Map (JSON)View Raw
1{"version":3,"names":[],"mappings":"","sources":["User.js"],"sourcesContent":["/**\n * @file\n * @copyright 2013 Michael Aufreiter (Development Seed) and 2016 Yahoo Inc.\n * @license Licensed under {@link https://spdx.org/licenses/BSD-3-Clause-Clear.html BSD-3-Clause-Clear}.\n * Github.js is freely distributable.\n */\n\nimport Requestable from './Requestable';\nimport debug from 'debug';\nconst log = debug('github:user');\n\n/**\n * A User allows scoping of API requests to a particular Github user.\n */\nclass User extends Requestable {\n /**\n * Create a User.\n * @param {string} [username] - the user to use for user-scoped queries\n * @param {Requestable.auth} [auth] - information required to authenticate to Github\n * @param {string} [apiBase=https://api.github.com] - the base Github API URL\n */\n constructor(username, auth, apiBase) {\n super(auth, apiBase);\n this.__user = username;\n }\n\n /**\n * Get the url for the request. (dependent on if we're requesting for the authenticated user or not)\n * @private\n * @param {string} endpoint - the endpoint being requested\n * @return {string} - the resolved endpoint\n */\n __getScopedUrl(endpoint) {\n if (this.__user) {\n return endpoint ?\n `/users/${this.__user}/${endpoint}` :\n `/users/${this.__user}`\n ;\n\n } else { // eslint-disable-line\n switch (endpoint) {\n case '':\n return '/user';\n\n case 'notifications':\n case 'gists':\n return `/${endpoint}`;\n\n default:\n return `/user/${endpoint}`;\n }\n }\n }\n\n /**\n * List the user's repositories\n * @see https://developer.github.com/v3/repos/#list-user-repositories\n * @param {Object} [options={}] - any options to refine the search\n * @param {Requestable.callback} [cb] - will receive the list of repositories\n * @return {Promise} - the promise for the http request\n */\n listRepos(options, cb) {\n if (typeof options === 'function') {\n cb = options;\n options = {};\n }\n\n options = this._getOptionsWithDefaults(options);\n\n log(`Fetching repositories with options: ${JSON.stringify(options)}`);\n return this._requestAllPages(this.__getScopedUrl('repos'), options, cb);\n }\n\n /**\n * List the orgs that the user belongs to\n * @see https://developer.github.com/v3/orgs/#list-user-organizations\n * @param {Requestable.callback} [cb] - will receive the list of organizations\n * @return {Promise} - the promise for the http request\n */\n listOrgs(cb) {\n return this._request('GET', this.__getScopedUrl('orgs'), null, cb);\n }\n\n /**\n * List followers of a user\n * @see https://developer.github.com/v3/users/followers/#list-followers-of-a-user\n * @param {Requestable.callback} [cb] - will receive the list of followers\n * @return {Promise} - the promise for the http request\n */\n listFollowers(cb) {\n return this._request('GET', this.__getScopedUrl('followers'), null, cb);\n }\n\n /**\n * List users followed by another user\n * @see https://developer.github.com/v3/users/followers/#list-users-followed-by-another-user\n * @param {Requestable.callback} [cb] - will receive the list of who a user is following\n * @return {Promise} - the promise for the http request\n */\n listFollowing(cb) {\n return this._request('GET', this.__getScopedUrl('following'), null, cb);\n }\n\n /**\n * List the user's gists\n * @see https://developer.github.com/v3/gists/#list-a-users-gists\n * @param {Requestable.callback} [cb] - will receive the list of gists\n * @return {Promise} - the promise for the http request\n */\n listGists(cb) {\n return this._request('GET', this.__getScopedUrl('gists'), null, cb);\n }\n\n /**\n * List the user's notifications\n * @see https://developer.github.com/v3/activity/notifications/#list-your-notifications\n * @param {Object} [options={}] - any options to refine the search\n * @param {Requestable.callback} [cb] - will receive the list of repositories\n * @return {Promise} - the promise for the http request\n */\n listNotifications(options, cb) {\n options = options || {};\n if (typeof options === 'function') {\n cb = options;\n options = {};\n }\n\n options.since = this._dateToISO(options.since);\n options.before = this._dateToISO(options.before);\n\n return this._request('GET', this.__getScopedUrl('notifications'), options, cb);\n }\n\n /**\n * Show the user's profile\n * @see https://developer.github.com/v3/users/#get-a-single-user\n * @param {Requestable.callback} [cb] - will receive the user's information\n * @return {Promise} - the promise for the http request\n */\n getProfile(cb) {\n return this._request('GET', this.__getScopedUrl(''), null, cb);\n }\n\n /**\n * Gets the list of starred repositories for the user\n * @see https://developer.github.com/v3/activity/starring/#list-repositories-being-starred\n * @param {Requestable.callback} [cb] - will receive the list of starred repositories\n * @return {Promise} - the promise for the http request\n */\n listStarredRepos(cb) {\n let requestOptions = this._getOptionsWithDefaults();\n return this._requestAllPages(this.__getScopedUrl('starred'), requestOptions, cb);\n }\n\n /**\n * Gets the list of starred gists for the user\n * @see https://developer.github.com/v3/gists/#list-starred-gists\n * @param {Object} [options={}] - any options to refine the search\n * @param {Requestable.callback} [cb] - will receive the list of gists\n * @return {Promise} - the promise for the http request\n */\n listStarredGists(options, cb) {\n options = options || {};\n if (typeof options === 'function') {\n cb = options;\n options = {};\n }\n options.since = this._dateToISO(options.since);\n return this._request('GET', '/gists/starred', options, cb);\n }\n\n /**\n * List email addresses for a user\n * @see https://developer.github.com/v3/users/emails/#list-email-addresses-for-a-user\n * @param {Requestable.callback} [cb] - will receive the list of emails\n * @return {Promise} - the promise for the http request\n */\n getEmails(cb) {\n return this._request('GET', '/user/emails', null, cb);\n }\n\n /**\n * Have the authenticated user follow this user\n * @see https://developer.github.com/v3/users/followers/#follow-a-user\n * @param {string} username - the user to follow\n * @param {Requestable.callback} [cb] - will receive true if the request succeeds\n * @return {Promise} - the promise for the http request\n */\n follow(username, cb) {\n return this._request('PUT', `/user/following/${username}`, null, cb);\n }\n\n /**\n * Have the currently authenticated user unfollow this user\n * @see https://developer.github.com/v3/users/followers/#follow-a-user\n * @param {string} username - the user to unfollow\n * @param {Requestable.callback} [cb] - receives true if the request succeeds\n * @return {Promise} - the promise for the http request\n */\n unfollow(username, cb) {\n return this._request('DELETE', `/user/following/${username}`, null, cb);\n }\n\n /**\n * Create a new repository for the currently authenticated user\n * @see https://developer.github.com/v3/repos/#create\n * @param {object} options - the repository definition\n * @param {Requestable.callback} [cb] - will receive the API response\n * @return {Promise} - the promise for the http request\n */\n createRepo(options, cb) {\n return this._request('POST', '/user/repos', options, cb);\n }\n}\n\nmodule.exports = User;\n"],"file":"User.js"}
\No newline at end of file