An adapter where you can define which function to run

Overview

Switch Functions

An adapter where you can define which function to run

Installation

This is a Node.js module available through the npm registry.

Before installing, download and install Node.js.

Installation is done using the npm install command:

npm install @badass-team-code/switch-functions

Or using the yarn:

yarn add @badass-team-code/switch-functions

Quick Start

After installation, you will have visibility of a function and an interface to use:

Interface - AdapterOptions

With it you will have a guide to create the object that will need to assign the function as a parameter.

It receives two types as a parameter, they are:

  • first argument: Thinking of it as an http request, the first argument would be the indication of which object we are going to analyze: body or headers

  • second argument: The second argument will be from which key the value will be analyzed

import { AdapterOptions } from "@badass-team-code/switch-functions";

type Targets = "body" | "headers";
type Values = "authentication" | "authorization";

const options: AdapterOptions<Targets, Values> = [];

Examples

Let's say you are creating a soccer player creation flow, the initial roster stays the same, but each team has its own unique characteristics in the roster, so you choose to use adaptSwitchFunctions to assist you.

Creating the object to use in the adapter:

type Targets = "body";
type Values = "sportClub" | "name";

const options: AdapterOptions<Targets, Values> = [
  {
    target: {
      body: "sportClub",
    },
    expected: {
      sportClub: 1,
    },
    handle: () => {
      // Will run what club 1 needs
    },
  },
  {
    target: {
      body: "sportClub",
    },
    expected: {
      sportClub: 2,
    },
    handle: () => {
      // Will run what club 2 needs
    },
  },
  {
    handle: () => {
      // Here it will be executed if you do not enter in any case.
    },
  },
];

So if the sportClub parameter is 1 or 2, it will be executed in different business rules.

To be executed, it is only necessary to assign the function:

import {
  adaptSwitchFunctions,
  AdapterOptions,
} from "@badass-team-code/switch-functions";

const result = adaptSwitchFunctions(options);

Express

Using lib with express. You can use it as Middleware or as a Controller.

Here is a usage example showing the two use cases:

import { Router, Request, Response } from "express";
import {
  adaptSwitchFunctions,
  AdapterOptions,
} from "@badass-team-code/switch-functions";

type Targets = "body" | "params" | "query" | "headers";
type Values = "sportClub";

const options: AdapterOptions<Targets, Values> = [
  {
    target: {
      body: "sportClub",
    },
    expected: {
      sportClub: 1,
    },
    handle: (request: Request, response: Response) => {
      // Will run what club 1 needs
      return response.send();
    },
  },
  {
    handle: (request: Request, response: Response, next: NexFunction) => {
      // Here it will be executed if you do not enter in any case.
      return next();
    },
  },
];

const router = Router();

router.get("/", adaptSwitchFunctions(options));

export { router };
You might also like...

A quickstart AWS Lambda function code generator. Downloads a template function code file, test harness file, sample SAM deffiniation and appropriate file structure.

Welcome to function-stencil 👋 A quickstart AWS Lambda function code generator. Downloads a template function code file, test harness file, sample SAM

Jun 20, 2022

Tries to execute sync/async function, returns a specified default value if the function throws

good-try Tries to execute sync/async function, returns a specified default value if the function throws. Why Why not nice-try with it's 70+ million do

Dec 8, 2022

Wrap a function with bun-livereload to automatically reload any imports inside the function the next time it is called

bun-livereload Wrap a function with bun-livereload to automatically reload any imports inside the function the next time it is called. import liveRelo

Dec 19, 2022

This is a simple web application of a To-do List in which you can add, remove and edit all your tasks and also you can mark each task as completed and delete all completed ones.

To-Do List A simple web application of a to do list Built With HTML, CSS, JS, ES6 & Webpack Getting Started In this repository I created To-Do List SP

Apr 11, 2022

Run a command, watch the filesystem, stop the process on file change and then run the command again...

hubmon Run a command, watch the filesystem, stop the process on file change and then run the command again... Install You can install this command lin

Jul 30, 2022

A Virtual Interactive Keyboard which replicates every key you press and a Text-Area in which everything is written and can be copied to the clipboard with a click of a button.

A Virtual Interactive Keyboard which replicates every key you press and a Text-Area in which everything is written and can be copied to the clipboard with a click of a button.

Mar 1, 2021

A Feathers service adapter for Prisma ORM.

feathers-prisma A Feathers service adapter for Prisma ORM. Installation npm install feathers-prisma --save Documentation This adapter supports all me

Dec 9, 2022

🔨 A more engineered, highly customizable, standard output format commitizen adapter.

🔨 A more engineered, highly customizable, standard output format commitizen adapter.

cz-git Github | Installation | Website | 简体中文文档 Introduction A more engineered, highly customizable, standard output format commitizen adapter. What i

Dec 31, 2022

An example on how to use Solana Wallet Adapter as a Web Authentication Method.

An example on how to use Solana Wallet Adapter as a Web Authentication Method.

Solana Wallet Auth: A FullStack example This example uses Solana's wallet adapter to sign messages and verifies their signatures on the backend, allow

Dec 20, 2022
Owner
Badass Team
Three friends posting their codes on github 👨‍💻
Badass Team
This package enables you to define your routes using the flat-routes convention.

Remix Flat Routes This package enables you to define your routes using the flat-routes convention. This is based on the gist by Ryan Florence ?? Insta

Kiliman 180 Jan 3, 2023
A RESP 'Redis Serialization Protocol' library implementation to generate a server, uses a similar approach to express to define you serer, making it easy and fast.

RESPRESS A RESP 'Redis Serialization Protocol' library implementation to generate a server, uses a similar approach to express to define you serer, ma

Yousef Wadi 9 Aug 29, 2022
Define tool for JS/TS

JSDef A Define tool for js/ts Install npm i jsdefn HOW TO Create Defs import { JSDef } from 'jsdefn' const $ = JSDef({ "A": "apple" }) Get define

LAZPbanahaker 2 Apr 18, 2022
Bun-Bakery is a web framework for Bun. It uses a file based router in style like svelte-kit. No need to define routes during runtime.

Bun Bakery Bun-Bakery is a web framework for Bun. It uses a file based router in style like svelte-kit. No need to define routes during runtime. Quick

Dennis Dudek 44 Dec 6, 2022
This is a place to define better practices in code.

Better Practices Categories Angular General Programming GitHub Pages JavaScript Naming Conventions React Influence This guide is heavily influenced by

Bob Fornal 18 Sep 3, 2022
MerLoc is a live AWS Lambda function development and debugging tool. MerLoc allows you to run AWS Lambda functions on your local while they are still part of a flow in the AWS cloud remote.

MerLoc MerLoc is a live AWS Lambda function development and debugging tool. MerLoc allows you to run AWS Lambda functions on your local while they are

Thundra 165 Dec 21, 2022
Multithread emulator. The wrun allows you to dynamically run a function inside a Web Worker on the client side, without the needing of a dedicated file

wrun This lib allows you to dynamically run a function inside a Web Worker on the client side, without the needing of a dedicated file. This means tha

Felippe Regazio 9 Nov 5, 2022
Visual scraper interface, exports to puppeteer script which you can run anywhere.

Jawa - Visual Scraper Visual scraper interface, exports to puppeteer script which you can run anywhere. You can try it out here https://jawa.kickass.c

Ante Barić 4 Nov 16, 2022
The website which can help you to organize your daily or weekly activities and review them when you need them. you can add, remove and delete an activity

To Do list To do project is webpack project that list activities someone can do at a specific time In this TO-DO list, you can add or remove you activ

Joffrey NKESHIMANA 5 Jul 21, 2022