"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.formatResponseError = exports.formatSuperAgentError = exports.createFatalAPIFormat = exports.transformAPIResponse = exports.ResourceClient = exports.TokenPaginator = exports.Paginator = exports.Client = exports.ERROR_UNKNOWN_RESPONSE_FORMAT = exports.ERROR_UNKNOWN_CONTENT_TYPE = void 0; const chalk = require("chalk"); const lodash = require("lodash"); const util = require("util"); const guards_1 = require("../guards"); const color_1 = require("./color"); const errors_1 = require("./errors"); const http_1 = require("./utils/http"); const FORMAT_ERROR_BODY_MAX_LENGTH = 1000; exports.ERROR_UNKNOWN_CONTENT_TYPE = 'UNKNOWN_CONTENT_TYPE'; exports.ERROR_UNKNOWN_RESPONSE_FORMAT = 'UNKNOWN_RESPONSE_FORMAT'; class Client { constructor(config) { this.config = config; } async make(method, path, contentType = "application/json" /* JSON */) { const url = path.startsWith('http://') || path.startsWith('https://') ? path : `${this.config.getAPIUrl()}${path}`; const { req } = await http_1.createRequest(method, url, this.config.getHTTPConfig()); req .set('Content-Type', contentType) .set('Accept', "application/json" /* JSON */); return { req }; } async do(req) { const res = await req; const r = transformAPIResponse(res); if (guards_1.isAPIResponseError(r)) { throw new errors_1.FatalException('API request was successful, but the response output format was that of an error.\n' + formatAPIResponse(req, r)); } return r; } paginate(args) { return new Paginator({ client: this, ...args }); } } exports.Client = Client; class Paginator { constructor({ client, reqgen, guard, state, max }) { const defaultState = { page: 1, done: false, loaded: 0 }; this.client = client; this.reqgen = reqgen; this.guard = guard; this.max = max; if (!state) { state = { page_size: 100, ...defaultState }; } this.state = lodash.assign({}, state, defaultState); } next() { if (this.state.done) { return { done: true }; // TODO: why can't I exclude value? } return { done: false, value: (async () => { const { req } = await this.reqgen(); req.query(lodash.pick(this.state, ['page', 'page_size'])); const res = await this.client.do(req); if (!this.guard(res)) { throw createFatalAPIFormat(req, res); } this.state.loaded += res.data.length; if (res.data.length === 0 || // no resources in this page, we're done (typeof this.max === 'number' && this.state.loaded >= this.max) || // met or exceeded maximum requested (typeof this.state.page_size === 'number' && res.data.length < this.state.page_size) // number of resources less than page size, so nothing on next page ) { this.state.done = true; } this.state.page++; return res; })(), }; } [Symbol.iterator]() { return this; } } exports.Paginator = Paginator; class TokenPaginator { constructor({ client, reqgen, guard, state, max }) { const defaultState = { done: false, loaded: 0 }; this.client = client; this.reqgen = reqgen; this.guard = guard; this.max = max; if (!state) { state = { ...defaultState }; } this.state = lodash.assign({}, state, defaultState); } next() { if (this.state.done) { return { done: true }; // TODO: why can't I exclude value? } return { done: false, value: (async () => { const { req } = await this.reqgen(); if (this.state.page_token) { req.query({ page_token: this.state.page_token }); } const res = await this.client.do(req); if (!this.isPageTokenResponseMeta(res.meta)) { throw createFatalAPIFormat(req, res); } const nextPageToken = res.meta.next_page_token; if (!this.guard(res)) { throw createFatalAPIFormat(req, res); } this.state.loaded += res.data.length; if (res.data.length === 0 || // no resources in this page, we're done (typeof this.max === 'number' && this.state.loaded >= this.max) || // met or exceeded maximum requested !nextPageToken // no next page token, must be done ) { this.state.done = true; } this.state.page_token = nextPageToken; return res; })(), }; } isPageTokenResponseMeta(meta) { return meta && (!meta.prev_page_token || typeof meta.prev_page_token === 'string') && (!meta.next_page_token || typeof meta.next_page_token === 'string'); } [Symbol.iterator]() { return this; } } exports.TokenPaginator = TokenPaginator; class ResourceClient { applyModifiers(req, modifiers) { if (!modifiers) { return; } if (modifiers.fields) { req.query({ fields: modifiers.fields }); } } applyAuthentication(req, token) { req.set('Authorization', `Bearer ${token}`); } } exports.ResourceClient = ResourceClient; function transformAPIResponse(r) { if (r.status === 204) { r.body = { meta: { status: 204, version: '', request_id: '' } }; } if (r.status !== 204 && r.type !== "application/json" /* JSON */) { throw exports.ERROR_UNKNOWN_CONTENT_TYPE; } const j = r.body; if (!j.meta) { throw exports.ERROR_UNKNOWN_RESPONSE_FORMAT; } return j; } exports.transformAPIResponse = transformAPIResponse; function createFatalAPIFormat(req, res) { return new errors_1.FatalException('API request was successful, but the response format was unrecognized.\n' + formatAPIResponse(req, res)); } exports.createFatalAPIFormat = createFatalAPIFormat; function formatSuperAgentError(e) { const res = e.response; const req = res.request; // TODO: `req` and `request` exist: https://visionmedia.github.io/superagent/docs/test.html const statusCode = e.response.status; let f = ''; try { const r = transformAPIResponse(res); f += formatAPIResponse(req, r); } catch (e) { f += (`HTTP Error ${statusCode}: ${req.method.toUpperCase()} ${req.url}\n` + '\n' + (res.text ? res.text.substring(0, FORMAT_ERROR_BODY_MAX_LENGTH) : '<no buffered body>')); if (res.text && res.text.length > FORMAT_ERROR_BODY_MAX_LENGTH) { f += ` ...\n\n[ truncated ${res.text.length - FORMAT_ERROR_BODY_MAX_LENGTH} characters ]`; } } return color_1.failure(color_1.strong(f)); } exports.formatSuperAgentError = formatSuperAgentError; function formatAPIResponse(req, r) { return formatResponseError(req, r.meta.status, guards_1.isAPIResponseSuccess(r) ? r.data : r.error); } function formatResponseError(req, status, body) { return color_1.failure(`Request: ${req.method} ${req.url}\n` + (status ? `Response: ${status}\n` : '') + (body ? `Body: \n${util.inspect(body, { colors: chalk.level > 0 })}` : '')); } exports.formatResponseError = formatResponseError;
Name | Type | Size | Permission | Actions |
---|---|---|---|---|
doctor | Folder | 0755 |
|
|
integrations | Folder | 0755 |
|
|
oauth | Folder | 0755 |
|
|
project | Folder | 0755 |
|
|
utils | Folder | 0755 |
|
|
app.d.ts | File | 1.15 KB | 0644 |
|
app.js | File | 2.5 KB | 0644 |
|
build.d.ts | File | 3.43 KB | 0644 |
|
build.js | File | 8.51 KB | 0644 |
|
color.d.ts | File | 484 B | 0644 |
|
color.js | File | 768 B | 0644 |
|
command.d.ts | File | 924 B | 0644 |
|
command.js | File | 5.47 KB | 0644 |
|
config.d.ts | File | 843 B | 0644 |
|
config.js | File | 3.83 KB | 0644 |
|
cordova-res.d.ts | File | 1.07 KB | 0644 |
|
cordova-res.js | File | 2.62 KB | 0644 |
|
diff.d.ts | File | 100 B | 0644 |
|
diff.js | File | 709 B | 0644 |
|
environment.d.ts | File | 1004 B | 0644 |
|
environment.js | File | 516 B | 0644 |
|
errors.d.ts | File | 891 B | 0644 |
|
errors.js | File | 1.77 KB | 0644 |
|
events.d.ts | File | 240 B | 0644 |
|
events.js | File | 498 B | 0644 |
|
executor.d.ts | File | 1 KB | 0644 |
|
executor.js | File | 3.7 KB | 0644 |
|
generate.d.ts | File | 923 B | 0644 |
|
generate.js | File | 351 B | 0644 |
|
git.d.ts | File | 747 B | 0644 |
|
git.js | File | 1.66 KB | 0644 |
|
help.d.ts | File | 2.4 KB | 0644 |
|
help.js | File | 3.66 KB | 0644 |
|
helper.d.ts | File | 252 B | 0644 |
|
helper.js | File | 654 B | 0644 |
|
hooks.d.ts | File | 841 B | 0644 |
|
hooks.js | File | 4.72 KB | 0644 |
|
http.d.ts | File | 2.62 KB | 0644 |
|
http.js | File | 7.56 KB | 0644 |
|
index.d.ts | File | 226 B | 0644 |
|
index.js | File | 6.09 KB | 0644 |
|
ionitron.d.ts | File | 148 B | 0644 |
|
ionitron.js | File | 4.72 KB | 0644 |
|
namespace.d.ts | File | 854 B | 0644 |
|
namespace.js | File | 686 B | 0644 |
|
native-run.d.ts | File | 1.5 KB | 0644 |
|
native-run.js | File | 4.98 KB | 0644 |
|
open.d.ts | File | 164 B | 0644 |
|
open.js | File | 507 B | 0644 |
|
prompts.d.ts | File | 402 B | 0644 |
|
prompts.js | File | 716 B | 0644 |
|
security.d.ts | File | 511 B | 0644 |
|
security.js | File | 785 B | 0644 |
|
serve.d.ts | File | 5.79 KB | 0644 |
|
serve.js | File | 22.4 KB | 0644 |
|
session.d.ts | File | 921 B | 0644 |
|
session.js | File | 7.82 KB | 0644 |
|
shell.d.ts | File | 1.95 KB | 0644 |
|
shell.js | File | 8.41 KB | 0644 |
|
snapshot.d.ts | File | 765 B | 0644 |
|
snapshot.js | File | 1.13 KB | 0644 |
|
ssh-config.d.ts | File | 646 B | 0644 |
|
ssh-config.js | File | 3.04 KB | 0644 |
|
ssh.d.ts | File | 1.67 KB | 0644 |
|
ssh.js | File | 3.53 KB | 0644 |
|
start.d.ts | File | 1.34 KB | 0644 |
|
start.js | File | 12.1 KB | 0644 |
|
telemetry.d.ts | File | 972 B | 0644 |
|
telemetry.js | File | 3.59 KB | 0644 |
|
updates.d.ts | File | 1.01 KB | 0644 |
|
updates.js | File | 3.82 KB | 0644 |
|
user.d.ts | File | 865 B | 0644 |
|
user.js | File | 2.37 KB | 0644 |
|