0.0.1 ā€¢ Published 2 years ago

edifact-serializer v0.0.1

Weekly downloads
-
License
Apache-2.0
Repository
github
Last release
2 years ago

edifact-serializer

comment: <> (Sponsor) TypeScript version Node.js version APLv2

comment: <> (Build Status - GitHub Actions)

Template: node-typescript-boilerplate

šŸ‘©šŸ»šŸ’» Developer Ready: A comprehensive template. Works out of the box for most Node.js projects.

Getting Started

This project is intended to be used with the latest Active LTS release of Node.js.

Clone repository

To clone the repository, use the following commands:

git clone https://github.com/bloodred17/edifact-serializer.git
cd edifact-serializer
npm install

Usage

The following example demonstrates the creation of a Segment definition:

export const interchangeTrailerSegment = new EdifactSegment({
  name: 'interchange_trailer',
  tag: 'UNZ',
  elements: [
    // Elements and Compund Element definition
  ],
});
  • name : Identifier
  • tag : Valid Segment tag
  • elements : List of Element and Composite Element definitions

The following example demonstrates the creation of a single EDIFACT message Element:

const interchangeControlCount = new EdifactElement({
  name: 'interchange_control_count',
  tag: '0036',
  status: UseStatus.M,
  _format: 'n..6'
})
  • name : Identifier
  • tag : String
  • status : Enum that specifies if the value Mandatory or Conditional
  • _format : Edifact specified format for values

The following example demonstrates the creation of a EDIFACT Composite message element:

const documentMesageIdentification = new EdifactCompositeElement({
  name: 'document_message_identification',
  tag: 'C106',
  status: UseStatus.C,
  elements: [
    // Single message elements
  ]
})
  • name : Identifier
  • tag : String
  • status : Enum that specifies if the value Mandatory or Conditional
  • elements: List of Element definitions

The following example demonstrates a CUSDEC BGM segment definition:

import { EdifactCompositeElement, EdifactElement, EdifactSegment, UseStatus } from 'edifact-serializer';

export const beginningOfMessageSegment = new EdifactSegment({
  name: 'beginning_of_message',
  tag: 'BGM',
  counter: '0020',
  number: 3,
  level: 0,
  elements: [
    new EdifactCompositeElement({
      name: 'document_message_name',
      tag: 'C002',
      status: UseStatus.C,
      elements: [
        new EdifactElement({
          name: 'document_message_name_coded',
          tag: '1001',
          status: UseStatus.C,
          _format: 'an..3'
        }),
        new EdifactElement({
          name: 'code_list_qualifier',
          tag: '1131',
          status: UseStatus.C,
          _format: 'an..3'
        }),
        new EdifactElement({
          name: 'code_list_responsible_agency',
          tag: '3055',
          status: UseStatus.C,
          _format: 'an..3'
        }),
        new EdifactElement({
          name: 'document_message_name',
          tag: '1000',
          status: UseStatus.C,
          _format: 'an..3'
        }),
      ],
    }),
    new EdifactCompositeElement({
      name: 'document_message_identification',
      tag: 'C106',
      status: UseStatus.C,
      elements: [
        new EdifactElement({
          name: 'document_message_number',
          tag: '1004',
          status: UseStatus.C,
          _format: 'an..35'
        }),
        new EdifactElement({
          name: 'version',
          tag: '1056',
          status: UseStatus.C,
          _format: 'an..9'
        }),
        new EdifactElement({
          name: 'revision_number',
          tag: '1060',
          status: UseStatus.C,
          _format: 'an..6',
        }),
      ]
    }),
    new EdifactElement({
      name: 'message_function_coded',
      tag: '1225',
      status: UseStatus.C,
      _format: 'an..3'
    }),
  ],
});

Using the serializer:

const interchangeHeaderData = {
  syntax_identifier: 'UNOB',
  syntax_version_number: '4',
  interchange_sender_identification: '12345678ABC',
  interchange_code_qualifier: '',
  interchange_sender_internal_identification: 'ABCDEFGHIJKLMNOP',
  interchange_sender_internal_sub_identification: 'SPCAS2',
  interchange_recipient_identification: 'SARSDEC',
  date_of_preparation: '20190501',
  time_of_preparation: '1327',
  interchange_control_reference: '1234567890',
  recipient_reference_password: '',
  application_reference: 'CUSDEC',
  processing_priority_code: '',
  acknowledgement_request: '1',
  interchange_agreement_identifier: '',
  test_indicator: '1'
};

const messageHeaderData = {
  message_reference_number: '00000000155033',
  message_type_identifier: 'CUSDEC',
  message_type_version_number: 'D',
  message_type_release_number: '96B',
  controlling_agency: 'UN',
  association_assigned_code: 'ZZZ01',
};

const beginningOfMessageData = {
  document_message_name_coded: '929',
  code_list_qualifier: '',
  code_list_responsible_agency: '',
  document_message_name: 'RCD',
  document_message_number: '12345678ABC20190228654321',
  version: '012345',
  revision_number: '00001',
  message_function_coded: '9'
};

const interchangeMessage = await renderInterchangeMessage([
  { segment: unbSegment, data: interchangeHeaderData },
  { segment: unhSegment, data: messageHeaderData },
  { segment: beginningOfMessageSegment, data: beginningOfMessageData },
  //...other segments following...
]);

renderInterchangeMessageRenders EDIFACT message for a segment() function accepts array of type Message and outputs serialized data.

Message is an interface of segment {EdifactSegment} and data {any}

data is a map of edifact element identifier (name) and value. Refer to the example of 'beginningOfMessageData' and 'beginningOfMessageSegment' above.

Additional functions

renderSegment Renders EDIFACT message for a input segment and data.

validateValue Validates the value based on the EdifactElement definition.

getElementFormat Parses the EDIFACT data format string to useful values that can be used to validate the value

These functions are subject to change. Refer to the internal JSDoc definition for usage.

Features

The definitions are validated using the class-validator package and the data is validated on the basis of the specified format (specified using the _format property).

Bugs and Issue reporting

Add new issues here.

comment: <> (## Backers & Sponsors)

comment: <> (Support this project by becoming a sponsor.)

License

Licensed under the APLv2. See the LICENSE file for details.