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

@mojaloop/ml-schema-transformer-lib

v2.5.1

Published

Shared component for ML schemas translation

Downloads

1,391

Readme

ML Schema Transformer (MLST)

Git Commit Git Releases Npm Version CircleCI

A shared component for transforming mojaloop payloads from one schema to another.

Contributing

Refer to CONTRIBUTING.md for information on how to contribute, committing changes, releases and snapshots.

Getting Started

Installing

npm i @mojaloop/ml-schema-transformer-lib

Usage

MLST can be used via the exported facades or the creator function.

Facades

// ESM
import { TransformFacades } from '@mojaloop/ml-schema-transformer-lib';
// CJS
const { TransformFacades } = require('@mojaloop/ml-schema-transformer-lib')

// `source` is an object with `body` property containing FSPIOP POST /quotes payload
const source = {
  body: {
    quoteId: 'random quote id',
    ...,
  }
};
// `target` is an object with `body` property containing  transformed FSPIOP ISO 20022 POST /quotes payload
const target = await TransformFacades.FSPIOP.quotes.post(source);

The source parameter may require params and/or headers properties depending on the use-case. target will always be { body } for ISO targets and { body, headers?, params? } for FSPIOP targets. The optional FSPIOP target headers and params will be populated as needed on use-case basis. Be sure to check the signature for the facade function you're using to be sure of what is expected as parameter and result. The facade functions have type guards that validate the source parameter to ensure they are of the right shape. The type guards will throw exceptions when the source parameter passed are not in the shape expected. The facade functions work with built-in mappings which are located in src/mappings directory. The facade functions take optional second parameter of type TransformFacadeOptions for controlling certain aspects of the function.

For example:

  • To override the mapping used in the function, pass in { overrideMapping: 'your mapping JSON string' }
  • To pass in additional options for the instantiation of MapTransform (the underlying transformation library), pass in { mapTransformOptions: { ...additionalMapTransformOptions } }.
  • To pass in additional options for the MapTransform mapper call, pass in { mapperOptions: { ...additionalMapperOptions } }

Creator Function

// ESM
import { createTransformer } from ' @mojaloop/ml-schema-transformer-lib';
// CJS
const { createTransformer } = require('@mojaloop/ml-schema-transformer-lib')

const mapping = { "quoteId": "CdtTrfTxInf.PmtId.TxId", ... }

const transformer = await createTransformer(mapping);
const transformer = createTransformer(mapping)

const source = { body: { quoteId: 'random quote id', ... }, ... };
const target = transformer.transform(source);

The createTransform function takes an optional second parameter of type CreateTransformerOptions for controlling the instantiation of the MapTransform object.

For example:

  • To pass in additional options for the instantiation of MapTransform, pass in { mapTransformOptions: { ...additionalMapTransformOptions } }

Custom Transform Functions

Custom transform functions are special functions which are embedded in field mappings and are executed during transformation of those fields. To provide custom transform functions (for both Facades and createTransformer use cases):

  const fn1 = (options) => () => (data, state) => {
    const modified = // apply custom transformation to `data`;
    return modified;
  }
  const fn2 = (options) => () => (data, state) => {
    const modified = // apply custom transformation to `data`;
    return modified;
  }
  const options = {
    mapTransformOptions: {
      transformers: {
        fn1,
        fn2,
        ...
      }
    }
  }

Replace fn1 and fn2 with the actual names of your functions. See src/lib/transforms/index.ts or here for more on authoring and using custom transform functions in mappings.

Extension List Support

MLST supports rolling up and unrolling FSPIOP extension lists as follows:

  • Extension list unrolling: Converts extension lists from FSPIOP sources to properties in ISO 20022 targets.
  • Extension list roll-up: Converts unmapped properties in ISO sources to an extension list in FSPIOP targets.

This feature can be enabled via facade configuration, for example:

TransformFacades.FSPIOP.configure({ unrollExtensions: true })
TransformFacades.FSPIOPISO20022.configure({ rollUpUnmappedAsExtensions: true })

Alternatively, the feature can be activated through specific endpoints, for example:

TransformFacades.FSPIOP.quotes.post(source, { unrollExtensions: true })
TransformFacades.FSPIOPISO20022.quotes.post(source, { rollUpUnmappedAsExtensions: true })

Note:

  • unrollExtensions is supported only in TransformFacades.FSPIOP, while rollUpUnmappedAsExtensions is supported only in TransformFacades.FSPIOPISO20022.
  • With the exception of parties, fxQuotes and errorInformation payloads/APIs, unrollExtensions unrolls only the top-level body.extensionList property. This is because without a mapping it is impossible to determine automatically how to apply multiple unrolled extension lists in different locations in an FSPIOP object to a target ISO object.
  • unrollExtensions does not overwrite existing properties in the target object. If an unrolled extension property already exists in the target object, it will be skipped.

Environment Variables

| Env Variable Name | Default Value | Description | |-----------------------------|---------------|------------------------------------------------------| | MLST_LOG_LEVEL | warn | The log level for MLST | | MLST_ILP_VERSION | v4 | ILP version used for ilpPacketToCondition transform. Note: ILP v1 is not supported| | MLST_TESTING_MODE | false | When true, some mapping contains defaults that don't need context from past requests. Useful for one off request transformations.

Performance

Hardware → Apple M3 Max, 14 Cores, 36 GB RAM

| Use Case | No. of Iterations | Duration | |-------------------------------------------------|-------------------|----------------| | POST /quotes transformation { FSPIOP → ISO } | 1000 | ~301ms | | POST /quotes transformation { ISO → FSPIOP } | 1000 |~205ms | | POST /transfers transformation { FSPIOP → ISO } | 1000 | ~135ms | | POST /transfers transformation { ISO → FSPIOP } | 1000 | ~79ms |

Development

Install Depednencies

npm i

Run Tests

npm test

Build

npm run build