Skip to content

Commit d672695

Browse files
mbwhiteheatherlp
authored andcommitted
[FAB-15882] Typescript corrections
- If a type is missing from a transaciton function fail fast - Add in the missing serializer type definitions Change-Id: I6ca70989a5eddfe999693b7b5300e6cd3951ceaa Signed-off-by: Matthew B. White <whitemat@uk.ibm.com>
1 parent 6946d91 commit d672695

3 files changed

Lines changed: 30 additions & 4 deletions

File tree

fabric-contract-api/lib/annotations/transaction.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ const Logger = require('../logger');
1010
const logger = Logger.getLogger('./lib/annotations/transaction.js');
1111
require('reflect-metadata');
1212

13-
module.exports.Transaction = function Transaction(commit = true) {
13+
module.exports.Transaction = function Transaction (commit = true) {
1414
return (target, propertyKey) => {
1515
logger.info('@Transaction args:', `Property Key -> ${propertyKey}, Commit -> ${commit},`, 'Target ->', target.constructor.name);
1616

@@ -45,7 +45,9 @@ module.exports.Transaction = function Transaction(commit = true) {
4545
};
4646

4747
const type = typeof paramType === 'function' ? paramType.name : paramType.toString();
48-
48+
if (type === 'Object') {
49+
throw new Error(`Type not properly specified for parameter ${paramName}, can not process pure Object types`);
50+
}
4951
obj.schema = utils.generateSchema(type);
5052

5153
return obj;
@@ -76,7 +78,7 @@ module.exports.Transaction = function Transaction(commit = true) {
7678
};
7779
};
7880

79-
module.exports.Returns = function Returns(returnType) {
81+
module.exports.Returns = function Returns (returnType) {
8082
return (target, propertyKey) => {
8183
logger.info('@Returns args:', `, Property Key -> ${propertyKey}, Return Type -> ${returnType},`, 'Target ->', target.constructor.name);
8284

@@ -93,7 +95,7 @@ module.exports.Returns = function Returns(returnType) {
9395
};
9496
};
9597

96-
module.exports.Param = function Param(paramName, paramType, description) {
98+
module.exports.Param = function Param (paramName, paramType, description) {
9799
return (target, propertyKey) => {
98100
logger.info('@Param args:', `Property Key -> ${propertyKey}, Param Name -> ${paramName}, Param Type -> ${paramType}, Description -> ${description},`, 'Target ->', target.constructor.name);
99101

fabric-contract-api/test/unit/annotations/transaction.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,25 @@ describe('Transaction.js', () => {
118118
]
119119
}], mockTarget);
120120
});
121+
it ('should error if missing type', () => {
121122

123+
sandbox.stub(Reflect, 'getMetadata').onFirstCall().returns([{
124+
name: 'someTransaction',
125+
tag: ['submitTx']
126+
}]).onSecondCall().returns([
127+
MockContext,
128+
'Object'
129+
]);
130+
131+
TransactionAnnotations.__set__('getParams', () => {
132+
return ['ctx', 'param1'];
133+
});
134+
135+
(() => {
136+
transaction(mockTarget, 'mockKey');
137+
}).should.throw(/Type not properly specified for parameter .*?, can not process pure Object types/);
138+
139+
});
122140
it ('should handle existing transactions of which matches name and already has param metadata', () => {
123141
const transactions = [{
124142
name: 'mockKey',

fabric-contract-api/types/index.d.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,12 @@ declare module 'fabric-contract-api' {
3333

3434
}
3535

36+
37+
export class JSONSerializer {
38+
toBuffer(result: any,schema:any,loggerPrefix?:string): Buffer;
39+
fromBuffer(data: Buffer,schema:any,loggerPrefix?:string): any;
40+
}
41+
3642
export function Transaction(commit?: boolean): (target: any, propertyKey: string | symbol) => void;
3743
export function Param(paramName: string, paramType: string, description?: string): (target: any, propertyKey: string | symbol) => void;
3844
export function Returns(returnType?: string): (target: any, propertyKey: string | symbol) => void;

0 commit comments

Comments
 (0)