Skip to content

Commit

Permalink
style: prettier 💅
Browse files Browse the repository at this point in the history
  • Loading branch information
gr2m committed Sep 17, 2019
1 parent ee57e51 commit 6fe1515
Show file tree
Hide file tree
Showing 81 changed files with 3,922 additions and 3,947 deletions.
19 changes: 11 additions & 8 deletions README.md
Expand Up @@ -8,23 +8,26 @@
[![Greenkeeper](https://badges.greenkeeper.io/octokit/rest.js.svg)](https://greenkeeper.io/)

## Installation

```shell
npm install @octokit/rest
```

## Usage

```js
const Octokit = require('@octokit/rest')
const octokit = new Octokit()
const Octokit = require("@octokit/rest");
const octokit = new Octokit();

// Compare: https://developer.github.com/v3/repos/#list-organization-repositories
octokit.repos.listForOrg({
org: 'octokit',
type: 'public'
}).then(({ data }) => {
// handle data
})
octokit.repos
.listForOrg({
org: "octokit",
type: "public"
})
.then(({ data }) => {
// handle data
});
```

See https://octokit.github.io/rest.js/ for full documentation.
Expand Down
24 changes: 12 additions & 12 deletions index.js
@@ -1,16 +1,16 @@
const Octokit = require('./lib/core')
const Octokit = require("./lib/core");

const CORE_PLUGINS = [
require('./plugins/log'),
require('./plugins/authentication-deprecated'), // deprecated: remove in v17
require('./plugins/authentication'),
require('./plugins/pagination'),
require('./plugins/normalize-git-reference-responses'),
require('./plugins/register-endpoints'),
require('./plugins/rest-api-endpoints'),
require('./plugins/validate'),
require("./plugins/log"),
require("./plugins/authentication-deprecated"), // deprecated: remove in v17
require("./plugins/authentication"),
require("./plugins/pagination"),
require("./plugins/normalize-git-reference-responses"),
require("./plugins/register-endpoints"),
require("./plugins/rest-api-endpoints"),
require("./plugins/validate"),

require('octokit-pagination-methods') // deprecated: remove in v17
]
require("octokit-pagination-methods") // deprecated: remove in v17
];

module.exports = Octokit.plugin(CORE_PLUGINS)
module.exports = Octokit.plugin(CORE_PLUGINS);
35 changes: 19 additions & 16 deletions lib/constructor.js
@@ -1,26 +1,29 @@
module.exports = Octokit
module.exports = Octokit;

const { request } = require('@octokit/request')
const Hook = require('before-after-hook')
const { request } = require("@octokit/request");
const Hook = require("before-after-hook");

const parseClientOptions = require('./parse-client-options')
const parseClientOptions = require("./parse-client-options");

function Octokit (plugins, options) {
options = options || {}
const hook = new Hook.Collection()
const log = Object.assign({
debug: () => {},
info: () => {},
warn: console.warn,
error: console.error
}, options && options.log)
function Octokit(plugins, options) {
options = options || {};
const hook = new Hook.Collection();
const log = Object.assign(
{
debug: () => {},
info: () => {},
warn: console.warn,
error: console.error
},
options && options.log
);
const api = {
hook,
log,
request: request.defaults(parseClientOptions(options, log, hook))
}
};

plugins.forEach(pluginFunction => pluginFunction(api, options))
plugins.forEach(pluginFunction => pluginFunction(api, options));

return api
return api;
}
4 changes: 2 additions & 2 deletions lib/core.js
@@ -1,3 +1,3 @@
const factory = require('./factory')
const factory = require("./factory");

module.exports = factory()
module.exports = factory();
14 changes: 7 additions & 7 deletions lib/factory.js
@@ -1,10 +1,10 @@
module.exports = factory
module.exports = factory;

const Octokit = require('./constructor')
const registerPlugin = require('./register-plugin')
const Octokit = require("./constructor");
const registerPlugin = require("./register-plugin");

function factory (plugins) {
const Api = Octokit.bind(null, plugins || [])
Api.plugin = registerPlugin.bind(null, plugins || [])
return Api
function factory(plugins) {
const Api = Octokit.bind(null, plugins || []);
Api.plugin = registerPlugin.bind(null, plugins || []);
return Api;
}
75 changes: 48 additions & 27 deletions lib/parse-client-options.js
@@ -1,64 +1,85 @@
module.exports = parseOptions
module.exports = parseOptions;

const { Deprecation } = require('deprecation')
const { getUserAgent } = require('universal-user-agent')
const once = require('once')
const { Deprecation } = require("deprecation");
const { getUserAgent } = require("universal-user-agent");
const once = require("once");

const pkg = require('../package.json')
const pkg = require("../package.json");

const deprecateOptionsTimeout = once((log, deprecation) => log.warn(deprecation))
const deprecateOptionsAgent = once((log, deprecation) => log.warn(deprecation))
const deprecateOptionsHeaders = once((log, deprecation) => log.warn(deprecation))
const deprecateOptionsTimeout = once((log, deprecation) =>
log.warn(deprecation)
);
const deprecateOptionsAgent = once((log, deprecation) => log.warn(deprecation));
const deprecateOptionsHeaders = once((log, deprecation) =>
log.warn(deprecation)
);

function parseOptions (options, log, hook) {
function parseOptions(options, log, hook) {
if (options.headers) {
options.headers = Object.keys(options.headers).reduce((newObj, key) => {
newObj[key.toLowerCase()] = options.headers[key]
return newObj
}, {})
newObj[key.toLowerCase()] = options.headers[key];
return newObj;
}, {});
}

const clientDefaults = {
headers: options.headers || {},
request: options.request || {},
mediaType: {
previews: [],
format: ''
format: ""
}
}
};

if (options.baseUrl) {
clientDefaults.baseUrl = options.baseUrl
clientDefaults.baseUrl = options.baseUrl;
}

if (options.userAgent) {
clientDefaults.headers['user-agent'] = options.userAgent
clientDefaults.headers["user-agent"] = options.userAgent;
}

if (options.previews) {
clientDefaults.mediaType.previews = options.previews
clientDefaults.mediaType.previews = options.previews;
}

if (options.timeout) {
deprecateOptionsTimeout(log, new Deprecation('[@octokit/rest] new Octokit({timeout}) is deprecated. Use {request: {timeout}} instead. See https://github.com/octokit/request.js#request'))
clientDefaults.request.timeout = options.timeout
deprecateOptionsTimeout(
log,
new Deprecation(
"[@octokit/rest] new Octokit({timeout}) is deprecated. Use {request: {timeout}} instead. See https://github.com/octokit/request.js#request"
)
);
clientDefaults.request.timeout = options.timeout;
}

if (options.agent) {
deprecateOptionsAgent(log, new Deprecation('[@octokit/rest] new Octokit({agent}) is deprecated. Use {request: {agent}} instead. See https://github.com/octokit/request.js#request'))
clientDefaults.request.agent = options.agent
deprecateOptionsAgent(
log,
new Deprecation(
"[@octokit/rest] new Octokit({agent}) is deprecated. Use {request: {agent}} instead. See https://github.com/octokit/request.js#request"
)
);
clientDefaults.request.agent = options.agent;
}

if (options.headers) {
deprecateOptionsHeaders(log, new Deprecation('[@octokit/rest] new Octokit({headers}) is deprecated. Use {userAgent, previews} instead. See https://github.com/octokit/request.js#request'))
deprecateOptionsHeaders(
log,
new Deprecation(
"[@octokit/rest] new Octokit({headers}) is deprecated. Use {userAgent, previews} instead. See https://github.com/octokit/request.js#request"
)
);
}

const userAgentOption = clientDefaults.headers['user-agent']
const defaultUserAgent = `octokit.js/${pkg.version} ${getUserAgent()}`
const userAgentOption = clientDefaults.headers["user-agent"];
const defaultUserAgent = `octokit.js/${pkg.version} ${getUserAgent()}`;

clientDefaults.headers['user-agent'] = [userAgentOption, defaultUserAgent].filter(Boolean).join(' ')
clientDefaults.headers["user-agent"] = [userAgentOption, defaultUserAgent]
.filter(Boolean)
.join(" ");

clientDefaults.request.hook = hook.bind(null, 'request')
clientDefaults.request.hook = hook.bind(null, "request");

return clientDefaults
return clientDefaults;
}
10 changes: 6 additions & 4 deletions lib/register-plugin.js
@@ -1,7 +1,9 @@
module.exports = registerPlugin
module.exports = registerPlugin;

const factory = require('./factory')
const factory = require("./factory");

function registerPlugin (plugins, pluginFunction) {
return factory(plugins.includes(pluginFunction) ? plugins : plugins.concat(pluginFunction))
function registerPlugin(plugins, pluginFunction) {
return factory(
plugins.includes(pluginFunction) ? plugins : plugins.concat(pluginFunction)
);
}
20 changes: 3 additions & 17 deletions package.json
Expand Up @@ -73,7 +73,6 @@
"sinon": "^7.2.4",
"sinon-chai": "^3.0.0",
"sort-keys": "^4.0.0",
"standard": "^14.0.2",
"string-to-arraybuffer": "^1.0.0",
"string-to-jsdoc-comment": "^1.0.0",
"typescript": "^3.3.1",
Expand All @@ -84,7 +83,9 @@
"types": "index.d.ts",
"scripts": {
"coverage": "nyc report --reporter=html && open coverage/index.html",
"pretest": "standard",
"lint": "prettier --check '{lib,plugins,scripts,test}/**/*.{js,json}' index.js README.md package.json",
"lint:fix": "prettier --write '{lib,plugins,scripts,test}/**/*.{js,json}' index.js README.md package.json",
"pretest": "npm run -s lint",
"test": "nyc mocha test/mocha-node-setup.js \"test/*/**/*-test.js\"",
"test:browser": "cypress run --browser chrome",
"test:memory": "mocha test/memory-test",
Expand Down Expand Up @@ -125,21 +126,6 @@
}
]
},
"standard": {
"globals": [
"describe",
"before",
"beforeEach",
"afterEach",
"after",
"it",
"expect",
"cy"
],
"ignore": [
"/docs"
]
},
"bundlesize": [
{
"path": "./dist/octokit-rest.min.js.gz",
Expand Down
51 changes: 31 additions & 20 deletions plugins/authentication-deprecated/authenticate.js
@@ -1,41 +1,52 @@
module.exports = authenticate
module.exports = authenticate;

const { Deprecation } = require('deprecation')
const once = require('once')
const { Deprecation } = require("deprecation");
const once = require("once");

const deprecateAuthenticate = once((log, deprecation) => log.warn(deprecation))
const deprecateAuthenticate = once((log, deprecation) => log.warn(deprecation));

function authenticate (state, options) {
deprecateAuthenticate(state.octokit.log, new Deprecation('[@octokit/rest] octokit.authenticate() is deprecated. Use "auth" constructor option instead.'))
function authenticate(state, options) {
deprecateAuthenticate(
state.octokit.log,
new Deprecation(
'[@octokit/rest] octokit.authenticate() is deprecated. Use "auth" constructor option instead.'
)
);

if (!options) {
state.auth = false
return
state.auth = false;
return;
}

switch (options.type) {
case 'basic':
case "basic":
if (!options.username || !options.password) {
throw new Error('Basic authentication requires both a username and password to be set')
throw new Error(
"Basic authentication requires both a username and password to be set"
);
}
break
break;

case 'oauth':
case "oauth":
if (!options.token && !(options.key && options.secret)) {
throw new Error('OAuth2 authentication requires a token or key & secret to be set')
throw new Error(
"OAuth2 authentication requires a token or key & secret to be set"
);
}
break
break;

case 'token':
case 'app':
case "token":
case "app":
if (!options.token) {
throw new Error('Token authentication requires a token to be set')
throw new Error("Token authentication requires a token to be set");
}
break
break;

default:
throw new Error("Invalid authentication type, must be 'basic', 'oauth', 'token' or 'app'")
throw new Error(
"Invalid authentication type, must be 'basic', 'oauth', 'token' or 'app'"
);
}

state.auth = options
state.auth = options;
}

0 comments on commit 6fe1515

Please sign in to comment.