@signumjs/core
Version:
Principal package with functions and models for building Signum Network applications.
148 lines • 4.38 kB
JavaScript
"use strict";
/**
* Copyright (c) 2019 Burst Apps Team
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.ApiComposer = void 0;
class ApiImpl {
service;
constructor(service) {
this.service = service;
}
alias;
account;
asset;
block;
message;
network;
transaction;
contract;
}
/**
* The API composer mounts the API for given service and selected methods
*
* Usually you would use{@link LedgerClientFactory} (or older style {@link composeApi}), which gives you _all_ available API methods.
* Unfortunately, this will import almost all dependencies, even if you need only a fraction
* of the methods. To take advantage of tree-shaking (dead code elimination) you can
* compose your own API with the methods you need. This can reduce your final bundle significantly.
*
* Usage:
* ```typescript
*
* const chainService = new ChainService({
* nodeHost: 'https://testnet.burst.fun',
* })
*
* const api = apiComposer
* .create(chainService)
* .withMessageApi({
* sendTextMessage
* })
* .withAccountApi({
* getAccountTransactions,
* getUnconfirmedAccountTransactions,
* getAccountBalance,
* generateSendTransactionQRCode,
* generateSendTransactionQRCodeAddress,
* })
* .compose();
* ```
*
* The `with<section>Api` uses factory methods from the [api.core.factories](/phoenix/docs/modules/core_api_factories.html) package
*
*
* */
class ApiComposer {
service;
api;
/**
* Creates the composer instance
* @param service
* @return the composer instance
*/
static create(service) {
return new ApiComposer(service);
}
constructor(service) {
this.service = service;
this.api = new ApiImpl(service);
}
mapCreators(apiSection, creatorMap) {
this.api[apiSection] = {};
Object.keys(creatorMap)
.forEach(creatorName => this.api[apiSection][creatorName] = creatorMap[creatorName](this.service));
}
/**
* Adds the {@link BlockApi} to be composed
* @param creatorMap A map of creator/factory functions for the endpoints
*/
withBlockApi(creatorMap) {
this.mapCreators('block', creatorMap);
return this;
}
/**
* Adds the {@link AccountApi} to be composed
* @param creatorMap A map of creator/factory functions for the endpoints
*/
withAccountApi(creatorMap) {
this.mapCreators('account', creatorMap);
return this;
}
/**
* Adds the {@link NetworkApi} to be composed
* @param creatorMap A map of creator/factory functions for the endpoints
*/
withNetworkApi(creatorMap) {
this.mapCreators('network', creatorMap);
return this;
}
/**
* Adds the {@link MessageApi} to be composed
* @param creatorMap A map of creator/factory functions for the endpoints
*/
withMessageApi(creatorMap) {
this.mapCreators('message', creatorMap);
return this;
}
/**
* Adds the {@link TransactionApi} to be composed
* @param creatorMap A map of creator/factory functions for the endpoints
*/
withTransactionApi(creatorMap) {
this.mapCreators('transaction', creatorMap);
return this;
}
/**
* Adds the {@link AliasApi} to be composed
* @param creatorMap A map of creator/factory functions for the endpoints
*/
withAliasApi(creatorMap) {
this.mapCreators('alias', creatorMap);
return this;
}
/**
* Adds the {@link AssetApi} to be composed
* @param creatorMap A map of creator/factory functions for the endpoints
*/
withAssetApi(creatorMap) {
this.mapCreators('asset', creatorMap);
return this;
}
/**
* Adds the {@link ContractApi} to be composed
* @param creatorMap A map of creator/factory functions for the endpoints
*/
withContractApi(creatorMap) {
this.mapCreators('contract', creatorMap);
return this;
}
/**
* Composes the API
* Note: As of being a builder pattern, this need to call this method as last
*/
compose() {
return this.api;
}
}
exports.ApiComposer = ApiComposer;
//# sourceMappingURL=apiComposer.js.map