diff --git a/package.json b/package.json index af98545..558a409 100644 --- a/package.json +++ b/package.json @@ -34,8 +34,8 @@ "typescript": "^4.8.2" }, "dependencies": { - "@alien-worlds/aw-broadcast": "^0.0.2", - "@alien-worlds/aw-core": "^0.0.2", + "@alien-worlds/aw-broadcast": "^0.0.3", + "@alien-worlds/aw-core": "^0.0.3", "@alien-worlds/aw-workers": "^0.0.2", "async": "^3.2.4", "commander": "^10.0.1", diff --git a/src/api/endpoints/actions/domain/models/list-actions.input.ts b/src/api/endpoints/actions/domain/models/list-actions.input.ts index 26df54e..c301031 100644 --- a/src/api/endpoints/actions/domain/models/list-actions.input.ts +++ b/src/api/endpoints/actions/domain/models/list-actions.input.ts @@ -1,66 +1,14 @@ -import { ListActionsQueryParams } from '../actions.types'; -import { Request, parseToBigInt } from '@alien-worlds/aw-core'; +import { IO, UnknownObject } from '@alien-worlds/aw-core'; /** * @class */ -export class ListActionsInput { - /** - * - * @param {ListActionsRequestDto} dto - * @returns {ListActionsInput} - */ - public static fromRequest( - request: Request - ): ListActionsInput { - const { - query: { contracts, names, accounts, from, to, limit, offset, block_numbers }, - } = request; - - let fromBlock: bigint; - let toBlock: bigint; - let fromDate: Date; - let toDate: Date; - let blockNumbers = []; - - if (from) { - if (/^[0-9]+$/.test(from)) { - fromBlock = parseToBigInt(from); - } else { - fromDate = new Date(from); - } - } - - if (to) { - if (/^[0-9]+$/.test(to)) { - toBlock = parseToBigInt(to); - } else { - toDate = new Date(to); - } - } - - if (block_numbers) { - blockNumbers = block_numbers.split(',').map(parseToBigInt); - } - - return new ListActionsInput( - contracts ? contracts.split(',') : [], - names ? names.split(',') : [], - accounts ? accounts.split(',') : [], - fromBlock, - toBlock, - fromDate, - toDate, - blockNumbers, - offset || 0, - limit || 10 - ); - } +export class ListActionsInput implements IO { /** * * @constructor * @private */ - private constructor( + constructor( public readonly contracts: string[], public readonly names: string[], public readonly accounts: string[], @@ -72,4 +20,28 @@ export class ListActionsInput { public readonly offset: number, public readonly limit: number ) {} + + public toJSON(): UnknownObject { + const { + contracts, + names, + accounts, + blockNumbers, + startBlock, + endBlock, + offset, + limit, + } = this; + + return { + contracts, + names, + accounts, + block_numbers: blockNumbers.map(blockNumber => blockNumber.toString()), + from: startBlock.toString(), + to: endBlock.toString(), + offset, + limit, + }; + } } diff --git a/src/api/endpoints/actions/domain/models/list-actions.output.ts b/src/api/endpoints/actions/domain/models/list-actions.output.ts index 5dd06da..f70aeeb 100644 --- a/src/api/endpoints/actions/domain/models/list-actions.output.ts +++ b/src/api/endpoints/actions/domain/models/list-actions.output.ts @@ -1,29 +1,21 @@ -import { ContractAction, Result } from '@alien-worlds/aw-core'; +import { ContractAction, IO, Result, UnknownObject } from '@alien-worlds/aw-core'; -export class ListActionsOutput { +export class ListActionsOutput implements IO { public static create(result: Result): ListActionsOutput { return new ListActionsOutput(result); } - private constructor(public readonly result: Result) {} + constructor(public readonly result: Result) {} - public toResponse() { + toJSON(): UnknownObject { const { result } = this; + if (result.isFailure) { - const { - failure: { error }, - } = result; - if (error) { - return { - status: 500, - body: [], - }; - } + return {}; } - + return { - status: 200, - body: result.content + result: result.content.map(r => r.toJSON()), }; } } diff --git a/src/api/endpoints/actions/routes/list-actions.route-io.ts b/src/api/endpoints/actions/routes/list-actions.route-io.ts new file mode 100644 index 0000000..7df8f46 --- /dev/null +++ b/src/api/endpoints/actions/routes/list-actions.route-io.ts @@ -0,0 +1,81 @@ +import { + IO, + Response, + RouteIO, + UnknownObject, + Request, + parseToBigInt, +} from '@alien-worlds/aw-core'; +import { ListActionsInput } from '../domain/models/list-actions.input'; +import { ListActionsQueryParams } from '../domain/actions.types'; +import { ListActionsOutput } from '../domain/models/list-actions.output'; + +export class ListActionsRouteIO implements RouteIO { + public toResponse(output: ListActionsOutput): Response { + const { result } = output; + + if (result.isFailure) { + const { + failure: { error }, + } = result; + if (error) { + return { + status: 500, + body: [], + }; + } + } + + return { + status: 200, + body: output.toJSON(), + }; + } + + public fromRequest( + request: Request + ): ListActionsInput { + const { + query: { contracts, names, accounts, from, to, limit, offset, block_numbers }, + } = request; + + let fromBlock: bigint; + let toBlock: bigint; + let fromDate: Date; + let toDate: Date; + let blockNumbers = []; + + if (from) { + if (/^[0-9]+$/.test(from)) { + fromBlock = parseToBigInt(from); + } else { + fromDate = new Date(from); + } + } + + if (to) { + if (/^[0-9]+$/.test(to)) { + toBlock = parseToBigInt(to); + } else { + toDate = new Date(to); + } + } + + if (block_numbers) { + blockNumbers = block_numbers.split(',').map(parseToBigInt); + } + + return new ListActionsInput( + contracts ? contracts.split(',') : [], + names ? names.split(',') : [], + accounts ? accounts.split(',') : [], + fromBlock, + toBlock, + fromDate, + toDate, + blockNumbers, + offset || 0, + limit || 10 + ); + } +} diff --git a/src/api/endpoints/actions/routes/list-actions.route.ts b/src/api/endpoints/actions/routes/list-actions.route.ts index 079f7fd..e36cfd4 100644 --- a/src/api/endpoints/actions/routes/list-actions.route.ts +++ b/src/api/endpoints/actions/routes/list-actions.route.ts @@ -1,25 +1,12 @@ import { GetRoute, RouteHandler } from '@alien-worlds/aw-core'; -import { ListActionsInput } from '../domain/models/list-actions.input'; -import { ListActionsOutput } from '../domain/models/list-actions.output'; +import { ListActionsRouteIO } from './list-actions.route-io'; -/*imports*/ - -/** - * @class - * - * - */ export class ListActionsRoute extends GetRoute { public static create(handler: RouteHandler) { return new ListActionsRoute(handler); } private constructor(handler: RouteHandler) { - super('actions', handler, { - hooks: { - pre: ListActionsInput.fromRequest, - post: (output: ListActionsOutput) => output.toResponse(), - }, - }); + super('actions', handler, new ListActionsRouteIO()); } } diff --git a/yarn.lock b/yarn.lock index 032d210..212fb35 100644 --- a/yarn.lock +++ b/yarn.lock @@ -2,18 +2,18 @@ # yarn lockfile v1 -"@alien-worlds/aw-broadcast@^0.0.2": - version "0.0.2" - resolved "https://npm.pkg.github.com/download/@alien-worlds/aw-broadcast/0.0.2/ae8f1ae3485c3c1d0581dc06999cbfcd342803de#ae8f1ae3485c3c1d0581dc06999cbfcd342803de" - integrity sha512-I2X5akaAfvARklvq1fCu6syzEUtBHgyc45wbZKQ3GL7LVP6UAbU765GGR1vFQ+dA3r3wMEe9CdXNKVnZMv7+pA== +"@alien-worlds/aw-broadcast@^0.0.3": + version "0.0.3" + resolved "https://npm.pkg.github.com/download/@alien-worlds/aw-broadcast/0.0.3/ee62d2dccf49ae3da426e8c92ddf01520a677616#ee62d2dccf49ae3da426e8c92ddf01520a677616" + integrity sha512-L254cFKKRey6ni68ftE1w0rRPtz5nedUG1lB4sMe+Q5e0wXDyMLGi09dlAUN6PBBQQDCW6BDiD5pyTGgk1I5SA== dependencies: - "@alien-worlds/aw-core" "^0.0.2" + "@alien-worlds/aw-core" "^0.0.3" nanoid "^3.0.0" -"@alien-worlds/aw-core@^0.0.2": - version "0.0.2" - resolved "https://npm.pkg.github.com/download/@alien-worlds/aw-core/0.0.2/a0ff00b1c50a06173e99a1185af0428a24b4a3ac#a0ff00b1c50a06173e99a1185af0428a24b4a3ac" - integrity sha512-Ld5QrJVhtFqvmgxFYNQUlojPBp4AEw+iqPJ8xX00pFJ2shUO9wJk0cVPMP1a7KADkCYirA9AxPn45C8CXcHKjA== +"@alien-worlds/aw-core@^0.0.3": + version "0.0.3" + resolved "https://npm.pkg.github.com/download/@alien-worlds/aw-core/0.0.3/1b5af094fced3d97824d909190d446c116aa6bec#1b5af094fced3d97824d909190d446c116aa6bec" + integrity sha512-5aWRA7svi9qWEZO0ROJNUNGnw/KftSPLkcekCjm5ecB0ufMo01eI68A+RlufuCbLaeTKH8K8pKWFgycSxGRBwQ== dependencies: inversify "^6.0.1" node-fetch "2.6.6"