Skip to content

Commit 0184e35

Browse files
committed
feat: add generate options
1 parent d11ee37 commit 0184e35

File tree

22 files changed

+1673
-681
lines changed

22 files changed

+1673
-681
lines changed

.sage/einrideeslint.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ const (
1414
name = "eslint"
1515
packageJSONContent = `{
1616
"dependencies": {
17-
"@einride/eslint-plugin": "4.2.0",
18-
"eslint": "8.5.0"
17+
"@einride/eslint-plugin": "7.4.0",
18+
"eslint": "8.51.0"
1919
}
2020
}`
2121
)

README.md

+52-2
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,53 @@ Or download a prebuilt binary from [releases](./releases).
2323
```bash
2424
protoc
2525
--typescript-http_out [OUTPUT DIR] \
26+
--typescript-http_opt use_enum_numbers=true,use_multi_line_comment=true
2627
[.proto files ...]
2728
```
2829

30+
#### Support options
31+
32+
```ts
33+
// UseProtoNames controls the casing of generated field names.
34+
// If set to true, fields will use proto names (typically snake_case).
35+
// If omitted or set to false, fields will use JSON names (typically camelCase).
36+
use_proto_names: bool
37+
38+
// UseEnumNumbers emits enum values as numbers.
39+
use_enum_numbers: bool
40+
41+
// The method names of service methods naming case.
42+
// Only work when `UseEnumNumbers=true`
43+
// opt:
44+
// camelcase: convert name to lower camel case like `camelCase`
45+
// pascalcase: convert name to pascalcase like `PascalCase`
46+
// default is pascalcase
47+
enum_field_naming: string
48+
49+
// Generate comments as multiline comments.
50+
// multiline comments: /** ... */
51+
// single line comments: // ...
52+
use_multi_line_comment: bool
53+
54+
// force add `undefined` to message field.
55+
// default true
56+
force_message_field_undefinable: bool
57+
58+
// If set to true, body will be JSON.stringify before send
59+
// default true
60+
use_body_stringify: bool
61+
62+
// The method names of service methods naming case.
63+
// opt:
64+
// camelcase: convert name to lower camel case like `camelCase`
65+
// pascalcase: convert name to pascalcase like `PascalCase`
66+
// default is pascalcase
67+
service_method_naming: 'camelcase' | 'pascalcase'
68+
69+
// If set to true, field int64 and uint64 will convert to string
70+
force_long_as_string: bool
71+
```
72+
2973
---
3074

3175
The generated clients can be used with any HTTP client that returns a Promise containing JSON data.
@@ -39,11 +83,17 @@ type Request = {
3983
body: string | null
4084
}
4185

42-
function fetchRequestHandler({path, method, body}: Request) {
86+
// This is optional
87+
type RequestOptions = {
88+
useCache?: boolean;
89+
}
90+
91+
function fetchRequestHandler({path, method, body}: Request & RequestOptions) {
4392
return fetch(rootUrl + path, {method, body}).then(response => response.json())
4493
}
4594

4695
export function siteClient() {
47-
return createShipperServiceClient(fetchRequestHandler);
96+
// This Generics is optional
97+
return createShipperServiceClient<RequestOptions>(fetchRequestHandler);
4898
}
4999
```

examples/proto/buf.gen.yaml

+1
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@ version: v1
33
plugins:
44
- name: typescript-http
55
out: gen/typescript
6+
opt: use_enum_numbers=true,force_message_field_undefined=false,use_body_stringify=false,service_method_naming=camelcase,force_long_as_string=true

examples/proto/einride/example/freight/v1/freight_service.proto

+10-6
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,19 @@ import "google/protobuf/field_mask.proto";
2121
// - Each Shipper has a collection of [Site][einride.example.freight.v1.Site]
2222
// resources, named `shippers/*/sites/*`
2323
//
24-
// - Each Shipper has a collection of [Shipment][einride.example.freight.v1.Shipment]
24+
// - Each Shipper has a collection of
25+
// [Shipment][einride.example.freight.v1.Shipment]
2526
// resources, named `shippers/*/shipments/*`
2627
service FreightService {
2728
option (google.api.default_host) = "freight-example.einride.tech";
2829

2930
// Get a shipper.
3031
// See: https://google.aip.dev/131 (Standard methods: Get).
3132
rpc GetShipper(GetShipperRequest) returns (Shipper) {
32-
option (google.api.http) = {get: "/v1/{name=shippers/*}"};
33+
option (google.api.http) = {
34+
get: "/v1/{name=shippers/*}",
35+
additional_bindings {get: "/v1/{name=shippers-alias/*}"}
36+
};
3337
option (google.api.method_signature) = "name";
3438
}
3539

@@ -261,8 +265,8 @@ message ListSitesResponse {
261265

262266
// Request message for FreightService.CreateSite.
263267
message CreateSiteRequest {
264-
// The resource name of the parent shipper for which this site will be created.
265-
// Format: shippers/{shipper}
268+
// The resource name of the parent shipper for which this site will be
269+
// created. Format: shippers/{shipper}
266270
string parent = 1 [
267271
(google.api.field_behavior) = REQUIRED,
268272
(google.api.resource_reference) = {child_type: "freight-example.einride.tech/Shipper"}
@@ -339,8 +343,8 @@ message ListShipmentsResponse {
339343

340344
// Request message for FreightService.CreateShipment.
341345
message CreateShipmentRequest {
342-
// The resource name of the parent shipper for which this shipment will be created.
343-
// Format: shippers/{shipper}
346+
// The resource name of the parent shipper for which this shipment will be
347+
// created. Format: shippers/{shipper}
344348
string parent = 1 [
345349
(google.api.field_behavior) = REQUIRED,
346350
(google.api.resource_reference) = {child_type: "freight-example.einride.tech/Shipper"}

examples/proto/einride/example/freight/v1/shipment.proto

+21-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,16 @@ package einride.example.freight.v1;
55
import "google/api/field_behavior.proto";
66
import "google/api/resource.proto";
77
import "google/protobuf/timestamp.proto";
8+
import "google/protobuf/wrappers.proto";
9+
10+
enum ShipmentState {
11+
// The shipment' state is unknown
12+
UNKNOWN_UNSPECIFIED = 0;
13+
// The shipment'state is active
14+
ACTIVE = 1;
15+
// The shipment'state is inactive
16+
INACTIVE = 2;
17+
}
818

919
// A shipment represents transportation of goods between an origin
1020
// [site][einride.example.freight.v1.Site] and a destination
@@ -62,6 +72,16 @@ message Shipment {
6272

6373
// Annotations of the shipment.
6474
map<string, string> annotations = 12;
75+
76+
// The state of the shipment
77+
ShipmentState state = 14;
78+
79+
// wrapper string
80+
optional google.protobuf.StringValue wrapper_string = 15;
81+
// long number
82+
int64 long_number = 16;
83+
// wrapper long number
84+
google.protobuf.Int64Value wrapper_long_number = 17;
6585
}
6686

6787
// A shipment line item.
@@ -73,5 +93,5 @@ message LineItem {
7393
// The weight of the line item in kilograms.
7494
float weight_kg = 3;
7595
// The volume of the line item in cubic meters.
76-
float volume_m3 = 4;
96+
optional float volume_m3 = 4;
7797
}

0 commit comments

Comments
 (0)