Skip to content

M4JSAPI inside Node.js (Promises & Observables in Typescript)

Notifications You must be signed in to change notification settings

franjmr/m4Node.js

Repository files navigation

M4Node.js

M4Node.js abstracts the M4JSAPI library and provides a instances builder with an api of it per user session.

The prototype is implemented in typescript and offers the possibility of interacting with the M4JSAPI library with promises (async / await) and observables (Rxjs)

Purpose:

  • Create a user session per instance to work in different contexts with the M4JSAPI library.
  • Provide an API that can be easily integrated with Node.js developments.
  • Object Oriented Programming Features.
  • Back-end Developer.
  • TypeScript: Strict typing, Structural typing, Type annotations and Type inference.
  • Observable Rxjs: Observer pattern (reactive programming) to write asynchronous code.
  • Promises: To write asynchronous code a promise represents a value that may be available now, in the future, or never.
  • Testing with Jasmine framework.

Getting Started

  • M4Node.js: provides an api to work, mock and debug the M4JSAPI
  • M4Utils: provides tools to make test implementation easier

Running from command line

Install using npm

npm install @automation/m4nodejs

Usage M4Node.js in a node app

Create a file named myfirstcode.ts, this will contain:

import { M4ApiNodejs } from "@automation/m4nodejs";
import { M4ApiNode } from "@automation/m4nodejs/dist/m4apiNode";;

async function executeLogon(){
    const m4ApiNode = await M4ApiNodejs('http://myserver.domain.com:9999','Pau','Phoenix');
    const m4LogonResult = await m4ApiNodejs.logon();
    console.log(m4LogonResult.getToken());
    await m4ApiNodejs.logout();
}

executeLogon();

Usage M4Node.js in a Jasmine test

Create a file named myfirstcode.spec.ts, this will contain:

import { M4ApiNodejs } from "@automation/m4nodejs";
import { M4ApiNode } from "@automation/m4nodejs/dist/m4apiNode";

describe("M4JSAPI - Logon suite", () => {
    let m4ApiNodejs: M4ApiNode ;
    
    beforeAll(async ()=>{
        m4ApiNodejs = await M4ApiNodejs('http://myserver.domain.com:9999','Marshall','Law');
    });

    afterAll(async() => {
        await m4ApiNodejs.logout();
    })

    it("should do logon", async () => {
        const token = await m4ApiNodejs.logon()
        expect(token).toBeTruthy();
    });
});

Usage M4Node.js Mocking in a Jasmine test

  • Set M4Object XML metadata in "./mocks/metadata/" project folder
  • Create a file named myfirstcode.mock.spec.ts, this will contain:
import {M4ApiNodejs} from "@automation/m4nodejs"
import { M4ApiNode } from "@automation/m4nodejs/dist/m4apiNode";
import * as fs from "fs";

describe("M4JSAPI - Mock suite", ()=>{
    
    let m4Nodejs : M4ApiNode;
    
    beforeAll(async()=>{
        m4Nodejs = await M4ApiNodejs("http://myserver.domain.com:9999", "Chuck_Norris", "Walker Texas Ranger");
        m4Nodejs.__mock__initialize__();
        m4Nodejs.__mock__setM4ObjectMetadata__("PLCO_LOAD_ALL_PERSONAL_INFO", fs.readFileSync("./__mocks__/metadata/PLCO_LOAD_ALL_PERSONAL_INFO.xml", 'utf8'))
    })

    it("should create M4JSAPI objects with mocked metadata",async()=>{
        const m4ObjLoadAllPersonInfoMock = await m4Nodejs.createM4Object("PLCO_LOAD_ALL_PERSONAL_INFO");
        const m4RequestLoadAllPersonalInfoMock = m4Nodejs.createM4Request(m4ObjLoadAllPersonInfoMock, "PLCO_PERSONAL_EMPLOYEE_INFORMT", "PLCO_LOAD_ALL_PERSONAL_INFO", ["","",""]);
        expect(m4ObjLoadAllPersonInfoMock).toBeTruthy();
        expect(m4RequestLoadAllPersonalInfoMock).toBeTruthy();
    });
});

Usage M4TestUtils to extract Metadata content to a xml file

Create a file named myfirstcode.create.xml.spec.ts, this will contain:

import { M4TestUtils } from "@automation/m4nodejs";

async function example(){
    const m4TestUtils = new M4TestUtils();
    await m4TestUtils.createXmlMetadataFile("http://myserver.domain.com:9999","Lee","Chaolan","M4OBJECT_ID","C:/Temp");
}

example();

M4Node.js

Api Reference to interact with M4JSAPI

  • createM4Object(m4objectId: string): Promise; Create object instance asynchronous. [Implicitly load metadata]
  • createM4Request(m4object: M4Object, nodeId: string, methodId: string, methodArgs: any[]): M4Request; Create M4Request instance
  • createObservableByNodeCurrentChanged(m4Node: M4Node): rxjs.Observable; Register node current changed callback as RxJS Observable
  • createObservableByNodeItemChanged(m4Node: M4Node): rxjs.Observable; Register node item changed callback as RxJS Observable
  • createObservableByNodeRecordsChanged(m4Node: M4Node): rxjs.Observable; Register node records changed callback as RxJS Observable
  • executeM4ObjectMethod(m4object: M4Object, nodeId: string, methodId: string, methodArgs: any[]): Promise; Execute method promise-based asynchronous
  • executeM4Request(m4Request: M4Request): Promise; Execute MRequest instance
  • executeMethod(m4objectId: string, nodeId: string, methodId: string, methodArgs: any[]): Promise; Execute method promise-based asynchronous. [Implicitly load metadata]
  • executeMethodObservable(m4objectId: string, nodeId: string, methodId: string, methodArgs: any[]): rxjs.Observable; Convert execute method Promise to Observable RxJS. [Implicitly load metadata]
  • loadMetadata(m4objects: string[]): Promise; Load Metadata promise-based asynchronous
  • logon(): Promise; Logon User promise-based asynchronous
  • logout(): Promise; Logout User promise-based asynchronous

Api Reference to mocking M4JSAPI

  • mock__initialize(): void; Initialize M4JSAPI Mock (Override M4Executor.LoadMetadata: Load XML Metadata from mock)
  • mock__reset(): void; Reset Mock (Clear M4Object XML Metadata mocked)
  • mock__finalize(): void; Finalize M4JSAPI Mock (Restores original M4Executor.LoadMetadata)
  • mock__setM4ObjectMetadata(m4objectId: string, m4ObjectMetadata: string): void; Set M4Object Metadata Content to mocking M4Executor.LoadMetadata
  • getWindowObject(): Window; Get Window Object from this instance

Api Reference to Debugging

  • enableConsoleMessages(): void; Enable Console messages
  • disableConsoleMessages(): void; Disable Console messages
  • getApiUrl(): string; Returns Api URL (M4JSAPI Node URL)
  • getCookieStore(): tough.MemoryCookieStore; Returns Cookie Storage (https://www.npmjs.com/package/tough-cookie)
  • getUser(): string; Returns User property value setted in constructor
  • getServer(): string; Returns Server property value setted in constructor

M4TestUtils

Api Reference

  • createXmlMetadataFile(server: string, user: string, pass :string, m4objectId: string, filepath: string): void; Create xml file with the M4Object metadata definition

Documentation

About

M4JSAPI inside Node.js (Promises & Observables in Typescript)

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages