Skip to content

Instantly share code, notes, and snippets.

Avatar

TSH code sharing tsh-code

View GitHub Profile
View command.ts
import { Command } from "../../../../shared/command-bus";
export const CREATE_USER_COMMAND_TYPE = "user/CREATE_USER";
export interface CreateUserCommandPayload {
name: string;
}
export class CreateUserCommand implements Command<CreateUserCommandPayload> {
public type: string = CREATE_USER_COMMAND_TYPE;
View action.ts
import { Request, Response } from "express";
import { celebrate, Joi } from "celebrate";
import { CommandBus } from "@tshio/command-bus";
import { CreateUserCommand } from "../commands/create-user.command";
import { Action } from "../../../../shared/http/types";
export interface CreateUserActionDependencies {
commandBus: CommandBus;
}
View handler.ts
import { CommandHandler } from "../../../../shared/command-bus";
import { CREATE_USER_COMMAND_TYPE, CreateUserCommand } from "../commands/create-user.command";
import { UserRepository } from "../repository/user.repository";
import { EventDispatcher } from "../../../../shared/event-dispatcher";
import EmailNotificationEvent from "../events/email-notification.event";
export interface CreateUserHandlerHandlerDependencies {
eventDispatcher: EventDispatcher;
userRepository: UserRepository;
}
View routing.ts
import * as express from "express";
import { Action } from "../../../shared/http/types";
import { createUserActionValidation } from "./actions/create-users.action";
export interface UsersRoutingDependencies {
createUserAction: Action;
}
export const usersRouting = (actions: UsersRoutingDependencies) => {
@tsh-code
tsh-code / comments.js
Created Oct 18, 2021
Apollo Federation - comments service
View comments.js
const { ApolloServer, gql } = require("apollo-server");
const { buildSubgraphSchema } = require("@apollo/federation");
const comments = [
{
id: "1",
post: { id: "1" },
text: "Super Cool",
},
{
@tsh-code
tsh-code / posts.js
Created Oct 18, 2021
Apollo Federation - posts service
View posts.js
const { ApolloServer, gql } = require("apollo-server");
const { buildSubgraphSchema } = require("@apollo/federation");
const posts = [
{
id: "1",
title: "JS Rulez!",
text: "",
},
{
@tsh-code
tsh-code / gateway.js
Created Oct 18, 2021
Apollo Federation - gateway service
View gateway.js
const { ApolloServer } = require("apollo-server");
const { ApolloGateway } = require("@apollo/gateway");
const gateway = new ApolloGateway({
serviceList: [
{ name: "posts", url: "http://localhost:5001" },
{ name: "comments", url: "http://localhost:5002" },
],
});
View backoff.ts
import { backOff } from "exponential-backoff";
function getUsers() {
return fetch("/api/users");
}
async function main() {
try {
// If request fails (ex. we are blocked by rate limit) it will be
//retried automatically after a random period of time!
View limiter.ts
import rateLimit from "express-rate-limit";
const limiter = rateLimit({
windowMs: 15 * 60 * 1000, // 15 minutes
max: 100 // limit each IP to 100 requests per windowMs
});
// apply rate limiter
app.use(limiter);
View validation.ts
import { RequestHandler } from "express";
import { Connection } from "./connection";
import Joi from "joi";
export interface User {
email: string;
password: string;
}
export class UserModel implements User {