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

feat: add TSA module to shared lib

parent ede9cb95
No related branches found
No related tags found
1 merge request!40End to end run preparation
...@@ -18,5 +18,6 @@ export * from './dto/pagination-params.dto.js'; ...@@ -18,5 +18,6 @@ export * from './dto/pagination-params.dto.js';
export * from './dto/multitenancy-params.dto.js'; export * from './dto/multitenancy-params.dto.js';
export * from './modules/health/health.module.js'; export * from './modules/health/health.module.js';
export * from './modules/tsa/index.js';
export * from './interceptors/response-format.interceptor.js'; export * from './interceptors/response-format.interceptor.js';
import type { TestingModule } from '@nestjs/testing';
import type { AxiosResponse } from 'axios' assert { 'resolution-mode': 'require' };
import { HttpService } from '@nestjs/axios';
import { Test } from '@nestjs/testing';
import { of } from 'rxjs';
import { TSAService } from '../tsa.service.js';
describe('TSA Service', () => {
const httpServiceMock = {
post: jest.fn(),
} as unknown as jest.Mocked<HttpService>;
let service: TSAService;
beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
providers: [
{ provide: HttpService, useValue: httpServiceMock },
TSAService,
],
}).compile();
service = module.get<TSAService>(TSAService);
jest.clearAllMocks();
});
it('should do something', () => {
expect(service).toBeDefined();
expect(service).toBeInstanceOf(TSAService);
});
it('should evaluate a policy', async () => {
const expectedResult = {};
const expectedResponse = {
data: expectedResult,
} as AxiosResponse;
httpServiceMock.post.mockReturnValueOnce(of(expectedResponse));
const result = await service.evaluatePolicy('policies/xfsc/didresolve/1.0');
expect(result).toStrictEqual(expectedResult);
});
it('should handle string response', async () => {
const expectedResult = '{}';
const expectedResponse = {
data: expectedResult,
} as AxiosResponse;
httpServiceMock.post.mockReturnValueOnce(of(expectedResponse));
const result = await service.evaluatePolicy('policies/xfsc/didresolve/1.0');
expect(result).toEqual(JSON.parse(expectedResult));
});
});
export * from './tsa.module.js';
export * from './tsa.service.js';
export interface TSAModuleOptions {
tsaBaseUrl: string;
}
import type { TSAModuleOptions } from './interfaces/tsa-module-options.interface.js';
import { ConfigurableModuleBuilder } from '@nestjs/common';
export const {
ConfigurableModuleClass,
MODULE_OPTIONS_TOKEN,
OPTIONS_TYPE,
ASYNC_OPTIONS_TYPE,
} = new ConfigurableModuleBuilder<TSAModuleOptions>().build();
import type { TSAModuleOptions } from './interfaces/tsa-module-options.interface.js';
import { HttpModule } from '@nestjs/axios';
import { Module } from '@nestjs/common';
import {
ConfigurableModuleClass,
MODULE_OPTIONS_TOKEN,
} from './tsa.module-definition.js';
import { TSAService } from './tsa.service.js';
@Module({
imports: [
HttpModule.registerAsync({
inject: [MODULE_OPTIONS_TOKEN],
useFactory: (moduleOptions: TSAModuleOptions) => ({
baseURL: moduleOptions.tsaBaseUrl,
}),
}),
],
providers: [TSAService],
exports: [TSAService],
})
export class TSAModule extends ConfigurableModuleClass {}
import { HttpService } from '@nestjs/axios';
import { Injectable } from '@nestjs/common';
import { firstValueFrom } from 'rxjs';
@Injectable()
export class TSAService {
public constructor(private readonly http: HttpService) {}
/**
* Evaluates the given policy.
* The policy should be specified in the format `repository/group/policy/version`.
*
* @param policy - The policy to evaluate. The format is `repository/group/policy/version`,
* @example `policies/xfsc/didresolve/1.0`
*/
public async evaluatePolicy(
policy: `${string}/${string}/${string}/${string}`,
input?: Record<string, unknown>,
) {
const { data } = await firstValueFrom(
this.http.post(`/policy/${policy}/evaluation`, input),
);
if (typeof data === 'string') {
return JSON.parse(data);
}
return data;
}
}
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