mtwichel / backbone Goto Github PK
View Code? Open in Web Editor NEWA Dart framework for writing REST APIs from an Open API spec.
A Dart framework for writing REST APIs from an Open API spec.
I'm following the instructions here:
https://github.com/mtwichel/backbone/tree/main/packages/backbone_cli
I ran dart pub global activate backbone
and it was successfully installed (version 0.0.1-dev.13
) but when I run any backbone
command I get:
zsh: command not found: backbone
I tried running the command as follow but also got an error:
[~] dart pub global run backbone help
Could not find bin/backbone.dart in package backbone.
Side note:
The link for the open api example in backbone_cli readme is broken, it should be:
https://github.com/mtwichel/backbone/blob/main/packages/backbone_cli/example/openapi.yaml
Hey everyone ๐ first off I want to share a thank you to everyone that has checked out the package and shown interest! We'll figure out Dart on the server best practices together! ๐
While the current iteration of backbone works, there are a few issues I've come across:
I would like to change backbone so that instead of writing an Open API spec and generating code from there, you write the Dart backend code, and it generates the client code and an Open API spec (for documentation). The code you write would look something like this:
@CloudFunction()
Future<GetItemsResponse> getItems(GetItemsRequest request, RequestContext context) async {
// connect to db and get the items
}
The RequestContext
would still contain a logger, the raw shelf request, userId, and dependency caching that backbone currently has. The GetItemsResponse
and GetItemsRequest
objects would just be regular Dart objects that can be serialized (have a fromJson
and toJson
method)
You would then use the build_runner
package to generate your code. (dart run build_runner build
). It would generate a client library for calling your functions that would look something like this:
class ItemsRepository {
final BackboneApi api;
// ...
Future<List<Items>> getItems(String group) async {
try {
final response = await api.getItems(
GetItemsRequest(group: group),
);
return response.items;
} on BadRequestException catch (e) {
// handle issues with the request being bad
} catch (e) {
// handle other exceptions thrown by the backend function
}
}
}
Right now, backbone generates 3 packages:
However, if you're defining the shared objects by hand in Dart, then you would probably put them in your backend package. Thus, for the frontend to use them, it would have to depend on the backend in its pubspec
, which I don't love. Of course you could put your functions objects in their own package when you write them, but I don't think backbone could enforce that.
Right now, backbone uses Mason internally to generate the source code. It seems natural to use the Mason CLI to build your code (ie mason make backbone
), and I would love to make it that easy. However, that would be tough because the configuration is stored as plain Dart code. Anyone who has ideas/experience merging the build_runner
package and mason
together, please share!
This point gets a bit nerdy, so be prepared ๐ค ๐
Two of the bigger theories for designing a web API are Remote Procedure Call (RPC) and Representational State Transfer (REST). As I understand it, from a high level:
getItems
). They need to be documented because you'll need to know what methods are available to you and how to use them. Also, each function is generally specific to a single-use case./items
points to all the Item
s in the system. A POST
call to /items
would add a new item, whereas a 'GET' call to /items/3
would retrieve the item with id 3). In theory, they don't need to be documented because they follow standard HTTP protocols, and each route can be used for different use-cases because when you retrieve an entity, you get the entire thing.Here are some resources I've read/watched to better understand the topic:
Right now, backbone uses an Open API spec, which doesn't dictate a style you should use to design your API; it simply allows all the HTTP protocols to be used. You as the developer gets to decide if you follow a REST or RPC pattern. With this change, backbone forces an RPC style API because it takes functions from the backend and exposes them to the frontend. You won't get any control over your routes or HTTP verbs or anything like that. Thus, I think it would be impossible to make REST APIs with backbone, only RPC style APIs.
If you've made it this far, here are some questions I'd love opinions on:
Thank you again to the wonderful Dart community, and I look forward to your ideas ๐ก ๐ ๐
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.