@anglr/rest
Version:
Angular module representing rest services
109 lines • 5.23 kB
JavaScript
import { Injectable, Inject, Optional } from '@angular/core';
import { HTTP_INTERCEPTORS, HttpEventType } from '@angular/common/http';
import { IGNORED_INTERCEPTORS } from '@anglr/common';
import { HTTP_HEADER_CONTENT_TYPE } from '@anglr/rest';
import { map } from 'rxjs';
import { Type } from 'avsc';
import { AvroAdapterInterceptorOptions } from './avroAdapter.options';
import { AVRO_ADAPTER_SCHEMA_PROVIDER, AVRO_REQUEST_DATA, AVRO_RESPONSE_DATA } from '../../misc/tokens';
import * as i0 from "@angular/core";
import * as i1 from "./avroAdapter.options";
/**
* Interceptor that will enable usage of AVRO for request and response data streams (binary format)
*/
export class AvroAdapterInterceptor {
//######################### constructor #########################
constructor(_options, _schemaProvider) {
this._options = _options;
this._schemaProvider = _schemaProvider;
if (!_options) {
this._options = new AvroAdapterInterceptorOptions();
}
}
//######################### public methods - implementation of HttpInterceptor #########################
/**
* Intercepts http request
* @param req - Request to be intercepted
* @param next - Next middleware that can be called for next processing
*/
intercept(req, next) {
//interceptor is ignored
if (req.context.get(IGNORED_INTERCEPTORS).some(itm => itm == AvroAdapterInterceptor)) {
return next.handle(req);
}
const avroReq = req.context.get(AVRO_REQUEST_DATA);
const avroRes = req.context.get(AVRO_RESPONSE_DATA);
const schemaObj = this._schemaProvider.schema;
if (this._options.disabled ||
(!avroReq && !avroRes)) {
return next.handle(req);
}
//process request with avro
if (avroReq) {
let schema;
//body present and schema for specified type exists and content type provided
if (req.body && schemaObj[avroReq.namespace] && (schema = schemaObj[avroReq.namespace][avroReq.name]) && this._options.customAcceptContentTypeHeader) {
const type = Type.forSchema(schema);
const additionalHeaders = {};
additionalHeaders[HTTP_HEADER_CONTENT_TYPE] = this._options.customAcceptContentTypeHeader;
if (this._options.typeHeaderName && type.name) {
additionalHeaders[this._options.typeHeaderName] = type.name;
}
if (this._options.fingerprintHeaderName) {
additionalHeaders[this._options.fingerprintHeaderName] = type.fingerprint().toString('hex');
}
req = req.clone({
body: type.toBuffer(req.body).buffer,
setHeaders: additionalHeaders
});
}
}
//adds header for Accept header
if (avroRes && this._options.customAcceptContentTypeHeader) {
req = req.clone({
responseType: 'arraybuffer',
setHeaders: {
'Accept': this._options.customAcceptContentTypeHeader
}
});
}
return next.handle(req)
.pipe(map(result => {
//process only response
if (result.type != HttpEventType.Response) {
return result;
}
let schema;
//process response with avro
if (avroRes && result.headers.get(HTTP_HEADER_CONTENT_TYPE) == this._options.customAcceptContentTypeHeader &&
schemaObj[avroRes.namespace] && (schema = schemaObj[avroRes.namespace][avroRes.name])) {
const type = Type.forSchema(schema);
return result.clone({
body: type.fromBuffer(Buffer.from(new Uint8Array(result.body)))
});
}
return result;
}));
}
static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "19.1.0", ngImport: i0, type: AvroAdapterInterceptor, deps: [{ token: i1.AvroAdapterInterceptorOptions, optional: true }, { token: AVRO_ADAPTER_SCHEMA_PROVIDER, optional: true }], target: i0.ɵɵFactoryTarget.Injectable }); }
static { this.ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "19.1.0", ngImport: i0, type: AvroAdapterInterceptor }); }
}
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "19.1.0", ngImport: i0, type: AvroAdapterInterceptor, decorators: [{
type: Injectable
}], ctorParameters: () => [{ type: i1.AvroAdapterInterceptorOptions, decorators: [{
type: Optional
}] }, { type: undefined, decorators: [{
type: Optional
}, {
type: Inject,
args: [AVRO_ADAPTER_SCHEMA_PROVIDER]
}] }] });
/**
* Provider for proper use of AvroAdapterInterceptor, use this provider to inject this interceptor
*/
export const AVRO_ADAPTER_INTERCEPTOR_PROVIDER = {
provide: HTTP_INTERCEPTORS,
multi: true,
useClass: AvroAdapterInterceptor
};
//# sourceMappingURL=avroAdapter.interceptor.js.map