1.0.29 • Published 3 days ago

@jaypie/core v1.0.29

Weekly downloads
-
License
-
Repository
-
Last release
3 days ago

Jaypie Core 🐦‍⬛🥧

Utility package with constants, errors, functions and logging

🐦‍⬛ Introduction

Jaypie is an opinionated approach to application development centered around JavaScript and the JSON:API specification in an event-driven architecture

Beyond that Jaypie core tries to stay neutral. Jaypie libraries such as @jaypie/express build off of this core into specific ecosystems

In an ideal world the bird would be looking at the pie

📋 Usage

Installation

npm install @jaypie/core

Example

Simple Example Using Lambda

event is passed to the handler when the event is triggered. It will include details about the event (e.g., type, parameters).

import { lambdaHandler, log } from "@jaypie/core";

export default lambdaHandler(
  // Handler function
  async (event) => {
    log.trace("\"Happy path\" events belong in trace");

    // "Your Code Here"

    return "Hello, world!";
  },
  // Configuration (optional)
  {
    name: "helloWorld",
  },
);

Throw jaypie errors below to convey semantic meaning. Thrown errors will be caught and logged. Non-jaypie errors will be treated as unhandled exceptions. Log your own errors with log.error.

import { log, NotFoundError } from "@jaypie/core";

log.error("Could not find resource");
throw NotFoundError();
Simple Logging
log.trace("\"Happy path\" events belong in trace");

// Important variables for debugging can be logged as "var"s
log.var("message", "Hello, world");
log.var({ message: "Hello, world" });
log.var({ message });

if(event.strangeCondition) {
  log.debug("Slight deviation from the happy path");
  // ...Helpful for someone debugging a real problem later
}

if(event.problemCondition) {
  log.warn("Someone should look into this");
  // ...It can at least be tracked to know how often it happens
}

if(event.errorCondition) {
  log.error("Something is not right!");
  // ...It might even be broken
}

log functions should be used in your logic. Following the "trace-only happy path" will simplify debugging and log management.

log.info exists for things that should be tracked external to debugging (e.g., transaction successes). By default the event and the response from the handler will be logged as info. This should be the least-used log level by code references (we hope error and warn are unused in runtime)

Complex Example Using Express

import { expressHandler, log } from "@jaypie/core";

export default expressHandler(
  // Handler function
  async ({ req }) => {
    log.trace("\"Happy path\" events belong in trace");

    // "Your Code Here"

    return json;
  },
  // Configuration (optional)
  {
    name: "expressApi",
    validate: [], // Array of validation functions
    setup: [], // Array of setup functions
    local: [], // Array of local values and getters
    teardown: [], // Array of teardown functions
  },
);

The function:

  • Only receives req and it must be destructured
  • Should return a JSON object, ideally JSON:API-compliant 🤩
  • May return a scalar which will be treated as text/html
  • Should throw a jaypie error if there is a problem

TODO: This example needs specifics on validate, setup, local, and teardown

📖 Reference

Constants

TODO: Complete list of constants in the package

Errors

TODO: Complete list of errors in the package

Functions

TODO: Complete list of utility functions in the package

Handlers

TODO: Complete list of utility functions in the package

Logging

import { 
  log,
  LOG, // LOG.FORMAT, LOG.LEVEL
} from "@jaypie/core";

log

import { log } from "@jaypie/core";

log.trace();
log.debug();
log.info();
log.warn();
log.error();
log.fatal();
log.lib({ lib: "myLib" })

Uses silent by default. if process.env.MODULE_LOG_LEVEL is true, follows process.env.LOG_LEVEL. If process.env.MODULE_LOG_LEVEL is also set, uses that log level.

import { log } from "@jaypie/core";

log.lib().trace();
log.lib({ lib: "myLib" }).trace();
log.var(key, value) or log.var({ key: value })

Log a key-value pair. In the json format, the key will be tagged as var and the value will be the value. Logging marker variables this way can be useful for debugging.

import { log } from "@jaypie/core";

log.var("message", "Hello, world");
log.var({ message: "Hello, world" });

const message = "Hello, world";
log.var({ message });
log.with() - clone

Create a new log object with additional tags

import { log as defaultLogger } from "@jaypie/core";

const log = defaultLogger.with({ customProperty: "customValue" });

🌠 Wishlist

  • Complete documentation

📝 Changelog

DateVersionSummary
3/19/20241.0.0First stable API
3/18/20240.5.8💥 Last version to support silent
3/18/20240.5.0💥 Removes expressHandler, changes logging APIs
3/16/20240.4.0💥 Breaking: lambdaHandler migrated to @jaypie/lambda
3/11/20240.3.0Export expressHandler
3/10/20240.2.0Export lambdaHandler
3/5/20240.1.0Export all the core helpers
3/5/20240.0.1Initial commit

📜 License

Published by Finlayson Studio. All rights reserved

1.0.29

3 days ago

1.0.28

3 days ago

1.0.27

10 days ago

1.0.26

11 days ago

1.0.25

13 days ago

1.0.24

13 days ago

1.0.22

16 days ago

1.0.23

16 days ago

1.0.19

16 days ago

1.0.21

16 days ago

1.0.20

16 days ago

1.0.18

18 days ago

1.0.17

21 days ago

1.0.16

21 days ago

1.0.15

22 days ago

1.0.14

22 days ago

1.0.13

22 days ago

1.0.12

22 days ago

1.0.9

23 days ago

1.0.11

22 days ago

1.0.10

23 days ago

1.0.8

1 month ago

1.0.7

1 month ago

1.0.4

1 month ago

1.0.3

1 month ago

1.0.2

1 month ago

1.0.1

1 month ago

1.0.0

2 months ago

0.5.8

2 months ago

0.5.7

2 months ago

0.5.4

2 months ago

0.5.3

2 months ago

0.5.6

2 months ago

0.5.5

2 months ago

0.5.0

2 months ago

0.5.2

2 months ago

0.5.1

2 months ago

0.3.6

2 months ago

0.3.5

2 months ago

0.3.7

2 months ago

0.4.1

2 months ago

0.4.0

2 months ago

0.4.3

2 months ago

0.3.4

2 months ago

0.4.2

2 months ago

0.3.0

2 months ago

0.3.2

2 months ago

0.3.1

2 months ago

0.3.3

2 months ago

0.2.3

2 months ago

0.2.1

2 months ago

0.2.0

2 months ago

0.2.2

2 months ago

0.1.0

2 months ago

0.0.4

2 months ago

0.0.3

2 months ago