|
1 | 1 | # api-types — Agent Conventions |
2 | 2 |
|
3 | | -Auto-generated TypeScript types from the OpenSea API v2 OpenAPI spec. Source of truth for API types across the devtools ecosystem. |
| 3 | +Auto-generated TypeScript types from the OpenSea API v2 OpenAPI spec. Source of truth for API types in the devtools ecosystem. |
4 | 4 |
|
5 | | -## Quick Reference |
| 5 | +## Quick commands |
6 | 6 |
|
7 | 7 | ```bash |
8 | 8 | cd packages/api-types |
9 | | -pnpm run update-spec # Fetch latest OpenAPI spec from api.opensea.io |
10 | | -pnpm run build # Regenerate types from spec + bundle with tsup |
11 | | -pnpm run lint # Lint with Biome |
12 | | -pnpm run type-check # Type-check with tsc --noEmit |
13 | | -pnpm run test # Run smoke tests with Vitest |
| 9 | +pnpm run update-spec # fetch latest OpenAPI spec |
| 10 | +pnpm run build # regenerate types and bundle |
| 11 | +pnpm run type-check |
| 12 | +pnpm run test |
| 13 | +pnpm run lint |
14 | 14 | ``` |
15 | 15 |
|
16 | | -## Architecture |
| 16 | +## Responsibilities |
17 | 17 |
|
18 | | -| File | Role | |
19 | | -|------|------| |
20 | | -| `opensea-api.json` | Local copy of the OpenAPI spec (fetched from `api.opensea.io/api/v2/openapi.json`) | |
21 | | -| `scripts/update-spec.mjs` | Fetches the latest spec; falls back to existing local file on network errors | |
22 | | -| `src/generated.ts` | Auto-generated types from `openapi-typescript` — do not edit manually | |
23 | | -| `src/schemas-generated.ts` | Auto-generated named re-exports of every `components.schemas.*` entry — do not edit manually | |
24 | | -| `src/auth-scopes-generated.ts` | Auto-generated runtime scope metadata (`AUTH_SCOPES`, `AuthScopeInfo`) derived from the spec's `AuthScope` schema — do not edit manually | |
25 | | -| `src/index.ts` | Hand-written. Re-exports `schemas-generated.ts` and `auth-scopes-generated.ts`, plus response envelopes and operation helpers | |
26 | | -| `scripts/generate-schema-exports.mjs` | Emits `schemas-generated.ts` from the spec; runs as part of `pnpm run generate` | |
27 | | -| `scripts/generate-auth-scopes.mjs` | Emits `auth-scopes-generated.ts` from the spec's `AuthScope` schema; runs as part of `pnpm run generate` | |
28 | | -| `scripts/check-consumer-imports.mjs` | CI guard — verifies every name workspace packages import from `@opensea/api-types` is in the built `dist/index.d.ts` | |
29 | | -| `test/smoke.test.ts` | Smoke test verifying the generated types compile and export correctly | |
| 18 | +- Keep `opensea-api.json` as the committed, versioned OpenAPI snapshot. |
| 19 | +- Generate named schema exports, auth-scope metadata, and response helpers from the spec. |
| 20 | +- Provide the types that `@opensea/sdk` and `@opensea/cli` import. |
30 | 21 |
|
31 | | -## Review Checklist |
| 22 | +## Rules |
32 | 23 |
|
33 | | -When reviewing changes to this package, verify: |
34 | | - |
35 | | -1. **Never hand-edit `src/generated.ts` or `src/schemas-generated.ts`**. Both are produced from `opensea-api.json` by `pnpm run generate`. Changes are overwritten on the next build. |
36 | | - |
37 | | -2. **Adding a new schema export — no action needed**. When the spec adds a new `components.schemas.X`, it surfaces as a named export automatically via `src/schemas-generated.ts`. Only edit `src/index.ts` for non-schema additions: response envelopes (`components.responses`), operation helpers, or namespace re-exports. |
38 | | - |
39 | | -3. **Downstream consumers**: The SDK (`@opensea/sdk`) and CLI (`@opensea/cli`) depend on this package. After updating the spec, rebuild api-types and verify downstream packages still compile: |
40 | | - ```bash |
41 | | - pnpm --filter @opensea/api-types run build |
42 | | - pnpm --filter sdk run check-types |
43 | | - pnpm --filter cli run build |
44 | | - ``` |
45 | | - |
46 | | - **Always-update-this-first rule**: when adding a new OpenSea v2 endpoint to the SDK or CLI, refresh the spec and regenerate types here BEFORE writing the SDK/CLI method. Hand-rolling request/response types in `packages/sdk/src/api/types.ts` or `packages/cli/src/types/api.ts` is forbidden and CI will block it (`pnpm check-api-paths`). |
47 | | - |
48 | | -4. **Chain enum sync**: The SDK's `Chain` enum has a compile-time assertion against `ChainIdentifier` from this package. If the spec adds a new chain, the SDK build will fail until `Chain` is updated. |
49 | | - |
50 | | -5. **Consumer-imports CI guard**: A standalone CI job (`API Types consumer imports` in `.github/workflows/ci.yml`) runs `scripts/check-consumer-imports.mjs` on every PR. It greps every `from "@opensea/api-types"` import across the workspace and verifies each named import is exported by the built `dist/index.d.ts`. If a name is missing, the job fails with the file/line that imports it. This catches the failure mode where a downstream change happens to resolve through the workspace but would break against the published artifact (the exact bug that briefly broke order posting in SDK 11.0). |
| 24 | +1. **Never hand-edit generated files** in `src/generated.ts`, `src/schemas-generated.ts`, or `src/auth-scopes-generated.ts`. Run `pnpm run build` after `update-spec`. |
| 25 | +2. **Add API endpoints via the spec first**. Before writing SDK/CLI methods, run `pnpm --filter @opensea/api-types run update-spec && build`. Hand-rolling request/response types is forbidden and `pnpm check-api-paths` will fail. |
| 26 | +3. **Schema additions are automatic**. New `components.schemas.*` entries become named exports automatically; only edit `src/index.ts` for non-schema helpers or response envelopes. |
| 27 | +4. **Rebuild downstream**. After spec changes, rebuild api-types, then run `pnpm --filter sdk run check-types` and `pnpm --filter cli run build`. |
| 28 | +5. **Export check**. CI runs `node packages/api-types/scripts/check-consumer-imports.mjs` to ensure every workspace import from `@opensea/api-types` exists in `dist/index.d.ts`. |
| 29 | +6. **Auth-scope drift**. CI runs `node scripts/check-auth-scope-drift.mjs` (repo root) to compare the auth scopes in `opensea-api.json` against the live `/api/v2/auth/scopes` registry. Re-run `update-spec` + `build` when it reports drift. |
51 | 30 |
|
52 | 31 | ## Conventions |
53 | 32 |
|
54 | | -- ESM-only (`"type": "module"`). |
55 | | -- Dual CJS/ESM output via tsup (consumers can `import` or `require`). |
56 | | -- The `update-spec` script is idempotent — safe to run anytime. It prints the path/schema counts on success. |
57 | | -- Use `/sync-openapi` (monorepo slash command) for the full flow: fetch spec, regenerate, open a PR if changed. |
| 33 | +- ESM-only, dual CJS/ESM output via tsup. |
| 34 | +- `pnpm run update-spec` is idempotent and falls back to the local file on network errors. |
| 35 | +- Use `/sync-openapi` for the full fetch → regenerate → PR flow. |
0 commit comments