UNPKG

@veecode-platform/safira-cli

Version:

Generate a microservice project from your spec.

69 lines (68 loc) 2.85 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.GithubUserService = void 0; const tslib_1 = require("tslib"); const properties = tslib_1.__importStar(require("../../properties.json")); const https = tslib_1.__importStar(require("https")); const url_1 = require("url"); const string_utils_1 = require("../../utils/string-utils"); const json_utils_1 = require("../../utils/json-utils"); const git_exception_1 = require("../../exception/git-exception"); class GithubUserService { constructor() { } async getUserInfo(githubCredential) { return new Promise((resolve, reject) => { const url = new url_1.URL(`${properties.github["api-host"]}/user`); const options = { hostname: url.hostname, port: 443, path: url.pathname, method: "GET", headers: { Authorization: `token ${githubCredential.token}`, Accept: "application/vnd.github+json", "User-Agent": string_utils_1.StringUtils.getUserAgent(), }, }; const req = https.request(options, res => { res.setEncoding("utf8"); let body = ""; res.on("data", d => { body += d; }); res.on("end", () => { const bodyMap = JSON.parse(json_utils_1.JsonUtils.isJsonString(body) ? body : "{}"); switch (res.statusCode) { case 200: resolve({ email: bodyMap.email, name: bodyMap.name, username: bodyMap.login, id: bodyMap.id, }); break; case 404: reject(new git_exception_1.GitPublicKeyNotFoundException(bodyMap?.message || res.statusMessage)); break; case 401: reject(new git_exception_1.GitAccessDeniedException("Github Access Denied, check your token")); break; default: reject(new git_exception_1.GitException(`${res.statusCode}-${bodyMap?.message || res.statusMessage}`)); break; } }); }); req.on("error", error => { reject(error); }); req.end(); }); } static get instance() { if (!this._instance) this._instance = new this(); return this._instance; } } exports.GithubUserService = GithubUserService;