@protobuf-ts/runtime-angular
Advanced tools
Comparing version 1.0.0-alpha.28 to 1.0.0-alpha.29
(function (global, factory) { | ||
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('@angular/core'), require('@angular/common'), require('@protobuf-ts/runtime')) : | ||
typeof define === 'function' && define.amd ? define('@protobuf-ts/runtime-angular', ['exports', '@angular/core', '@angular/common', '@protobuf-ts/runtime'], factory) : | ||
(global = global || self, factory((global['protobuf-ts'] = global['protobuf-ts'] || {}, global['protobuf-ts']['runtime-angular'] = {}), global.ng.core, global.ng.common, global.protobufts.runtime)); | ||
}(this, (function (exports, core, common, runtime) { 'use strict'; | ||
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('@angular/core'), require('@angular/common'), require('@protobuf-ts/runtime'), require('@angular/common/http'), require('@protobuf-ts/runtime-rpc'), require('rxjs'), require('@protobuf-ts/twirp-transport')) : | ||
typeof define === 'function' && define.amd ? define('@protobuf-ts/runtime-angular', ['exports', '@angular/core', '@angular/common', '@protobuf-ts/runtime', '@angular/common/http', '@protobuf-ts/runtime-rpc', 'rxjs', '@protobuf-ts/twirp-transport'], factory) : | ||
(global = global || self, factory((global['protobuf-ts'] = global['protobuf-ts'] || {}, global['protobuf-ts']['runtime-angular'] = {}), global.ng.core, global.ng.common, global.protobufTs.runtime, global.ng.common.http, global.protobufTs.runtimeRpc, global.rxjs, global.protobufTs.twirpTransport)); | ||
}(this, (function (exports, core, common, runtime, http, runtimeRpc, rxjs, twirpTransport) { 'use strict'; | ||
@@ -104,3 +104,590 @@ /** | ||
/*! ***************************************************************************** | ||
Copyright (c) Microsoft Corporation. | ||
Permission to use, copy, modify, and/or distribute this software for any | ||
purpose with or without fee is hereby granted. | ||
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH | ||
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY | ||
AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, | ||
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM | ||
LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR | ||
OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR | ||
PERFORMANCE OF THIS SOFTWARE. | ||
***************************************************************************** */ | ||
/* global Reflect, Promise */ | ||
var extendStatics = function (d, b) { | ||
extendStatics = Object.setPrototypeOf || | ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || | ||
function (d, b) { for (var p in b) | ||
if (Object.prototype.hasOwnProperty.call(b, p)) | ||
d[p] = b[p]; }; | ||
return extendStatics(d, b); | ||
}; | ||
function __extends(d, b) { | ||
extendStatics(d, b); | ||
function __() { this.constructor = d; } | ||
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); | ||
} | ||
var __assign = function () { | ||
__assign = Object.assign || function __assign(t) { | ||
for (var s, i = 1, n = arguments.length; i < n; i++) { | ||
s = arguments[i]; | ||
for (var p in s) | ||
if (Object.prototype.hasOwnProperty.call(s, p)) | ||
t[p] = s[p]; | ||
} | ||
return t; | ||
}; | ||
return __assign.apply(this, arguments); | ||
}; | ||
function __rest(s, e) { | ||
var t = {}; | ||
for (var p in s) | ||
if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0) | ||
t[p] = s[p]; | ||
if (s != null && typeof Object.getOwnPropertySymbols === "function") | ||
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) { | ||
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i])) | ||
t[p[i]] = s[p[i]]; | ||
} | ||
return t; | ||
} | ||
function __decorate(decorators, target, key, desc) { | ||
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d; | ||
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") | ||
r = Reflect.decorate(decorators, target, key, desc); | ||
else | ||
for (var i = decorators.length - 1; i >= 0; i--) | ||
if (d = decorators[i]) | ||
r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r; | ||
return c > 3 && r && Object.defineProperty(target, key, r), r; | ||
} | ||
function __param(paramIndex, decorator) { | ||
return function (target, key) { decorator(target, key, paramIndex); }; | ||
} | ||
function __metadata(metadataKey, metadataValue) { | ||
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") | ||
return Reflect.metadata(metadataKey, metadataValue); | ||
} | ||
function __awaiter(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()); | ||
}); | ||
} | ||
function __generator(thisArg, body) { | ||
var _ = { label: 0, sent: function () { if (t[0] & 1) | ||
throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g; | ||
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function () { return this; }), g; | ||
function verb(n) { return function (v) { return step([n, v]); }; } | ||
function step(op) { | ||
if (f) | ||
throw new TypeError("Generator is already executing."); | ||
while (_) | ||
try { | ||
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) | ||
return t; | ||
if (y = 0, t) | ||
op = [op[0] & 2, t.value]; | ||
switch (op[0]) { | ||
case 0: | ||
case 1: | ||
t = op; | ||
break; | ||
case 4: | ||
_.label++; | ||
return { value: op[1], done: false }; | ||
case 5: | ||
_.label++; | ||
y = op[1]; | ||
op = [0]; | ||
continue; | ||
case 7: | ||
op = _.ops.pop(); | ||
_.trys.pop(); | ||
continue; | ||
default: | ||
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { | ||
_ = 0; | ||
continue; | ||
} | ||
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { | ||
_.label = op[1]; | ||
break; | ||
} | ||
if (op[0] === 6 && _.label < t[1]) { | ||
_.label = t[1]; | ||
t = op; | ||
break; | ||
} | ||
if (t && _.label < t[2]) { | ||
_.label = t[2]; | ||
_.ops.push(op); | ||
break; | ||
} | ||
if (t[2]) | ||
_.ops.pop(); | ||
_.trys.pop(); | ||
continue; | ||
} | ||
op = body.call(thisArg, _); | ||
} | ||
catch (e) { | ||
op = [6, e]; | ||
y = 0; | ||
} | ||
finally { | ||
f = t = 0; | ||
} | ||
if (op[0] & 5) | ||
throw op[1]; | ||
return { value: op[0] ? op[1] : void 0, done: true }; | ||
} | ||
} | ||
var __createBinding = Object.create ? (function (o, m, k, k2) { | ||
if (k2 === undefined) | ||
k2 = k; | ||
Object.defineProperty(o, k2, { enumerable: true, get: function () { return m[k]; } }); | ||
}) : (function (o, m, k, k2) { | ||
if (k2 === undefined) | ||
k2 = k; | ||
o[k2] = m[k]; | ||
}); | ||
function __exportStar(m, o) { | ||
for (var p in m) | ||
if (p !== "default" && !Object.prototype.hasOwnProperty.call(o, p)) | ||
__createBinding(o, m, p); | ||
} | ||
function __values(o) { | ||
var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0; | ||
if (m) | ||
return m.call(o); | ||
if (o && typeof o.length === "number") | ||
return { | ||
next: function () { | ||
if (o && i >= o.length) | ||
o = void 0; | ||
return { value: o && o[i++], done: !o }; | ||
} | ||
}; | ||
throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined."); | ||
} | ||
function __read(o, n) { | ||
var m = typeof Symbol === "function" && o[Symbol.iterator]; | ||
if (!m) | ||
return o; | ||
var i = m.call(o), r, ar = [], e; | ||
try { | ||
while ((n === void 0 || n-- > 0) && !(r = i.next()).done) | ||
ar.push(r.value); | ||
} | ||
catch (error) { | ||
e = { error: error }; | ||
} | ||
finally { | ||
try { | ||
if (r && !r.done && (m = i["return"])) | ||
m.call(i); | ||
} | ||
finally { | ||
if (e) | ||
throw e.error; | ||
} | ||
} | ||
return ar; | ||
} | ||
function __spread() { | ||
for (var ar = [], i = 0; i < arguments.length; i++) | ||
ar = ar.concat(__read(arguments[i])); | ||
return ar; | ||
} | ||
function __spreadArrays() { | ||
for (var s = 0, i = 0, il = arguments.length; i < il; i++) | ||
s += arguments[i].length; | ||
for (var r = Array(s), k = 0, i = 0; i < il; i++) | ||
for (var a = arguments[i], j = 0, jl = a.length; j < jl; j++, k++) | ||
r[k] = a[j]; | ||
return r; | ||
} | ||
; | ||
function __await(v) { | ||
return this instanceof __await ? (this.v = v, this) : new __await(v); | ||
} | ||
function __asyncGenerator(thisArg, _arguments, generator) { | ||
if (!Symbol.asyncIterator) | ||
throw new TypeError("Symbol.asyncIterator is not defined."); | ||
var g = generator.apply(thisArg, _arguments || []), i, q = []; | ||
return i = {}, verb("next"), verb("throw"), verb("return"), i[Symbol.asyncIterator] = function () { return this; }, i; | ||
function verb(n) { if (g[n]) | ||
i[n] = function (v) { return new Promise(function (a, b) { q.push([n, v, a, b]) > 1 || resume(n, v); }); }; } | ||
function resume(n, v) { try { | ||
step(g[n](v)); | ||
} | ||
catch (e) { | ||
settle(q[0][3], e); | ||
} } | ||
function step(r) { r.value instanceof __await ? Promise.resolve(r.value.v).then(fulfill, reject) : settle(q[0][2], r); } | ||
function fulfill(value) { resume("next", value); } | ||
function reject(value) { resume("throw", value); } | ||
function settle(f, v) { if (f(v), q.shift(), q.length) | ||
resume(q[0][0], q[0][1]); } | ||
} | ||
function __asyncDelegator(o) { | ||
var i, p; | ||
return i = {}, verb("next"), verb("throw", function (e) { throw e; }), verb("return"), i[Symbol.iterator] = function () { return this; }, i; | ||
function verb(n, f) { i[n] = o[n] ? function (v) { return (p = !p) ? { value: __await(o[n](v)), done: n === "return" } : f ? f(v) : v; } : f; } | ||
} | ||
function __asyncValues(o) { | ||
if (!Symbol.asyncIterator) | ||
throw new TypeError("Symbol.asyncIterator is not defined."); | ||
var m = o[Symbol.asyncIterator], i; | ||
return m ? m.call(o) : (o = typeof __values === "function" ? __values(o) : o[Symbol.iterator](), i = {}, verb("next"), verb("throw"), verb("return"), i[Symbol.asyncIterator] = function () { return this; }, i); | ||
function verb(n) { i[n] = o[n] && function (v) { return new Promise(function (resolve, reject) { v = o[n](v), settle(resolve, reject, v.done, v.value); }); }; } | ||
function settle(resolve, reject, d, v) { Promise.resolve(v).then(function (v) { resolve({ value: v, done: d }); }, reject); } | ||
} | ||
function __makeTemplateObject(cooked, raw) { | ||
if (Object.defineProperty) { | ||
Object.defineProperty(cooked, "raw", { value: raw }); | ||
} | ||
else { | ||
cooked.raw = raw; | ||
} | ||
return cooked; | ||
} | ||
; | ||
var __setModuleDefault = Object.create ? (function (o, v) { | ||
Object.defineProperty(o, "default", { enumerable: true, value: v }); | ||
}) : function (o, v) { | ||
o["default"] = v; | ||
}; | ||
function __importStar(mod) { | ||
if (mod && mod.__esModule) | ||
return mod; | ||
var result = {}; | ||
if (mod != null) | ||
for (var k in mod) | ||
if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) | ||
__createBinding(result, mod, k); | ||
__setModuleDefault(result, mod); | ||
return result; | ||
} | ||
function __importDefault(mod) { | ||
return (mod && mod.__esModule) ? mod : { default: mod }; | ||
} | ||
function __classPrivateFieldGet(receiver, privateMap) { | ||
if (!privateMap.has(receiver)) { | ||
throw new TypeError("attempted to get private field on non-instance"); | ||
} | ||
return privateMap.get(receiver); | ||
} | ||
function __classPrivateFieldSet(receiver, privateMap, value) { | ||
if (!privateMap.has(receiver)) { | ||
throw new TypeError("attempted to set private field on non-instance"); | ||
} | ||
privateMap.set(receiver, value); | ||
return value; | ||
} | ||
/** | ||
* Injection token for `TwirpOptions` for the `TwirpAngularTransport`. | ||
* | ||
* For easy setup, just add TwirpAngularModule.forRoot() to the "imports" | ||
* of your `AppComponent`. The method takes a `TwirpOptions` argument. | ||
*/ | ||
var TWIRP_TRANSPORT_OPTIONS = new core.InjectionToken('TwirpOptions'); | ||
/** | ||
* Implements the Twirp protocol, supporting JSON or binary format on | ||
* the wire. See https://twitchtv.github.io/twirp/docs/spec_v5.html | ||
* | ||
* Uses `HttpClient` of @angular/common/http for requests. | ||
* | ||
* This is an injectable service. To inject, you must provide the | ||
* injection token TWIRP_TRANSPORT_OPTIONS. For easy integration, | ||
* just add TwirpAngularModule.forRoot() to the "imports" of your | ||
* `AppComponent`. The method takes a `TwirpOptions` argument. | ||
*/ | ||
var TwirpAngularTransport = /** @class */ (function () { | ||
function TwirpAngularTransport(options, http) { | ||
this.defaultOptions = options; | ||
this.http = http; | ||
} | ||
TwirpAngularTransport.prototype.mergeOptions = function (options) { | ||
return runtimeRpc.mergeExtendedRpcOptions(this.defaultOptions, options); | ||
}; | ||
TwirpAngularTransport.prototype.unary = function (service, method, input, options) { | ||
var _a; | ||
var opt = options, url = this.makeUrl(service, method, opt), requestBody = opt.sendJson ? method.I.toJsonString(input, opt) : method.I.toBinary(input).buffer, defHeader = new runtimeRpc.Deferred(), defMessage = new runtimeRpc.Deferred(), defStatus = new runtimeRpc.Deferred(), defTrailer = new runtimeRpc.Deferred(); | ||
// TODO | ||
var cancellationToken = new rxjs.Subject(); | ||
this.http.request('POST', url, { | ||
body: requestBody, | ||
headers: createTwirpRequestHeader(!!opt.sendJson, opt.meta), | ||
responseType: "arraybuffer", | ||
observe: "response", | ||
}).toPromise() | ||
.then(function (ngResponse) { | ||
defHeader.resolve(parseMetadataFromResponseHeaders(ngResponse.headers)); | ||
if (!ngResponse.body) | ||
throw new runtimeRpc.RpcError('premature end of response', twirpTransport.TwirpErrorCode[twirpTransport.TwirpErrorCode.dataloss]); | ||
if (!ngResponse.ok) | ||
throw parseTwirpErrorResponse(ngResponse.body); | ||
if (opt.sendJson) { | ||
try { | ||
return method.O.fromJsonString(runtime.utf8read(new Uint8Array(ngResponse.body)), opt); | ||
} | ||
catch (e) { | ||
throw new runtimeRpc.RpcError('unable to read response body as json', twirpTransport.TwirpErrorCode[twirpTransport.TwirpErrorCode.dataloss]); | ||
} | ||
} | ||
try { | ||
return method.O.fromBinary(new Uint8Array(ngResponse.body), opt); | ||
} | ||
catch (e) { | ||
throw new runtimeRpc.RpcError('unable to read response body', twirpTransport.TwirpErrorCode[twirpTransport.TwirpErrorCode.dataloss]); | ||
} | ||
}, function (reason) { | ||
// TODO AbortError | ||
if (reason instanceof Error && reason.name === 'AbortError') | ||
throw new runtimeRpc.RpcError(reason.message, twirpTransport.TwirpErrorCode[twirpTransport.TwirpErrorCode.cancelled]); | ||
if (reason instanceof http.HttpErrorResponse) { | ||
if (reason.error instanceof ArrayBuffer) | ||
throw parseTwirpErrorResponse(reason.error); | ||
throw new runtimeRpc.RpcError(reason.message, twirpTransport.TwirpErrorCode[twirpTransport.TwirpErrorCode.unknown]); | ||
} | ||
throw new runtimeRpc.RpcError("unknown error", twirpTransport.TwirpErrorCode[twirpTransport.TwirpErrorCode.unknown]); | ||
}) | ||
.then(function (message) { | ||
defMessage.resolve(message); | ||
defStatus.resolve({ code: 'OK', detail: '' }); | ||
defTrailer.resolve({}); | ||
}) | ||
.catch(function (reason) { | ||
// RpcErrors are thrown by us, everything else is an internal error | ||
var error = reason instanceof runtimeRpc.RpcError ? reason | ||
: new runtimeRpc.RpcError(reason instanceof Error ? reason.message : reason, twirpTransport.TwirpErrorCode[twirpTransport.TwirpErrorCode.internal]); | ||
defHeader.rejectPending(error); | ||
defMessage.rejectPending(error); | ||
defStatus.rejectPending(error); | ||
defTrailer.rejectPending(error); | ||
}); | ||
return new runtimeRpc.UnaryCall(service, method, (_a = opt.meta) !== null && _a !== void 0 ? _a : {}, input, defHeader.promise, defMessage.promise, defStatus.promise, defTrailer.promise, function () { return cancellationToken.next(); }); | ||
}; | ||
/** | ||
* Create an URI for a RPC call. | ||
* | ||
* Takes the `baseUrl` option and appends: | ||
* - slash "/" | ||
* - package name | ||
* - dot "." | ||
* - service name | ||
* - slash "/" | ||
* - method name | ||
* | ||
* If the service was declared without a package, the package name and dot | ||
* are omitted. | ||
* | ||
* The method name is CamelCased just as it would be in Go, unless the | ||
* option `useProtoMethodName` is `true`. | ||
*/ | ||
TwirpAngularTransport.prototype.makeUrl = function (service, method, options) { | ||
var base = options.baseUrl; | ||
if (base.endsWith('/')) | ||
base = base.substring(0, base.length - 1); | ||
var serviceName = service.typeName; | ||
if (serviceName.startsWith('.')) | ||
serviceName = serviceName.substring(1); | ||
var methodName = method.name; | ||
if (options.useProtoMethodName !== true) { | ||
methodName = runtime.lowerCamelCase(methodName); | ||
methodName = methodName.substring(0, 1).toUpperCase() + methodName.substring(1); | ||
} | ||
return base + "/" + serviceName + "/" + methodName; | ||
}; | ||
TwirpAngularTransport.prototype.clientStreaming = function (service, method, options) { | ||
throw new runtimeRpc.RpcError('Client streaming is not supported by Twirp', twirpTransport.TwirpErrorCode[twirpTransport.TwirpErrorCode.unimplemented]); | ||
}; | ||
TwirpAngularTransport.prototype.duplex = function (service, method, options) { | ||
throw new runtimeRpc.RpcError('Duplex streaming is not supported by Twirp', twirpTransport.TwirpErrorCode[twirpTransport.TwirpErrorCode.unimplemented]); | ||
}; | ||
TwirpAngularTransport.prototype.serverStreaming = function (service, method, input, options) { | ||
throw new runtimeRpc.RpcError('Server streaming is not supported by Twirp', twirpTransport.TwirpErrorCode[twirpTransport.TwirpErrorCode.unimplemented]); | ||
}; | ||
return TwirpAngularTransport; | ||
}()); | ||
TwirpAngularTransport.decorators = [ | ||
{ type: core.Injectable } | ||
]; | ||
TwirpAngularTransport.ctorParameters = function () { return [ | ||
{ type: undefined, decorators: [{ type: core.Inject, args: [TWIRP_TRANSPORT_OPTIONS,] }] }, | ||
{ type: http.HttpClient } | ||
]; }; | ||
/** | ||
* Create Angular headers for a Twirp request. | ||
*/ | ||
function createTwirpRequestHeader(sendJson, meta) { | ||
var e_1, _b, e_2, _c; | ||
var headers = new http.HttpHeaders(); | ||
// add meta as headers | ||
if (meta) { | ||
try { | ||
for (var _d = __values(Object.entries(meta)), _e = _d.next(); !_e.done; _e = _d.next()) { | ||
var _f = __read(_e.value, 2), k = _f[0], v = _f[1]; | ||
if (typeof v === "string") | ||
headers = headers.append(k, v); | ||
else | ||
try { | ||
for (var v_1 = (e_2 = void 0, __values(v)), v_1_1 = v_1.next(); !v_1_1.done; v_1_1 = v_1.next()) { | ||
var i = v_1_1.value; | ||
headers = headers.append(k, i); | ||
} | ||
} | ||
catch (e_2_1) { e_2 = { error: e_2_1 }; } | ||
finally { | ||
try { | ||
if (v_1_1 && !v_1_1.done && (_c = v_1.return)) _c.call(v_1); | ||
} | ||
finally { if (e_2) throw e_2.error; } | ||
} | ||
} | ||
} | ||
catch (e_1_1) { e_1 = { error: e_1_1 }; } | ||
finally { | ||
try { | ||
if (_e && !_e.done && (_b = _d.return)) _b.call(_d); | ||
} | ||
finally { if (e_1) throw e_1.error; } | ||
} | ||
} | ||
// set standard headers (possibly overwriting meta) | ||
headers = headers.set('Content-Type', sendJson ? "application/json" : "application/protobuf"); | ||
headers = headers.set('Accept', sendJson ? "application/json" : "application/protobuf, application/json"); | ||
return headers; | ||
} | ||
/** | ||
* Parse Twirp error message from JSON (given as array buffer) and create | ||
* RpcError from the Twirp error. | ||
* | ||
* see https://twitchtv.github.io/twirp/docs/spec_v5.html | ||
*/ | ||
function parseTwirpErrorResponse(data) { | ||
var e_3, _b; | ||
var json; | ||
try { | ||
json = JSON.parse(runtime.utf8read(new Uint8Array(data))); | ||
} | ||
catch (e) { | ||
return new runtimeRpc.RpcError('cannot read twirp error response', twirpTransport.TwirpErrorCode[twirpTransport.TwirpErrorCode.internal]); | ||
} | ||
if (!runtime.isJsonObject(json) || typeof json.code !== "string" || typeof json.msg !== "string") | ||
return new runtimeRpc.RpcError('cannot read twirp error response', twirpTransport.TwirpErrorCode[twirpTransport.TwirpErrorCode.internal]); | ||
var meta = {}; | ||
if (runtime.isJsonObject(json.meta)) { | ||
try { | ||
for (var _c = __values(Object.entries(json.meta)), _d = _c.next(); !_d.done; _d = _c.next()) { | ||
var _e = __read(_d.value, 2), k = _e[0], v = _e[1]; | ||
if (typeof v === "string") | ||
meta[k] = v; | ||
} | ||
} | ||
catch (e_3_1) { e_3 = { error: e_3_1 }; } | ||
finally { | ||
try { | ||
if (_d && !_d.done && (_b = _c.return)) _b.call(_c); | ||
} | ||
finally { if (e_3) throw e_3.error; } | ||
} | ||
} | ||
return new runtimeRpc.RpcError(json.msg, json.code, meta); | ||
} | ||
/** | ||
* Parses Angular response headers to RpcMetaData. | ||
* Drops the headers Content-Type and Content-Length. | ||
*/ | ||
function parseMetadataFromResponseHeaders(headers) { | ||
var e_4, _b, e_5, _c; | ||
var meta = {}; | ||
try { | ||
for (var _d = __values(headers.keys()), _e = _d.next(); !_e.done; _e = _d.next()) { | ||
var key = _e.value; | ||
if (key.toLowerCase() === 'content-type') | ||
continue; | ||
if (key.toLowerCase() === 'content-length') | ||
continue; | ||
var values = headers.getAll(key); | ||
if (!values) | ||
continue; | ||
try { | ||
for (var values_1 = (e_5 = void 0, __values(values)), values_1_1 = values_1.next(); !values_1_1.done; values_1_1 = values_1.next()) { | ||
var val = values_1_1.value; | ||
if (meta.hasOwnProperty(key)) | ||
meta[key].push(val); | ||
else | ||
meta[key] = val; | ||
} | ||
} | ||
catch (e_5_1) { e_5 = { error: e_5_1 }; } | ||
finally { | ||
try { | ||
if (values_1_1 && !values_1_1.done && (_c = values_1.return)) _c.call(values_1); | ||
} | ||
finally { if (e_5) throw e_5.error; } | ||
} | ||
} | ||
} | ||
catch (e_4_1) { e_4 = { error: e_4_1 }; } | ||
finally { | ||
try { | ||
if (_e && !_e.done && (_b = _d.return)) _b.call(_d); | ||
} | ||
finally { if (e_4) throw e_4.error; } | ||
} | ||
return meta; | ||
} | ||
/** | ||
* Provides the Twirp transport `TwirpAngularTransport`, which uses | ||
* `HttpClient` of @angular/common/http for requests. | ||
*/ | ||
var TwirpAngularModule = /** @class */ (function () { | ||
function TwirpAngularModule() { | ||
} | ||
TwirpAngularModule.forRoot = function (twirpOptions) { | ||
return { | ||
ngModule: TwirpAngularModule, | ||
providers: [ | ||
{ provide: TWIRP_TRANSPORT_OPTIONS, useValue: twirpOptions }, | ||
{ provide: RPC_TRANSPORT, useExisting: TwirpAngularTransport } | ||
] | ||
}; | ||
}; | ||
return TwirpAngularModule; | ||
}()); | ||
TwirpAngularModule.decorators = [ | ||
{ type: core.NgModule, args: [{ | ||
imports: [ | ||
http.HttpClientModule, | ||
], | ||
providers: [ | ||
TwirpAngularTransport, | ||
] | ||
},] } | ||
]; | ||
/** | ||
* Generated bundle index. Do not edit. | ||
@@ -112,2 +699,5 @@ */ | ||
exports.RPC_TRANSPORT = RPC_TRANSPORT; | ||
exports.TWIRP_TRANSPORT_OPTIONS = TWIRP_TRANSPORT_OPTIONS; | ||
exports.TwirpAngularModule = TwirpAngularModule; | ||
exports.TwirpAngularTransport = TwirpAngularTransport; | ||
@@ -114,0 +704,0 @@ Object.defineProperty(exports, '__esModule', { value: true }); |
@@ -1,2 +0,16 @@ | ||
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports,require("@angular/core"),require("@angular/common"),require("@protobuf-ts/runtime")):"function"==typeof define&&define.amd?define("@protobuf-ts/runtime-angular",["exports","@angular/core","@angular/common","@protobuf-ts/runtime"],t):t(((e=e||self)["protobuf-ts"]=e["protobuf-ts"]||{},e["protobuf-ts"]["runtime-angular"]={}),e.ng.core,e.ng.common,e.protobufts.runtime)}(this,(function(e,t,n,r){"use strict";var o=function(){function e(e){this.locale=e}return e.prototype.transform=function(e,t,o,f){if(void 0===t&&(t="mediumDate"),null==e||""===e||e!=e)return null;if(function(e){if("object"!=typeof e||null===e)return!1;if("number"!=typeof e.year)return!1;if("number"!=typeof e.month)return!1;if("number"!=typeof e.day)return!1;if("number"!=typeof e.hours)return!1;if("number"!=typeof e.minutes)return!1;if("number"!=typeof e.seconds)return!1;if("number"!=typeof e.nanos)return!1;if(!r.isOneofGroup(e.timeOffset))return!1;var t=e.timeOffset.oneofKind;return(void 0===t||"timeZone"===t||"utcOffset"===t)&&(void 0===e.timeOffset.utcOffset||function(e){if("object"!=typeof e||null===e)return!1;if("number"!=typeof e.nanos)return!1;var t=typeof e.seconds;return"number"===t||"string"===t||"bigint"===t}(e.timeOffset.utcOffset))}(e)){var u=new Date(e.year,e.month-1,e.day,e.hours,e.minutes,e.seconds,e.nanos/1e3);if(e.timeOffset){if("timeZone"===e.timeOffset.oneofKind)throw new Error("Do not understand IANA time zone. Cannot convert to javascript Date.");if("utcOffset"===e.timeOffset.oneofKind){var i=r.PbLong.from(e.timeOffset.utcOffset.seconds).toNumber()/60,s=u.getTimezoneOffset();u.setMinutes(u.getMinutes()+(i-s))}}return n.formatDate(u,t,null!=f?f:this.locale,o)}if(function(e){if("object"!=typeof e||null===e)return!1;if("number"!=typeof e.nanos)return!1;var t=typeof e.seconds;return"number"===t||"string"===t||"bigint"===t}(e)){var a=r.PbLong.from(e.seconds).toNumber();u=new Date(1e3*a+Math.ceil(e.nanos/1e6));return n.formatDate(u,t,null!=f?f:this.locale,o)}return n.formatDate(e,t,null!=f?f:this.locale,o)},e}();o.decorators=[{type:t.Pipe,args:[{name:"date",pure:!0}]}],o.ctorParameters=function(){return[{type:String,decorators:[{type:t.Inject,args:[t.LOCALE_ID]}]}]};var f=function(){};f.decorators=[{type:t.NgModule,args:[{declarations:[o],exports:[o]}]}];var u=new t.InjectionToken("RpcTransport");e.PbDatePipe=o,e.PbDatePipeModule=f,e.RPC_TRANSPORT=u,Object.defineProperty(e,"__esModule",{value:!0})})); | ||
!function(r,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports,require("@angular/core"),require("@angular/common"),require("@protobuf-ts/runtime"),require("@angular/common/http"),require("@protobuf-ts/runtime-rpc"),require("rxjs"),require("@protobuf-ts/twirp-transport")):"function"==typeof define&&define.amd?define("@protobuf-ts/runtime-angular",["exports","@angular/core","@angular/common","@protobuf-ts/runtime","@angular/common/http","@protobuf-ts/runtime-rpc","rxjs","@protobuf-ts/twirp-transport"],e):e(((r=r||self)["protobuf-ts"]=r["protobuf-ts"]||{},r["protobuf-ts"]["runtime-angular"]={}),r.ng.core,r.ng.common,r.protobufTs.runtime,r.ng.common.http,r.protobufTs.runtimeRpc,r.rxjs,r.protobufTs.twirpTransport)}(this,(function(r,e,t,o,n,i,a,s){"use strict";var u=function(){function r(r){this.locale=r}return r.prototype.transform=function(r,e,n,i){if(void 0===e&&(e="mediumDate"),null==r||""===r||r!=r)return null;if(function(r){if("object"!=typeof r||null===r)return!1;if("number"!=typeof r.year)return!1;if("number"!=typeof r.month)return!1;if("number"!=typeof r.day)return!1;if("number"!=typeof r.hours)return!1;if("number"!=typeof r.minutes)return!1;if("number"!=typeof r.seconds)return!1;if("number"!=typeof r.nanos)return!1;if(!o.isOneofGroup(r.timeOffset))return!1;var e=r.timeOffset.oneofKind;return(void 0===e||"timeZone"===e||"utcOffset"===e)&&(void 0===r.timeOffset.utcOffset||function(r){if("object"!=typeof r||null===r)return!1;if("number"!=typeof r.nanos)return!1;var e=typeof r.seconds;return"number"===e||"string"===e||"bigint"===e}(r.timeOffset.utcOffset))}(r)){var a=new Date(r.year,r.month-1,r.day,r.hours,r.minutes,r.seconds,r.nanos/1e3);if(r.timeOffset){if("timeZone"===r.timeOffset.oneofKind)throw new Error("Do not understand IANA time zone. Cannot convert to javascript Date.");if("utcOffset"===r.timeOffset.oneofKind){var s=o.PbLong.from(r.timeOffset.utcOffset.seconds).toNumber()/60,u=a.getTimezoneOffset();a.setMinutes(a.getMinutes()+(s-u))}}return t.formatDate(a,e,null!=i?i:this.locale,n)}if(function(r){if("object"!=typeof r||null===r)return!1;if("number"!=typeof r.nanos)return!1;var e=typeof r.seconds;return"number"===e||"string"===e||"bigint"===e}(r)){var f=o.PbLong.from(r.seconds).toNumber();a=new Date(1e3*f+Math.ceil(r.nanos/1e6));return t.formatDate(a,e,null!=i?i:this.locale,n)}return t.formatDate(r,e,null!=i?i:this.locale,n)},r}();u.decorators=[{type:e.Pipe,args:[{name:"date",pure:!0}]}],u.ctorParameters=function(){return[{type:String,decorators:[{type:e.Inject,args:[e.LOCALE_ID]}]}]};var f=function(){};f.decorators=[{type:e.NgModule,args:[{declarations:[u],exports:[u]}]}];var p=new e.InjectionToken("RpcTransport"); | ||
/*! ***************************************************************************** | ||
Copyright (c) Microsoft Corporation. | ||
Permission to use, copy, modify, and/or distribute this software for any | ||
purpose with or without fee is hereby granted. | ||
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH | ||
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY | ||
AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, | ||
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM | ||
LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR | ||
OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR | ||
PERFORMANCE OF THIS SOFTWARE. | ||
***************************************************************************** */Object.create;function c(r){var e="function"==typeof Symbol&&Symbol.iterator,t=e&&r[e],o=0;if(t)return t.call(r);if(r&&"number"==typeof r.length)return{next:function(){return r&&o>=r.length&&(r=void 0),{value:r&&r[o++],done:!r}}};throw new TypeError(e?"Object is not iterable.":"Symbol.iterator is not defined.")}function l(r,e){var t="function"==typeof Symbol&&r[Symbol.iterator];if(!t)return r;var o,n,i=t.call(r),a=[];try{for(;(void 0===e||e-- >0)&&!(o=i.next()).done;)a.push(o.value)}catch(r){n={error:r}}finally{try{o&&!o.done&&(t=i.return)&&t.call(i)}finally{if(n)throw n.error}}return a}Object.create;var d=new e.InjectionToken("TwirpOptions"),m=function(){function r(r,e){this.defaultOptions=r,this.http=e}return r.prototype.mergeOptions=function(r){return i.mergeExtendedRpcOptions(this.defaultOptions,r)},r.prototype.unary=function(r,e,t,u){var f,p=u,l=this.makeUrl(r,e,p),d=p.sendJson?e.I.toJsonString(t,p):e.I.toBinary(t).buffer,m=new i.Deferred,b=new i.Deferred,v=new i.Deferred,h=new i.Deferred,g=new a.Subject;return this.http.request("POST",l,{body:d,headers:y(!!p.sendJson,p.meta),responseType:"arraybuffer",observe:"response"}).toPromise().then((function(r){if(m.resolve(function(r){var e,t,o,n,i={};try{for(var a=c(r.keys()),s=a.next();!s.done;s=a.next()){var u=s.value;if("content-type"!==u.toLowerCase()&&"content-length"!==u.toLowerCase()){var f=r.getAll(u);if(f)try{for(var p=(o=void 0,c(f)),l=p.next();!l.done;l=p.next()){var d=l.value;i.hasOwnProperty(u)?i[u].push(d):i[u]=d}}catch(r){o={error:r}}finally{try{l&&!l.done&&(n=p.return)&&n.call(p)}finally{if(o)throw o.error}}}}}catch(r){e={error:r}}finally{try{s&&!s.done&&(t=a.return)&&t.call(a)}finally{if(e)throw e.error}}return i}(r.headers)),!r.body)throw new i.RpcError("premature end of response",s.TwirpErrorCode[s.TwirpErrorCode.dataloss]);if(!r.ok)throw w(r.body);if(p.sendJson)try{return e.O.fromJsonString(o.utf8read(new Uint8Array(r.body)),p)}catch(r){throw new i.RpcError("unable to read response body as json",s.TwirpErrorCode[s.TwirpErrorCode.dataloss])}try{return e.O.fromBinary(new Uint8Array(r.body),p)}catch(r){throw new i.RpcError("unable to read response body",s.TwirpErrorCode[s.TwirpErrorCode.dataloss])}}),(function(r){if(r instanceof Error&&"AbortError"===r.name)throw new i.RpcError(r.message,s.TwirpErrorCode[s.TwirpErrorCode.cancelled]);if(r instanceof n.HttpErrorResponse){if(r.error instanceof ArrayBuffer)throw w(r.error);throw new i.RpcError(r.message,s.TwirpErrorCode[s.TwirpErrorCode.unknown])}throw new i.RpcError("unknown error",s.TwirpErrorCode[s.TwirpErrorCode.unknown])})).then((function(r){b.resolve(r),v.resolve({code:"OK",detail:""}),h.resolve({})})).catch((function(r){var e=r instanceof i.RpcError?r:new i.RpcError(r instanceof Error?r.message:r,s.TwirpErrorCode[s.TwirpErrorCode.internal]);m.rejectPending(e),b.rejectPending(e),v.rejectPending(e),h.rejectPending(e)})),new i.UnaryCall(r,e,null!==(f=p.meta)&&void 0!==f?f:{},t,m.promise,b.promise,v.promise,h.promise,(function(){return g.next()}))},r.prototype.makeUrl=function(r,e,t){var n=t.baseUrl;n.endsWith("/")&&(n=n.substring(0,n.length-1));var i=r.typeName;i.startsWith(".")&&(i=i.substring(1));var a=e.name;return!0!==t.useProtoMethodName&&(a=(a=o.lowerCamelCase(a)).substring(0,1).toUpperCase()+a.substring(1)),n+"/"+i+"/"+a},r.prototype.clientStreaming=function(r,e,t){throw new i.RpcError("Client streaming is not supported by Twirp",s.TwirpErrorCode[s.TwirpErrorCode.unimplemented])},r.prototype.duplex=function(r,e,t){throw new i.RpcError("Duplex streaming is not supported by Twirp",s.TwirpErrorCode[s.TwirpErrorCode.unimplemented])},r.prototype.serverStreaming=function(r,e,t,o){throw new i.RpcError("Server streaming is not supported by Twirp",s.TwirpErrorCode[s.TwirpErrorCode.unimplemented])},r}();function y(r,e){var t,o,i,a,s=new n.HttpHeaders;if(e)try{for(var u=c(Object.entries(e)),f=u.next();!f.done;f=u.next()){var p=l(f.value,2),d=p[0],m=p[1];if("string"==typeof m)s=s.append(d,m);else try{for(var y=(i=void 0,c(m)),w=y.next();!w.done;w=y.next()){var b=w.value;s=s.append(d,b)}}catch(r){i={error:r}}finally{try{w&&!w.done&&(a=y.return)&&a.call(y)}finally{if(i)throw i.error}}}}catch(r){t={error:r}}finally{try{f&&!f.done&&(o=u.return)&&o.call(u)}finally{if(t)throw t.error}}return s=(s=s.set("Content-Type",r?"application/json":"application/protobuf")).set("Accept",r?"application/json":"application/protobuf, application/json")}function w(r){var e,t,n;try{n=JSON.parse(o.utf8read(new Uint8Array(r)))}catch(r){return new i.RpcError("cannot read twirp error response",s.TwirpErrorCode[s.TwirpErrorCode.internal])}if(!o.isJsonObject(n)||"string"!=typeof n.code||"string"!=typeof n.msg)return new i.RpcError("cannot read twirp error response",s.TwirpErrorCode[s.TwirpErrorCode.internal]);var a={};if(o.isJsonObject(n.meta))try{for(var u=c(Object.entries(n.meta)),f=u.next();!f.done;f=u.next()){var p=l(f.value,2),d=p[0],m=p[1];"string"==typeof m&&(a[d]=m)}}catch(r){e={error:r}}finally{try{f&&!f.done&&(t=u.return)&&t.call(u)}finally{if(e)throw e.error}}return new i.RpcError(n.msg,n.code,a)}m.decorators=[{type:e.Injectable}],m.ctorParameters=function(){return[{type:void 0,decorators:[{type:e.Inject,args:[d]}]},{type:n.HttpClient}]};var b=function(){function r(){}return r.forRoot=function(e){return{ngModule:r,providers:[{provide:d,useValue:e},{provide:p,useExisting:m}]}},r}();b.decorators=[{type:e.NgModule,args:[{imports:[n.HttpClientModule],providers:[m]}]}],r.PbDatePipe=u,r.PbDatePipeModule=f,r.RPC_TRANSPORT=p,r.TWIRP_TRANSPORT_OPTIONS=d,r.TwirpAngularModule=b,r.TwirpAngularTransport=m,Object.defineProperty(r,"__esModule",{value:!0})})); | ||
//# sourceMappingURL=protobuf-ts-runtime-angular.umd.min.js.map |
export { PbDatePipe } from "./lib/pb-date-pipe"; | ||
export { PbDatePipeModule } from "./lib/pb-date-pipe.module"; | ||
export { RPC_TRANSPORT } from "./lib/rpc-transport"; | ||
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoicHVibGljLWFwaS5qcyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbIi4uLy4uLy4uL3Byb2plY3RzL3J1bnRpbWUtYW5ndWxhci9zcmMvcHVibGljLWFwaS50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiQUFBQSxPQUFPLEVBQUMsVUFBVSxFQUFDLE1BQU0sb0JBQW9CLENBQUM7QUFDOUMsT0FBTyxFQUFDLGdCQUFnQixFQUFDLE1BQU0sMkJBQTJCLENBQUM7QUFDM0QsT0FBTyxFQUFDLGFBQWEsRUFBQyxNQUFNLHFCQUFxQixDQUFDIiwic291cmNlc0NvbnRlbnQiOlsiZXhwb3J0IHtQYkRhdGVQaXBlfSBmcm9tIFwiLi9saWIvcGItZGF0ZS1waXBlXCI7XG5leHBvcnQge1BiRGF0ZVBpcGVNb2R1bGV9IGZyb20gXCIuL2xpYi9wYi1kYXRlLXBpcGUubW9kdWxlXCI7XG5leHBvcnQge1JQQ19UUkFOU1BPUlR9IGZyb20gXCIuL2xpYi9ycGMtdHJhbnNwb3J0XCI7XG4iXX0= | ||
export { TwirpAngularTransport } from "./lib/twirp-angular-transport"; | ||
export { TwirpAngularModule } from "./lib/twirp-angular.module"; | ||
export { TWIRP_TRANSPORT_OPTIONS } from "./lib/twirp-transport-options"; | ||
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoicHVibGljLWFwaS5qcyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbIi4uLy4uLy4uL3Byb2plY3RzL3J1bnRpbWUtYW5ndWxhci9zcmMvcHVibGljLWFwaS50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiQUFBQSxPQUFPLEVBQUUsVUFBVSxFQUFFLE1BQU0sb0JBQW9CLENBQUM7QUFDaEQsT0FBTyxFQUFFLGdCQUFnQixFQUFFLE1BQU0sMkJBQTJCLENBQUM7QUFDN0QsT0FBTyxFQUFFLGFBQWEsRUFBRSxNQUFNLHFCQUFxQixDQUFDO0FBQ3BELE9BQU8sRUFBRSxxQkFBcUIsRUFBRSxNQUFNLCtCQUErQixDQUFBO0FBQ3JFLE9BQU8sRUFBRSxrQkFBa0IsRUFBRSxNQUFNLDRCQUE0QixDQUFDO0FBQ2hFLE9BQU8sRUFBRSx1QkFBdUIsRUFBRSxNQUFNLCtCQUErQixDQUFDIiwic291cmNlc0NvbnRlbnQiOlsiZXhwb3J0IHsgUGJEYXRlUGlwZSB9IGZyb20gXCIuL2xpYi9wYi1kYXRlLXBpcGVcIjtcbmV4cG9ydCB7IFBiRGF0ZVBpcGVNb2R1bGUgfSBmcm9tIFwiLi9saWIvcGItZGF0ZS1waXBlLm1vZHVsZVwiO1xuZXhwb3J0IHsgUlBDX1RSQU5TUE9SVCB9IGZyb20gXCIuL2xpYi9ycGMtdHJhbnNwb3J0XCI7XG5leHBvcnQgeyBUd2lycEFuZ3VsYXJUcmFuc3BvcnQgfSBmcm9tIFwiLi9saWIvdHdpcnAtYW5ndWxhci10cmFuc3BvcnRcIlxuZXhwb3J0IHsgVHdpcnBBbmd1bGFyTW9kdWxlIH0gZnJvbSBcIi4vbGliL3R3aXJwLWFuZ3VsYXIubW9kdWxlXCI7XG5leHBvcnQgeyBUV0lSUF9UUkFOU1BPUlRfT1BUSU9OUyB9IGZyb20gXCIuL2xpYi90d2lycC10cmFuc3BvcnQtb3B0aW9uc1wiO1xuXG4iXX0= |
@@ -1,4 +0,8 @@ | ||
import { Pipe, Inject, LOCALE_ID, NgModule, InjectionToken } from '@angular/core'; | ||
import { Pipe, Inject, LOCALE_ID, NgModule, InjectionToken, Injectable } from '@angular/core'; | ||
import { formatDate } from '@angular/common'; | ||
import { PbLong, isOneofGroup } from '@protobuf-ts/runtime'; | ||
import { PbLong, isOneofGroup, utf8read, lowerCamelCase, isJsonObject } from '@protobuf-ts/runtime'; | ||
import { HttpErrorResponse, HttpClient, HttpHeaders, HttpClientModule } from '@angular/common/http'; | ||
import { mergeExtendedRpcOptions, Deferred, RpcError, UnaryCall } from '@protobuf-ts/runtime-rpc'; | ||
import { Subject } from 'rxjs'; | ||
import { TwirpErrorCode } from '@protobuf-ts/twirp-transport'; | ||
@@ -98,6 +102,234 @@ /** | ||
/** | ||
* Injection token for `TwirpOptions` for the `TwirpAngularTransport`. | ||
* | ||
* For easy setup, just add TwirpAngularModule.forRoot() to the "imports" | ||
* of your `AppComponent`. The method takes a `TwirpOptions` argument. | ||
*/ | ||
const TWIRP_TRANSPORT_OPTIONS = new InjectionToken('TwirpOptions'); | ||
/** | ||
* Implements the Twirp protocol, supporting JSON or binary format on | ||
* the wire. See https://twitchtv.github.io/twirp/docs/spec_v5.html | ||
* | ||
* Uses `HttpClient` of @angular/common/http for requests. | ||
* | ||
* This is an injectable service. To inject, you must provide the | ||
* injection token TWIRP_TRANSPORT_OPTIONS. For easy integration, | ||
* just add TwirpAngularModule.forRoot() to the "imports" of your | ||
* `AppComponent`. The method takes a `TwirpOptions` argument. | ||
*/ | ||
class TwirpAngularTransport { | ||
constructor(options, http) { | ||
this.defaultOptions = options; | ||
this.http = http; | ||
} | ||
mergeOptions(options) { | ||
return mergeExtendedRpcOptions(this.defaultOptions, options); | ||
} | ||
unary(service, method, input, options) { | ||
var _a; | ||
let opt = options, url = this.makeUrl(service, method, opt), requestBody = opt.sendJson ? method.I.toJsonString(input, opt) : method.I.toBinary(input).buffer, defHeader = new Deferred(), defMessage = new Deferred(), defStatus = new Deferred(), defTrailer = new Deferred(); | ||
// TODO | ||
let cancellationToken = new Subject(); | ||
this.http.request('POST', url, { | ||
body: requestBody, | ||
headers: createTwirpRequestHeader(!!opt.sendJson, opt.meta), | ||
responseType: "arraybuffer", | ||
observe: "response", | ||
}).toPromise() | ||
.then(ngResponse => { | ||
defHeader.resolve(parseMetadataFromResponseHeaders(ngResponse.headers)); | ||
if (!ngResponse.body) | ||
throw new RpcError('premature end of response', TwirpErrorCode[TwirpErrorCode.dataloss]); | ||
if (!ngResponse.ok) | ||
throw parseTwirpErrorResponse(ngResponse.body); | ||
if (opt.sendJson) { | ||
try { | ||
return method.O.fromJsonString(utf8read(new Uint8Array(ngResponse.body)), opt); | ||
} | ||
catch (e) { | ||
throw new RpcError('unable to read response body as json', TwirpErrorCode[TwirpErrorCode.dataloss]); | ||
} | ||
} | ||
try { | ||
return method.O.fromBinary(new Uint8Array(ngResponse.body), opt); | ||
} | ||
catch (e) { | ||
throw new RpcError('unable to read response body', TwirpErrorCode[TwirpErrorCode.dataloss]); | ||
} | ||
}, reason => { | ||
// TODO AbortError | ||
if (reason instanceof Error && reason.name === 'AbortError') | ||
throw new RpcError(reason.message, TwirpErrorCode[TwirpErrorCode.cancelled]); | ||
if (reason instanceof HttpErrorResponse) { | ||
if (reason.error instanceof ArrayBuffer) | ||
throw parseTwirpErrorResponse(reason.error); | ||
throw new RpcError(reason.message, TwirpErrorCode[TwirpErrorCode.unknown]); | ||
} | ||
throw new RpcError("unknown error", TwirpErrorCode[TwirpErrorCode.unknown]); | ||
}) | ||
.then(message => { | ||
defMessage.resolve(message); | ||
defStatus.resolve({ code: 'OK', detail: '' }); | ||
defTrailer.resolve({}); | ||
}) | ||
.catch((reason) => { | ||
// RpcErrors are thrown by us, everything else is an internal error | ||
let error = reason instanceof RpcError ? reason | ||
: new RpcError(reason instanceof Error ? reason.message : reason, TwirpErrorCode[TwirpErrorCode.internal]); | ||
defHeader.rejectPending(error); | ||
defMessage.rejectPending(error); | ||
defStatus.rejectPending(error); | ||
defTrailer.rejectPending(error); | ||
}); | ||
return new UnaryCall(service, method, (_a = opt.meta) !== null && _a !== void 0 ? _a : {}, input, defHeader.promise, defMessage.promise, defStatus.promise, defTrailer.promise, () => cancellationToken.next()); | ||
} | ||
/** | ||
* Create an URI for a RPC call. | ||
* | ||
* Takes the `baseUrl` option and appends: | ||
* - slash "/" | ||
* - package name | ||
* - dot "." | ||
* - service name | ||
* - slash "/" | ||
* - method name | ||
* | ||
* If the service was declared without a package, the package name and dot | ||
* are omitted. | ||
* | ||
* The method name is CamelCased just as it would be in Go, unless the | ||
* option `useProtoMethodName` is `true`. | ||
*/ | ||
makeUrl(service, method, options) { | ||
let base = options.baseUrl; | ||
if (base.endsWith('/')) | ||
base = base.substring(0, base.length - 1); | ||
let serviceName = service.typeName; | ||
if (serviceName.startsWith('.')) | ||
serviceName = serviceName.substring(1); | ||
let methodName = method.name; | ||
if (options.useProtoMethodName !== true) { | ||
methodName = lowerCamelCase(methodName); | ||
methodName = methodName.substring(0, 1).toUpperCase() + methodName.substring(1); | ||
} | ||
return `${base}/${serviceName}/${methodName}`; | ||
} | ||
clientStreaming(service, method, options) { | ||
throw new RpcError('Client streaming is not supported by Twirp', TwirpErrorCode[TwirpErrorCode.unimplemented]); | ||
} | ||
duplex(service, method, options) { | ||
throw new RpcError('Duplex streaming is not supported by Twirp', TwirpErrorCode[TwirpErrorCode.unimplemented]); | ||
} | ||
serverStreaming(service, method, input, options) { | ||
throw new RpcError('Server streaming is not supported by Twirp', TwirpErrorCode[TwirpErrorCode.unimplemented]); | ||
} | ||
} | ||
TwirpAngularTransport.decorators = [ | ||
{ type: Injectable } | ||
]; | ||
TwirpAngularTransport.ctorParameters = () => [ | ||
{ type: undefined, decorators: [{ type: Inject, args: [TWIRP_TRANSPORT_OPTIONS,] }] }, | ||
{ type: HttpClient } | ||
]; | ||
/** | ||
* Create Angular headers for a Twirp request. | ||
*/ | ||
function createTwirpRequestHeader(sendJson, meta) { | ||
let headers = new HttpHeaders(); | ||
// add meta as headers | ||
if (meta) { | ||
for (let [k, v] of Object.entries(meta)) { | ||
if (typeof v === "string") | ||
headers = headers.append(k, v); | ||
else | ||
for (let i of v) | ||
headers = headers.append(k, i); | ||
} | ||
} | ||
// set standard headers (possibly overwriting meta) | ||
headers = headers.set('Content-Type', sendJson ? "application/json" : "application/protobuf"); | ||
headers = headers.set('Accept', sendJson ? "application/json" : "application/protobuf, application/json"); | ||
return headers; | ||
} | ||
/** | ||
* Parse Twirp error message from JSON (given as array buffer) and create | ||
* RpcError from the Twirp error. | ||
* | ||
* see https://twitchtv.github.io/twirp/docs/spec_v5.html | ||
*/ | ||
function parseTwirpErrorResponse(data) { | ||
let json; | ||
try { | ||
json = JSON.parse(utf8read(new Uint8Array(data))); | ||
} | ||
catch (e) { | ||
return new RpcError('cannot read twirp error response', TwirpErrorCode[TwirpErrorCode.internal]); | ||
} | ||
if (!isJsonObject(json) || typeof json.code !== "string" || typeof json.msg !== "string") | ||
return new RpcError('cannot read twirp error response', TwirpErrorCode[TwirpErrorCode.internal]); | ||
let meta = {}; | ||
if (isJsonObject(json.meta)) { | ||
for (let [k, v] of Object.entries(json.meta)) { | ||
if (typeof v === "string") | ||
meta[k] = v; | ||
} | ||
} | ||
return new RpcError(json.msg, json.code, meta); | ||
} | ||
/** | ||
* Parses Angular response headers to RpcMetaData. | ||
* Drops the headers Content-Type and Content-Length. | ||
*/ | ||
function parseMetadataFromResponseHeaders(headers) { | ||
let meta = {}; | ||
for (let key of headers.keys()) { | ||
if (key.toLowerCase() === 'content-type') | ||
continue; | ||
if (key.toLowerCase() === 'content-length') | ||
continue; | ||
let values = headers.getAll(key); | ||
if (!values) | ||
continue; | ||
for (let val of values) { | ||
if (meta.hasOwnProperty(key)) | ||
meta[key].push(val); | ||
else | ||
meta[key] = val; | ||
} | ||
} | ||
return meta; | ||
} | ||
/** | ||
* Provides the Twirp transport `TwirpAngularTransport`, which uses | ||
* `HttpClient` of @angular/common/http for requests. | ||
*/ | ||
class TwirpAngularModule { | ||
static forRoot(twirpOptions) { | ||
return { | ||
ngModule: TwirpAngularModule, | ||
providers: [ | ||
{ provide: TWIRP_TRANSPORT_OPTIONS, useValue: twirpOptions }, | ||
{ provide: RPC_TRANSPORT, useExisting: TwirpAngularTransport } | ||
] | ||
}; | ||
} | ||
} | ||
TwirpAngularModule.decorators = [ | ||
{ type: NgModule, args: [{ | ||
imports: [ | ||
HttpClientModule, | ||
], | ||
providers: [ | ||
TwirpAngularTransport, | ||
] | ||
},] } | ||
]; | ||
/** | ||
* Generated bundle index. Do not edit. | ||
*/ | ||
export { PbDatePipe, PbDatePipeModule, RPC_TRANSPORT }; | ||
export { PbDatePipe, PbDatePipeModule, RPC_TRANSPORT, TWIRP_TRANSPORT_OPTIONS, TwirpAngularModule, TwirpAngularTransport }; | ||
//# sourceMappingURL=protobuf-ts-runtime-angular.js.map |
{ | ||
"name": "@protobuf-ts/runtime-angular", | ||
"version": "1.0.0-alpha.28", | ||
"version": "1.0.0-alpha.29", | ||
"description": "Runtime library for using protobuf-ts with angular", | ||
@@ -26,4 +26,5 @@ "license": "Apache-2.0", | ||
"@angular/core": ">=8.0.0 <=11.0.0", | ||
"@protobuf-ts/runtime": "^1.0.0", | ||
"@protobuf-ts/runtime-rpc": "^1.0.0" | ||
"@protobuf-ts/runtime": "^1.0.0-alpha.28", | ||
"@protobuf-ts/runtime-rpc": "^1.0.0-alpha.28", | ||
"@protobuf-ts/twirp-transport": "^1.0.0-alpha.28" | ||
}, | ||
@@ -30,0 +31,0 @@ "dependencies": { |
@@ -1,1 +0,1 @@ | ||
{"__symbolic":"module","version":4,"metadata":{"PbDatePipe":{"__symbolic":"class","decorators":[{"__symbolic":"call","expression":{"__symbolic":"reference","module":"@angular/core","name":"Pipe","line":9,"character":1},"arguments":[{"name":"date","pure":true}]}],"members":{"__ctor__":[{"__symbolic":"constructor","parameterDecorators":[[{"__symbolic":"call","expression":{"__symbolic":"reference","module":"@angular/core","name":"Inject","line":13,"character":15},"arguments":[{"__symbolic":"reference","module":"@angular/core","name":"LOCALE_ID","line":13,"character":22}]}]],"parameters":[{"__symbolic":"reference","name":"string"}]}],"transform":[{"__symbolic":"method"}]}},"PbDatePipeModule":{"__symbolic":"class","decorators":[{"__symbolic":"call","expression":{"__symbolic":"reference","module":"@angular/core","name":"NgModule","line":3,"character":1},"arguments":[{"declarations":[{"__symbolic":"reference","name":"PbDatePipe"}],"exports":[{"__symbolic":"reference","name":"PbDatePipe"}]}]}],"members":{}},"RPC_TRANSPORT":{"__symbolic":"new","expression":{"__symbolic":"reference","module":"@angular/core","name":"InjectionToken","line":4,"character":33},"arguments":["RpcTransport"]}},"origins":{"PbDatePipe":"./lib/pb-date-pipe","PbDatePipeModule":"./lib/pb-date-pipe.module","RPC_TRANSPORT":"./lib/rpc-transport"},"importAs":"@protobuf-ts/runtime-angular"} | ||
{"__symbolic":"module","version":4,"metadata":{"PbDatePipe":{"__symbolic":"class","decorators":[{"__symbolic":"call","expression":{"__symbolic":"reference","module":"@angular/core","name":"Pipe","line":9,"character":1},"arguments":[{"name":"date","pure":true}]}],"members":{"__ctor__":[{"__symbolic":"constructor","parameterDecorators":[[{"__symbolic":"call","expression":{"__symbolic":"reference","module":"@angular/core","name":"Inject","line":13,"character":15},"arguments":[{"__symbolic":"reference","module":"@angular/core","name":"LOCALE_ID","line":13,"character":22}]}]],"parameters":[{"__symbolic":"reference","name":"string"}]}],"transform":[{"__symbolic":"method"}]}},"PbDatePipeModule":{"__symbolic":"class","decorators":[{"__symbolic":"call","expression":{"__symbolic":"reference","module":"@angular/core","name":"NgModule","line":3,"character":1},"arguments":[{"declarations":[{"__symbolic":"reference","name":"PbDatePipe"}],"exports":[{"__symbolic":"reference","name":"PbDatePipe"}]}]}],"members":{}},"RPC_TRANSPORT":{"__symbolic":"new","expression":{"__symbolic":"reference","module":"@angular/core","name":"InjectionToken","line":4,"character":33},"arguments":["RpcTransport"]},"TwirpAngularTransport":{"__symbolic":"class","decorators":[{"__symbolic":"call","expression":{"__symbolic":"reference","module":"@angular/core","name":"Injectable","line":34,"character":1}}],"members":{"__ctor__":[{"__symbolic":"constructor","parameterDecorators":[[{"__symbolic":"call","expression":{"__symbolic":"reference","module":"@angular/core","name":"Inject","line":40,"character":15},"arguments":[{"__symbolic":"reference","name":"TWIRP_TRANSPORT_OPTIONS"}]}],null],"parameters":[{"__symbolic":"reference","module":"@protobuf-ts/twirp-transport","name":"TwirpOptions","line":40,"character":56},{"__symbolic":"reference","module":"@angular/common/http","name":"HttpClient","line":40,"character":76}]}],"mergeOptions":[{"__symbolic":"method"}],"unary":[{"__symbolic":"method"}],"makeUrl":[{"__symbolic":"method"}],"clientStreaming":[{"__symbolic":"method"}],"duplex":[{"__symbolic":"method"}],"serverStreaming":[{"__symbolic":"method"}]}},"TwirpAngularModule":{"__symbolic":"class","decorators":[{"__symbolic":"call","expression":{"__symbolic":"reference","module":"@angular/core","name":"NgModule","line":12,"character":1},"arguments":[{"imports":[{"__symbolic":"reference","module":"@angular/common/http","name":"HttpClientModule","line":14,"character":4}],"providers":[{"__symbolic":"reference","name":"TwirpAngularTransport"}]}]}],"members":{},"statics":{"forRoot":{"__symbolic":"function","parameters":["twirpOptions"],"value":{"ngModule":{"__symbolic":"reference","name":"TwirpAngularModule"},"providers":[{"provide":{"__symbolic":"reference","name":"TWIRP_TRANSPORT_OPTIONS"},"useValue":{"__symbolic":"reference","name":"twirpOptions"}},{"provide":{"__symbolic":"reference","name":"RPC_TRANSPORT"},"useExisting":{"__symbolic":"reference","name":"TwirpAngularTransport"}}]}}}},"TWIRP_TRANSPORT_OPTIONS":{"__symbolic":"new","expression":{"__symbolic":"reference","module":"@angular/core","name":"InjectionToken","line":9,"character":43},"arguments":["TwirpOptions"]}},"origins":{"PbDatePipe":"./lib/pb-date-pipe","PbDatePipeModule":"./lib/pb-date-pipe.module","RPC_TRANSPORT":"./lib/rpc-transport","TwirpAngularTransport":"./lib/twirp-angular-transport","TwirpAngularModule":"./lib/twirp-angular.module","TWIRP_TRANSPORT_OPTIONS":"./lib/twirp-transport-options"},"importAs":"@protobuf-ts/runtime-angular"} |
export { PbDatePipe } from "./lib/pb-date-pipe"; | ||
export { PbDatePipeModule } from "./lib/pb-date-pipe.module"; | ||
export { RPC_TRANSPORT } from "./lib/rpc-transport"; | ||
export { TwirpAngularTransport } from "./lib/twirp-angular-transport"; | ||
export { TwirpAngularModule } from "./lib/twirp-angular.module"; | ||
export { TWIRP_TRANSPORT_OPTIONS } from "./lib/twirp-transport-options"; |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
251132
26
1497
6
1