5.0.9 • Published 8 months ago

@myunisoft/events v5.0.9

Weekly downloads
-
License
MIT
Repository
github
Last release
8 months ago

🚧 Requirements

  • Node.js version 18 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
variabledescriptiondefault
MYUNISOFT_EVENTS_LOGGER_MODESet log level for the default loggerinfo
Dispatcher
MYUNISOFT_DISPATCHER_IDLE_TIMEInterval threshold when Dispatcher become idle600_000
MYUNISOFT_DISPATCHER_CHECK_LAST_ACTIVITY_INTERVALDispatcher checking last activity interval120_000
MYUNISOFT_DISPATCHER_BACKUP_TRANSACTION_STORE_NAMEDefault name for backup transaction storebackup
MYUNISOFT_DISPATCHER_INIT_TIMEOUTDispatcher initialisation timeout3_500
MYUNISOFT_DISPATCHER_PING_INTERVALDispatcher ping interval3_500
Incomer
MYUNISOFT_INCOMER_INIT_TIMEOUTIncomer initialisation timeout3_500
MYUNISOFT_EVENTS_INIT_EXTERNALWhenever Incomer should initialize an external Dispatcherfalse
MYUNISOFT_INCOMER_MAX_PING_INTERVALMaximum ping interval60_000
MYUNISOFT_INCOMER_PUBLISH_INTERVALPublish interval60_000
MYUNISOFT_INCOMER_IS_DISPATCHERWeither Incomer is a Dispatcherfalse

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.

📚 Usage example

import * as Events, { type 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"
  }
};

Events.validate(event);

You can also use additional APIs to validate and narrow the data type depending on the operation:

if (Events.isCreateOperation(event.operation)) {
  // Do some code
}
else if (Events.isUpdateOperation(event.operation)) {
  // Do some code
}
else if (Events.isDeleteOperation(event.operation)) {
  // Do some code
}

!NOTE 👀 See here for the exhaustive list of Events.

💡 What is an Event?

A fully constituted event is composed of a name, an operation, and multiple objects such as data, scope and metadata.

  • The name identifies the event.
  • The operation defines if it is a creation, update, or deletion.
  • Based on the name, we know the data and the different metadata.origin.method related to it.
  • The metadata object is used to determine various pieces of information, such as the entry point.
  • The scope defines 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 interface Metadata {
  agent: string;
  origin?: {
    endpoint: string;
    method: "GET" | "POST" | "PATCH" | "PUT" | "DELETE" | "HEAD" | "OPTIONS" | (string & {});
    requestId?: string;
  };
  createdAt: number;
}

API

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

Throw an error if a given event is not recognized internally.

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"


EventOptions is described by the following type:

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

Exploiting Webhooks

👀 See the root example/fastify for an example of utilizing webhooks with an HTTP server.

In TypeScript, webhooks can be described using the WebhookResponse type:

import type { WebhookResponse } from "@myunisoft/events";

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()
  },
];
{
  "description": "Webhook",
  "type": "array",
  "items": {
    "type": "object",
    "properties": {
      "scope": {
        "$ref": "Scope"
      },
      "webhookId": {
        "type": "string"
      },
      "createdAt": {
        "type": "number"
      },
      "name": {
        "type": "string",
        "description": "event related name"
      },
      "operation": {
        "type": "string",
        "description": "event related operation",
        "enum": ["CREATE", "UPDATE", "DELETE", "VOID"]
      },
      "data": {
        "type": "object",
        "description": "event related data",
        "properties": {
          "id": {
            "type": "string"
          },
          "required": ["id"],
          "additionalProperties": true
        }
      }
    },
    "required": ["scope", "webhookId", "createdAt", "name", "operation", "data"],
    "additionalProperties": false
  }
}

Contributors ✨

All Contributors

Thanks goes to these wonderful people (emoji key):

License

MIT

5.0.9

8 months ago

5.0.8

10 months ago

5.0.7

11 months ago

5.0.6

11 months ago

5.0.5

11 months ago

5.0.4

12 months ago

5.0.3

1 year ago

5.0.2

1 year ago

5.0.1

1 year ago

5.0.0

1 year ago

4.3.2

1 year ago

4.3.4

1 year ago

4.3.3

1 year ago

4.3.6

1 year ago

4.3.1

1 year ago

4.3.0

1 year ago

4.2.24

1 year ago

4.2.23

1 year ago

4.2.22

1 year ago

4.2.20

1 year ago

4.2.19

1 year ago

4.2.18

1 year ago

4.2.17

1 year ago

4.2.16

1 year ago

4.2.15

1 year ago

4.2.14

1 year ago

4.2.13

2 years ago

4.2.11

2 years ago

4.2.12

2 years ago

3.4.0

2 years ago

3.4.4

2 years ago

3.4.3

2 years ago

3.4.2

2 years ago

3.4.1

2 years ago

4.2.3

2 years ago

4.0.5

2 years ago

4.2.2

2 years ago

4.0.4

2 years ago

4.2.5

2 years ago

4.0.7

2 years ago

4.2.4

2 years ago

4.0.6

2 years ago

4.0.1

2 years ago

4.0.0

2 years ago

4.2.1

2 years ago

4.0.3

2 years ago

4.2.0

2 years ago

4.0.2

2 years ago

4.2.7

2 years ago

4.2.6

2 years ago

4.0.8

2 years ago

4.2.9

2 years ago

4.2.8

2 years ago

3.4.8

2 years ago

3.4.7

2 years ago

3.4.6

2 years ago

3.4.5

2 years ago

3.3.1

2 years ago

3.3.0

2 years ago

3.3.5

2 years ago

3.3.4

2 years ago

3.3.3

2 years ago

3.3.2

2 years ago

4.1.4

2 years ago

4.2.10

2 years ago

4.1.3

2 years ago

4.1.6

2 years ago

4.1.5

2 years ago

4.1.0

2 years ago

4.1.2

2 years ago

4.1.1

2 years ago

3.2.0

2 years ago

3.1.12

2 years ago

3.1.11

2 years ago

3.1.10

2 years ago

3.1.9

2 years ago

3.1.8

2 years ago

3.1.3

2 years ago

3.1.2

2 years ago

3.1.7

2 years ago

3.1.6

2 years ago

3.1.4

2 years ago

3.1.1

2 years ago

3.1.0

2 years ago

3.0.1

3 years ago

3.0.0

3 years ago

1.4.2

3 years ago

2.0.0

3 years ago

1.4.1

3 years ago

1.4.0

3 years ago

1.2.0

3 years ago

1.3.0

3 years ago

1.2.1

3 years ago

1.1.0

3 years ago

1.0.4

3 years ago

1.0.3

3 years ago

1.0.2

3 years ago

1.0.1

3 years ago

1.0.0

3 years ago