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

@myunisoft/events

v4.3.1

Published

MyUnisoft Events validation

Downloads

450

Readme

🚧 Requirements

  • Node.js version 16 or higher
  • Docker (for running tests).

🚀 Getting Started

This package is available in the Node Package Repository and can be easily installed with npm or yarn

$ npm i @myunisoft/events
# or
$ yarn add @myunisoft/events

Publishing Events

Events

An Event fully constituted is composed by a name, an operation and multiple objects such as data, scope and metadata.

  • The name identify the event.
  • The operation will define if it is a creation, update or deletion.
  • According to the name, we know the data and the different metadata.origin.method related.
  • The metadata object is used to determine different information as the ecosystem, the entry point etc.
  • The scope will define the who.
export interface Scope {
  schemaId: number;
  firmId?: number | null;
  firmSIRET?: number | null;
  accountingFolderId?: number | null;
  accountingFolderSIRET?: number | null;
  accountingFolderRef?: string | null;
  persPhysiqueId?: number | null;
}

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

export interface Metadata {
  agent: string;
  origin?: {
    endpoint: string;
    method: Method;
    requestId?: string;
  };
  createdAt: number;
}

📚 Usage

Define and validate an event.

import * as MyEvents, { EventOptions } from "@myunisoft/events";

const event: EventOptions<"connector"> = {
  name: "connector",
  operation: "CREATE",
  scope: {
    schemaId: 1
  },
  metadata: {
    agent: "Node",
    origin: {
      endpoint: "http://localhost:12080/api/v1/my-custom-feature",
      method: "POST",
      requestId: crypto.randomUUID();
    },
    createdAt: Date.now()
  },
  data: {
    id: 1,
    code: "JFAC"
  }
};

MyEvents.validate<"connector">(event);

Define which operation the event has.

const event: EventOptions<"connector"> = {
  name: "connector",
  operation: "CREATE",
  scope: {
    schemaId: 1
  },
  metadata: {
    agent: "Node",
    origin: {
      endpoint: "http://localhost:12080/api/v1/my-custom-feature",
      method: "POST",
      requestId: crypto.randomUUID();
    },
    createdAt: Date.now()
  },
  data: {
    id: 1,
    code: "JFAC"
  }
};

if (isCreateOperation(event.operation)) {
  // Do some code
}

if (isUpdateOperation(event.operation)) {
  // Do some code
}

if (isDeleteOperation(event.operation)) {
  // Do some code
}

Environment Variables

[!IMPORTANT] Some options takes the lead over environment variables. For instance with: new Incomer({ dispatcherInactivityOptions: { maxPingInterval: 900_000 }}) the max ping interval will be 900_000 even if MYUNISOFT_INCOMER_MAX_PING_INTERVAL variable is set.

| variable | description | default | | --- | --- | --- | | MYUNISOFT_INCOMER_INIT_TIMEOUT | Incomer initialisation timeout | 3_500 | | MYUNISOFT_EVENTS_INIT_EXTERNAL | Weither Incomer should initialise an external Dispatcher | false | | MYUNISOFT_EVENTS_LOGGER_MODE | Set log level for the default logger | info | | MYUNISOFT_INCOMER_MAX_PING_INTERVAL | Maximum ping interval | 60_000 | | MYUNISOFT_INCOMER_PUBLISH_INTERVAL | Publish interval | 60_000 | | MYUNISOFT_INCOMER_IS_DISPATCHER | Weither Incomer is a Dispatcher | false | | MYUNISOFT_DISPATCHER_IDLE_TIME | Interval threshold when Dispatcher become idle | 600_000 | | MYUNISOFT_DISPATCHER_CHECK_LAST_ACTIVITY_INTERVAL | Dispatcher checking last activity interval | 120_000 | | MYUNISOFT_DISPATCHER_BACKUP_TRANSACTION_STORE_NAME | Default name for backup transaction store | backup | | MYUNISOFT_DISPATCHER_INIT_TIMEOUT | Dispatcher initialisation timeout | 3_500 | | MYUNISOFT_DISPATCHER_PING_INTERVAL | Dispatcher ping interval | 3_500 |

API

Dispatcher & Incomer class

There is the documentation of Dispatcher, and Incomer classes.


validate< T extends keyof Events >(options: EventOptions): void

Throw an error if a given event is not internaly known.


isCreateOperation< T extends keyof Events >(operation: EventOptions["operation"]): operation is Operation["create"]


isUpdateOperation< T extends keyof Events >(operation: EventOptions["operation"]): operation is Operation["update"]


isDeleteOperation< T extends keyof Events >(operation: EventOptions["operation"]): operation is Operation["delete"]

Types

EventOptions

export type EventOptions<K extends keyof EventsDefinition.Events> = {
  scope: Scope;
  metadata: Metadata;
} & EventsDefinition.Events[K];

const event: EventOptions<"connector"> = {
  name: "connector",
  operation: "CREATE",
  scope: {
    schemaId: 1
  },
  metadata: {
    agent: "Node",
    createdAt: Date.now(),
    requestId: crypto.randomUUID();
  },
  data: {
    id: 1,
    code: "JFAC"
  }
}

EventsOptions

type TupleToObject<T extends readonly any[],
  M extends Record<Exclude<keyof T, keyof any[]>, PropertyKey>> =
  { [K in Exclude<keyof T, keyof any[]> as M[K]]: T[K] };

export type EventsOptions<T extends (keyof EventsDefinition.Events)[] = (keyof EventsDefinition.Events)[]> = TupleToObject<[
  ...(EventOptions<T[number]>)[]
], []>;

const events: EventsOptions<["connector", "accountingFolder"]> = [
  {
    name: "connector",
    operation: "CREATE",
    scope: {
      schemaId: 1
    },
    metadata: {
      agent: "Node",
      createdAt: Date.now(),
      requestId: crypto.randomUUID();
    },
    data: {
      id: 1,
      code: "JFAC"
    }
  },
  {
    name: "accountingFolder",
    operation: "CREATE",
    scope: {
      schemaId: 1
    },
    metadata: {
      agent: "Windev",
      createdAt: Date.now(),
      requestId: crypto.randomUUID();
    },
    data: {
      id: 1
    }
  }
];

const event: EventsOptions<["connector", "accountingFolder"]> = {
  name: "connector",
  operation: "CREATE",
  scope: {
    schemaId: 1
  },
  metadata: {
    agent: "Node",
    createdAt: Date.now(),
    requestId: 0
  },
  data: {
    id: 1,
    code: "JFAC"
  }
}

Exploiting Webhooks

📚 Usage

👀 See here for an example of exploiting webhooks with an http server.

👀 See here for an exhaustive list of MyUnisoft Events you can subscribe to.

⚠️ A Webhook can send multiple Events on a single HTTP POST request.

Types

WebhooksResponse

JSON Schema

type WebhookResponse<K extends keyof EventTypes.Events> = {
  scope: Scope;
  webhookId: string;
  createdAt: number;
} & EventTypes.Events[K];

export type WebhooksResponse<T extends (keyof EventTypes.Events)[] = (keyof EventTypes.Events)[]> = [
  ...(WebhookResponse<T[number]>)[]
];

const response: WebhooksResponse<["connector", "accountingFolder"]> = [
  {
    name: "connector",
    operation: "CREATE",
    scope: {
      schemaId: 1
    },
    data: {
      id: 1,
      code: "JFAC"
    },
    webhookId: "1",
    createdAt: Date.now()
  },
  {
    name: "accountingFolder",
    operation: "CREATE",
    scope: {
      schemaId: 1
    },
    data: {
      id: 1
    },
    webhookId: "2",
    createdAt: Date.now()
  },
];

Contributors ✨

All Contributors

Thanks goes to these wonderful people (emoji key):

License

MIT