Git Product home page Git Product logo

Comments (6)

LaysDragon avatar LaysDragon commented on July 26, 2024 1

I encounter the same problem just in a hour, I use zenrpc to build server side rpc server. And I try to convert its smd schema into openrpc and generate client from the converted one. But the problem is zenrpc have the concept called namespace, which just add namespace in from of the method name. And cursed the openrpc generator generated invalid typescript like this.

/**
   * 
   */
  // tslint:disable-next-line:max-line-length
  public user.Login: UserLogin = (...params) => {
    return this.request("user.Login", params);
  }

from generator.

ivankhm avatar ivankhm commented on July 26, 2024 1

I figured out a workaround - I just wrote a post-generation script, that replaces dots with underscores in the final file. Here is the function, it's quite naive, but works:

import { readJson, readFile, writeFile } from "fs-extra";
import { OpenrpcDocument as OpenRPC, MethodObject } from "@open-rpc/meta-schema";

const fixMethodsWithDots = async (openrpcFilePath: string, generatedFilePath: string, finalOutputPath: string) => {
  const openrpcDocument: OpenRPC = await readJson(openrpcFilePath);
  let generatedFileContents: string = await readFile(generatedFilePath, "utf-8");
  const methodNames: string[] = openrpcDocument.methods.map(method => (<MethodObject>method).name);

  const methodNamesReplacements: [string, string][] = methodNames.map(methodName => (["public "+methodName, "public "+methodName.replace(".", "_")]));

  for (const [withDot, withUnderscore] of methodNamesReplacements) {
    generatedFileContents = generatedFileContents.replace(withDot, withUnderscore);
  }

  await writeFile(finalOutputPath, generatedFileContents);
}

I'll keep it for now, so it just works, but I'm really looking forward to this bugfix.

from generator.

LaysDragon avatar LaysDragon commented on July 26, 2024 1

I modify the master to generate code I want. https://github.com/Laysi/open-rpc-generator
It would generated nested class to move the method into its own namespace class.

  ...
  public user = new class{
    constructor(public superThis: Catcus) {
    }
    
    /**
     * 
     */
    // tslint:disable-next-line:max-line-length
    public Login: UserLogin = (...params) => {
      return this.superThis.request("user.Login", params);
    }
  }
  ...

And I have add some tweak to made it working under angular environment and ci. Compare to openapi generator it still lack some custom field for further usage in template.

from generator.

BelfordZ avatar BelfordZ commented on July 26, 2024 1

Thanks for the issue and nice catch.

Im happy to implement or take a PR for the initially suggested fix. The nesting fix is very interesting, and I don't want to completely dismiss it, but its quite a bit bigger of a change. Great work on creating a fork to implement it though - really nice!

comment if you want to make a PR, otherwise ill put one together soon.

from generator.

stale avatar stale commented on July 26, 2024 1

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

from generator.

LaysDragon avatar LaysDragon commented on July 26, 2024

Thanks for the issue and nice catch.

Im happy to implement or take a PR for the initially suggested fix. The nesting fix is very interesting, and I don't want to completely dismiss it, but its quite a bit bigger of a change. Great work on creating a fork to implement it though - really nice!

comment if you want to make a PR, otherwise ill put one together soon.

I'm glad to make a PR. :)

from generator.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.