2021-02-08 12:58:32 +08:00
'use strict' ;
Object . defineProperty ( exports , '__esModule' , { value : true } ) ;
var universalUserAgent = require ( 'universal-user-agent' ) ;
var beforeAfterHook = require ( 'before-after-hook' ) ;
var request = require ( '@octokit/request' ) ;
var graphql = require ( '@octokit/graphql' ) ;
var authToken = require ( '@octokit/auth-token' ) ;
2021-06-04 22:19:14 +08:00
function _objectWithoutPropertiesLoose ( source , excluded ) {
if ( source == null ) return { } ;
var target = { } ;
var sourceKeys = Object . keys ( source ) ;
var key , i ;
for ( i = 0 ; i < sourceKeys . length ; i ++ ) {
key = sourceKeys [ i ] ;
if ( excluded . indexOf ( key ) >= 0 ) continue ;
target [ key ] = source [ key ] ;
2021-02-08 12:58:32 +08:00
}
2021-06-04 22:19:14 +08:00
return target ;
2021-02-08 12:58:32 +08:00
}
2021-06-04 22:19:14 +08:00
function _objectWithoutProperties ( source , excluded ) {
if ( source == null ) return { } ;
2021-02-08 12:58:32 +08:00
2021-06-04 22:19:14 +08:00
var target = _objectWithoutPropertiesLoose ( source , excluded ) ;
2021-02-08 12:58:32 +08:00
2021-06-04 22:19:14 +08:00
var key , i ;
2021-02-08 12:58:32 +08:00
2021-06-04 22:19:14 +08:00
if ( Object . getOwnPropertySymbols ) {
var sourceSymbolKeys = Object . getOwnPropertySymbols ( source ) ;
2021-02-08 12:58:32 +08:00
2021-06-04 22:19:14 +08:00
for ( i = 0 ; i < sourceSymbolKeys . length ; i ++ ) {
key = sourceSymbolKeys [ i ] ;
if ( excluded . indexOf ( key ) >= 0 ) continue ;
if ( ! Object . prototype . propertyIsEnumerable . call ( source , key ) ) continue ;
target [ key ] = source [ key ] ;
2021-02-08 12:58:32 +08:00
}
}
return target ;
}
2021-06-04 22:19:14 +08:00
const VERSION = "3.4.0" ;
2021-02-08 12:58:32 +08:00
class Octokit {
constructor ( options = { } ) {
const hook = new beforeAfterHook . Collection ( ) ;
const requestDefaults = {
baseUrl : request . request . endpoint . DEFAULTS . baseUrl ,
headers : { } ,
request : Object . assign ( { } , options . request , {
2021-06-04 22:19:14 +08:00
// @ts-ignore internal usage only, no need to type
2021-02-08 12:58:32 +08:00
hook : hook . bind ( null , "request" )
} ) ,
mediaType : {
previews : [ ] ,
format : ""
}
} ; // prepend default user agent with `options.userAgent` if set
requestDefaults . headers [ "user-agent" ] = [ options . userAgent , ` octokit-core.js/ ${ VERSION } ${ universalUserAgent . getUserAgent ( ) } ` ] . filter ( Boolean ) . join ( " " ) ;
if ( options . baseUrl ) {
requestDefaults . baseUrl = options . baseUrl ;
}
if ( options . previews ) {
requestDefaults . mediaType . previews = options . previews ;
}
if ( options . timeZone ) {
requestDefaults . headers [ "time-zone" ] = options . timeZone ;
}
this . request = request . request . defaults ( requestDefaults ) ;
2021-06-04 22:19:14 +08:00
this . graphql = graphql . withCustomRequest ( this . request ) . defaults ( requestDefaults ) ;
2021-02-08 12:58:32 +08:00
this . log = Object . assign ( {
debug : ( ) => { } ,
info : ( ) => { } ,
warn : console . warn . bind ( console ) ,
error : console . error . bind ( console )
} , options . log ) ;
this . hook = hook ; // (1) If neither `options.authStrategy` nor `options.auth` are set, the `octokit` instance
2021-06-04 22:19:14 +08:00
// is unauthenticated. The `this.auth()` method is a no-op and no request hook is registered.
2021-02-08 12:58:32 +08:00
// (2) If only `options.auth` is set, use the default token authentication strategy.
// (3) If `options.authStrategy` is set then use it and pass in `options.auth`. Always pass own request as many strategies accept a custom request instance.
// TODO: type `options.auth` based on `options.authStrategy`.
if ( ! options . authStrategy ) {
if ( ! options . auth ) {
// (1)
this . auth = async ( ) => ( {
type : "unauthenticated"
} ) ;
} else {
// (2)
const auth = authToken . createTokenAuth ( options . auth ) ; // @ts-ignore ¯\_(ツ)_/¯
hook . wrap ( "request" , auth . hook ) ;
this . auth = auth ;
}
} else {
2021-06-04 22:19:14 +08:00
const {
authStrategy
} = options ,
otherOptions = _objectWithoutProperties ( options , [ "authStrategy" ] ) ;
const auth = authStrategy ( Object . assign ( {
request : this . request ,
log : this . log ,
// we pass the current octokit instance as well as its constructor options
// to allow for authentication strategies that return a new octokit instance
// that shares the same internal state as the current one. The original
// requirement for this was the "event-octokit" authentication strategy
// of https://github.com/probot/octokit-auth-probot.
octokit : this ,
octokitOptions : otherOptions
2021-02-08 12:58:32 +08:00
} , options . auth ) ) ; // @ts-ignore ¯\_(ツ)_/¯
hook . wrap ( "request" , auth . hook ) ;
this . auth = auth ;
} // apply plugins
// https://stackoverflow.com/a/16345172
const classConstructor = this . constructor ;
classConstructor . plugins . forEach ( plugin => {
Object . assign ( this , plugin ( this , options ) ) ;
} ) ;
}
static defaults ( defaults ) {
const OctokitWithDefaults = class extends this {
constructor ( ... args ) {
const options = args [ 0 ] || { } ;
if ( typeof defaults === "function" ) {
super ( defaults ( options ) ) ;
return ;
}
super ( Object . assign ( { } , defaults , options , options . userAgent && defaults . userAgent ? {
userAgent : ` ${ options . userAgent } ${ defaults . userAgent } `
} : null ) ) ;
}
} ;
return OctokitWithDefaults ;
}
/ * *
* Attach a plugin ( or many ) to your Octokit instance .
*
* @ example
* const API = Octokit . plugin ( plugin1 , plugin2 , plugin3 , ... )
* /
static plugin ( ... newPlugins ) {
var _a ;
const currentPlugins = this . plugins ;
const NewOctokit = ( _a = class extends this { } , _a . plugins = currentPlugins . concat ( newPlugins . filter ( plugin => ! currentPlugins . includes ( plugin ) ) ) , _a ) ;
return NewOctokit ;
}
}
Octokit . VERSION = VERSION ;
Octokit . plugins = [ ] ;
exports . Octokit = Octokit ;
//# sourceMappingURL=index.js.map