Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: export metadata for rpc overrides #111

Merged
merged 3 commits into from
Nov 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/creating-a-custom-generator.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
},
};
Expand Down
4 changes: 2 additions & 2 deletions internal/scripts/scaffold-package.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};
},
);
Expand Down
4 changes: 2 additions & 2 deletions languages/dart/dart-codegen/src/_index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -72,7 +73,6 @@ export const dartClientGenerator = defineGeneratorPlugin(
console.error(err);
}
},
options,
};
},
);
Expand Down
4 changes: 2 additions & 2 deletions languages/kotlin/kotlin-codegen/src/_index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};
},
);
Expand Down
4 changes: 2 additions & 2 deletions languages/rust/rust-codegen/src/_index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -83,7 +84,6 @@ export const rustClientGenerator = defineGeneratorPlugin(
}
}
},
options,
};
},
);
Expand Down
4 changes: 2 additions & 2 deletions languages/swift/swift-codegen/src/_index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};
},
);
Expand Down
9 changes: 6 additions & 3 deletions languages/ts/ts-codegen/src/_index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -42,12 +45,12 @@ export interface TypescriptGeneratorOptions {
/**
* Override the default functions used for creating procedures
*/
rpcGenerators?: Record<RpcDefinition["transport"], RpcGeneratorFunction>;
rpcGenerators?: Record<RpcDefinition["transport"], RpcGenerator>;
}

export const typescriptClientGenerator = defineGeneratorPlugin(
(options: TypescriptGeneratorOptions) => ({
generator: async (def) => {
run: async (def) => {
if (!options.clientName) {
throw new Error("Name is requires");
}
Expand Down
4 changes: 2 additions & 2 deletions languages/ts/ts-codegen/src/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
stringStartsWithNumber,
} from "@arrirpc/codegen-utils";

import { RpcGeneratorFunction } from "./rpc";
import { RpcGenerator } from "./rpc";

export interface TsProperty {
typeName: string;
Expand Down Expand Up @@ -35,7 +35,7 @@ export interface CodegenContext {
sse: boolean;
ws: boolean;
};
rpcGenerators: Record<string, RpcGeneratorFunction>;
rpcGenerators: Record<string, RpcGenerator>;
}

export function getJsDocComment(metadata: Schema["metadata"]) {
Expand Down
6 changes: 5 additions & 1 deletion languages/ts/ts-codegen/src/rpc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
4 changes: 2 additions & 2 deletions tooling/cli/src/serverConfigs/goServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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`,
Expand Down Expand Up @@ -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`,
Expand Down
4 changes: 2 additions & 2 deletions tooling/cli/src/serverConfigs/tsServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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`,
Expand Down Expand Up @@ -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`,
Expand Down
2 changes: 1 addition & 1 deletion tooling/codegen-utils/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
},
};
Expand Down
2 changes: 1 addition & 1 deletion tooling/codegen-utils/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ export function normalizeWhitespace(input: string) {
}

export interface Generator<TOptions extends Record<string, any> | undefined> {
generator: (def: AppDefinition, isDevServer?: boolean) => any;
run: (def: AppDefinition, isDevServer?: boolean) => any;
options: TOptions;
}

Expand Down
Loading