TypeScript
Platform API belirteci oluşturmak ve platform.proto indirmek için gRPC kılavuzuyla başlayın. Aşağıdaki komutları bu dosyanın bulunduğu dizinden çalıştırın.
Tarayıcılar yerel gRPC HTTP/2 bağlantıları açamaz. Platform API, HTTP/1.1 uç noktasında gRPC-Web de kabul eder. protoc, protoc-gen-js ve protoc-gen-grpc-web eklentilerini ve çalışma zamanı paketlerini kurun:
npm install grpc-web google-protobuf
npm install --save-dev @types/google-protobuf
mkdir -p src/generated
protoc -I. platform.proto \
--js_out=import_style=commonjs,binary:src/generated \
--grpc-web_out=import_style=typescript,mode=grpcweb:src/generated
Derleyici eklentilerini resmî gRPC-Web sürümlerinden ve kurulum kılavuzundan indirin; PATH üzerinde bulunmalıdırlar. Komut platform_pb.js, TypeScript bildirimlerini ve PlatformServiceClientPb.ts dosyasını üretir.
import {PlatformServicePromiseClient} from './generated/PlatformServiceClientPb';
import {GetCatalogRequest, ListCatalogsRequest} from './generated/platform_pb';
// Supply a token at runtime. Do not bundle a long-lived API token in browser code.
export async function listCatalogDetails(token: string) {
const client = new PlatformServicePromiseClient('https://reconstruct.dev');
const metadata = {authorization: `Bearer ${token}`};
const listRequest = new ListCatalogsRequest();
listRequest.setPageSize(10);
const page = await client.listCatalogs(listRequest, metadata);
for (const catalog of page.getCatalogsList()) {
const detailRequest = new GetCatalogRequest();
detailRequest.setCatalogId(catalog.getCatalogId());
const detail = await client.getCatalog(detailRequest, metadata);
console.log(detail.getCatalogId(), detail.getDescription());
}
}
Bir tarayıcı uygulaması için herkese açık API, uygulamanın origin adresine CORS üzerinden izin vermelidir. Kalıcı API belirteçlerini güvenilir bir sunucuda tutun; tarayıcı paketi içine gömülmüş bir sırrı koruyamaz.