UNPKG

node-socialite

Version:

Socialite is an OAuth2 authentication tool for Node.js

83 lines (82 loc) 3.06 kB
'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); exports.SocialiteManager = void 0; const Config_1 = require("./Config"); const BaseProvider_1 = require("./BaseProvider"); const Github_1 = require("../Providers/Github"); const WeChat_1 = require("../Providers/WeChat"); const WeWork_1 = require("../Providers/WeWork"); const OpenWeWork_1 = require("../Providers/OpenWeWork"); const Weibo_1 = require("../Providers/Weibo"); const QQ_1 = require("../Providers/QQ"); const DouYin_1 = require("../Providers/DouYin"); const Taobao_1 = require("../Providers/Taobao"); const Gitee_1 = require("../Providers/Gitee"); const Coding_1 = require("../Providers/Coding"); /** * 社交服务管理器 */ class SocialiteManager { constructor(config) { this.config = null; this.resolved = {}; this.customCreators = {}; this.providers = { [Github_1.Github.NAME]: Github_1.Github, [WeChat_1.WeChat.NAME]: WeChat_1.WeChat, [WeWork_1.WeWork.NAME]: WeWork_1.WeWork, [OpenWeWork_1.OpenWeWork.NAME]: OpenWeWork_1.OpenWeWork, [Weibo_1.Weibo.NAME]: Weibo_1.Weibo, [QQ_1.QQ.NAME]: QQ_1.QQ, [DouYin_1.DouYin.NAME]: DouYin_1.DouYin, [Taobao_1.Taobao.NAME]: Taobao_1.Taobao, [Gitee_1.Gitee.NAME]: Gitee_1.Gitee, [Coding_1.Coding.NAME]: Coding_1.Coding, }; this.config = new Config_1.Config(config); } setConfig(config) { this.config = new Config_1.Config(config); } create(name) { name = name.toLowerCase(); if (!this.resolved[name]) { this.resolved[name] = this.createProvider(name); } return this.resolved[name]; } createProvider(name) { let config = this.config.get(name, {}); if (typeof config.provider == 'undefined') { config.provider = name; } if (typeof config.provider == 'string') { let provider = config.provider; if (typeof this.customCreators[provider] != 'undefined') { return this.callCustomCreator(provider, config); } if (typeof this.providers[provider] == 'undefined') { throw new Error('Invalid provider: ' + provider); } return this.buildProvider(this.providers[provider], config); } return this.buildProvider(config.provider, config); } buildProvider(provider, config) { return new provider(config); } callCustomCreator(provider, config) { return this.customCreators[provider](config); } isValidProvider(provider) { return (typeof provider == 'string' && typeof this.providers[provider] != 'undefined') || provider instanceof BaseProvider_1.BaseProvider; } getResolvedProviders() { return this.resolved; } extend(name, func) { this.customCreators[name.toLowerCase()] = func; return this; } } exports.SocialiteManager = SocialiteManager;