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

Creates api-client generator #8784

Merged
merged 4 commits into from
Aug 9, 2024
Merged

Creates api-client generator #8784

merged 4 commits into from
Aug 9, 2024

Conversation

Rotorsoft
Copy link
Contributor

@Rotorsoft Rotorsoft commented Aug 7, 2024

This is the first pass at generating a client proxy using openapi-generator

Requirements

  • Java JRE -> in MacOS use brew install openjdk
  • Run pnpm install to install modules

How to generate a new client api

  • Run the server with pnpm start. API spec will be found in http://localhost:8080/api/v1/openapi.json
  • Run pnpm -F api-client generate-client

Notes

  • Replaced trpc-openapi with trpc-swagger since the original project is dead and this new fork has been regularly mantained
  • Using factory names as api paths. OpenAPI generator appends API group names to paths
  • The generator will create a new API for each artifact group defined in /packages/commonwealth/server/api/index.ts:
/**
 * API v1 - tRPC Router
 */
const artifacts = {
  user: user.trpcRouter,
  community: community.trpcRouter,
  thread: thread.trpcRouter,
  integrations: integrations.trpcRouter,
  feed: feed.trpcRouter,
  contest: contest.trpcRouter,
  subscription: subscription.trpcRouter,
  loadTest: loadTest.trpcRouter,
};
  • The naming convention used by the generator follows this pattern: {group}{methodName}, so for example, if the user group defines one command updateUser, the UserAPI will expose:
// in /api/users.ts
export const trpcRouter = trpc.router({
  updateUser: trpc.command(User.UpdateUser, trpc.Tag.User),
});

UserAPI.userUpdateUser(...)

Let's discuss:

  • Usability
  • Authentication
  • Documentation
  • Versioning
  • Deployment to NPM

Link to Issue

Closes: #8782

Description of Changes

  • Creates new lib for api-client
  • Adds script to generate client proxy from the OpenAPI spec found in http://localhost:8080/api/v1/openapi.json
  • Adds script to run a basic test

"How We Fixed It"

Copy link
Collaborator

@timolegros timolegros left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I get an error when trying to run the generate-client command. Do I need to test the command or are there some installation steps not included in the PR - intentionally for now?

Error: Error: A JNI error has occurred, please check your installation and try again
Exception in thread "main" java.lang.UnsupportedClassVersionError: org/openapitools/codegen/OpenAPIGenerator has been compiled by a more recent version of the Java Runtime (class file version 55.0), this version of the Java Runtime only recognizes class file versions up to 52.0
        at java.lang.ClassLoader.defineClass1(Native Method)
        at java.lang.ClassLoader.defineClass(ClassLoader.java:763)
        at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
        at java.net.URLClassLoader.defineClass(URLClassLoader.java:468)
        at java.net.URLClassLoader.access$100(URLClassLoader.java:74)
        at java.net.URLClassLoader$1.run(URLClassLoader.java:369)
        at java.net.URLClassLoader$1.run(URLClassLoader.java:363)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(URLClassLoader.java:362)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
        at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:349)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
        at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:495)

    at /home/timolegros/Projects/commonwealth/node_modules/.pnpm/@[email protected]/node_modules/@openapitools/openapi-generator-cli/main.js:2:27569
    at ChildProcess.exithandler (node:child_process:430:5)
    at ChildProcess.emit (node:events:518:28)
    at maybeClose (node:internal/child_process:1105:16)
    at Socket.<anonymous> (node:internal/child_process:457:11)
    at Socket.emit (node:events:518:28)
    at Pipe.<anonymous> (node:net:337:12)

Node.js v20.12.1
 ELIFECYCLE  Command failed with exit code 1.

libs/adapters/src/trpc/index.ts Show resolved Hide resolved
libs/api-client/src/configuration.ts Show resolved Hide resolved
@Rotorsoft
Copy link
Contributor Author

I get an error when trying to run the generate-client command. Do I need to test the command or are there some installation steps not included in the PR - intentionally for now?

Error: Error: A JNI error has occurred, please check your installation and try again
Exception in thread "main" java.lang.UnsupportedClassVersionError: org/openapitools/codegen/OpenAPIGenerator has been compiled by a more recent version of the Java Runtime (class file version 55.0), this version of the Java Runtime only recognizes class file versions up to 52.0
        at java.lang.ClassLoader.defineClass1(Native Method)
        at java.lang.ClassLoader.defineClass(ClassLoader.java:763)
        at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
        at java.net.URLClassLoader.defineClass(URLClassLoader.java:468)
        at java.net.URLClassLoader.access$100(URLClassLoader.java:74)
        at java.net.URLClassLoader$1.run(URLClassLoader.java:369)
        at java.net.URLClassLoader$1.run(URLClassLoader.java:363)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(URLClassLoader.java:362)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
        at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:349)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
        at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:495)

    at /home/timolegros/Projects/commonwealth/node_modules/.pnpm/@[email protected]/node_modules/@openapitools/openapi-generator-cli/main.js:2:27569
    at ChildProcess.exithandler (node:child_process:430:5)
    at ChildProcess.emit (node:events:518:28)
    at maybeClose (node:internal/child_process:1105:16)
    at Socket.<anonymous> (node:internal/child_process:457:11)
    at Socket.emit (node:events:518:28)
    at Pipe.<anonymous> (node:net:337:12)

Node.js v20.12.1
 ELIFECYCLE  Command failed with exit code 1.

I'll work on the documentation later, this is just the first draft so we can see the results and adjust a few things like the tooling, tags, naming conventions, governance, etc. This specific error is telling you that you are either missing a JRE or have an invalid one. In my MacOS I just installed it with homebrew => brew install openjdk, not sure what you use in Linux

@Rotorsoft Rotorsoft merged commit 58b7e86 into master Aug 9, 2024
9 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Generate API client proxy from OpenAPI spec
3 participants