4.3.1 • Published 26 days ago

@myunisoft/events v4.3.1

Weekly downloads
-
License
MIT
Repository
github
Last release
26 days ago

🚧 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.

variabledescriptiondefault
MYUNISOFT_INCOMER_INIT_TIMEOUTIncomer initialisation timeout3_500
MYUNISOFT_EVENTS_INIT_EXTERNALWeither Incomer should initialise an external Dispatcherfalse
MYUNISOFT_EVENTS_LOGGER_MODESet log level for the default loggerinfo
MYUNISOFT_INCOMER_MAX_PING_INTERVALMaximum ping interval60_000
MYUNISOFT_INCOMER_PUBLISH_INTERVALPublish interval60_000
MYUNISOFT_INCOMER_IS_DISPATCHERWeither Incomer is a Dispatcherfalse
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

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

4.3.1

26 days ago

4.3.0

1 month ago

4.2.24

1 month ago

4.2.23

2 months ago

4.2.22

2 months ago

4.2.20

3 months ago

4.2.19

3 months ago

4.2.18

3 months ago

4.2.17

3 months ago

4.2.16

4 months ago

4.2.15

4 months ago

4.2.14

4 months ago

4.2.13

5 months ago

4.2.11

5 months ago

4.2.12

5 months ago

3.4.0

10 months ago

3.4.4

9 months ago

3.4.3

9 months ago

3.4.2

10 months ago

3.4.1

10 months ago

4.2.3

6 months ago

4.0.5

8 months ago

4.2.2

6 months ago

4.0.4

8 months ago

4.2.5

6 months ago

4.0.7

7 months ago

4.2.4

6 months ago

4.0.6

7 months ago

4.0.1

8 months ago

4.0.0

8 months ago

4.2.1

6 months ago

4.0.3

8 months ago

4.2.0

6 months ago

4.0.2

8 months ago

4.2.7

6 months ago

4.2.6

6 months ago

4.0.8

7 months ago

4.2.9

6 months ago

4.2.8

6 months ago

3.4.8

9 months ago

3.4.7

9 months ago

3.4.6

9 months ago

3.4.5

9 months ago

3.3.1

10 months ago

3.3.0

10 months ago

3.3.5

10 months ago

3.3.4

10 months ago

3.3.3

10 months ago

3.3.2

10 months ago

4.1.4

7 months ago

4.2.10

5 months ago

4.1.3

7 months ago

4.1.6

7 months ago

4.1.5

7 months ago

4.1.0

7 months ago

4.1.2

7 months ago

4.1.1

7 months ago

3.2.0

11 months ago

3.1.12

11 months ago

3.1.11

11 months ago

3.1.10

11 months ago

3.1.9

11 months ago

3.1.8

11 months ago

3.1.3

11 months ago

3.1.2

11 months ago

3.1.7

11 months ago

3.1.6

11 months ago

3.1.4

11 months ago

3.1.1

1 year ago

3.1.0

1 year ago

3.0.1

1 year ago

3.0.0

1 year ago

1.4.2

2 years ago

2.0.0

2 years ago

1.4.1

2 years ago

1.4.0

2 years ago

1.2.0

2 years ago

1.3.0

2 years ago

1.2.1

2 years ago

1.1.0

2 years ago

1.0.4

2 years ago

1.0.3

2 years ago

1.0.2

2 years ago

1.0.1

2 years ago

1.0.0

2 years ago