npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

ceddd

v0.1.9

Published

Library to structure a proyect with DDD, testing, types end to end, verification params.

Downloads

10

Readme

CEDDD

Library to structure a proyect with DDD, testing, types end to end, verification params.

Entities

Command/Event

It is a DTO that contains

  • topic a type of action to be performed
  • id a unique identifier
  • datetime creation datetime in utc
  • context is as authentication, traced of the route, information of the agent who performs the request or IP, etc.
  • data information necessary to carry out the topic action.
// Message Context

interface Context extends JSONObject {
	trace: { id: string }[];
	http: null | {
		ip: string;
		userAgent: string;
	};
	auth: null | {
		token: string;
	};
}

// Command

type CommandTopic = `cmd.${string}`;

interface Command<T extends CommandTopic, D extends JSONValue> {
	ctx: Context;
	id: string;
	datetime: string;
	topic: T;
	data: D;
}

// Event

export type EventTopic = `evt.${string}`;

export interface Event<T extends EventTopic, D extends JSONValue> {
	ctx: Context;
	id: string;
	datetime: string;
	topic: T;
	data: D;
}

Command/Event Definition

Define the relationship of a topic with the data and the result that returns the resolution of that command/event. Data and the result is used Zod for type validation.

// Commands

const SignInCommandDefinition = new CommandDefinition({
	topic: 'cmd.auth.signIn',
	data: z.object({
		username: z.string(),
		password: z.string()
	}),
	result: z.object({
		id: z.string(),
		firstName: z.string(),
		lastName: z.string(),
		email: z.string()
	})
});

const CheckBusinessEmailCommandDefinition = new CommandDefinition({
	topic: 'cmd.auth.email.check',
	data: z.object({
		email: z.string()
	}),
	result: z.object({
		isBusiness: z.boolean()
	})
});

// Event

export const AuthSignInEventDefinition = new EventDefinition({
	topic: 'evt.auth.signedIn',
	data: z.object({
		userId: z.string()
	})
});

Command/Event Resolver

It is the implementation of a function that resolves a command/event definition, defining the units you need (other commands/events that it emits and infrastructure as repository to databases, external services, etc.) and the errors you can throw.

interface SignInInfrastructure {
	repositories: {
		user: {
			findByEmail(email: string): Promise<User | null>
		}
	};
	services: {
		password: {
			check(password: string, hash: string): Promise<{ isSame: boolean }>
		}
	}
}

export const SignInCommandResolver = new CommandResolver({
	// interface with services/repositories requerid
	// this is put with an empty object and "as" 
	// to be able to infer the type of the interface
	infrastructure: {} as SignInInfrastructure,
	// Definition of Command
	definition: SignInCommandDefinition,
	// Different types of effects that the resolver can dispatch/emit/throw
	effects: {
		// Array of command defintions to dispath
		commands: [
			CheckBusinessEmailCommandDefinition
        ] as const,
		// Array of events defintions to emit 
		events: [
			AuthSignInEventDefinition
		] as const,
		// Array of domain errors to throw
		errors: [
			new DomainError('email.incorrect', 'correo incorrecto'),
			new DomainError('password.incorrect', 'contraseña incorrecta')
		] as const,
	},
	// Function with business logic
	method: async ({ cmd, infra, commands, events, errors }) => {
		// Implementation
		const checkEmail = await commands.dispatch({
			topic: 'cmd.auth.email.check',
			data: {
				email: cmd.data.email
			}
		});
		if (!checkEmail.isBusiness) {
			throw errors['email:incorrect'];
		}

		const user = await infra.repositories.user.findByEmail({
			email: cmd.data.email,
		});
		if (!user) {
			throw errors['email:incorrect'];
		}

		const checkPassword = await infra.services.password.check(
			cmd.data.password,
			user.passwordHash
		);
		if (!checkPassword.isSame) {
			throw errors['password:incorrect'];
		}

		events.emit({
			topic: 'evt.auth.signedIn',
			data: {
				userId: user.id
			}
		});

		return {
			id: user.id,
			firstName: user.firstName,
			lastName: user.lastName,
			email: user.email
		};
	}
});

Module

To section the resolvers by different modules, for example authentication, profile, mailing, etc., the modules are used, which are a group of resolvers. These modules are the ones that set up the infrastructure for each resolver.

export const AuthModule = new Module({
	// Setup of Resolvers
	setup: async () => {
		const userRepository = new FileUserRepository();
		const cryptoService = new CrytoService();

		return {
			// infrastructure of commands resolvers
			commands: {
				'cmd.auth.signIn': {
					repositories: {
						user: userRepository
					};
					services: {
						password: cryptoService
					}
				},
				'cmd.auth.email.check': {}
			},
			// infrastructure of event resolvers
			events: {}
		};
	},
	resolvers: {
		commands: [
			SignInCommandResolver, 
			CheckBusinessEmailCommandDefinition
		] as const,
		events: [] as const
	}
});

App

app is a main unique class with all modules of application.

export const app = new App({
	modules: [AuthModule] as const
});

export type AppCommandSchema = AppInferCommandSchema<typeof app>;

export type AppEventSchema = AppInferEventSchema<typeof app>;

iInitialization

in file index.ts start a main funcion of applicacion with express example.

// index.ts

const main = async () => {
	// create a implementation of command bus and event bus
	// in ceddd/utils you have MemoryCommandBus and MemoryEventBus examples
	const commands = new MemoryCommandBus<AppCommandSchema>(app.schemas.commands);
	const events = new MemoryEventBus<AppEventSchema>(app.schemas.events);

	// this initialize infrastructure and register al resolvers in buses
	await app.init({ commands, events });

	// you can dispath commands in command or subscribe to events
	// EJ: with express

	const app = express();

	app.use(urlencoded({ extended: false }));
	app.use(json());

	app.post('/api/cmd', (req, res) => {
		const command = req.body;

		const ip = req.headers['x-forwarded-for'] || req.socket.remoteAddress;
		const userAgent = req.headers['user-agent'];

		command
			.dispath({
				ctx: {
					trace: [],
					http: {
						ip: ip?.toString() || '',
						userAgent: userAgent?.toString() || ''
					},
					auth: command.ctx?.auth || null
				},
				topic: command.topic,
				data: command.data
			})
			.then((result) => res.status(200).json(result))
			.catch((error) => {
				res.status(400).json({
					error: {
						code: error?.code || 'internal',
						message: error.message || 'unknown error'
					}
				});
			});
	});

	app.listen('4000', () => {
		console.log(`🚀 Started app in http://localhost:4000`);
	});	
};

main().catch(console.error);

HTTP Command

curl --location 'http://localhost:4000/api/cmd' \
--header 'Content-Type: application/json' \
--data-raw '{
    "topic": "cmd.auth.signIn",
    "data": {
        "email": "[email protected]",
        "password": "1234"
    }
}'