Skip to content

Commit

Permalink
refactor: enhance utils, fixtures & imports
Browse files Browse the repository at this point in the history
  • Loading branch information
fraxken committed Aug 8, 2024
1 parent fb5b023 commit b7f47f3
Show file tree
Hide file tree
Showing 30 changed files with 385 additions and 288 deletions.
9 changes: 8 additions & 1 deletion jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,12 @@ module.exports = {
"**/test/**/*.spec.ts"
],
maxWorkers: 1,
globalSetup: "./start-container.js"
globalSetup: "./start-container.js",
transform: {
"\\.[jt]sx?$": "ts-jest"
},
moduleNameMapper: {
"(.+)\\.js": "$1"
},
extensionsToTreatAsEsm: [".ts"]
};
22 changes: 13 additions & 9 deletions src/class/eventManagement/dispatcher.class.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import {
PartialTransaction,
Transaction,
TransactionStore
} from "../store/transaction.class";
} from "../store/transaction.class.js";
import {
Prefix,
DispatcherChannelMessages,
Expand All @@ -29,14 +29,18 @@ import {
GenericEvent,
CloseMessage,
RetryMessage,
DispatcherTransactionMetadata,
DistributedEventMessage
} from "../../types/eventManagement/index";
import { defaultStandardLog, handleLoggerMode, StandardLog } from "../../utils/index";
import { IncomerStore, RegisteredIncomer } from "../store/incomer.class";
import { TransactionHandler } from "./dispatcher/transaction-handler.class";
import { IncomerChannelHandler } from "./dispatcher/incomer-channel.class";
import { EventsHandler, customValidationCbFn, eventsValidationFn } from "./dispatcher/events.class";
DistributedEventMessage,
DispatcherTransactionMetadata
} from "../../types/eventManagement/index.js";
import { defaultStandardLog, handleLoggerMode, StandardLog } from "../../utils/index.js";
import { IncomerStore, RegisteredIncomer } from "../store/incomer.class.js";
import { TransactionHandler } from "./dispatcher/transaction-handler.class.js";
import { IncomerChannelHandler } from "./dispatcher/incomer-channel.class.js";
import {
EventsHandler,
customValidationCbFn,
eventsValidationFn
} from "./dispatcher/events.class.js";

// CONSTANTS
const kIdleTime = Number.isNaN(Number(process.env.MYUNISOFT_DISPATCHER_IDLE_TIME)) ? 60_000 * 10 :
Expand Down
18 changes: 10 additions & 8 deletions src/class/eventManagement/dispatcher/events.class.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import { Channel } from "@myunisoft/redis";
import Ajv, { ValidateFunction } from "ajv";

// Import Internal Dependencies
import { TransactionStore } from "../../store/transaction.class";
import {
import { TransactionStore } from "../../store/transaction.class.js";
import type {
DispatcherChannelMessages,
DispatcherApprovementMessage,
EventMessage,
Expand All @@ -17,16 +17,16 @@ import {
CloseMessage,
RetryMessage,
DispatcherTransactionMetadata
} from "../../../types";
import * as eventsSchema from "../../../schema/eventManagement/index";
} from "../../../types/index.js";
import * as eventsSchema from "../../../schema/eventManagement/index.js";
import {
NestedValidationFunctions,
StandardLog,
StandardLogOpts,
type StandardLog,
type StandardLogOpts,
concatErrors,
defaultStandardLog
} from "../../../utils";
import { PartialLogger } from "../dispatcher.class";
} from "../../../utils/index.js";
import { PartialLogger } from "../dispatcher.class.js";

// CONSTANTS
const ajv = new Ajv();
Expand Down Expand Up @@ -225,6 +225,7 @@ export class EventsHandler<T extends GenericEvent> extends EventEmitter {
private dispatcherChannelMessagesSchemaValidation(
event: IncomerRegistrationMessage | DispatcherApprovementMessage
): void {
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const { redisMetadata, ...eventRest } = event;

this.redisMetadataValidation(event);
Expand All @@ -245,6 +246,7 @@ export class EventsHandler<T extends GenericEvent> extends EventEmitter {
private incomerChannelMessagesSchemaValidation(
event: IncomerChannelMessages<T>["IncomerMessages"] | RetryMessage
): void {
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const { redisMetadata, ...eventRest } = event;

this.redisMetadataValidation(event);
Expand Down
8 changes: 6 additions & 2 deletions src/class/eventManagement/dispatcher/incomer-channel.class.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,12 @@
import { Channel, getRedis } from "@myunisoft/redis";

// Import Internal Dependencies
import { DistributedEventMessage, GenericEvent, IncomerChannelMessages } from "../../../types";
import { PartialLogger } from "../dispatcher.class";
import type {
DistributedEventMessage,
GenericEvent,
IncomerChannelMessages
} from "../../../types/index.js";
import { PartialLogger } from "../dispatcher.class.js";

export interface IncomerChannelHandlerOptions<T extends GenericEvent> {
logger: PartialLogger;
Expand Down
30 changes: 20 additions & 10 deletions src/class/eventManagement/dispatcher/transaction-handler.class.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint-disable @typescript-eslint/no-unused-vars */
/* eslint-disable max-lines */
// Import Node.js Dependencies
import { randomUUID } from "node:crypto";
Expand All @@ -8,21 +9,30 @@ import { Mutex } from "@openally/mutex";

// Import Internal Dependencies
import {
IncomerHandlerTransaction,
Transaction,
TransactionStore,
Transactions
} from "../../store/transaction.class";
import { IncomerStore, RegisteredIncomer } from "../../store/incomer.class";
type IncomerHandlerTransaction,
type Transaction,
type Transactions
} from "../../store/transaction.class.js";
import { IncomerStore, RegisteredIncomer } from "../../store/incomer.class.js";
import { IncomerChannelHandler } from "./incomer-channel.class.js";
import {
type StandardLog,
type StandardLogOpts,
defaultStandardLog
} from "../../../utils/index.js";
import { EventsHandler } from "./events.class.js";
import type {
DispatcherChannelMessages,
GenericEvent,
IncomerChannelMessages
} from "../../../types";
import { IncomerChannelHandler } from "./incomer-channel.class";
import { DefaultOptions, PartialLogger, SharedOptions } from "../dispatcher.class";
import { StandardLog, StandardLogOpts, defaultStandardLog } from "../../../utils";
import { EventsHandler } from "./events.class";
} from "../../../types/index.js";
import type {
DefaultOptions,
PartialLogger,
SharedOptions
} from "../dispatcher.class.js";


interface DistributeMainTransactionOptions {
isoPublisherIncomer: RegisteredIncomer;
Expand Down
10 changes: 5 additions & 5 deletions src/class/eventManagement/externals.class.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
// Import Internal Dependencies
import { Incomer, IncomerOptions } from "./incomer.class";
import { Dispatcher } from "./dispatcher.class";
import {
import { Incomer, type IncomerOptions } from "./incomer.class.js";
import { Dispatcher } from "./dispatcher.class.js";
import type {
GenericEvent
} from "../../types";
} from "../../types/index.js";
import {
AVAILABLE_EVENTS
} from "../../index";
} from "../../index.js";

export class Externals<T extends GenericEvent = GenericEvent> {
public incomer: Incomer<T>;
Expand Down
26 changes: 13 additions & 13 deletions src/class/eventManagement/incomer.class.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,16 @@ import {
} from "@myunisoft/redis";
import { pino } from "pino";
import { match } from "ts-pattern";
import { ValidateFunction } from "ajv";
import { Result } from "@openally/result";
import type { ValidateFunction } from "ajv";
import type { Result } from "@openally/result";

// Import Internal Dependencies
import {
IncomerMainTransaction,
PartialTransaction,
Transaction,
type IncomerMainTransaction,
type PartialTransaction,
type Transaction,
TransactionStore
} from "../store/transaction.class";
} from "../store/transaction.class.js";
import {
Prefix,
EventCast,
Expand All @@ -34,16 +34,16 @@ import {
GenericEvent,
IncomerRegistrationMessage,
RetryMessage
} from "../../types/eventManagement/index";
} from "../../types/eventManagement/index.js";
import {
NestedValidationFunctions,
StandardLog,
type NestedValidationFunctions,
type StandardLog,
defaultStandardLog,
handleLoggerMode
} from "../../utils/index";
import { Externals } from "./externals.class";
import { DISPATCHER_CHANNEL_NAME, DispatcherChannelEvents, PartialLogger } from "./dispatcher.class";
import { customValidationCbFn, eventsValidationFn } from "./dispatcher/events.class";
} from "../../utils/index.js";
import { Externals } from "./externals.class.js";
import { DISPATCHER_CHANNEL_NAME, DispatcherChannelEvents, PartialLogger } from "./dispatcher.class.js";
import { customValidationCbFn, eventsValidationFn } from "./dispatcher/events.class.js";

// CONSTANTS
// Arbitrary value according to fastify default pluginTimeout
Expand Down
11 changes: 6 additions & 5 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@
import Ajv from "ajv";

// Import Internal Dependencies
import { concatErrors, eventsValidationFn } from "./utils/index";
import { metadata as metadataSchema, scope as scopeSchema } from "./schema";

// Import Types
import { concatErrors, eventsValidationFn } from "./utils/index.js";
import {
metadata as metadataSchema,
scope as scopeSchema
} from "./schema/index.js";
import type {
EventOptions,
EventSubscribe,
Events,
Expand Down Expand Up @@ -80,8 +81,8 @@ export const AVAILABLE_EVENTS = Object.freeze<Record<keyof Events, EventSubscrib
})).reduce((prev, curr) => Object.assign(prev, { [curr.name]: curr }), {}) as Record<keyof Events, EventSubscribe>
);

export { eventsValidationFn } from "./utils/index";
export * as EventSchemas from "./schema/events/index";
export * from "./types/index";
export { eventsValidationFn } from "./utils/index";
export * from "./class/eventManagement/dispatcher.class";
export * from "./class/eventManagement/incomer.class";
3 changes: 1 addition & 2 deletions src/schema/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
export * as eventsValidationSchemas from "./events/index";
export * as eventsValidationSchemas from "./events/index.js";
export { metadata } from "./metadata.json";
export { scope } from "./scope.json";

2 changes: 1 addition & 1 deletion src/types/eventManagement/dispatcherChannel.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Import Internal Dependencies
import {
import type {
DispatcherTransactionMetadata,
IncomerTransactionMetadata,
EventSubscribe,
Expand Down
4 changes: 2 additions & 2 deletions src/types/eventManagement/incomerChannel.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
// Import Internal Dependencies
import {
import type {
DispatcherTransactionMetadata,
GenericEvent,
IncomerTransactionMetadata,
Prefix
} from "./index";
} from "./index.js";

export type DispatcherPingMessage = {
name: "PING";
Expand Down
6 changes: 3 additions & 3 deletions src/types/eventManagement/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Events } from "../index";
import { Events } from "../index.js";

export type Prefix = "test" | "development" | "staging" | "production";

Expand Down Expand Up @@ -33,5 +33,5 @@ export type GenericEvent = {
[key: string]: any;
};

export * from "./dispatcherChannel";
export * from "./incomerChannel";
export * from "./dispatcherChannel.js";
export * from "./incomerChannel.js";
6 changes: 3 additions & 3 deletions src/types/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Import Internal Dependencies
import { Events } from "./events";
import { Events } from "./events.js";

export type Method = "POST" | "PATCH" | "PUT" | "DELETE";

Expand Down Expand Up @@ -30,5 +30,5 @@ export type WebhooksResponse<T extends (keyof Events)[] = (keyof Events)[]> = [
...(WebhookResponse<T[number]>)[]
];

export * from "./eventManagement/index";
export * from "./events";
export * from "./eventManagement/index.js";
export * from "./events.js";
32 changes: 32 additions & 0 deletions src/utils/ajv.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// Import Third-party Dependencies
import Ajv, { ErrorObject, ValidateFunction } from "ajv";

// Import Internal Dependencies
import { eventsValidationSchemas } from "../schema/index.js";

// CONSTANTS
const ajv = new Ajv();

export type NestedValidationFunctions = Map<string, ValidateFunction<Record<string, any>>>;

type MappedEventsValidationFn = Map<string, NestedValidationFunctions>;

export const eventsValidationFn: MappedEventsValidationFn = new Map<string, NestedValidationFunctions>();

for (const [name, validationSchemas] of Object.entries(eventsValidationSchemas)) {
const operationsValidationFunctions: Map<string, ValidateFunction<Record<string, any>>> = new Map();

for (const [operation, validationSchema] of Object.entries(validationSchemas)) {
operationsValidationFunctions.set(operation, ajv.compile(validationSchema));
}

eventsValidationFn.set(name, operationsValidationFunctions);
}

export function concatErrors(
errors: ErrorObject<string, Record<string, any>, unknown>[]
): string {
return errors
.map((error) => `${error.instancePath ? `${error.instancePath}: ` : ""}${error.message}`)
.join("|");
}
Loading

0 comments on commit b7f47f3

Please sign in to comment.