Skip to content
Snippets Groups Projects
Verified Commit 3f337435 authored by Konstantin Tsabolov's avatar Konstantin Tsabolov
Browse files

feat: add supporting types and modules

parent fcaeb90d
No related branches found
No related tags found
No related merge requests found
......@@ -2,6 +2,7 @@ import type { ConfigType } from '@nestjs/config';
import { Module } from '@nestjs/common';
import { ConfigModule } from '@nestjs/config';
import { RouterModule } from '@nestjs/core';
import { ClientsModule, Transport } from '@nestjs/microservices';
import { NATS_CLIENT } from './common/constants.js';
......@@ -9,6 +10,7 @@ import { httpConfig } from './config/http.config.js';
import { natsConfig } from './config/nats.config.js';
import { ssiConfig } from './config/ssi.config.js';
import { validationSchema } from './config/validation.js';
import { CredentialDefinitionsModule } from './credential-definitions/credential-definitions.module.js';
import { HealthModule } from './health/health.module.js';
import { SchemasModule } from './schemas/schemas.module.js';
......@@ -43,7 +45,15 @@ import { SchemasModule } from './schemas/schemas.module.js';
}),
HealthModule,
SchemasModule,
CredentialDefinitionsModule,
RouterModule.register([
{ module: HealthModule, path: '/health' },
{ module: SchemasModule, path: '/schemas' },
{ module: CredentialDefinitionsModule, path: '/credential-definitions' },
]),
],
})
export class Application {}
import type { ExecutionContext } from '@nestjs/common';
import type { TestingModule } from '@nestjs/testing';
import { Test } from '@nestjs/testing';
import { of } from 'rxjs';
import { ResponseFormatInterceptor } from '../response-format.interceptor.js';
describe('ResponseFormatInterceptor', () => {
let interceptor: ResponseFormatInterceptor;
beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
providers: [ResponseFormatInterceptor],
}).compile();
interceptor = module.get<ResponseFormatInterceptor>(
ResponseFormatInterceptor,
);
});
it('should be defined', () => {
expect(interceptor).toBeDefined();
expect(interceptor).toBeInstanceOf(ResponseFormatInterceptor);
});
it('should intercept the request and format the response', (done) => {
const context: ExecutionContext = {
switchToHttp: () => ({
getResponse: () => ({
statusCode: 200,
}),
}),
} as ExecutionContext;
const next = {
handle: jest.fn().mockReturnValue(of('Hello World')),
};
const result = interceptor.intercept(context, next);
expect(result).toBeDefined();
expect(next.handle).toHaveBeenCalled();
result.subscribe((response) => {
expect(response).toEqual({ statusCode: 200, data: 'Hello World' });
done();
});
});
});
import type {
CallHandler,
ExecutionContext,
NestInterceptor,
} from '@nestjs/common';
import { Injectable } from '@nestjs/common';
import { map, type Observable } from 'rxjs';
@Injectable()
export class ResponseFormatInterceptor implements NestInterceptor {
public intercept(
context: ExecutionContext,
next: CallHandler,
): Observable<unknown> {
const ctx = context.switchToHttp();
const response = ctx.getResponse();
return next.handle().pipe(
map((data) => {
return {
statusCode: response.statusCode,
data,
};
}),
);
}
}
......@@ -8,7 +8,7 @@ import {
HttpHealthIndicator,
} from '@nestjs/terminus';
@Controller('health')
@Controller()
export class HealthController {
public constructor(
private readonly config: ConfigService,
......
export interface Response<T = unknown> {
statusCode: number;
message: string;
data?: T;
error?: unknown;
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment