From ddfea3dae0bdffeaf20115470f7e5f0be6bb3e1b Mon Sep 17 00:00:00 2001 From: Joshua Sosso Date: Thu, 14 Nov 2024 13:44:11 -0600 Subject: [PATCH] Feature: export metadata for rpc overrides (#111) * rename plugin "generator" fn to "run" * export CodegenContext, RpcGenerator, defineRpcGenerator for public use * fix build issue --- docs/creating-a-custom-generator.md | 2 +- internal/scripts/scaffold-package.ts | 4 ++-- languages/dart/dart-codegen/src/_index.ts | 4 ++-- languages/kotlin/kotlin-codegen/src/_index.ts | 4 ++-- languages/rust/rust-codegen/src/_index.ts | 4 ++-- languages/swift/swift-codegen/src/_index.ts | 4 ++-- languages/ts/ts-codegen/src/_index.ts | 9 ++++++--- languages/ts/ts-codegen/src/common.ts | 4 ++-- languages/ts/ts-codegen/src/rpc.ts | 6 +++++- tooling/cli/src/serverConfigs/goServer.ts | 4 ++-- tooling/cli/src/serverConfigs/tsServer.ts | 4 ++-- tooling/codegen-utils/README.md | 2 +- tooling/codegen-utils/src/index.ts | 2 +- 13 files changed, 30 insertions(+), 23 deletions(-) diff --git a/docs/creating-a-custom-generator.md b/docs/creating-a-custom-generator.md index 9058458d..b7b00ce6 100644 --- a/docs/creating-a-custom-generator.md +++ b/docs/creating-a-custom-generator.md @@ -30,7 +30,7 @@ export interface MyPluginOptions { export default defineGeneratorPlugin((options: MyPluginOptions) => { return { options, - generator: async (appDef, isDevServer) => { + run: async (appDef, isDevServer) => { // generate something using the app definition and the specified options }, }; diff --git a/internal/scripts/scaffold-package.ts b/internal/scripts/scaffold-package.ts index 8b9f0486..4598f4b6 100644 --- a/internal/scripts/scaffold-package.ts +++ b/internal/scripts/scaffold-package.ts @@ -107,10 +107,10 @@ export interface MyGeneratorOptions { export const myGenerator = defineGeneratorPlugin( (options: MyGeneratorOptions) => { return { - generator(appDef, isDevServer) { + options, + run(appDef, isDevServer) { // todo: use the app definition to output some code }, - options, }; }, ); diff --git a/languages/dart/dart-codegen/src/_index.ts b/languages/dart/dart-codegen/src/_index.ts index 92613fa8..7cee026d 100644 --- a/languages/dart/dart-codegen/src/_index.ts +++ b/languages/dart/dart-codegen/src/_index.ts @@ -55,7 +55,8 @@ export interface DartClientGeneratorOptions { export const dartClientGenerator = defineGeneratorPlugin( (options: DartClientGeneratorOptions) => { return { - async generator(def) { + options, + async run(def) { if (!options.outputFile) { throw new Error( 'Missing "outputFile" cannot generate dart code', @@ -72,7 +73,6 @@ export const dartClientGenerator = defineGeneratorPlugin( console.error(err); } }, - options, }; }, ); diff --git a/languages/kotlin/kotlin-codegen/src/_index.ts b/languages/kotlin/kotlin-codegen/src/_index.ts index f273e9a8..cac46b4c 100644 --- a/languages/kotlin/kotlin-codegen/src/_index.ts +++ b/languages/kotlin/kotlin-codegen/src/_index.ts @@ -63,11 +63,11 @@ export interface KotlinClientOptions { export const kotlinClientGenerator = defineGeneratorPlugin( (options: KotlinClientOptions) => { return { - generator(def) { + options, + run(def) { const client = kotlinClientFromAppDefinition(def, options); fs.writeFileSync(options.outputFile, client); }, - options, }; }, ); diff --git a/languages/rust/rust-codegen/src/_index.ts b/languages/rust/rust-codegen/src/_index.ts index 8b95d901..0b42d10c 100644 --- a/languages/rust/rust-codegen/src/_index.ts +++ b/languages/rust/rust-codegen/src/_index.ts @@ -58,7 +58,8 @@ export interface RustClientGeneratorOptions { export const rustClientGenerator = defineGeneratorPlugin( (options: RustClientGeneratorOptions) => { return { - generator(def) { + options, + run(def) { const context: GeneratorContext = { clientVersion: def.info?.version ?? "", clientName: options.clientName ?? "Client", @@ -83,7 +84,6 @@ export const rustClientGenerator = defineGeneratorPlugin( } } }, - options, }; }, ); diff --git a/languages/swift/swift-codegen/src/_index.ts b/languages/swift/swift-codegen/src/_index.ts index 4d9ce167..8bb295d0 100644 --- a/languages/swift/swift-codegen/src/_index.ts +++ b/languages/swift/swift-codegen/src/_index.ts @@ -40,11 +40,11 @@ export interface SwiftClientGeneratorOptions { export const swiftClientGenerator = defineGeneratorPlugin( (options: SwiftClientGeneratorOptions) => { return { - async generator(def, _isDevServer) { + options, + async run(def, _isDevServer) { const content = createSwiftClient(def, options); fs.writeFileSync(options.outputFile, content, "utf8"); }, - options, }; }, ); diff --git a/languages/ts/ts-codegen/src/_index.ts b/languages/ts/ts-codegen/src/_index.ts index 7bed6761..a2e7d4d5 100644 --- a/languages/ts/ts-codegen/src/_index.ts +++ b/languages/ts/ts-codegen/src/_index.ts @@ -31,9 +31,12 @@ import { } from "./primitives"; import { tsRecordFromSchema } from "./record"; import { tsRefFromSchema } from "./ref"; -import { RpcGeneratorFunction } from "./rpc"; +import { RpcGenerator } from "./rpc"; import { tsServiceFromDefinition } from "./service"; +export * from "./common"; +export * from "./rpc"; + export interface TypescriptGeneratorOptions { clientName: string; outputFile: string; @@ -42,12 +45,12 @@ export interface TypescriptGeneratorOptions { /** * Override the default functions used for creating procedures */ - rpcGenerators?: Record; + rpcGenerators?: Record; } export const typescriptClientGenerator = defineGeneratorPlugin( (options: TypescriptGeneratorOptions) => ({ - generator: async (def) => { + run: async (def) => { if (!options.clientName) { throw new Error("Name is requires"); } diff --git a/languages/ts/ts-codegen/src/common.ts b/languages/ts/ts-codegen/src/common.ts index 83a48538..7de936ee 100644 --- a/languages/ts/ts-codegen/src/common.ts +++ b/languages/ts/ts-codegen/src/common.ts @@ -5,7 +5,7 @@ import { stringStartsWithNumber, } from "@arrirpc/codegen-utils"; -import { RpcGeneratorFunction } from "./rpc"; +import { RpcGenerator } from "./rpc"; export interface TsProperty { typeName: string; @@ -35,7 +35,7 @@ export interface CodegenContext { sse: boolean; ws: boolean; }; - rpcGenerators: Record; + rpcGenerators: Record; } export function getJsDocComment(metadata: Schema["metadata"]) { diff --git a/languages/ts/ts-codegen/src/rpc.ts b/languages/ts/ts-codegen/src/rpc.ts index 67c3645a..b8a780b1 100644 --- a/languages/ts/ts-codegen/src/rpc.ts +++ b/languages/ts/ts-codegen/src/rpc.ts @@ -7,11 +7,15 @@ import { import { CodegenContext, getJsDocComment, validVarName } from "./common"; -export type RpcGeneratorFunction = ( +export type RpcGenerator = ( def: RpcDefinition, context: CodegenContext, ) => string; +export function defineRpcGenerator(fn: RpcGenerator): RpcGenerator { + return fn; +} + export function tsRpcFromDefinition( def: RpcDefinition, context: CodegenContext, diff --git a/tooling/cli/src/serverConfigs/goServer.ts b/tooling/cli/src/serverConfigs/goServer.ts index e3c53444..c71c6d8b 100644 --- a/tooling/cli/src/serverConfigs/goServer.ts +++ b/tooling/cli/src/serverConfigs/goServer.ts @@ -126,7 +126,7 @@ export function goServer(options: GoServerOptions = {}) { defFileCache = defFile; const appDef = JSON.parse(defFile); await Promise.allSettled( - generators.map((gen) => gen.generator(appDef, true)), + generators.map((generator) => generator.run(appDef, true)), ); logger.success( `Ran ${generators.length} generator(s) in ${new Date().getTime() - startTime.getTime()}ms`, @@ -167,7 +167,7 @@ export function goServer(options: GoServerOptions = {}) { fs.readFileSync(`${resolvedOutDir}/__definition.json`, "utf8"), ); await Promise.all( - generators.map((gen) => gen.generator(appDef, false)), + generators.map((generator) => generator.run(appDef, false)), ); logger.success( `Ran ${generators.length} generator(s) in ${new Date().getTime() - startTime.getTime()}ms`, diff --git a/tooling/cli/src/serverConfigs/tsServer.ts b/tooling/cli/src/serverConfigs/tsServer.ts index ef41f1dc..72970d40 100644 --- a/tooling/cli/src/serverConfigs/tsServer.ts +++ b/tooling/cli/src/serverConfigs/tsServer.ts @@ -205,7 +205,7 @@ export async function startBuild( const startTime = new Date().getTime(); logger.log(`Generating ${clientCount} client(s)...`); await Promise.all( - generators.map((plugin) => plugin.generator(def)) ?? [], + generators.map((generator) => generator.run(def)) ?? [], ); logger.log( `${clientCount} client(s) generated in ${new Date().getTime() - startTime}ms`, @@ -546,7 +546,7 @@ async function generateClientsFromDefinition( try { const clientCount = generators.length; await Promise.allSettled( - generators.map((generator) => generator.generator(appDef, true)), + generators.map((generator) => generator.run(appDef, true)), ); logger.success( `Generated ${clientCount} client${clientCount === 1 ? "" : "s"} in ${new Date().getTime() - startTime}ms`, diff --git a/tooling/codegen-utils/README.md b/tooling/codegen-utils/README.md index d4b9d820..70600cbf 100644 --- a/tooling/codegen-utils/README.md +++ b/tooling/codegen-utils/README.md @@ -16,7 +16,7 @@ export interface MyPluginOptions { export default defineGeneratorPlugin((options: MyPluginOptions) => { return { options, - generator: async (appDef, isDevServer) => { + run: async (appDef, isDevServer) => { // generate something using the app definition and the specified options }, }; diff --git a/tooling/codegen-utils/src/index.ts b/tooling/codegen-utils/src/index.ts index 210224db..ca937b06 100644 --- a/tooling/codegen-utils/src/index.ts +++ b/tooling/codegen-utils/src/index.ts @@ -232,7 +232,7 @@ export function normalizeWhitespace(input: string) { } export interface Generator | undefined> { - generator: (def: AppDefinition, isDevServer?: boolean) => any; + run: (def: AppDefinition, isDevServer?: boolean) => any; options: TOptions; }