Files
shields/services/github/github-auth-service.js
Paul Melnikow 226fa67a02 Create shortcut for BaseService-related imports (#2809)
Continue to implement #2698:

- Add `core/base-service/index.js` (but hold off on moving the things it imports)
- Add shortcuts in `services/index.js` for Base*Service, errors, and deprecatedService. This file will be streamlined later to avoid cluttering it with rarely used bits.
- Apply consistent ordering of imports and use of `module.exports` in testers.
- Remove some renaming of imports.
- Remove obsolete tests here and there.
2019-01-21 15:41:24 -05:00

46 lines
1.3 KiB
JavaScript

'use strict'
const { BaseJsonService } = require('..')
const { staticAuthConfigured } = require('./github-helpers')
function createRequestFetcher(context, config) {
const { sendAndCacheRequestWithCallbacks, githubApiProvider } = context
return async (url, { qs }) =>
githubApiProvider.requestAsPromise(
sendAndCacheRequestWithCallbacks,
url,
qs
)
}
class GithubAuthService extends BaseJsonService {
constructor(context, config) {
super(context, config)
this._requestFetcher = createRequestFetcher(context, config)
this.staticAuthConfigured = true
}
}
// Use Github auth, but only when static auth is configured. By using this
// class, in production it will behave like GithubAuthService, and in self-
// hosting (i.e. with a configured token) like BaseJsonService. This is
// useful when consuming GitHub endpoints which are not rate-limited: it
// avoids wasting API quota on them in production.
class ConditionalGithubAuthService extends BaseJsonService {
constructor(context, config) {
super(context, config)
if (staticAuthConfigured()) {
this._requestFetcher = createRequestFetcher(context, config)
this.staticAuthConfigured = true
} else {
this.staticAuthConfigured = false
}
}
}
module.exports = {
GithubAuthService,
ConditionalGithubAuthService,
}