Skip to content

armgit / spree

Public Imported from GitHub
0

Open Source Platform for DTC, B2B Commerce, Marketplaces & Omnichannel. REST APIs, TypeScript SDKs, and production-ready Next.js storefront. Self-host it. Own your data. No vendor lock-in. Zero platform fees.

3.1 KB
Raw Blame
/**
 * Bundles a slimmed snapshot of the Admin API OpenAPI spec into the package.
 *
 * Usage: node scripts/bundle-spec.mjs
 *
 * Reads:  docs/api-reference/admin.yaml (rswag-generated — never hand-edit)
 * Writes: src/generated/admin-spec.json (not committed; generated before every
 *         build, test, type check and dev run)
 *
 * The snapshot keeps what `spree api endpoints` / `spree api schema` need —
 * operation metadata, parameters, request/response schemas, the scope lines
 * rswag writes into descriptions, and component schemas for $ref resolution —
 * and strips examples and code samples, which dominate the YAML's size.
 */

import * as fs from 'node:fs'
import * as path from 'node:path'
import { fileURLToPath } from 'node:url'
import { parse } from 'yaml'

const here = path.dirname(fileURLToPath(import.meta.url))
const SPEC_PATH = path.resolve(here, '../../../docs/api-reference/admin.yaml')
const OUT_PATH = path.resolve(here, '../src/generated/admin-spec.json')
// A tiny sibling of just the top-level resource paths, so shell completion
// (which fires on every Tab) imports ~40 strings instead of materializing the
// full ~250KB operation graph.
const PATHS_OUT = path.resolve(here, '../src/generated/resource-paths.json')

function stripExamples(node) {
  if (Array.isArray(node)) {
    for (const item of node) stripExamples(item)
    return
  }
  if (node && typeof node === 'object') {
    delete node.example
    delete node.examples
    delete node['x-codeSamples']
    for (const value of Object.values(node)) stripExamples(value)
  }
}

// Build, test and type check each run this script, and Turbo runs them at the
// same time: writing in place would let one read a file another is halfway
// through writing. A rename replaces the file in one step.
function writeAtomically(file, contents) {
  const temporary = `${file}.${process.pid}.tmp`
  fs.writeFileSync(temporary, contents)
  fs.renameSync(temporary, file)
}

const spec = parse(fs.readFileSync(SPEC_PATH, 'utf-8'))
stripExamples(spec)

const slim = {
  info: spec.info,
  paths: spec.paths,
  components: { schemas: spec.components?.schemas ?? {} },
}

fs.mkdirSync(path.dirname(OUT_PATH), { recursive: true })
writeAtomically(OUT_PATH, `${JSON.stringify(slim, null, 2)}\n`)

// Distinct top-level resource path segments (e.g. "/products", "/orders"),
// excluding `{...}` placeholders — the only data shell completion needs.
const resourcePaths = [
  ...new Set(
    Object.keys(slim.paths ?? {})
      .map((p) => p.replace('/api/v3/admin', '').split('/')[1])
      .filter((seg) => seg && !seg.startsWith('{'))
      .map((seg) => `/${seg}`),
  ),
].sort()
writeAtomically(PATHS_OUT, `${JSON.stringify(resourcePaths, null, 2)}\n`)

const HTTP_METHODS = new Set(['get', 'post', 'put', 'patch', 'delete'])
const size = (fs.statSync(OUT_PATH).size / 1024).toFixed(0)
const operations = Object.values(slim.paths ?? {}).reduce(
  (n, methods) => n + Object.keys(methods).filter((m) => HTTP_METHODS.has(m)).length,
  0,
)
console.log(
  `admin-spec.json written: ${size} KB, ${Object.keys(slim.paths ?? {}).length} paths, ${operations} operations`,
)