UNPKG

postchain-client

Version:

Client library for accessing a Postchain node through REST.

134 lines 6.51 kB
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()); }); }; import { http, HttpResponse } from "msw"; import { ResponseStatus } from "../src/blockchainClient/enums"; import { blockDataResponse } from "../resources/testData"; import { encodeValue } from "../src/gtx/serialization"; import { mockHexStringOfThirtyTwoBytesBuffer, mockStringBlockchainRid, mockStringDirectoryChainRid, mockStringFaultyBrid, exampleTxHashString, contentTypes, LOCAL_POOL, mockBinaryHexString, } from "../test/common/mocks"; import { toBuffer } from "../src/formatter"; const rejectReason = "[bc-rid=8E:DFB4, chain-id=0] Operation 'news_feed_ch5.news_feed:make_post' failed: Expected at least two operations, make sure that you included auth"; const invalidFormatRejectReason = "Operation 'news_feed_ch5.news_feed:make_post' failed: Expected at least two operations, make sure that you included auth"; const transactionInfoResponse = { blockRID: mockHexStringOfThirtyTwoBytesBuffer, blockHeight: 0, blockHeader: mockHexStringOfThirtyTwoBytesBuffer, witness: mockHexStringOfThirtyTwoBytesBuffer, timestamp: 0, txRID: mockHexStringOfThirtyTwoBytesBuffer, txHash: mockHexStringOfThirtyTwoBytesBuffer, txData: mockHexStringOfThirtyTwoBytesBuffer, }; export const handlers = [ http.get(`*/config/*/features`, () => { return HttpResponse.json({ merkle_hash_version: 2 }); }), http.post(`${LOCAL_POOL}/tx/${mockStringBlockchainRid}`, () => { return HttpResponse.json({}); }), http.get(`${LOCAL_POOL}/tx/:blockchainRid/:txRid/status`, () => { const responseBody = { status: ResponseStatus.Confirmed, }; return HttpResponse.json(responseBody); }), http.get(`${LOCAL_POOL}/blocks/${mockStringBlockchainRid}/height/*`, () => { return HttpResponse.json(blockDataResponse); }), http.get(`${LOCAL_POOL}/blocks/${mockStringBlockchainRid}`, () => { return HttpResponse.json([blockDataResponse]); }), http.get(`${LOCAL_POOL}/brid/iid_0`, () => { return HttpResponse.json(mockStringDirectoryChainRid); }), http.get(`${LOCAL_POOL}/transactions/${mockStringBlockchainRid}/${exampleTxHashString}`, () => { return HttpResponse.json(transactionInfoResponse); }), http.get(`${LOCAL_POOL}/transactions/${mockStringBlockchainRid}/count`, () => { return HttpResponse.json({ transactionsCount: 0 }); }), http.get(`${LOCAL_POOL}/tx/${mockStringBlockchainRid}/${exampleTxHashString}/confirmationProof`, () => { return HttpResponse.json({ proof: "0" }); }), http.get(`${LOCAL_POOL}/tx/${mockStringBlockchainRid}/${exampleTxHashString}`, () => { return HttpResponse.json({ tx: mockHexStringOfThirtyTwoBytesBuffer }); }), http.get(`${LOCAL_POOL}/transactions/:blockchainRid`, () => __awaiter(void 0, void 0, void 0, function* () { return HttpResponse.json([transactionInfoResponse]); })), http.post(`${LOCAL_POOL}/query_gtv/${mockStringBlockchainRid}`, () => { return HttpResponse.arrayBuffer(encodeValue(null), { status: 200, headers: { "Content-Type": contentTypes.octetStream, }, }); }), http.post(`${LOCAL_POOL}/query_gtv/${mockStringDirectoryChainRid}`, () => { return HttpResponse.arrayBuffer(encodeValue(null), { status: 200, headers: { "Content-Type": contentTypes.octetStream, }, }); }), http.get(`${LOCAL_POOL}/tx/:blockchainRid/waiting/:txRid`, () => __awaiter(void 0, void 0, void 0, function* () { return new HttpResponse(toBuffer(mockBinaryHexString), { headers: { "X-Transaction-Timestamp": Date.now().toString(), "Content-Type": "application/octet-stream", }, }); })), http.get(`${LOCAL_POOL}/tx/:blockchainRid/waiting`, () => __awaiter(void 0, void 0, void 0, function* () { return HttpResponse.json([exampleTxHashString]); })), ]; const responseBody = { status: ResponseStatus.Rejected, rejectReason, }; const responseBodyInvalidRejectError = { status: ResponseStatus.Rejected, rejectReason: invalidFormatRejectReason, }; export const errorHandler = { txError: http.post(`${LOCAL_POOL}/tx/${mockStringBlockchainRid}`, () => { return new HttpResponse('{"error": "Could not parse JSON"}', { status: 400, }); }, { once: true }), txStatusInvalidRejectFormat: http.get(`${LOCAL_POOL}/tx/${mockStringBlockchainRid}/${exampleTxHashString}/status`, () => { return HttpResponse.json(responseBodyInvalidRejectError); }, { once: true }), statusRejected: http.get(`${LOCAL_POOL}/tx/${mockStringBlockchainRid}/${exampleTxHashString}/status`, () => { return HttpResponse.json(responseBody); }, { once: true }), statusWaiting: http.get(`${LOCAL_POOL}/tx/${mockStringBlockchainRid}/${exampleTxHashString}/status`, () => { return HttpResponse.json({ status: ResponseStatus.Waiting, }); }), statusUnknown: http.get(`${LOCAL_POOL}/tx/${mockStringBlockchainRid}/${exampleTxHashString}/status`, () => { return HttpResponse.json({ status: ResponseStatus.Unknown, }); }), getTransactionInfo: http.get(`${LOCAL_POOL}/transactions/*`, () => { return new HttpResponse(`{"error":"Can't find blockchain with blockchainRID: ${mockStringFaultyBrid}"}`, { status: 404, }); }, { once: true }), queryInvalidBrid: http.post(`${LOCAL_POOL}/query_gtv/${mockStringFaultyBrid}`, () => { return new HttpResponse(`{"error": "Can't find blockchain with blockchainRID: ${mockStringFaultyBrid}"}`, { status: 404, }); }), }; //# sourceMappingURL=handlers.js.map