Merge pull request #716 from MiguelMedeiros/documentation-api

Improvements to API documentation and examples
This commit is contained in:
wiz 2021-09-04 09:10:59 +09:00 committed by GitHub
commit 8c2dfea6a6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 4455 additions and 4393 deletions

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

View File

@ -1,20 +1,20 @@
<div class="code"> <div class="code">
<ul ngbNav #navCodeTemplate="ngbNav" class="nav-tabs code-tab"> <ul ngbNav #navCodeTemplate="ngbNav" class="nav-tabs code-tab">
<li ngbNavItem *ngIf="code.codeSample.curl"> <li ngbNavItem *ngIf="code.codeTemplate.curl && method !== 'websocket'">
<a ngbNavLink>cURL</a> <a ngbNavLink>cURL</a>
<ng-template ngbNavContent> <ng-template ngbNavContent>
<div class="subtitle"><ng-container i18n="API Docs code example">Code Example</ng-container> <app-clipboard [text]="wrapCurl(code.codeSample.curl)"></app-clipboard></div> <div class="subtitle"><ng-container i18n="API Docs code example">Code Example</ng-container> <app-clipboard [text]="wrapCurlTemplate(code)"></app-clipboard></div>
<pre><code [innerText]="wrapCurl(code.codeSample.curl)"></code></pre> <pre><code [innerText]="wrapCurlTemplate(code)"></code></pre>
</ng-template> </ng-template>
</li> </li>
<li ngbNavItem> <li ngbNavItem>
<a ngbNavLink>CommonJS</a> <a ngbNavLink>CommonJS</a>
<ng-template ngbNavContent> <ng-template ngbNavContent>
<div class="subtitle"><ng-container i18n="API Docs code example">Code Example</ng-container> <app-clipboard [text]="wrapCommonJS(code.codeSample.commonJS)"></app-clipboard></div> <div class="subtitle"><ng-container i18n="API Docs code example">Code Example</ng-container> <app-clipboard [text]="wrapCommonJS(code)"></app-clipboard></div>
<div class="links"> <div class="links">
<a [href]="npmGithubLink()" target="_blank">github repository</a> <a [href]="npmGithubLink()" target="_blank">github repository</a>
</div> </div>
<pre><code [innerText]="wrapCommonJS(code.codeSample.commonJS)"></code></pre> <pre><code [innerText]="wrapCommonJS(code)"></code></pre>
</ng-template> </ng-template>
</li> </li>
<li ngbNavItem> <li ngbNavItem>
@ -26,14 +26,14 @@
<a [href]="npmModuleLink()" target="_blank">npm package</a> <a [href]="npmModuleLink()" target="_blank">npm package</a>
</div> </div>
<pre><code [innerText]="wrapImportTemplate()"></code></pre> <pre><code [innerText]="wrapImportTemplate()"></code></pre>
<div class="subtitle"><ng-container i18n="API Docs code example">Code Example</ng-container> <app-clipboard [text]="wrapESmodule(code.codeSample.esModule)"></app-clipboard></div> <div class="subtitle"><ng-container i18n="API Docs code example">Code Example</ng-container> <app-clipboard [text]="wrapEsModule(code)"></app-clipboard></div>
<pre><code [innerText]="wrapESmodule(code.codeSample.esModule)"></code></pre> <pre><code [innerText]="wrapEsModule(code)"></code></pre>
</ng-template> </ng-template>
</li> </li>
</ul> </ul>
<div [ngbNavOutlet]="navCodeTemplate"></div> <div [ngbNavOutlet]="navCodeTemplate"></div>
<div *ngIf="code.responseSample" class="response"> <div *ngIf="code.codeTemplate && wrapResponse(code) !== ''" class="response">
<div class="subtitle"><ng-container i18n="API Docs API response">Response</ng-container> <app-clipboard [text]="code.responseSample"></app-clipboard></div> <div class="subtitle"><ng-container i18n="API Docs API response">Response</ng-container> <app-clipboard [text]="wrapResponse(code)"></app-clipboard></div>
<pre><code [innerText]="code.responseSample"></code></pre> <pre><code [innerText]="wrapResponse(code)"></code></pre>
</div> </div>
</div> </div>

View File

@ -1,32 +1,32 @@
import { Component, Input } from '@angular/core'; import { Component, Input, OnInit } from '@angular/core';
import { Env, StateService } from 'src/app/services/state.service';
@Component({ @Component({
selector: 'app-code-template', selector: 'app-code-template',
templateUrl: './code-template.component.html', templateUrl: './code-template.component.html',
styleUrls: ['./code-template.component.scss'] styleUrls: ['./code-template.component.scss']
}) })
export class CodeTemplateComponent { export class CodeTemplateComponent implements OnInit {
@Input() network: string; @Input() network: string;
@Input() layer: string; @Input() code: any;
@Input() code: { @Input() hostname: string;
codeSample: { @Input() method: 'get' | 'post' | 'websocket' = 'get';
esModule: string; env: Env;
commonJS: string;
curl: string;
},
responseSample: string;
};
hostname = document.location.hostname;
constructor( constructor(
private stateService: StateService,
) { } ) { }
ngOnInit(): void {
this.env = this.stateService.env;
}
npmGithubLink(){ npmGithubLink(){
let npmLink = `https://github.com/mempool/mempool.js`; let npmLink = `https://github.com/mempool/mempool.js`;
if (this.layer === 'bisq') { if (this.network === 'bisq') {
npmLink = `https://github.com/mempool/mempool.js/tree/main/npm-bisq-js`; npmLink = `https://github.com/mempool/mempool.js/tree/main/npm-bisq-js`;
} }
if (this.layer === 'liquid') { if (this.network === 'liquid') {
npmLink = `https://github.com/mempool/mempool.js/tree/main/npm-liquid-js`; npmLink = `https://github.com/mempool/mempool.js/tree/main/npm-liquid-js`;
} }
return npmLink; return npmLink;
@ -34,67 +34,153 @@ export class CodeTemplateComponent {
npmModuleLink() { npmModuleLink() {
let npmLink = `https://www.npmjs.org/package/@mempool/mempool.js`; let npmLink = `https://www.npmjs.org/package/@mempool/mempool.js`;
if (this.layer === 'bisq') { if (this.network === 'bisq') {
npmLink = `https://www.npmjs.org/package/@mempool/bisq.js`; npmLink = `https://www.npmjs.org/package/@mempool/bisq.js`;
} }
if (this.layer === 'liquid') { if (this.network === 'liquid') {
npmLink = `https://www.npmjs.org/package/@mempool/liquid.js`; npmLink = `https://www.npmjs.org/package/@mempool/liquid.js`;
} }
return npmLink; return npmLink;
} }
normalizeCodeHostname(code: string) { normalizeHostsESModule(codeText: string) {
let codeText: string; if (this.env.BASE_MODULE === 'mempool') {
if (this.network === 'bisq' || this.network === 'liquid'){ if (['liquid', 'bisq'].includes(this.network)) {
codeText = code.replace('%{1}', this.network); codeText = codeText.replace('%{0}', this.network);
}else{ } else {
codeText = code.replace('%{1}', 'bitcoin'); 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}'
});`);
}
}
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}: ', '');
} }
return codeText; return codeText;
} }
wrapESmodule(code: string) { normalizeHostsCommonJS(codeText: string) {
let codeText = this.normalizeCodeHostname(code); if (this.env.BASE_MODULE === 'mempool') {
if (['liquid', 'bisq'].includes(this.network)) {
if (this.network && this.network !== 'mainnet') { codeText = codeText.replace('%{0}', this.network);
codeText = codeText.replace('mempoolJS();', `mempoolJS({ } else {
hostname: '${this.hostname}/${this.network}' 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}'
});`);
}
} }
let importText = `import mempoolJS from "@mempool/mempool.js";`; if (this.env.BASE_MODULE === 'bisq') {
if (this.layer === 'bisq') { codeText = codeText.replace('} = mempoolJS();', ` = bisqJS();`);
importText = `import bisqJS from "@mempool/bisq.js";`; codeText = codeText.replace('{ %{0}: ', '');
}
if (this.layer === 'liquid') {
importText = `import liquidJS from "@mempool/liquid.js";`;
} }
return `${importText} if (this.env.BASE_MODULE === 'liquid') {
codeText = codeText.replace('} = mempoolJS();', ` = liquidJS();`);
codeText = codeText.replace('{ %{0}: ', '');
}
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);
}
if (this.network === 'liquid') {
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";`;
}
return `${importText}
const init = async () => { const init = async () => {
${codeText} ${codeText}
}; };
init();`; init();`;
}
} }
wrapCommonJS(code: string) { wrapCommonJS(code: any) {
let codeText = this.normalizeCodeHostname(code); let codeText: string;
if (code.codeTemplate) {
codeText = this.normalizeHostsCommonJS(code.codeTemplate.commonJS);
if (this.network && this.network !== 'mainnet') { if(this.network === '' || this.network === 'main') {
codeText = codeText.replace('mempoolJS();', `mempoolJS({ codeText = this.replaceJSPlaceholder(codeText, code.codeSampleMainnet.esModule);
hostname: '${this.hostname}/${this.network}' }
});` ); if (this.network === 'testnet') {
} codeText = this.replaceJSPlaceholder(codeText, code.codeSampleTestnet.esModule);
}
if (this.network === 'signet') {
codeText = this.replaceJSPlaceholder(codeText, code.codeSampleSignet.esModule);
}
if (this.network === 'liquid') {
codeText = this.replaceJSPlaceholder(codeText, code.codeSampleLiquid.esModule);
}
if (this.network === 'bisq') {
codeText = this.replaceJSPlaceholder(codeText, code.codeSampleBisq.esModule);
}
let importText = `<script src="https://mempool.space/mempool.js"></script>`; let importText = `<script src="https://mempool.space/mempool.js"></script>`;
if (this.layer === 'bisq') { if (this.env.BASE_MODULE === 'bisq') {
importText = `<script src="https://bisq.markets/bisq.js"></script>`; importText = `<script src="https://bisq.markets/bisq.js"></script>`;
} }
if (this.layer === 'liquid') { if (this.env.BASE_MODULE === 'liquid') {
importText = `<script src="https://liquid.network/liquid.js"></script>`; importText = `<script src="https://liquid.network/liquid.js"></script>`;
} }
return `<!DOCTYPE html>
let resultHtml = '<pre id="result"></pre>';
if (this.method === 'websocket') {
resultHtml = `<pre id="result-blocks"></pre>
<pre id="result-mempool-info"></pre>
<pre id="result-transactions"></pre>
<pre id="result-mempool-blocks"></pre>`;
}
return `<!DOCTYPE html>
<html> <html>
<head> <head>
${importText} ${importText}
@ -105,14 +191,11 @@ init();`;
init(); init();
</script> </script>
</head> </head>
<body></body> <body>
${resultHtml}
</body>
</html>`; </html>`;
}
wrapCurl(code: string) {
if (this.network && this.network !== 'mainnet') {
return code.replace('mempool.space/', `mempool.space/${this.network}/`);
} }
return code;
} }
wrapImportTemplate() { wrapImportTemplate() {
@ -123,7 +206,7 @@ npm install @mempool/mempool.js --save
# yarn # yarn
yarn add @mempool/mempool.js`; yarn add @mempool/mempool.js`;
if (this.layer === 'bisq') { if (this.env.BASE_MODULE === 'bisq') {
importTemplate = `# npm importTemplate = `# npm
npm install @mempool/bisq.js --save npm install @mempool/bisq.js --save
@ -131,7 +214,7 @@ npm install @mempool/bisq.js --save
yarn add @mempool/bisq.js`; yarn add @mempool/bisq.js`;
} }
if (this.layer === 'liquid') { if (this.env.BASE_MODULE === 'liquid') {
importTemplate = `# npm importTemplate = `# npm
npm install @mempool/liquid.js --save npm install @mempool/liquid.js --save
@ -142,4 +225,78 @@ yarn add @mempool/liquid.js`;
return importTemplate; return importTemplate;
} }
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);
}
if (this.network === 'liquid') {
return this.replaceCurlPlaceholder(code.codeTemplate.curl, code.codeSampleLiquid);
}
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;
}
if (this.network === 'liquid') {
return code.codeSampleLiquid.response;
}
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;
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 === '') {
if (this.method === 'post') {
return `curl POST -sSLd "${text}"`;
}
return `curl -sSL "${this.hostname}${text}"`;
}
if (this.method === 'post') {
text = text.replace('/api', `/${this.network}/api`);
return `curl POST -sSLd "${text}"`;
}
return `curl -sSL "${this.hostname}/${this.network}${text}"`;
}
if (this.env.BASE_MODULE !== 'mempool') {
return `curl -sSL "${this.hostname}${text}"`;
}
}
} }