Commit 92a8f01b authored by Panagiotis Skarvelis's avatar Panagiotis Skarvelis
Browse files

make it hybrid esm/commonjs

parent 963db353
......@@ -25,10 +25,25 @@ export declare type errorRecord = {
errorCode: string;
errorDescr: string;
};
/**
* @type Overrides
* @description Overrides for the SOAP client
* @param {boolean} prod - Set to true for production environment
* @param {string} auditInit - Audit record initializer to be used for the audit record produced
* @param {string} auditStoragePath - Path to the audit record storage
*/
export declare type Overides = {
prod?: boolean;
auditInit?: AuditRecord;
auditStoragePath?: string;
};
/**
*
* @param afm string;
* @param user string;
* @param pass string;
* @param overides Overides;
* @returns AuditRecord | errorRecord
*/
export declare const getIdentity: (afm: string, user: string, pass: string, overides?: Overides | undefined) => Promise<any>;
export default getIdentity;
"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.getIdentity = void 0;
const soapClient_js_1 = __importDefault(require("./soapClient.js"));
const gsis_audit_record_db_1 = require("@digigov-oss/gsis-audit-record-db");
const config_json_1 = __importDefault(require("./config.json"));
/**
*
* @param afm string;
* @param user string;
* @param pass string;
* @param overides Overides;
* @returns AuditRecord | errorRecord
*/
const getIdentity = (afm, user, pass, overides) => __awaiter(void 0, void 0, void 0, function* () {
var _a, _b, _c;
const prod = (_a = overides === null || overides === void 0 ? void 0 : overides.prod) !== null && _a !== void 0 ? _a : false;
const auditInit = (_b = overides === null || overides === void 0 ? void 0 : overides.auditInit) !== null && _b !== void 0 ? _b : {};
const auditStoragePath = (_c = overides === null || overides === void 0 ? void 0 : overides.auditStoragePath) !== null && _c !== void 0 ? _c : "/tmp";
const wsdl = prod == true ? config_json_1.default.prod.wsdl : config_json_1.default.test.wsdl;
const auditRecord = (0, gsis_audit_record_db_1.generateAuditRecord)(auditInit, auditStoragePath);
if (!auditRecord)
throw new Error('Audit record is not initialized');
try {
const s = new soapClient_js_1.default(wsdl, user, pass, auditRecord);
const Identity = yield s.getIdentity(afm);
return Identity;
}
catch (error) {
throw (error);
}
});
exports.getIdentity = getIdentity;
exports.default = exports.getIdentity;
{
"type": "commonjs"
}
import soap from 'soap';
import { AuditRecord } from '@digigov-oss/gsis-audit-record-db';
/**
* SOAP client for getNncIdentity
* @class Soap
* @description SOAP client for getNncIdentity
* @param {string} wsdl - The URL of the SOAP service
* @param {string} username
* @param {string} password
* @param {AuditRecord} auditRecord
*/
declare class Soap {
private _client;
private _wsdl;
......@@ -7,7 +15,7 @@ declare class Soap {
private _password;
private _auditRecord;
constructor(wsdl: string, username: string, password: string, auditRecord: AuditRecord);
init(): Promise<soap.Client>;
init(): Promise<any>;
getIdentity(vat: string): Promise<any>;
}
export default Soap;
"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const soap_1 = __importDefault(require("soap"));
let soap = soap_1.default;
try {
soap = require('soap');
}
catch (error) {
//my hackish way to make soap work on both esm and cjs
//theshoap on esm is undefined
//On esm require is not defined, however on cjs require can be used.
//So we try to use require and if it fails we use the thesoap module
}
/**
* SOAP client for getNncIdentity
* @class Soap
* @description SOAP client for getNncIdentity
* @param {string} wsdl - The URL of the SOAP service
* @param {string} username
* @param {string} password
* @param {AuditRecord} auditRecord
*/
class Soap {
constructor(wsdl, username, password, auditRecord) {
this._wsdl = wsdl;
this._username = username;
this._password = password;
this._auditRecord = auditRecord;
}
init() {
return __awaiter(this, void 0, void 0, function* () {
try {
const client = yield soap.createClientAsync(this._wsdl, {
wsdl_headers: {
'Authorization': 'Basic ' + Buffer.from(`${this._username}:${this._password}`).toString('base64'),
},
});
this._client = client;
return client;
}
catch (e) {
throw e;
}
});
}
getIdentity(vat) {
return __awaiter(this, void 0, void 0, function* () {
try {
const client = yield this.init();
var options = {
hasNonce: true,
actor: 'actor'
};
var wsSecurity = new soap.WSSecurity(this._username, this._password, options);
client.setSecurity(wsSecurity);
const auditRecord = this._auditRecord;
const args = {
auditRecord: auditRecord,
getNncIdentityInputRecord: {
afm: vat
}
};
const result = yield client.getNncIdentityAsync(args);
return result[0].getNncIdentityOutputRecord;
}
catch (e) {
throw e;
}
});
}
}
exports.default = Soap;
{
"test": {
"wsdl": "https://test.gsis.gr/esbpilot/notificationCenterElementsService?wsdl"
},
"prod": {
"wsdl": "https://www1.gsis.gr/esb/notificationCenterElementsService?wsdl"
}
}
import { AuditRecord } from '@digigov-oss/gsis-audit-record-db';
export declare type AuditInit = AuditRecord;
export declare type getNncIdentityOutputRecord = {
countryCode?: string;
countryDescr?: string;
addressStreet?: string;
addressNumber?: string;
addressCity?: string;
addressZipCode?: string;
telephone?: string;
countryCode2?: string;
countryDescr2?: string;
addressStreet2?: string;
addressNumber2?: string;
addressCity2?: string;
addressZipCode2?: string;
telephone2?: string;
mobile?: string;
email?: string;
epikForeisFlag?: string;
epidForeisFlag?: string;
message?: string;
};
export declare type errorRecord = {
errorCode: string;
errorDescr: string;
};
/**
* @type Overrides
* @description Overrides for the SOAP client
* @param {boolean} prod - Set to true for production environment
* @param {string} auditInit - Audit record initializer to be used for the audit record produced
* @param {string} auditStoragePath - Path to the audit record storage
*/
export declare type Overides = {
prod?: boolean;
auditInit?: AuditRecord;
auditStoragePath?: string;
};
/**
*
* @param afm string;
* @param user string;
* @param pass string;
* @param overides Overides;
* @returns AuditRecord | errorRecord
*/
export declare const getIdentity: (afm: string, user: string, pass: string, overides?: Overides | undefined) => Promise<any>;
export default getIdentity;
import soap from './soap';
import soapClient from './soapClient.js';
import { generateAuditRecord } from '@digigov-oss/gsis-audit-record-db';
import config from './config.json';
/**
*
* @param afm string;
* @param user string;
* @param pass string;
* @param overides Overides;
* @returns AuditRecord | errorRecord
*/
export const getIdentity = async (afm, user, pass, overides) => {
const prod = overides?.prod ?? false;
const auditInit = overides?.auditInit ?? {};
......@@ -10,7 +18,7 @@ export const getIdentity = async (afm, user, pass, overides) => {
if (!auditRecord)
throw new Error('Audit record is not initialized');
try {
const s = new soap(wsdl, user, pass, auditRecord);
const s = new soapClient(wsdl, user, pass, auditRecord);
const Identity = await s.getIdentity(afm);
return Identity;
}
......
{
"type": "module"
}
import soap from 'soap';
import { AuditRecord } from '@digigov-oss/gsis-audit-record-db';
/**
* SOAP client for getNncIdentity
* @class Soap
* @description SOAP client for getNncIdentity
* @param {string} wsdl - The URL of the SOAP service
* @param {string} username
* @param {string} password
* @param {AuditRecord} auditRecord
*/
declare class Soap {
private _client;
private _wsdl;
......@@ -7,7 +15,7 @@ declare class Soap {
private _password;
private _auditRecord;
constructor(wsdl: string, username: string, password: string, auditRecord: AuditRecord);
init(): Promise<soap.Client>;
init(): Promise<any>;
getIdentity(vat: string): Promise<any>;
}
export default Soap;
import soap from 'soap';
class Soap {
_client;
_wsdl;
_username;
_password;
_auditRecord;
constructor(wsdl, username, password, auditRecord) {
this._wsdl = wsdl;
this._username = username;
this._password = password;
this._auditRecord = auditRecord;
}
async init() {
try {
const client = await soap.createClientAsync(this._wsdl, {
wsdl_headers: {
'Authorization': 'Basic ' + Buffer.from(`${this._username}:${this._password}`).toString('base64'),
},
});
this._client = client;
return client;
}
catch (e) {
throw e;
}
}
async getIdentity(vat) {
try {
const client = await this.init();
var options = {
hasNonce: true,
actor: 'actor'
};
var wsSecurity = new soap.WSSecurity(this._username, this._password, options);
client.setSecurity(wsSecurity);
const auditRecord = this._auditRecord;
const args = {
auditRecord: auditRecord,
getNncIdentityInputRecord: {
afm: vat
}
};
const result = await client.getNncIdentityAsync(args);
return result[0].getNncIdentityOutputRecord;
}
catch (e) {
throw e;
}
}
}
export default Soap;
import { AuditRecord } from '@digigov-oss/gsis-audit-record-db';
export declare type Overides = {
test?: boolean;
auditInit?: AuditRecord;
auditStoragePath?: string;
};
export declare const getIdentity: (afm: string, user: string, pass: string, overides?: Overides | undefined) => Promise<any>;
export default getIdentity;
import soap from './soap';
import { generateAuditRecord } from '@digigov-oss/gsis-audit-record-db';
import config from '../config.json';
export const getIdentity = async (afm, user, pass, overides) => {
const test = overides?.test ?? false;
const auditInit = overides?.auditInit ?? {};
const auditStoragePath = overides?.auditStoragePath ?? "/tmp";
const wsdl = test ? config.test.wsdl : config.prod.wsdl;
const auditRecord = generateAuditRecord(auditInit, auditStoragePath);
if (!auditRecord)
throw new Error('Audit record is not initialized');
try {
const s = new soap(wsdl, user, pass, auditRecord);
const Identity = await s.getIdentity(afm);
return Identity;
}
catch (error) {
throw (error);
}
};
export default getIdentity;
import soap from 'soap';
class Soap {
_client;
_wsdl;
_username;
_password;
_auditRecord;
constructor(wsdl, username, password, auditRecord) {
this._wsdl = wsdl;
this._username = username;
this._password = password;
this._auditRecord = auditRecord;
}
async init() {
try {
const client = await soap.createClientAsync(this._wsdl, {
wsdl_headers: {
'Authorization': 'Basic ' + Buffer.from(`${this._username}:${this._password}`).toString('base64'),
},
});
this._client = client;
return client;
}
catch (e) {
throw e;
}
}
async getIdentity(vat) {
try {
const client = await this.init();
var options = {
hasNonce: true,
actor: 'actor'
};
var wsSecurity = new soap.WSSecurity(this._username, this._password, options);
client.setSecurity(wsSecurity);
const auditRecord = this._auditRecord;
const args = {
auditRecord: auditRecord,
getNncIdentityInputRecord: {
afm: vat
}
};
const result = await client.getNncIdentityAsync(args);
return result[0].getNncIdentityOutputRecord;
}
catch (e) {
throw e;
}
}
}
export default Soap;
......@@ -8,22 +8,30 @@
},
"author": "Panagiotis Skarvelis <pskarvelis@admin.grnet.gr>",
"dependencies": {
"@digigov-oss/gsis-audit-record-db": "^1.0.1",
"@digigov-oss/gsis-audit-record-db": "^1.0.2",
"soap": "^0.43.0"
},
"repository": {
"type": "git",
"url": "git+https://gitlab.grnet.gr/digigov-oss/emepclient.git"
},
"main": "./dist/index.js",
"types": "./dist/index.d.ts",
"main": "dist/cjs/index.js",
"module": "dist/esm/index.js",
"exports": {
".": {
"import": "./dist/esm/index.js",
"require": "./dist/cjs/index.js"
}
},
"directories": {
"src": "src",
"test": "test"
},
"scripts": {
"test": "node --experimental-specifier-resolution=node --experimental-modules --no-warnings --loader ts-node/esm ./test/test.ts",
"build": "tsc --declaration"
"testesm": "node --experimental-json-modules --no-warnings ./test/test.mjs",
"testcjs": "node ./test/test.cjs",
"build": "rm -fr dist/* && tsc -p tsconfig.json && tsc -p tsconfig.commonjs.json && ./setType.sh"
},
"eslintConfig": {
"parser": "@typescript-eslint/parser",
......@@ -61,7 +69,10 @@
"emep",
"getNncIdentity"
],
"license": "MIT",
"licenses": [{
"type": "MIT",
"url" : "http://en.wikipedia.org/wiki/MIT_License"
}],
"devDependencies": {
"@types/node": "^16.9.0",
"ts-node": "^10.4.0",
......
#!/bin/bash
cat > ./dist/cjs/package.json <<!EOF
{
"type": "commonjs"
}
!EOF
cat > ./dist/esm/package.json <<!EOF
{
"type": "module"
}
!EOF
\ No newline at end of file
import soap from './soap';
import soapClient from './soapClient.js';
import {generateAuditRecord,AuditRecord} from '@digigov-oss/gsis-audit-record-db';
import config from './config.json';
export type AuditInit = AuditRecord;
export type getNncIdentityOutputRecord = {
......@@ -42,7 +44,7 @@ export type Overides = {
auditStoragePath?: string;
}
import config from './config.json';
/**
*
......@@ -61,7 +63,7 @@ export const getIdentity = async (afm:string, user:string, pass:string, overides
if (!auditRecord) throw new Error('Audit record is not initialized');
try {
const s = new soap(wsdl, user, pass, auditRecord);
const s = new soapClient(wsdl, user, pass, auditRecord);
const Identity = await s.getIdentity(afm);
return Identity;
} catch (error) {
......
import soap from 'soap';
import thesoap from 'soap';
let soap:any = thesoap;
try {
soap = require('soap');
} catch (error) {
//my hackish way to make soap work on both esm and cjs
//theshoap on esm is undefined
//On esm require is not defined, however on cjs require can be used.
//So we try to use require and if it fails we use the thesoap module
}
import {AuditRecord} from '@digigov-oss/gsis-audit-record-db';
/**
* SOAP client for getNncIdentity
......
const getIdentity =require('../dist/cjs/index.js').default;
const config =require('./config.json');
const test = async () => {
try {
const Identity = await getIdentity("052704062", config.user, config.pass);
return Identity;
} catch (error) {
console.log(error);
}
}
test().then((identity) => { console.log('getNncIdentityOutputRecord',identity); });
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment