Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/decorators/services.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import * as _ from 'lodash';
import 'reflect-metadata';
import { ServiceClass, ServiceMethod } from '../server/model/metadata';
import { ParserType, ServiceProcessor } from '../server/model/server-types';
import { ParserType, ServicePostProcessor, ServicePreProcessor } from '../server/model/server-types';
import { ServerContainer } from '../server/server-container';

/**
Expand Down Expand Up @@ -107,7 +107,7 @@ export function Security(roles?: string | Array<string>, name?: string) {
* }
* ```
*/
export function PreProcessor(preprocessor: ServiceProcessor) {
export function PreProcessor(preprocessor: ServicePreProcessor) {
return new ProcessorServiceDecorator('PreProcessor')
.withArrayProperty('preProcessors', preprocessor, true)
.createDecorator();
Expand Down Expand Up @@ -139,7 +139,7 @@ export function PreProcessor(preprocessor: ServiceProcessor) {
* }
* ```
*/
export function PostProcessor(postprocessor: ServiceProcessor) {
export function PostProcessor(postprocessor: ServicePostProcessor) {
return new ProcessorServiceDecorator('PostProcessor')
.withArrayProperty('postProcessors', postprocessor, true)
.createDecorator();
Expand Down
10 changes: 5 additions & 5 deletions src/server/model/metadata.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict';

import { HttpMethod, ParserType, ServiceProcessor } from './server-types';
import { HttpMethod, ParserType, ServicePreProcessor, ServicePostProcessor } from './server-types';

export interface ServiceProperty {
type: ParamType;
Expand All @@ -17,8 +17,8 @@ export class ServiceClass {
public targetClass: any;
public path: string;
public authenticator: Record<string, Array<string>>;
public preProcessors: Array<ServiceProcessor>;
public postProcessors: Array<ServiceProcessor>;
public preProcessors: Array<ServicePreProcessor>;
public postProcessors: Array<ServicePostProcessor>;
public methods: Map<string, ServiceMethod>;
public bodyParserOptions: any;
public bodyParserType: ParserType;
Expand Down Expand Up @@ -65,8 +65,8 @@ export class ServiceMethod {
public accepts: Array<string>;
public resolvedLanguages: Array<string>;
public resolvedAccepts: Array<string>;
public preProcessors: Array<ServiceProcessor>;
public postProcessors: Array<ServiceProcessor>;
public preProcessors: Array<ServicePreProcessor>;
public postProcessors: Array<ServicePostProcessor>;
public ignoreNextMiddlewares: boolean = false;
}

Expand Down
3 changes: 2 additions & 1 deletion src/server/model/server-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,8 @@ export interface ServiceAuthenticator {
getMiddleware(): express.RequestHandler;
}

export type ServiceProcessor = (req: express.Request, res?: express.Response) => void;
export type ServicePreProcessor = (req: express.Request, res?: express.Response) => void;
export type ServicePostProcessor = (req: express.Request, res?: express.Response, result?: any) => void;
export type ParameterConverter = (paramValue: any) => any;

/**
Expand Down
12 changes: 6 additions & 6 deletions src/server/service-invoker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@ import * as _ from 'lodash';
import { Errors } from '../typescript-rest';
import { ServiceClass, ServiceMethod, ServiceProperty } from './model/metadata';
import { DownloadBinaryData, DownloadResource, NoResponse } from './model/return-types';
import { HttpMethod, ReferencedResource, ServiceContext, ServiceProcessor } from './model/server-types';
import { HttpMethod, ReferencedResource, ServiceContext, ServicePostProcessor, ServicePreProcessor } from './model/server-types';
import { ParameterProcessor } from './parameter-processor';
import { ServerContainer } from './server-container';

export class ServiceInvoker {
private serviceClass: ServiceClass;
private serviceMethod: ServiceMethod;
private preProcessors: Array<ServiceProcessor>;
private postProcessors: Array<ServiceProcessor>;
private preProcessors: Array<ServicePreProcessor>;
private postProcessors: Array<ServicePostProcessor>;
private debugger = debug('typescript-rest:service-invoker:runtime');

constructor(serviceClass: ServiceClass, serviceMethod: ServiceMethod) {
Expand Down Expand Up @@ -50,10 +50,10 @@ export class ServiceInvoker {
}
}

private async runPostProcessors(context: ServiceContext): Promise<void> {
private async runPostProcessors(context: ServiceContext, result: any): Promise<void> {
this.debugger('Running postprocessors');
for (const processor of this.postProcessors) {
await Promise.resolve(processor(context.request, context.response));
await Promise.resolve(processor(context.request, context.response, result));
}
}

Expand All @@ -71,7 +71,7 @@ export class ServiceInvoker {
}
const result = await toCall.apply(serviceObject, args);
if (this.postProcessors.length) {
await this.runPostProcessors(context);
await this.runPostProcessors(context, result);
}
this.processResponseHeaders(context);
await this.sendValue(result, context);
Expand Down
12 changes: 12 additions & 0 deletions test/integration/postprocessor.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,19 @@ import * as express from 'express';
import * as _ from 'lodash';
import 'mocha';
import * as request from 'request';
import * as sinon from 'sinon';
import { Path, POST, PostProcessor, Server } from '../../src/typescript-rest';

const expect = chai.expect;
const postprocessorSpy = sinon.spy();

@Path('postprocessor')
@PostProcessor(postprocessor1)
export class PostProcessedService {
@Path('test')
@POST
@PostProcessor(postprocessor2)
@PostProcessor(postprocessorSpy)
public test() {
return 'OK';
}
Expand Down Expand Up @@ -65,6 +68,15 @@ describe('Postprocessor Tests', () => {
done();
});
});
it('should pass the controller result to the processor', (done) => {
request.post({
headers: { 'content-type': 'application/json' },
url: 'http://localhost:5674/postprocessor/test'
}, (error, response, body) => {
expect(postprocessorSpy).to.be.calledWith(sinon.match({}), sinon.match({}) , 'OK');
done();
});
});
});

describe('Assynchronous Postprocessors', () => {
Expand Down