A simple yet extensible document converter backend application made with Nest.js.
$ npm install$ npm run build
# development
$ npm run start
# watch mode
$ npm run start:dev
# production mode
$ npm run start:prod# unit tests
$ npm run test
# e2e tests
$ npm run test:e2e
# test coverage
$ npm run test:covTo convert a document you can simply issue a POST request to the /convert route:
curl --request POST \
--url 'http://localhost:3000/conversion?targetFormat=json&segmentDelimiter=~&elementDelimiter=*' \
--header 'Content-Type: application/edi-x12' \
--data '<your-x12-edi-text-goes-here>'NOTE: Parameters: segmentDelimiter and elementDelimiter are always required when converting to and from EDI-X12.
curl --request POST \
--url 'http://localhost:3000/conversion?targetFormat=json' \
--header 'Content-Type: application/xml' \
--data '<your-xml-content-goes-here>'NOTE: if your XML content has a parent root element, make sure you send a querystring parameter called parentRootElement like so: http://localhost:3000/conversion?targetFormat=json&parentRootElement=root.
curl --request POST \
--url 'http://localhost:3000/conversion?targetFormat=x12&elementDelimiter=*&segmentDelimiter=~' \
--header 'Content-Type: application/json' \
--data '<your-json-content-goes-here>'NOTE: Parameters: segmentDelimiter and elementDelimiter are always required when converting to and from EDI-X12.
To support a new document type there are few things you must do.
Let's say that we want to add support for CSV files for example:
- Add your document type's content-type in
src/constants/allowed-content-types.ts:
export const AllowedContentTypes = {
x12: 'application/edi-x12',
json: 'application/json',
xml: 'application/xml',
csv: 'text/csv', // New content type 🎆!
};- Create a folder called
csvunder the directorysrc/app/documentand create aindex.tsfile inside of it:
import SerializableDocument from '../serializable-document';
import { SerializationOptions } from '../serialization-options';
import { SerializedType } from '../types';
export type CsvType = string;
export class CSVDocument extends SerializableDocument<CsvType> {
constructor(
protected readonly document: CsvType | SerializedType,
protected readonly contentType: string,
protected readonly options?: SerializationOptions,
) {
super(document, contentType, options);
}
async serialize(): Promise<SerializedType> {
if (this.options?.skipSerialization) {
return this.document as SerializedType;
}
this.serializedDocument = <implement a csv to json serialization algorithm here>;
return this.serializedDocument;
}
async deserialize(): Promise<JsonType> {
const deserializedDocument = <implement a json to csv deserialization algorithm here>;
return deserializedDocument;
}
}- Add your document type to the derived types of the
SerializableTypetype located atsrc/document/types.tsfile!
...
import { CsvType } from './csv';
export type SerializableType = EdiX12Type | XmlType | JsonType | CsvType;- Add your
CSVDocumentclass object factory to the list of document providers located at the filesrc/document/providers.ts:
...
[AllowedContentTypes.csv]: (document, contentType, options) => {
return new CSVDocument(document as CsvType, contentType, options);
},
};And that's it! You should now be able to transform any of the supported document types into a csv document!