Repository navigation
Expand file tree
/
Copy pathbuild.mjs
More file actions
99 lines (90 loc) · 5.71 KB
/
Copy pathbuild.mjs
File metadata and controls
99 lines (90 loc) · 5.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
import { createClient } from "@hey-api/openapi-ts";
import { cpSync, appendFileSync, writeFileSync } from "node:fs";
import { execSync } from "node:child_process";
import { fileURLToPath } from "node:url";
import { dirname, join } from "node:path";
import { normalizeSpec } from "./lib/normalize-spec.mjs";
const __dirname = dirname(fileURLToPath(import.meta.url));
// 0.5 Normalize verbose English sentence operationIds to short REST-conventional
// identifiers (e.g. "List Your Organizations" → "listOrganizations").
// Writes a temporary openapi-normalized.json that the generator reads instead
// of the source spec. The source spec is never modified.
// OperationIds that are already identifiers (no spaces) are left untouched —
// those were set intentionally via @extend_schema(operation_id=...).
normalizeSpec("./openapi-derefed.json", "./openapi-normalized.json");
// 1. Generate TypeScript client from OpenAPI spec, including optional
// Valibot and Zod schemas.
// When `plugins` is specified, the defaults (TypeScript + SDK + client) are
// no longer implicit — list them explicitly so the previous output is
// preserved alongside the validator schemas.
await createClient({
input: "./openapi-normalized.json",
output: "src",
plugins: [
"@hey-api/typescript",
"@hey-api/sdk",
"valibot",
{
name: "zod",
compatibilityVersion: 3,
},
],
});
// 2. Copy hand-written utilities into the generated src/ directory
cpSync("lib/sentry-pagination.ts", "src/sentry-pagination.ts");
cpSync("lib/sentry-errors.ts", "src/sentry-errors.ts");
cpSync("lib/browser-client.ts", "src/browser-client.ts");
cpSync("lib/source-analysis.ts", "src/source-analysis.ts");
// Keep the pinned Fetch generator's runtime and types aligned with our contract.
execSync(`node ${JSON.stringify(join(__dirname, "scripts", "generate-response-contract.mjs"))}`, { stdio: "inherit" });
execSync(`node ${JSON.stringify(join(__dirname, "scripts", "generate-request-preparation.mjs"))}`, { stdio: "inherit" });
// 3. Generate operation helpers and the bound client from the SDK output.
// This post-processor inspects src/sdk.gen.ts and openapi-derefed.json,
// detects every operation that accepts a `cursor` query parameter, and
// emits typed fetchPage / paginateAll / paginateUpTo wrappers for each.
// Done as a post-processor (not a Hey API plugin) because the plugin API
// is documented as in-development and unstable.
execSync(`node ${JSON.stringify(join(__dirname, "scripts", "generate-pagination.mjs"))}`, { stdio: "inherit" });
execSync(`node ${JSON.stringify(join(__dirname, "scripts", "generate-error-results.mjs"))}`, { stdio: "inherit" });
execSync(`node ${JSON.stringify(join(__dirname, "scripts", "generate-query-enum-types.mjs"))}`, { stdio: "inherit" });
execSync(`node ${JSON.stringify(join(__dirname, "scripts", "constrain-throw-options.mjs"))}`, { stdio: "inherit" });
// Client-side query defaults (lib/query-defaults.mjs) go into the operations
// themselves, so bound methods and generated wrappers inherit them.
execSync(`node ${JSON.stringify(join(__dirname, "scripts", "apply-query-defaults.mjs"))}`, { stdio: "inherit" });
execSync(`node ${JSON.stringify(join(__dirname, "scripts", "generate-client.mjs"))}`, { stdio: "inherit" });
execSync(`node ${JSON.stringify(join(__dirname, "scripts", "generate-operations.mjs"))}`, { stdio: "inherit" });
// 4. Append re-exports to the generated index.ts so the pagination
// utilities and the per-operation wrappers are part of the public API.
appendFileSync(
"src/index.ts",
[
"",
"export { callWithTypedErrors, narrowError, SentryApiError } from './sentry-errors.ts';",
"export type { DocumentedSentryApiError, NarrowedResult, SentryApiResultError, SentryApiTransportError, SdkResult, UndocumentedSentryApiError } from './sentry-errors.ts';",
"export { parseSentryLinkHeader, unwrapResult, unwrapPaginatedResult, fetchPage, paginateAll, paginateUpTo, consumePage } from './sentry-pagination.ts';",
"export type { UnwrappedResult, PaginatedResponse, PaginatedCollection, PaginateAllOptions, PaginateUpToOptions, PageFetcher, BudgetedPageFetcher, CollectionPage, CollectionPageFetcher, PagePosition, PaginationContinuation } from './sentry-pagination.ts';",
"export * from './error-results.gen.ts';",
"export type * from './query-enums.gen.ts';",
"export * from './pagination.gen.ts';",
"export { createSentryClient } from './sentry-client.gen.ts';",
"export type { SentryClient } from './sentry-client.gen.ts';",
"export type { Config } from './client/index.ts';",
"",
].join("\n"),
);
// 5. Create standalone entry points.
// valibot/zod: validator-specific schemas with optional peer dependencies.
// browser: CSRF + cookie auth helpers for browser/frontend use.
writeFileSync("src/valibot.ts", 'export * from "./valibot.gen.ts";\n');
writeFileSync("src/zod.ts", 'export * from "./zod.gen.ts";\n');
writeFileSync("src/browser.ts", 'export * from "./browser-client.ts";\n');
// 6. Bundle into JS files and emit type declarations.
// The main entry stays self-contained (zero runtime deps).
// Validator entries externalise their peers; the root stays dependency-free.
execSync("bun build src/index.ts --outdir dist", { stdio: "inherit" });
execSync('bun build src/valibot.ts --outdir dist --external valibot', { stdio: "inherit" });
execSync('bun build src/zod.ts --outdir dist --external zod', { stdio: "inherit" });
execSync("bun build src/browser.ts --outdir dist", { stdio: "inherit" });
execSync("bun build src/operations.ts --outdir dist", { stdio: "inherit" });
execSync("bun build src/source-analysis.ts --outdir dist --external typescript", { stdio: "inherit" });
execSync("tsc --emitDeclarationOnly", { stdio: "inherit" });