diff --git a/lerna.json b/lerna.json index cf8edcc4..98236549 100644 --- a/lerna.json +++ b/lerna.json @@ -1,7 +1,7 @@ { "$schema": "node_modules/lerna/schemas/lerna-schema.json", "useWorkspaces": true, - "version": "1.2.7", + "version": "1.2.8", "packages": [ "packages/*" ], diff --git a/package-lock.json b/package-lock.json index d5c3d6d6..a8a87d39 100644 --- a/package-lock.json +++ b/package-lock.json @@ -14117,7 +14117,7 @@ }, "packages/auth": { "name": "@multiversx/sdk-nestjs-auth", - "version": "1.2.7", + "version": "1.2.8", "license": "GPL-3.0-or-later", "dependencies": { "@multiversx/sdk-core": "*", @@ -14251,7 +14251,7 @@ }, "packages/cache": { "name": "@multiversx/sdk-nestjs-cache", - "version": "1.2.7", + "version": "1.2.8", "license": "GPL-3.0-or-later", "dependencies": { "lru-cache": "^8.0.4", @@ -14309,7 +14309,7 @@ }, "packages/common": { "name": "@multiversx/sdk-nestjs-common", - "version": "1.2.7", + "version": "1.2.8", "license": "GPL-3.0-or-later", "dependencies": { "@multiversx/sdk-core": "^11.4.1", @@ -14342,7 +14342,7 @@ }, "packages/elastic": { "name": "@multiversx/sdk-nestjs-elastic", - "version": "1.2.7", + "version": "1.2.8", "license": "GPL-3.0-or-later", "devDependencies": { "@typescript-eslint/eslint-plugin": "^5.12.0", @@ -14357,7 +14357,7 @@ }, "packages/http": { "name": "@multiversx/sdk-nestjs-http", - "version": "1.2.7", + "version": "1.2.8", "license": "GPL-3.0-or-later", "dependencies": { "@multiversx/sdk-wallet": "3.0.0", @@ -14424,7 +14424,7 @@ }, "packages/monitoring": { "name": "@multiversx/sdk-nestjs-monitoring", - "version": "1.2.7", + "version": "1.2.8", "license": "GPL-3.0-or-later", "dependencies": { "prom-client": "^14.0.1", @@ -14443,7 +14443,7 @@ }, "packages/rabbitmq": { "name": "@multiversx/sdk-nestjs-rabbitmq", - "version": "1.2.7", + "version": "1.2.8", "license": "GPL-3.0-or-later", "dependencies": { "@golevelup/nestjs-rabbitmq": "^3.0.0", @@ -14464,7 +14464,7 @@ }, "packages/redis": { "name": "@multiversx/sdk-nestjs-redis", - "version": "1.2.7", + "version": "1.2.8", "license": "GPL-3.0-or-later", "dependencies": { "ioredis": "^5.2.3" diff --git a/packages/auth/package.json b/packages/auth/package.json index 35b50eb1..a4ce2b58 100644 --- a/packages/auth/package.json +++ b/packages/auth/package.json @@ -1,6 +1,6 @@ { "name": "@multiversx/sdk-nestjs-auth", - "version": "1.2.7", + "version": "1.2.8", "description": "Multiversx SDK Nestjs auth package", "main": "lib/index.js", "types": "lib/index.d.ts", diff --git a/packages/cache/package.json b/packages/cache/package.json index 95267259..526ab993 100644 --- a/packages/cache/package.json +++ b/packages/cache/package.json @@ -1,6 +1,6 @@ { "name": "@multiversx/sdk-nestjs-cache", - "version": "1.2.7", + "version": "1.2.8", "description": "Multiversx SDK Nestjs cache package", "main": "lib/index.js", "types": "lib/index.d.ts", diff --git a/packages/common/package.json b/packages/common/package.json index d0137258..7709ff94 100644 --- a/packages/common/package.json +++ b/packages/common/package.json @@ -1,6 +1,6 @@ { "name": "@multiversx/sdk-nestjs-common", - "version": "1.2.7", + "version": "1.2.8", "description": "Multiversx SDK Nestjs common package", "main": "lib/index.js", "types": "lib/index.d.ts", diff --git a/packages/common/src/index.ts b/packages/common/src/index.ts index 6e75491f..6f1b741a 100644 --- a/packages/common/src/index.ts +++ b/packages/common/src/index.ts @@ -37,6 +37,8 @@ export * from './pipes/parse.record.pipe'; export * from './pipes/parse.token.pipe'; export * from './pipes/parse.token.or.nft.pipe'; export * from './pipes/parse.transaction.hash.pipe'; +export * from './pipes/parse.nft.array.pipe'; +export * from './pipes/parse.collection.array.pipe'; export * from './common/entities/amount'; export * from './common/config/erdnest.config.service'; export * from './common/swappable-settings'; diff --git a/packages/common/src/pipes/parse.collection.array.pipe.ts b/packages/common/src/pipes/parse.collection.array.pipe.ts new file mode 100644 index 00000000..08da9597 --- /dev/null +++ b/packages/common/src/pipes/parse.collection.array.pipe.ts @@ -0,0 +1,23 @@ +import { ArgumentMetadata, BadRequestException, PipeTransform } from "@nestjs/common"; +import { TokenUtils } from "../utils/token.utils"; + +export class ParseCollectionArrayPipe implements PipeTransform> { + + transform(value: string | string[] | undefined, metadata: ArgumentMetadata): Promise { + return new Promise((resolve) => { + if (value === undefined || value === '') { + return resolve(undefined); + } + + const values = Array.isArray(value) ? value : value.split(','); + + for (const value of values) { + if (!TokenUtils.isCollection(value)) { + throw new BadRequestException(`Validation failed for '${metadata.data}'. Value ${value} does not represent a valid collection identifier`); + } + } + + return resolve(values); + }); + } +} diff --git a/packages/common/src/pipes/parse.nft.array.pipe.ts b/packages/common/src/pipes/parse.nft.array.pipe.ts new file mode 100644 index 00000000..64a17331 --- /dev/null +++ b/packages/common/src/pipes/parse.nft.array.pipe.ts @@ -0,0 +1,23 @@ +import { ArgumentMetadata, BadRequestException, PipeTransform } from "@nestjs/common"; +import { TokenUtils } from "../utils/token.utils"; + +export class ParseNftArrayPipe implements PipeTransform> { + + transform(value: string | string[] | undefined, metadata: ArgumentMetadata): Promise { + return new Promise((resolve) => { + if (value === undefined || value === '') { + return resolve(undefined); + } + + const values = Array.isArray(value) ? value : value.split(','); + + for (const value of values) { + if (!TokenUtils.isNft(value)) { + throw new BadRequestException(`Validation failed for '${metadata.data}'. Value ${value} does not represent a valid NFT identifier`); + } + } + + return resolve(values); + }); + } +} diff --git a/packages/common/test/pipes/parse.collection.array.pipe.spec.ts b/packages/common/test/pipes/parse.collection.array.pipe.spec.ts new file mode 100644 index 00000000..15448b66 --- /dev/null +++ b/packages/common/test/pipes/parse.collection.array.pipe.spec.ts @@ -0,0 +1,39 @@ +import { ArgumentMetadata, BadRequestException } from "@nestjs/common"; +import { ParseCollectionArrayPipe } from "../../../common/src/pipes/parse.collection.array.pipe"; + +describe('ParseCollectionArrayPipe', () => { + let target: ParseCollectionArrayPipe; + + beforeEach(() => { + target = new ParseCollectionArrayPipe(); + }); + + describe('transform', () => { + describe('when validation passes', () => { + it('should return array of collection identifiers', async () => { + const validCollectionIdentifier = 'ABCDE-efb116'; + expect(await target.transform(validCollectionIdentifier, {} as ArgumentMetadata)).toStrictEqual([validCollectionIdentifier]); + }); + + it('should return undefined for an empty string', async () => { + expect(await target.transform('', {} as ArgumentMetadata)).toBeUndefined(); + }); + + it('should return undefined for undefined value', async () => { + expect(await target.transform(undefined, {} as ArgumentMetadata)).toBeUndefined(); + }); + }); + + describe('when validation fails', () => { + it('should throw BadRequestException', async () => { + const invalidCollectionIdentifier = 'ABCDE-efb116-02'; + await expect(target.transform(invalidCollectionIdentifier, {} as ArgumentMetadata)).rejects.toThrow(BadRequestException); + }); + + it('should throw BadRequestException even if array contains a valid collection identifier', async () => { + const invalidCollectionIdentifiers = ['ABCDE-efb116-02', 'ABCDE-efb116']; + await expect(target.transform(invalidCollectionIdentifiers, {} as ArgumentMetadata)).rejects.toThrow(BadRequestException); + }); + }); + }); +}); \ No newline at end of file diff --git a/packages/common/test/pipes/parse.nft.array.pipe.spec.ts b/packages/common/test/pipes/parse.nft.array.pipe.spec.ts new file mode 100644 index 00000000..8132d43c --- /dev/null +++ b/packages/common/test/pipes/parse.nft.array.pipe.spec.ts @@ -0,0 +1,44 @@ +import { ArgumentMetadata, BadRequestException } from "@nestjs/common"; +import { ParseNftArrayPipe } from "../../../common/src/pipes/parse.nft.array.pipe"; + +describe('ParseNftArrayPipe', () => { + let target: ParseNftArrayPipe; + + beforeEach(() => { + target = new ParseNftArrayPipe(); + }); + + describe('transform', () => { + describe('when validation passes', () => { + it('should return array of NFT identifiers', async () => { + const validNftIdentifier = 'ABCDE-efb116-02'; + expect(await target.transform(validNftIdentifier, {} as ArgumentMetadata)).toStrictEqual([validNftIdentifier]); + }); + + it('should throw BadRequestException even if array contains a valid identifier', async () => { + const validNftIdentifier = 'ABCDE-efb116-02'; + expect(await target.transform(validNftIdentifier, {} as ArgumentMetadata)).toStrictEqual([validNftIdentifier]); + }); + + it('should return undefined for an empty string', async () => { + expect(await target.transform('', {} as ArgumentMetadata)).toBeUndefined(); + }); + + it('should return undefined for undefined value', async () => { + expect(await target.transform(undefined, {} as ArgumentMetadata)).toBeUndefined(); + }); + }); + + describe('when validation fails', () => { + it('should throw BadRequestException', async () => { + const invalidNftIdentifier = 'ABCDE-efb116'; + await expect(target.transform(invalidNftIdentifier, {} as ArgumentMetadata)).rejects.toThrow(BadRequestException); + }); + + it('should throw BadRequestException even if array contains a valid identifier', async () => { + const invalidNftIdentifiers = ['ABCDE-efb116-02', 'ABCDE-efb116']; + await expect(target.transform(invalidNftIdentifiers, {} as ArgumentMetadata)).rejects.toThrow(BadRequestException); + }); + }); + }); +}); \ No newline at end of file diff --git a/packages/elastic/package.json b/packages/elastic/package.json index bc9304bc..017b602d 100644 --- a/packages/elastic/package.json +++ b/packages/elastic/package.json @@ -1,6 +1,6 @@ { "name": "@multiversx/sdk-nestjs-elastic", - "version": "1.2.7", + "version": "1.2.8", "description": "Multiversx SDK Nestjs elastic package", "main": "lib/index.js", "types": "lib/index.d.ts", diff --git a/packages/http/package.json b/packages/http/package.json index 87a61ae8..0ffa7a94 100644 --- a/packages/http/package.json +++ b/packages/http/package.json @@ -1,6 +1,6 @@ { "name": "@multiversx/sdk-nestjs-http", - "version": "1.2.7", + "version": "1.2.8", "description": "Multiversx SDK Nestjs http package", "main": "lib/index.js", "types": "lib/index.d.ts", diff --git a/packages/monitoring/package.json b/packages/monitoring/package.json index 7d93de05..1e549a9b 100644 --- a/packages/monitoring/package.json +++ b/packages/monitoring/package.json @@ -1,6 +1,6 @@ { "name": "@multiversx/sdk-nestjs-monitoring", - "version": "1.2.7", + "version": "1.2.8", "description": "Multiversx SDK Nestjs monitoring package", "main": "lib/index.js", "types": "lib/index.d.ts", diff --git a/packages/rabbitmq/package.json b/packages/rabbitmq/package.json index 888dfbd6..f218f6bb 100644 --- a/packages/rabbitmq/package.json +++ b/packages/rabbitmq/package.json @@ -1,6 +1,6 @@ { "name": "@multiversx/sdk-nestjs-rabbitmq", - "version": "1.2.7", + "version": "1.2.8", "description": "Multiversx SDK Nestjs rabbitmq client package", "main": "lib/index.js", "types": "lib/index.d.ts", diff --git a/packages/redis/package.json b/packages/redis/package.json index 860d9747..42e2f565 100644 --- a/packages/redis/package.json +++ b/packages/redis/package.json @@ -1,6 +1,6 @@ { "name": "@multiversx/sdk-nestjs-redis", - "version": "1.2.7", + "version": "1.2.8", "description": "Multiversx SDK Nestjs redis client package", "main": "lib/index.js", "types": "lib/index.d.ts",