Skip to content

🀯 LobeHub - Super-Agents on Your Team. Add super-agents to the chats your team already uses.They take on work by themselves and get better every week.

7.4 KB
Raw Blame
/**
 * Generate `src/generated/` (types + resource-style SDK) from the committed
 * OpenAPI spec (`packages/openapi/openapi.yml`) via @hey-api/openapi-ts.
 *
 * The generated output is committed so the published package never depends on
 * generation happening at install time, and `--check` keeps it from drifting
 * when routes change (same contract as `generate-openapi.ts --check`).
 *
 * Usage:
 *   bun scripts/generate-sdk.ts          # regenerate src/generated
 *   bun scripts/generate-sdk.ts --check  # verify src/generated is up to date
 */
import { mkdtempSync, readdirSync, readFileSync, rmSync, writeFileSync } from 'node:fs';
import { tmpdir } from 'node:os';
import path from 'node:path';

import { createClient } from '@hey-api/openapi-ts';

import configThenable from '../openapi-ts.config';

// defineConfig returns a thenable β€” resolve it once so the object can be
// spread and passed to createClient with proper types.
const config = await configThenable;

const PKG_ROOT = path.join(import.meta.dirname, '..');
const OUTPUT_DIR = path.join(PKG_ROOT, 'src', 'generated');

const MERGE_METHOD_HEADERS_HELPER = `
const mergeMethodHeaders = (
    defaults: Record<string, unknown>,
    headers: Options['headers'],
): Record<string, unknown> => {
    const merged = { ...defaults };
    if (!headers) {
        return merged;
    }
    const entries = headers instanceof Headers ? [...headers.entries()] : Array.isArray(headers) ? headers : Object.entries(headers);
    for (const [key, value] of entries) {
        merged[key] = value;
    }
    return merged;
};
`;

/**
 * hey-api 0.99's generated headers handling breaks on legal HeadersInit
 * shapes; rewrite the output in two places (applied identically in generate
 * and --check modes, so the byte-level comparison is unaffected):
 *
 * 1. `mergeHeaders` reads plain objects via `Object.entries`, which turns a
 *    tuple array (`[['X-Trace','1']]`) into numeric keys β€” normalize tuple
 *    arrays through `new Headers()` first. This covers client-level defaults
 *    and read-method per-call headers.
 * 2. Write methods merge caller headers with an object spread
 *    (`{ 'Content-Type': …, ...options.headers }`), which silently drops
 *    `Headers` instances and corrupts tuple arrays. Rewrite the spread to a
 *    helper that normalizes every HeadersInit shape into a PLAIN object β€”
 *    not a `Headers` β€” so the `'Content-Type': null` deletion sentinel on
 *    form-data methods survives until the client's final merge
 *    (`mergeHeaders(_config.headers, options.headers)`), where it must be
 *    able to delete a client-default Content-Type.
 */
const rewriteHeaderSpreads = (dir: string) => {
  const sdkPath = path.join(dir, 'sdk.gen.ts');
  const sdkSource = readFileSync(sdkPath, 'utf8');
  const importAnchor = "} from './types.gen';\n";
  const sdkRewritten = sdkSource
    .replaceAll(
      /headers: \{\s*'Content-Type': ('application\/json'|null),\s*\.\.\.options\.headers\s*\}/g,
      "headers: mergeMethodHeaders({ 'Content-Type': $1 }, options.headers)",
    )
    .replace(importAnchor, importAnchor + MERGE_METHOD_HEADERS_HELPER);
  if (sdkRewritten === sdkSource || !sdkRewritten.includes('mergeMethodHeaders(')) {
    throw new Error(
      'rewriteHeaderSpreads: expected generated header-spread pattern not found in sdk.gen.ts β€” check the generator output against the rewrite rules.',
    );
  }
  writeFileSync(sdkPath, sdkRewritten);

  const utilsPath = path.join(dir, 'client', 'utils.gen.ts');
  const utilsSource = readFileSync(utilsPath, 'utf8');
  const iteratorLine =
    'const iterator = header instanceof Headers ? headersEntries(header) : Object.entries(header);';
  const utilsRewritten = utilsSource.replace(
    iteratorLine,
    'const iterator = header instanceof Headers ? headersEntries(header) : Array.isArray(header) ? headersEntries(new Headers(header as Array<[string, string]>)) : Object.entries(header);',
  );
  if (utilsRewritten === utilsSource) {
    throw new Error(
      'rewriteHeaderSpreads: expected mergeHeaders iterator line not found in client/utils.gen.ts β€” check the generator output against the rewrite rules.',
    );
  }
  writeFileSync(utilsPath, utilsRewritten);
};

const CONFIG_ANCHOR = "  extends Omit<RequestInit, 'body' | 'headers' | 'method'>, CoreConfig {\n";
const CONFIG_FIELDS = `  /**
   * Retries on 408/429/5xx and network errors, for idempotent methods or a
   * POST with an \`idempotencyKey\` only. Applied by \`createLobeHub\`; default 2.
   */
  maxRetries?: number;
  /**
   * Milliseconds to wait for the response headers, per attempt; \`0\` disables.
   * Applied by \`createLobeHub\`; default 600000 (10 minutes).
   */
  timeout?: number;
`;
const REQUEST_OPTIONS_ANCHOR = `  /**
   * Any body that you want to add to your request.
   *
   * {@link https://developer.mozilla.org/docs/Web/API/fetch#body}
   */
  body?: unknown;
  path?: Record<string, unknown>;
`;
const REQUEST_OPTIONS_FIELDS = `  /**
   * Sent as \`Idempotency-Key\`: the server runs the request once and replays
   * its answer for repeats, which also makes a POST safe to retry.
   */
  idempotencyKey?: string;
`;

/**
 * Declare the hand-written runtime options of \`createLobeHub\` (see
 * \`src/client.ts\`) on the generated client types, so every method accepts
 * them. Done here rather than by module augmentation because the published
 * d.ts is bundled, and a relative \`declare module\` does not survive that.
 */
const injectRuntimeOptions = (dir: string) => {
  const typesPath = path.join(dir, 'client', 'types.gen.ts');
  const source = readFileSync(typesPath, 'utf8');
  if (!source.includes(CONFIG_ANCHOR) || !source.includes(REQUEST_OPTIONS_ANCHOR)) {
    throw new Error(
      'injectRuntimeOptions: expected Config / RequestOptions declarations not found in client/types.gen.ts β€” check the generator output.',
    );
  }
  writeFileSync(
    typesPath,
    source
      .replace(CONFIG_ANCHOR, CONFIG_ANCHOR + CONFIG_FIELDS)
      .replace(REQUEST_OPTIONS_ANCHOR, REQUEST_OPTIONS_FIELDS + REQUEST_OPTIONS_ANCHOR),
  );
};

const listFiles = (dir: string, base = dir): string[] =>
  readdirSync(dir, { withFileTypes: true }).flatMap((entry) => {
    const full = path.join(dir, entry.name);
    return entry.isDirectory() ? listFiles(full, base) : [path.relative(base, full)];
  });

process.chdir(PKG_ROOT);

if (process.argv.includes('--check')) {
  const tempDir = mkdtempSync(path.join(tmpdir(), 'lobehub-sdk-check-'));
  try {
    await createClient({ ...config, logs: { level: 'silent' }, output: tempDir });
    rewriteHeaderSpreads(tempDir);
    injectRuntimeOptions(tempDir);

    const expected = listFiles(tempDir).sort();
    const committed = listFiles(OUTPUT_DIR).sort();
    const stale =
      expected.join('\n') !== committed.join('\n') ||
      expected.some(
        (file) =>
          readFileSync(path.join(tempDir, file), 'utf8') !==
          readFileSync(path.join(OUTPUT_DIR, file), 'utf8'),
      );

    if (stale) {
      console.error(
        'βœ— src/generated is out of date with openapi.yml. Run `bun generate` in packages/sdk and commit the result.',
      );
      process.exit(1);
    }
    console.log('βœ“ src/generated is up to date with openapi.yml');
  } finally {
    rmSync(tempDir, { force: true, recursive: true });
  }
} else {
  rmSync(OUTPUT_DIR, { force: true, recursive: true });
  await createClient(config);
  rewriteHeaderSpreads(OUTPUT_DIR);
  injectRuntimeOptions(OUTPUT_DIR);
  console.log(`βœ“ Generated ${path.relative(PKG_ROOT, OUTPUT_DIR)} from openapi.yml`);
}