Apéndice F — JavaScript y TypeScript básico

Objetivo del apéndice

Lo mínimo de JS/TS que necesitas para escribir plugins de Backstage. Si vienes de otro lenguaje, este apéndice te da el vocabulario.

Variables y tipos básicos

const name: string = 'sazon';
const port: number = 7007;
const enabled: boolean = true;
const tags: string[] = ['api', 'restaurant'];
const map: Record<string, number> = { a: 1 };
const maybe: string | null = null;

Funciones

function add(a: number, b: number): number { return a + b; }
const greet = (name: string): string => `Hola, ${name}`;
const optional = (x?: number) => x ?? 0;     // nullish coalescing
async function load(): Promise<string> { return 'ok'; }

Objetos e interfaces

interface Service { name: string; port: number; tags?: string[]; }
const svc: Service = { name: 'api', port: 7007 };
const withDefaults: Service = { name: 'api', port: 7007, tags: [] };

Async/Await

async function getStatus(service: string) {
  const res = await fetch(`/api/sazon/status/${service}`);
  if (!res.ok) throw new Error(`HTTP ${res.status}`);
  return res.json() as Promise<{ status: string }>;
}

Destructuring

const { name, port = 80 } = svc;
const [first, ...rest] = ['a', 'b', 'c']; // first='a', rest=['b','c']

Modules: import/export

import { z } from 'zod';
import type { LoggerService } from '@backstage/backend-plugin-api';

export const StatusSchema = z.object({ service: z.string() });
export type Status = z.infer<typeof StatusSchema>;

Node: package.json mínimo

{
  "name": "@sazon/plugin-x",
  "version": "0.1.0",
  "main": "src/index.ts",
  "scripts": {
    "start": "backstage-cli package start",
    "test": "backstage-cli package test"
  }
}
Tipado estricto

Backstage activa :code:`strict: true` por defecto. Si vienes de JS, prepárate para tipar todo al principio; a la larga, el compilador atrapa errores caros.