Files
mempool/frontend/src/app/docs/code-template/code-template.component.ts
T

334 lines
11 KiB
TypeScript
Raw Normal View History

2021-09-03 07:04:19 -03:00
import { Component, Input, OnInit } from '@angular/core';
import { Env, StateService } from '../../services/state.service';
2021-06-06 17:06:56 -03:00
@Component({
selector: 'app-code-template',
templateUrl: './code-template.component.html',
styleUrls: ['./code-template.component.scss']
})
2021-09-03 07:04:19 -03:00
export class CodeTemplateComponent implements OnInit {
2021-06-06 17:06:56 -03:00
@Input() network: string;
2021-09-03 07:04:19 -03:00
@Input() code: any;
@Input() hostname: string;
2022-02-07 13:25:36 -05:00
@Input() baseNetworkUrl: string;
@Input() method: 'GET' | 'POST' | 'websocket' = 'GET';
2022-05-18 17:33:52 -04:00
@Input() showCodeExample: any;
2021-09-03 07:04:19 -03:00
env: Env;
2021-06-06 17:06:56 -03:00
constructor(
2021-09-03 07:04:19 -03:00
private stateService: StateService,
2021-06-06 17:06:56 -03:00
) { }
2021-09-03 07:04:19 -03:00
ngOnInit(): void {
this.env = this.stateService.env;
}
2022-03-14 14:33:41 -04:00
2022-03-12 15:59:59 -05:00
adjustContainerHeight( event ) {
2022-03-14 14:33:41 -04:00
if( ( window.innerWidth <= 992 ) && ( this.method !== "websocket" ) ) {
2022-03-12 15:59:59 -05:00
const urlObj = new URL( window.location + "" );
const endpointContainerEl = document.querySelector<HTMLElement>( urlObj.hash );
const endpointContentEl = document.querySelector<HTMLElement>( urlObj.hash + " .endpoint-content" );
window.setTimeout( function() {
endpointContainerEl.style.height = endpointContentEl.clientHeight + 90 + "px";
2022-03-14 14:33:41 -04:00
}, 550);
2022-03-12 15:59:59 -05:00
}
}
2021-09-03 07:04:19 -03:00
2021-08-11 12:50:49 -03:00
npmGithubLink(){
let npmLink = `https://github.com/mempool/mempool.js`;
2021-09-03 07:04:19 -03:00
if (this.network === 'bisq') {
2021-08-11 12:50:49 -03:00
npmLink = `https://github.com/mempool/mempool.js/tree/main/npm-bisq-js`;
}
2021-12-27 22:54:45 +04:00
if (this.network === 'liquid' || this.network === 'liquidtestnet') {
2021-08-11 12:50:49 -03:00
npmLink = `https://github.com/mempool/mempool.js/tree/main/npm-liquid-js`;
}
return npmLink;
}
npmModuleLink() {
let npmLink = `https://www.npmjs.org/package/@mempool/mempool.js`;
2021-09-03 07:04:19 -03:00
if (this.network === 'bisq') {
2021-08-11 12:50:49 -03:00
npmLink = `https://www.npmjs.org/package/@mempool/bisq.js`;
}
2021-12-27 22:54:45 +04:00
if (this.network === 'liquid' || this.network === 'liquidtestnet') {
2021-08-11 12:50:49 -03:00
npmLink = `https://www.npmjs.org/package/@mempool/liquid.js`;
}
return npmLink;
}
2021-09-03 07:04:19 -03:00
normalizeHostsESModule(codeText: string) {
if (this.env.BASE_MODULE === 'mempool') {
if (['liquid', 'bisq'].includes(this.network)) {
codeText = codeText.replace('%{0}', this.network);
} else {
codeText = codeText.replace('%{0}', 'bitcoin');
}
2021-12-27 22:54:45 +04:00
if(['', 'main', 'liquid', 'bisq', 'liquidtestnet'].includes(this.network)) {
2021-09-03 07:04:19 -03:00
codeText = codeText.replace('mempoolJS();', `mempoolJS({
hostname: '${document.location.hostname}'
});`);
} else {
codeText = codeText.replace('mempoolJS();', `mempoolJS({
hostname: '${document.location.hostname}',
network: '${this.network}'
});`);
}
}
if (this.env.BASE_MODULE === 'bisq') {
codeText = codeText.replace('} = mempoolJS();', ` = bisqJS();`);
codeText = codeText.replace('{ %{0}: ', '');
}
if (this.env.BASE_MODULE === 'liquid') {
codeText = codeText.replace('} = mempoolJS();', ` = liquidJS();`);
codeText = codeText.replace('{ %{0}: ', '');
2021-06-06 17:06:56 -03:00
}
return codeText;
}
2021-09-03 07:04:19 -03:00
normalizeHostsCommonJS(codeText: string) {
if (this.env.BASE_MODULE === 'mempool') {
if (['liquid', 'bisq'].includes(this.network)) {
codeText = codeText.replace('%{0}', this.network);
} else {
codeText = codeText.replace('%{0}', 'bitcoin');
}
if(['', 'main', 'liquid', 'bisq'].includes(this.network)) {
codeText = codeText.replace('mempoolJS();', `mempoolJS({
hostname: '${document.location.hostname}'
});`);
} else {
codeText = codeText.replace('mempoolJS();', `mempoolJS({
hostname: '${document.location.hostname}',
network: '${this.network}'
});`);
}
2021-06-06 17:06:56 -03:00
}
2021-09-03 07:04:19 -03:00
if (this.env.BASE_MODULE === 'bisq') {
codeText = codeText.replace('} = mempoolJS();', ` = bisqJS();`);
codeText = codeText.replace('{ %{0}: ', '');
2021-08-11 12:50:49 -03:00
}
2021-09-03 07:04:19 -03:00
if (this.env.BASE_MODULE === 'liquid') {
codeText = codeText.replace('} = mempoolJS();', ` = liquidJS();`);
codeText = codeText.replace('{ %{0}: ', '');
2021-08-11 12:50:49 -03:00
}
2021-09-03 07:04:19 -03:00
return codeText;
}
wrapEsModule(code: any) {
let codeText: string;
if (code.codeTemplate) {
codeText = this.normalizeHostsESModule(code.codeTemplate.esModule);
if(this.network === '' || this.network === 'main') {
codeText = this.replaceJSPlaceholder(codeText, code.codeSampleMainnet.esModule);
}
if (this.network === 'testnet') {
codeText = this.replaceJSPlaceholder(codeText, code.codeSampleTestnet.esModule);
}
if (this.network === 'signet') {
codeText = this.replaceJSPlaceholder(codeText, code.codeSampleSignet.esModule);
}
2021-12-27 22:54:45 +04:00
if (this.network === 'liquid' || this.network === 'liquidtestnet') {
2021-09-03 07:04:19 -03:00
codeText = this.replaceJSPlaceholder(codeText, code.codeSampleLiquid.esModule);
}
if (this.network === 'bisq') {
codeText = this.replaceJSPlaceholder(codeText, code.codeSampleBisq.esModule);
}
let importText = `import mempoolJS from "@mempool/mempool.js";`;
if (this.env.BASE_MODULE === 'bisq') {
importText = `import bisqJS from "@mempool/bisq.js";`;
}
if (this.env.BASE_MODULE === 'liquid') {
importText = `import liquidJS from "@mempool/liquid.js";`;
}
2021-08-11 12:50:49 -03:00
2021-09-03 07:04:19 -03:00
return `${importText}
2021-06-06 17:06:56 -03:00
const init = async () => {
${codeText}
};
init();`;
2021-09-03 07:04:19 -03:00
}
2021-06-06 17:06:56 -03:00
}
2021-09-03 07:04:19 -03:00
wrapCommonJS(code: any) {
let codeText: string;
if (code.codeTemplate) {
codeText = this.normalizeHostsCommonJS(code.codeTemplate.commonJS);
2021-06-06 17:06:56 -03:00
2021-09-03 07:04:19 -03:00
if(this.network === '' || this.network === 'main') {
codeText = this.replaceJSPlaceholder(codeText, code.codeSampleMainnet.esModule);
}
if (this.network === 'testnet') {
codeText = this.replaceJSPlaceholder(codeText, code.codeSampleTestnet.esModule);
}
if (this.network === 'signet') {
codeText = this.replaceJSPlaceholder(codeText, code.codeSampleSignet.esModule);
}
2021-12-27 22:54:45 +04:00
if (this.network === 'liquid' || this.network === 'liquidtestnet') {
2021-09-03 07:04:19 -03:00
codeText = this.replaceJSPlaceholder(codeText, code.codeSampleLiquid.esModule);
}
if (this.network === 'bisq') {
codeText = this.replaceJSPlaceholder(codeText, code.codeSampleBisq.esModule);
}
2021-08-11 12:50:49 -03:00
if (code.noWrap) {
return codeText;
}
2021-09-03 07:04:19 -03:00
let importText = `<script src="https://mempool.space/mempool.js"></script>`;
if (this.env.BASE_MODULE === 'bisq') {
importText = `<script src="https://bisq.markets/bisq.js"></script>`;
}
if (this.env.BASE_MODULE === 'liquid') {
importText = `<script src="https://liquid.network/liquid.js"></script>`;
}
2021-09-03 20:13:31 -03:00
let resultHtml = '<pre id="result"></pre>';
if (this.method === 'websocket') {
resultHtml = `<h2>Blocks</h2><pre id="result-blocks">Waiting for data</pre><br>
<h2>Mempool Info</h2><pre id="result-mempool-info">Waiting for data</pre><br>
<h2>Transactions</h2><pre id="result-transactions">Waiting for data</pre><br>
<h2>Mempool Blocks</h2><pre id="result-mempool-blocks">Waiting for data</pre><br>`;
2021-09-03 20:13:31 -03:00
}
2021-09-03 07:04:19 -03:00
return `<!DOCTYPE html>
2021-06-06 17:06:56 -03:00
<html>
<head>
2021-08-11 12:50:49 -03:00
${importText}
2021-06-06 17:06:56 -03:00
<script>
const init = async () => {
${codeText}
};
init();
</script>
</head>
2021-09-03 07:04:19 -03:00
<body>
2021-09-03 20:13:31 -03:00
${resultHtml}
2021-09-03 07:04:19 -03:00
</body>
2021-06-06 17:06:56 -03:00
</html>`;
}
}
2021-08-11 12:50:49 -03:00
wrapImportTemplate() {
let importTemplate = `# npm
npm install @mempool/mempool.js --save
# yarn
yarn add @mempool/mempool.js`;
2021-09-03 07:04:19 -03:00
if (this.env.BASE_MODULE === 'bisq') {
2021-08-11 12:50:49 -03:00
importTemplate = `# npm
npm install @mempool/bisq.js --save
# yarn
yarn add @mempool/bisq.js`;
}
2021-09-03 07:04:19 -03:00
if (this.env.BASE_MODULE === 'liquid') {
2021-08-11 12:50:49 -03:00
importTemplate = `# npm
npm install @mempool/liquid.js --save
# yarn
yarn add @mempool/liquid.js`;
}
return importTemplate;
}
2021-09-03 07:04:19 -03:00
wrapCurlTemplate(code: any) {
if (code.codeTemplate) {
if (this.network === 'testnet') {
return this.replaceCurlPlaceholder(code.codeTemplate.curl, code.codeSampleTestnet);
}
if (this.network === 'signet') {
return this.replaceCurlPlaceholder(code.codeTemplate.curl, code.codeSampleSignet);
}
2022-01-11 21:59:22 -05:00
if (this.network === 'liquid') {
2021-09-03 07:04:19 -03:00
return this.replaceCurlPlaceholder(code.codeTemplate.curl, code.codeSampleLiquid);
}
2022-01-11 21:59:22 -05:00
if (this.network === 'liquidtestnet') {
return this.replaceCurlPlaceholder(code.codeTemplate.curl, code.codeSampleLiquidTestnet);
}
2021-09-03 07:04:19 -03:00
if (this.network === 'bisq') {
return this.replaceCurlPlaceholder(code.codeTemplate.curl, code.codeSampleBisq);
}
if (this.network === '' || this.network === 'main') {
return this.replaceCurlPlaceholder(code.codeTemplate.curl, code.codeSampleMainnet);
}
}
}
wrapResponse(code: any) {
if (this.method === 'websocket') {
return '';
}
if (this.network === 'testnet') {
return code.codeSampleTestnet.response;
}
if (this.network === 'signet') {
return code.codeSampleSignet.response;
}
2022-01-11 21:59:22 -05:00
if (this.network === 'liquid') {
2021-09-03 07:04:19 -03:00
return code.codeSampleLiquid.response;
}
2022-01-11 21:59:22 -05:00
if (this.network === 'liquidtestnet') {
return code.codeSampleLiquidTestnet.response;
}
2021-09-03 07:04:19 -03:00
if (this.network === 'bisq') {
return code.codeSampleBisq.response;
}
return code.codeSampleMainnet.response;
}
replaceJSPlaceholder(text: string, code: any) {
for (let index = 0; index < code.length; index++) {
const textReplace = code[index];
const indexNumber = index + 1;
text = text.replace('%{' + indexNumber + '}', textReplace);
}
return text;
}
replaceCurlPlaceholder(curlText: any, code: any) {
let text = curlText;
2022-02-07 13:25:36 -05:00
text = text.replace( "[[hostname]]", this.hostname );
text = text.replace( "[[baseNetworkUrl]]", this.baseNetworkUrl );
2021-09-03 07:04:19 -03:00
for (let index = 0; index < code.curl.length; index++) {
const textReplace = code.curl[index];
const indexNumber = index + 1;
text = text.replace('%{' + indexNumber + '}', textReplace);
}
if (this.env.BASE_MODULE === 'mempool') {
if (this.network === 'main' || this.network === '') {
2022-02-07 13:25:36 -05:00
if (this.method === 'POST') {
2021-09-27 03:49:20 +09:00
return `curl -X POST -sSLd "${text}"`;
}
return `curl -sSL "${this.hostname}${text}"`;
}
2022-02-07 13:25:36 -05:00
if (this.method === 'POST') {
2021-09-27 03:49:20 +09:00
return `curl -X POST -sSLd "${text}"`;
2021-09-03 07:04:19 -03:00
}
return `curl -sSL "${this.hostname}/${this.network}${text}"`;
2022-01-12 00:43:41 -05:00
} else if (this.env.BASE_MODULE === 'liquid') {
2022-02-07 13:25:36 -05:00
if (this.method === 'POST') {
2022-01-12 00:43:41 -05:00
if (this.network !== 'liquid') {
text = text.replace('/api', `/${this.network}/api`);
}
return `curl -X POST -sSLd "${text}"`;
}
return ( this.network === 'liquid' ? `curl -sSL "${this.hostname}${text}"` : `curl -sSL "${this.hostname}/${this.network}${text}"` );
} else {
return `curl -sSL "${this.hostname}${text}"`;
2021-09-03 07:04:19 -03:00
}
2022-01-12 00:43:41 -05:00
2021-09-03 07:04:19 -03:00
}
2021-06-06 17:06:56 -03:00
}