Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: Use typescript #16

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
.vscode
node_modules
dist
125 changes: 111 additions & 14 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 9 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@
"main": "./src/index.js",
"scripts": {
"lint": "eslint .",
"test": "npm run lint && jest --runInBand",
"test": "jest --runInBand",
"start-dev-network": "node test/startDevNetwork.js",
"test-watch": "jest --watch --runInBand"
"test-watch": "jest --watch --runInBand",
"build": "tsc"
},
"repository": {
"type": "git",
Expand All @@ -26,11 +27,16 @@
"homepage": "https://github.com/Kunstmaan/hyperledger-fabric-client-utils#readme",
"devDependencies": {
"@kunstmaan/hyperledger-fabric-chaincode-dev-setup": "^0.5.2",
"@types/lodash.droprightwhile": "^4.6.4",
"@types/log4js": "^2.3.5",
"@types/node": "^8.10.38",
"eslint": "^4.19.1",
"eslint-config-airbnb-base": "^12.1.0",
"eslint-plugin-import": "^2.11.0",
"eslint-plugin-jest": "^21.15.1",
"jest": "^22.4.3"
"fabric-ca-client": "^1.3.0",
"jest": "^22.4.3",
"typescript": "^3.1.6"
},
"dependencies": {
"fabric-client": "^1.3.0",
Expand Down
13 changes: 0 additions & 13 deletions src/index.js

This file was deleted.

13 changes: 13 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import baseService from './lib/baseService';
import createFabricClient from './lib/createFabricClient';
import invoke from './lib/invoke';
import query from './lib/query';
import registerChaincodeEventListener from './lib/registerChaincodeEventListener';

export default {
baseService,
createFabricClient,
invoke,
query,
registerChaincodeEventListener
};
2 changes: 1 addition & 1 deletion src/lib/baseService.spec.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const baseService = require('./baseService');
const baseService = require('../../dist/lib/baseService').default;
const testSetup = require('../../test/testSetup');
const {
keyStorePath, channelId, peer, chaincodeId, orderer, userId: defaultUserId
Expand Down
63 changes: 47 additions & 16 deletions src/lib/baseService.js → src/lib/baseService.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,49 @@
const invoke = require('./invoke');
const query = require('./query');
const logger = require('../utils/logger').getLogger('lib/baseService');
const createFabricClient = require('./createFabricClient');
import invoke from './invoke';
import query from './query';
import getLogger from '../utils/getLogger';
import createFabricClient from './createFabricClient';

module.exports = (
keyStorePath,
chaincodeId,
getServices = () => {},
interface Options {
channelId?: string;
peers?: Peer[];
orderer?: Orderer;
}

interface QueryOptions {
chaincode: Chaincode;
userId: string;
peer?: Peer;
channelId?: string;
}

interface InvokeOptions {
chaincode: Chaincode;
userId: string;
peers?: Peer[];
channelId?: string;
orderer?: Orderer;
}

type getServicesFunction<Services extends {[key: string]: Function}> = (
query: (options: QueryOptions) => Promise<{} | string>,
invoke: (options: InvokeOptions) => Promise<{} | string>
) => Services | {};

const logger = getLogger('lib/baseService');

export default function baseService<Services extends {[key: string]: Function}>(
keyStorePath: string,
chaincodeId: string,
getServices: getServicesFunction<Services> = () => ({}),
{
channelId: defaultChannelId,
peers: defaultPeers = [],
orderer: defaultOrderer
} = {}
) => {
const setChaincodeOption = (chaincode) => {
}: Options = {}
): {
getChaincodeId: () => string;
} & Services {
const setChaincodeOption = (chaincode: Chaincode) => {
const updatedChaincode = {...chaincode};
if (typeof chaincode.id !== 'string') {
updatedChaincode.id = chaincodeId;
Expand All @@ -26,9 +56,9 @@ module.exports = (
};

const services = getServices(
async ({
async({
chaincode, channelId = defaultChannelId, peer = defaultPeers[0], userId
}) => {
}: QueryOptions) => {
const fabricClient = await createFabricClient(keyStorePath);
const options = {
chaincode: setChaincodeOption(chaincode),
Expand All @@ -44,7 +74,7 @@ module.exports = (
},
async ({
chaincode, channelId = defaultChannelId, peers = defaultPeers, orderer = defaultOrderer, userId
}) => {
}: InvokeOptions) => {
const fabricClient = await createFabricClient(keyStorePath);
const options = {
chaincode: setChaincodeOption(chaincode),
Expand All @@ -61,8 +91,9 @@ module.exports = (
}
);


return {
getChaincodeId: () => chaincodeId,
...services
};
...(services as object),
} as Services & { getChaincodeId: () => string };
};
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
const FabricClient = require('fabric-client');
const logger = require('../utils/logger').getLogger('lib/createFabricClient');
import * as FabricClient from 'fabric-client';
import getLogger from '../utils/getLogger';

module.exports = function createFabricClient(keyStorePath) {
const logger = getLogger('lib/createFabricClient');

export default function createFabricClient(keyStorePath: string): Promise<FabricClient> {
const fabricClient = new FabricClient();

return new Promise((resolve, reject) => {
Expand Down
6 changes: 3 additions & 3 deletions src/lib/invoke.spec.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const createFabricClient = require('./createFabricClient');
const invoke = require('./invoke');
const query = require('./query');
const createFabricClient = require('../../dist/lib/createFabricClient').default;
const invoke = require('../../dist/lib/invoke').default;
const query = require('../../dist/lib/query').default;
const testSetup = require('../../test/testSetup');
const {
keyStorePath, channelId, peer, chaincodeId, userId, orderer
Expand Down
Loading