Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(server): removed experimental decorators #259

Merged
merged 8 commits into from
Apr 19, 2023
Merged
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
11 changes: 10 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions packages/server-nodejs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@
"glob-promise": "6.0.2",
"mime-types": "^2.1.35",
"tslog": "^4.8.2",
"yargs": "^17.7.1"
"yargs": "^17.7.1",
"zod": "^3.21.4"
},
"engines": {
"node": ">=18.7"
Expand All @@ -45,4 +46,4 @@
"jitar",
"nodejs"
]
}
}
4 changes: 2 additions & 2 deletions packages/server-nodejs/src/JitarServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@ export default class JitarServer
{
console.log(STARTUP_MESSAGE);

const options = await ServerOptionsReader.read();
const configuration = await RuntimeConfigurationLoader.load(options.config);
const options = ServerOptionsReader.read();
const configuration = RuntimeConfigurationLoader.load(options.config);
const runtime = await RuntimeConfigurator.configure(configuration);

const logger = LogBuilder.build(options.loglevel);
Expand Down
20 changes: 15 additions & 5 deletions packages/server-nodejs/src/configuration/GatewayConfiguration.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,22 @@

import { IsOptional, IsNumber, IsUrl } from 'class-validator';
import { z } from 'zod';

export const gatewaySchema = z
.object({
monitor: z.number().optional(),
repository: z.string().url().optional()
})
.strict()
.transform((value) => new GatewayConfiguration(value.monitor, value.repository));

export default class GatewayConfiguration
{
@IsNumber()
@IsOptional()
monitor?: number;
repository?: string;

@IsUrl()
repository = '';
constructor(monitor?: number, repository?: string)
{
this.monitor = monitor;
this.repository = repository;
}
}
26 changes: 18 additions & 8 deletions packages/server-nodejs/src/configuration/NodeConfiguration.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,25 @@

import { IsArray, IsUrl, IsOptional } from 'class-validator';
import { z } from 'zod';

export const nodeSchema = z
.object({
gateway: z.string().url().optional(),
repository: z.string().url().optional(),
segments: z.array(z.string()).nonempty()
})
.strict()
.transform((value) => new NodeConfiguration(value.gateway, value.repository, value.segments));

export default class NodeConfiguration
{
@IsUrl()
@IsOptional()
gateway?: string;
repository?: string;
segments: string[];

@IsUrl()
repository = '';

@IsArray()
segments?: string[];
constructor(gateway: string | undefined, repository: string | undefined, segments: string[])
{
this.gateway = gateway;
this.repository = repository;
this.segments = segments;
}
}
46 changes: 38 additions & 8 deletions packages/server-nodejs/src/configuration/ProxyConfiguration.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,46 @@

import { IsUrl, IsOptional } from 'class-validator';
import { z } from 'zod';

export const proxySchema = z
.object({
node: z.string().url().optional(),
gateway: z.string().url().optional(),
repository: z.string().url()
})
.strict()
.superRefine((value, ctx) =>
{
if (value.node === undefined && value.gateway === undefined)
{
ctx.addIssue({
code: z.ZodIssueCode.custom,
message: 'Either node or gateway must be defined',
path: ['node', 'gateway']
});
}

if (value.node !== undefined && value.gateway !== undefined)
{
ctx.addIssue({
code: z.ZodIssueCode.custom,
message: 'Only node or gateway must be defined',
path: ['node', 'gateway'],

});
}
})
.transform((value) => new ProxyConfiguration(value.node, value.gateway, value.repository));

export default class ProxyConfiguration
{
@IsUrl()
@IsOptional()
node?: string;

@IsUrl()
@IsOptional()
gateway?: string;
repository: string;

@IsUrl()
repository?: string;
constructor(node: string | undefined, gateway: string | undefined, repository: string)
{
this.node = node;
this.gateway = gateway;
this.repository = repository;
}
}
31 changes: 19 additions & 12 deletions packages/server-nodejs/src/configuration/RepositoryConfiguration.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,28 @@

import { IsArray, IsOptional, IsString } from 'class-validator';
import { z } from 'zod';

export const repositorySchema = z
.object({
source: z.string().optional(),
cache: z.string().optional(),
index: z.string().optional(),
assets: z.array(z.string()).optional()
})
.strict()
.transform((value) => new RepositoryConfiguration(value.source, value.cache, value.index, value.assets));

export default class RepositoryConfiguration
{
@IsString()
@IsOptional()
source?: string;

@IsString()
@IsOptional()
cache?: string;

@IsString()
@IsOptional()
index?: string;

@IsArray()
@IsOptional()
assets?: string[];

constructor(source?: string, cache?: string, index?: string, assets?: string[])
{
this.source = source;
this.cache = cache;
this.index = index;
this.assets = assets;
}
}
48 changes: 29 additions & 19 deletions packages/server-nodejs/src/configuration/RuntimeConfiguration.ts
Original file line number Diff line number Diff line change
@@ -1,30 +1,40 @@

import { IsOptional, IsUrl } from 'class-validator';

import GatewayConfiguration from './GatewayConfiguration.js';
import NodeConfiguration from './NodeConfiguration.js';
import ProxyConfiguration from './ProxyConfiguration.js';
import RepositoryConfiguration from './RepositoryConfiguration.js';
import StandaloneConfiguration from './StandaloneConfiguration.js';
import { z } from 'zod';

import GatewayConfiguration, { gatewaySchema } from './GatewayConfiguration.js';
import NodeConfiguration, { nodeSchema } from './NodeConfiguration.js';
import ProxyConfiguration, { proxySchema } from './ProxyConfiguration.js';
import RepositoryConfiguration, { repositorySchema } from './RepositoryConfiguration.js';
import StandaloneConfiguration, { standaloneSchema } from './StandaloneConfiguration.js';

export const runtimeSchema = z
.object({
url: z.string().optional(),
standalone: standaloneSchema.optional(),
repository: repositorySchema.optional(),
gateway: gatewaySchema.optional(),
node: nodeSchema.optional(),
proxy: proxySchema.optional(),
})
.strict()
.transform((value) => new RuntimeConfiguration(value.url, value.standalone, value.repository, value.gateway, value.node, value.proxy));

export default class RuntimeConfiguration
{
@IsUrl()
@IsOptional()
url?: string;

@IsOptional()
standalone?: StandaloneConfiguration;

@IsOptional()
repository?: RepositoryConfiguration;

@IsOptional()
gateway?: GatewayConfiguration;

@IsOptional()
node?: NodeConfiguration;

@IsOptional()
proxy?: ProxyConfiguration;

constructor(url?: string, standalone?: StandaloneConfiguration, repository?: RepositoryConfiguration, gateway?: GatewayConfiguration, node?: NodeConfiguration, proxy?: ProxyConfiguration)
{
this.url = url;
this.standalone = standalone;
this.repository = repository;
this.gateway = gateway;
this.node = node;
this.proxy = proxy;
}
}
22 changes: 14 additions & 8 deletions packages/server-nodejs/src/configuration/ServerOptions.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,22 @@

import { Contains, IsEnum, IsOptional, IsString } from 'class-validator';
import { z } from 'zod';
import { LogLevel } from '../utils/LogBuilder.js';

export const serverOptionsSchema = z
.object({
loglevel: z.nativeEnum(LogLevel).optional(),
config: z.string().endsWith('.json').optional()
})
.transform((value) => new ServerOptions(value.loglevel, value.config));

export default class ServerOptions
{
@IsString()
@IsOptional()
@IsEnum(LogLevel)
loglevel = 'info';

@IsString()
@Contains('.json')
@IsOptional()
config = 'config.json';

constructor(loglevel = 'info', config = 'config.json')
{
this.loglevel = loglevel;
this.config = config;
}
}
36 changes: 21 additions & 15 deletions packages/server-nodejs/src/configuration/StandaloneConfiguration.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,31 @@

import { IsArray, IsOptional, IsString } from 'class-validator';
import { z } from 'zod';

export const standaloneSchema = z
.object({
source: z.string().optional(),
cache: z.string().optional(),
index: z.string().optional(),
segments: z.array(z.string()).optional(),
assets: z.array(z.string()).optional()
})
.strict()
.transform((value) => new StandaloneConfiguration(value.source, value.cache, value.index, value.segments, value.assets));

export default class StandaloneConfiguration
{
@IsString()
@IsOptional()
source?: string;

@IsString()
@IsOptional()
cache?: string;

@IsString()
@IsOptional()
index?: string;

@IsArray()
@IsOptional()
segments?: string[];

@IsArray()
@IsOptional()
assets?: string[];

constructor(source?: string, cache?: string, index?: string, segments?: string[], assets?: string[])
{
this.source = source;
this.cache = cache;
this.index = index;
this.segments = segments;
this.assets = assets;
}
}
4 changes: 2 additions & 2 deletions packages/server-nodejs/src/controllers/NodesController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { Logger } from 'tslog';

import { LocalGateway, RemoteNode } from '@jitar/runtime';

import NodeDto from '../models/NodeDto.js';
import NodeDto, { nodeDtoSchema } from '../models/NodeDto.js';
import DataConverter from '../utils/DataConverter.js';

export default class NodesController
Expand Down Expand Up @@ -34,7 +34,7 @@ export default class NodesController
{
try
{
const nodeDto = await DataConverter.convert(NodeDto, request.body);
const nodeDto = DataConverter.convert<NodeDto>(nodeDtoSchema, request.body);
const node = new RemoteNode(nodeDto.url, nodeDto.procedureNames);

this.#gateway.addNode(node);
Expand Down

This file was deleted.

11 changes: 0 additions & 11 deletions packages/server-nodejs/src/models/HealthDto.ts

This file was deleted.

Loading