News for hackers. Stuff that matters.
Re:tRPC – Move Fast and Break Nothing. End-to-end typesafe A... by purkka on Saturday August 12, 2023 @08:04AM (#37099340)
In our current project with a TS frontend and Python backend, we use an OpenAPI schema as the source of truth and openapi-typescript-codegen [0] to interface with it on the client side. While not perfect, it provides a very nice interface to our API with request/response typings.

I also wrote a 10-line mock API wrapper that you can call as mockApi<SomeService["operationName"]>((request) => response), and it will type-check that your mock function implements the API correctly and return a function that looks exactly like the real API function.

[0]: https://github.com/ferdikoomen/openapi-typescript-codegen

Re:tRPC – Move Fast and Break Nothing. End-to-end typesafe A... by iansinnott on Saturday August 12, 2023 @08:24AM (#37099477)
Can second this approach. At a past job we did the same, except to connect the frontend to a Go backend.

I really like that the openAPI approach is language agnostic, and makes it relatively simple to support SDKs for many other languages if needed. For any company where the API itself is a product, OpenAPI is great.

Re:tRPC – Move Fast and Break Nothing. End-to-end typesafe A... by nerdponx on Saturday August 12, 2023 @08:35AM (#37099547)
* * *
Re:tRPC – Move Fast and Break Nothing. End-to-end typesafe A... by Cannabat on Saturday August 12, 2023 @07:53PM (#37105261)
Another great library to generate TS types from OpenAPI is https://github.com/drwpow/openapi-typescript . It provides the types as single objects you access via indexing, which is pretty nice. There's a partner library to generate a typed fetch client.
Re:tRPC – Move Fast and Break Nothing. End-to-end typesafe A... by taswellian on Wednesday August 16, 2023 @03:39PM (#37152531)
Do you know if there's something similar for Python? Or what approach would you use for Python?
Re:tRPC – Move Fast and Break Nothing. End-to-end typesafe A... by Cannabat on Thursday August 17, 2023 @03:07AM (#37158225)
Like generating pydantic models or dataclasses for an OpenAPI schema? I haven't needed to go in that direction myself, but this[0] looks promising!

Apologies if I've misunderstood your comment

https://koxudaxi.github.io/datamodel-code-generator/

Re:tRPC – Move Fast and Break Nothing. End-to-end typesafe A... by taswellian on Thursday August 17, 2023 @04:54AM (#37158956)
This looks interesting, thank you!

I'm basically wondering what would help us produce (automatically?) a Python HTTP client based an OpenAPI spec, and achieve a development experience similar to using drwpow/openapi-typescript.

drwpow/openapi-typescript will generate the whole schema, along with paths, request and response models, as well as path and query parameters. In the IDE, all client methods will be type-safe and autocompleted.

Example:

  client = createClient<GeneratedSchema>();
  
  client.get("/my/autocompleted/endpoint/{some_param}", { params: { path: { some_param: "foobar" } } })
koxudaxi/datamodel-code-generator seems to generate the response models, but not much else. It seems we would have to manually write wrapper methods for each endpoint, manually specify the parameters and request models, and use a type hint for the return value of each method with the generated response model.

Example:

  def my_wrapped_endpoint(...) -> ResponseModel:
    pass

  client.my_wrapped_endpoint(some_param="foobar")

I'd like to avoid any manual wrapping or at least minimize the amount of it. Basically: to achieve a similar experience to what we have in TypeScript.

I hope my question is a bit clearer now. I'm not that familiar with Python, so I will appreciate any guidance regarding this problem. :)

Re:tRPC – Move Fast and Break Nothing. End-to-end typesafe A... by Cannabat on Thursday August 17, 2023 @05:15AM (#37159123)
Gotcha. I’m not sure what options are out there. Haven’t needed to do any codegen to python - only from python.

For all its issues, the JS world typically provides an excellent developer experience and takes types far more seriously than python.

Hope you find something that helps, and if you do I’d love to hear about it.

Re:tRPC – Move Fast and Break Nothing. End-to-end typesafe A... by taswellian on Thursday August 17, 2023 @05:51AM (#37159376)
Thanks! I'll keep looking for a solution and keep you in the loop once we figure something out.