-
Notifications
You must be signed in to change notification settings - Fork 346
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
feat: Add google.api.http support #1075
Open
jas-chen
wants to merge
20
commits into
stephenh:main
Choose a base branch
from
jas-chen:948-http-support
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 8 commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
68e8fbb
Add http support
jas-chen 709b03e
Format readme
jas-chen b0778af
Update vendor generator
jas-chen 3a3ef40
Update readme
jas-chen b9d0a55
Update package.json
jas-chen b04e1a0
Refine HTTP implementation
jas-chen 54ef26c
Fix Readme
jas-chen 56693fe
Fix Readme
jas-chen a1c3d5d
Use ts-proto-descriptors
jas-chen b1f61da
Make generic-definitions support http
jas-chen 039d364
Remove HTTP.markdown
jas-chen 2715391
Add generic-google-api-http-definitions
jas-chen 867034c
Revert src/generate-generic-service-definition.ts
jas-chen fb30279
Update implementation example
jas-chen 09765ca
Update implementation example
jas-chen fc884a3
Fix implementation example
jas-chen cada9a2
Simplify client implementation example
jas-chen 58dd958
Fix client implementation
jas-chen 94bbb54
Use google-gax
jas-chen bca889e
Fix body
jas-chen File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,6 +8,7 @@ build/ | |
coverage/ | ||
yarn-error.log | ||
.DS_Store | ||
tmp/ | ||
|
||
# Yarn | ||
.pnp.* | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
# Http | ||
|
||
If we have to following `.proto` file: | ||
|
||
```protobuf | ||
syntax = "proto3"; | ||
import "google/api/annotations.proto"; | ||
service Messaging { | ||
rpc GetMessage(GetMessageRequest) returns (GetMessageResponse) { | ||
option (google.api.http) = { | ||
get:"/v1/messages/{message_id}" | ||
}; | ||
} | ||
rpc CreateMessage(CreateMessageRequest) returns (CreateMessageResponse) { | ||
option (google.api.http) = { | ||
post:"/v1/messages/{message_id}" | ||
body: "message" | ||
}; | ||
} | ||
rpc UpdateMessage(CreateMessageRequest) returns (CreateMessageResponse) { | ||
option (google.api.http) = { | ||
patch:"/v1/messages/{message_id}" | ||
body: "*" | ||
}; | ||
} | ||
} | ||
message GetMessageRequest { | ||
string message_id = 1; | ||
} | ||
message GetMessageResponse { | ||
string message = 1; | ||
} | ||
message CreateMessageRequest { | ||
string message_id = 1; | ||
string message = 2; // mapped to the body | ||
} | ||
message CreateMessageResponse {} | ||
``` | ||
|
||
The output will be | ||
|
||
```typescript | ||
// ... | ||
export interface GetMessageRequest { | ||
messageId: string; | ||
} | ||
|
||
export interface GetMessageResponse { | ||
message: string; | ||
} | ||
|
||
export interface CreateMessageRequest { | ||
messageId: string; | ||
/** mapped to the body */ | ||
message: string; | ||
} | ||
|
||
export interface CreateMessageResponse {} | ||
|
||
export const Messaging = { | ||
getMessage: { | ||
path: "/v1/messages/{message_id}", | ||
method: "get", | ||
requestType: undefined as GetMessageRequest, | ||
responseType: undefined as GetMessageResponse, | ||
}, | ||
|
||
createMessage: { | ||
path: "/v1/messages/{message_id}", | ||
method: "post", | ||
body: "message", | ||
requestType: undefined as CreateMessageRequest, | ||
responseType: undefined as CreateMessageResponse, | ||
}, | ||
|
||
updateMessage: { | ||
path: "/v1/messages/{message_id}", | ||
method: "patch", | ||
body: "*", | ||
requestType: undefined as CreateMessageRequest, | ||
responseType: undefined as CreateMessageResponse, | ||
}, | ||
}; | ||
``` | ||
|
||
## Client implementation example | ||
|
||
```typescript | ||
// This is just an example, do not use it directly. | ||
function createApi<T extends { path: string; method: "get"; request?: unknown; response?: unknown }>(config: T) { | ||
return function api(payload: T["requestType"]): Promise<T["responseType"]> { | ||
const path = config.path.replace("{message_id}", payload.messageId); | ||
const method = config.method; | ||
const body = method === "get" ? undefined : JSON.stringify({ message: payload.message }); | ||
|
||
return fetch(path, { method, body }); | ||
}; | ||
} | ||
|
||
const getMessage = createApi(Messaging.getMessage); | ||
|
||
getMessage({ | ||
messageId: "123", | ||
}).then((res) => { | ||
// ... | ||
}); | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
.PHONY: build-vendor | ||
build-vendor: | ||
@rm -rf tmp | ||
@mkdir -p tmp/googleapis | ||
@cd tmp/googleapis && \ | ||
git init && \ | ||
git remote add origin git@github.com:googleapis/googleapis.git && \ | ||
git fetch origin 47947b2fb9bdde9b02a7dd173a5077a1cc2beb25 && \ | ||
git checkout FETCH_HEAD && \ | ||
cd ../.. | ||
@rm -rf vendor | ||
@mkdir vendor | ||
@protoc \ | ||
--js_out=import_style=commonjs,binary:vendor \ | ||
-I tmp/googleapis \ | ||
tmp/googleapis/google/api/http.proto tmp/googleapis/google/api/annotations.proto | ||
@rm -rf tmp |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
// Copyright 2024 Google LLC | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
syntax = "proto3"; | ||
|
||
package google.api; | ||
|
||
import "google/api/http.proto"; | ||
import "google/protobuf/descriptor.proto"; | ||
|
||
option go_package = "google.golang.org/genproto/googleapis/api/annotations;annotations"; | ||
option java_multiple_files = true; | ||
option java_outer_classname = "AnnotationsProto"; | ||
option java_package = "com.google.api"; | ||
option objc_class_prefix = "GAPI"; | ||
|
||
extend google.protobuf.MethodOptions { | ||
// See `HttpRule`. | ||
HttpRule http = 72295728; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
// Code generated by protoc-gen-ts_proto. DO NOT EDIT. | ||
// source: google/api/annotations.proto | ||
|
||
/* eslint-disable */ | ||
|
||
export const protobufPackage = "google.api"; |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hey @jas-chen , I think this is a great so far--I'm wondering if we could further and remove the need for this
createApi
method...I.e. if a user has ~10 services with ~10 methods each, they''ll have to call
createApi
against each of them--granted, they could write a method to do that in a loop...Like our current non-google impl looks like:
And it seems like it'd be nice to provide a similar sort of API to users...
I suppose with the
export const Messaging
, the users could provide their own mapped types & helper methods to achieve this same output, i.e. something like:I'm thinking it'd be great for ts-proto to eventually provide those
ClientService
andcreateClient
implementations (i.e. that could automatically parse & replace the{message_id}
based on the rules in their docs).Granted, sometimes I've just codegen-d out the
export interface MyService { ... }
andexport class MyServiceImpl
, which was a very old-school approach code generation, but it's definitely doable to use TS mapped types & runtime libraries to achieve the same effect.Wdyt about the goal of shipping a
ClientService
&createClient
method that took in theMessaging
config? Maybe it could be like ats-proto-google-http
runtime library, which didn't bloat the codegen output. Would you be interesting in contributing that as well as this config output?Thanks!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I updated the client implementation example code, the
createApi
takes a service definition now.