bugyo-cloud-client
Version:
HTTP client for Bugyo Cloud
66 lines • 2.82 kB
JavaScript
;
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.TopPage = void 0;
const bugyo_cloud_client_1 = require("../bugyo-cloud-client");
const config_1 = require("../config");
const url_utils_1 = require("../utils/url-utils");
const base_endpoint_1 = require("./base/base-endpoint");
class TopPage extends base_endpoint_1.BaseEndpoint {
constructor() {
super(...arguments);
this.requestConfig = {
maxRedirects: 0,
// Locationが相対なので指定
baseURL: config_1.BASE_URL,
// 302のときに、エラーにならないようにする
validateStatus: (status) => (status >= 200 && status < 300) || status === 302,
};
}
/**
* リダイレクト後のURLからUserCodeを返します。
*
* @param client
* @param url
*/
invoke(client, url) {
return __awaiter(this, void 0, void 0, function* () {
const resp = yield this.wrappedHttpGet(client, url);
if (!resp.config.url) {
const msg = "Cannot determin toppage url.";
this.logger.error(msg);
throw new bugyo_cloud_client_1.BugyoCloudClientError(msg);
}
this.logger.info("Toppage url is %s.", resp.config.url);
return (0, url_utils_1.extractUserCode)(resp.config.url);
});
}
/**
* リダイレクト中の set-cookie が動かない様子なので、
* 自前でリダイレクトを追いかける
* @param client
* @param url
*/
wrappedHttpGet(client, url) {
return __awaiter(this, void 0, void 0, function* () {
let nextUrl = url;
let resp;
do {
this.logger.debug("Trying to GET, url=%s", nextUrl);
resp = yield client.session.get(nextUrl, this.requestConfig);
} while (resp.status === 302 && (nextUrl = resp.headers["location"]));
this.throwIfNgStatus(resp);
return resp;
});
}
}
exports.TopPage = TopPage;
//# sourceMappingURL=top-page.js.map