### Install Project Dependencies Source: https://github.com/connectrpc/connect-es/blob/main/packages/example/README.md Download the example project and install its dependencies using npm. Ensure Node.js version 16 or later is installed. ```shell curl -L https://github.com/connectrpc/connect-es/archive/refs/heads/main.zip > connect-es-main.zip unzip connect-es-main.zip 'connect-es-main/packages/example/*' cd connect-es-main/packages/example npm install ``` -------------------------------- ### Start the Connect Server Source: https://github.com/connectrpc/connect-es/blob/main/packages/example/README.md Start the Connect server using npm. Once running, the example can be accessed via a web browser at https://localhost:8443. ```shell npm start ``` -------------------------------- ### Install new Connect-ES generated SDK Source: https://github.com/connectrpc/connect-es/blob/main/MIGRATING.md Install the v2 Protobuf-ES generated SDK dependency using npm install. ```shell npm install @buf/googleapis_googleapis.bufbuild_es@latest ``` -------------------------------- ### Run Connect v2 Migration Tool Source: https://github.com/connectrpc/connect-es/blob/main/MIGRATING.md Execute this command to start the automated migration process for dependencies and minor code changes. ```shell npx @connectrpc/connect-migrate@latest ``` -------------------------------- ### Make Connect request with curl Source: https://github.com/connectrpc/connect-es/blob/main/packages/connect-fastify/README.md Example of making a Connect protocol request to the Fastify server using curl. ```bash curl \ --header "Content-Type: application/json" \ --data '{"sentence": "I feel happy."}' \ --http2-prior-knowledge \ http://localhost:8080/connectrpc.eliza.v1.ElizaService/Say ``` -------------------------------- ### Update package.json dependencies for Connect-ES v2 Source: https://github.com/connectrpc/connect-es/blob/main/MIGRATING.md Example of updated dependencies in package.json, showing the switch from connectrpc_es to bufbuild_es. ```diff "dependencies": { ... - "@buf/googleapis_googleapis.connectrpc_es": "^1.6.1-20241107203341-553fd4b4b3a6.1", + "@buf/googleapis_googleapis.bufbuild_es": "^2.2.2-20241107203341-553fd4b4b3a6.1", "@connectrpc/connect-web": "^2.0.0", "@bufbuild/protobuf": "^2.2.0", ... ``` -------------------------------- ### Implement Connect RPCs with Express Middleware Source: https://github.com/connectrpc/connect-es/blob/main/packages/connect-express/README.md Define your RPC methods within the ConnectRouter. This example shows how to implement the 'say' RPC. ```typescript // connect.ts import { ConnectRouter } from "@connectrpc/connect"; export default function (router: ConnectRouter) { // implement rpc Say(SayRequest) returns (SayResponse) router.rpc(ElizaService, ElizaService.methods.say, async (req) => ({ sentence: `you said: ${req.sentence}`, })); } ``` -------------------------------- ### Struct Field Usage Example Source: https://github.com/connectrpc/connect-es/blob/main/MIGRATING.md Example demonstrating the simplified usage of Struct fields with JsonObject. ```ts myMessage.struct = { text: "abc", number: 123, }; ``` -------------------------------- ### Proto Definitions for Type Migration Example Source: https://github.com/connectrpc/connect-es/blob/main/MIGRATING.md These proto definitions illustrate the scenario where MessageB was previously accepted as MessageA due to type supersets. ```protobuf syntax = "proto3"; package example.v1; message MessageA { string field_a = 1; } message MessageB { string field_a = 1; int64 field_b = 2; } service ExampleService { rpc RequestA(MessageA) returns (Empty) {} rpc RequestB(MessageB) returns (Empty) {} } ``` -------------------------------- ### Make gRPC request with buf curl Source: https://github.com/connectrpc/connect-es/blob/main/packages/connect-fastify/README.md Example of making a gRPC request to the Fastify server using buf curl. ```bash buf curl --schema buf.build/connectrpc/eliza \ --protocol grpc --http2-prior-knowledge \ -d '{"sentence": "I feel happy."}' \ http://localhost:8080/connectrpc.eliza.v1.ElizaService/Say ``` -------------------------------- ### Node.js gRPC client with @connectrpc/connect-node Source: https://github.com/connectrpc/connect-es/blob/main/packages/connect-fastify/README.md Example of creating a gRPC client in Node.js using @connectrpc/connect-node to interact with the Fastify server. ```typescript import { createClient } from "@connectrpc/connect"; import { createGrpcTransport } from "@connectrpc/connect-node"; import { ElizaService } from "./gen/eliza_connect.js"; const transport = createGrpcTransport({ baseUrl: "http://localhost:8080", httpVersion: "2", }); const client = createClient(ElizaService, transport); const { sentence } = await client.say({ sentence: "I feel happy." }); console.log(sentence); // you said: I feel happy. ``` -------------------------------- ### Make a Connect RPC Request using curl Source: https://github.com/connectrpc/connect-es/blob/main/packages/connect-express/README.md Example of how to make a request to a Connect RPC endpoint using curl with the Connect protocol. ```bash curl \ --header "Content-Type: application/json" \ --data '{"sentence": "I feel happy."}' \ http://localhost:8080/connectrpc.eliza.v1.ElizaService/Say ``` -------------------------------- ### Call RPC from TypeScript Client Source: https://github.com/connectrpc/connect-es/blob/main/packages/connect/README.md Example of calling an RPC method from a generated TypeScript client. Ensure the client is properly initialized. ```typescript const answer = await eliza.say({ sentence: "I feel happy." }); console.log(answer); // {sentence: 'When you feel happy, what do you do?'} ``` -------------------------------- ### Make Connect RPC Request with curl Source: https://github.com/connectrpc/connect-es/blob/main/packages/connect-next/README.md Example of making a Connect RPC request using curl with HTTP/2 prior knowledge. Note the API prefix and JSON content type. ```bash curl \ --header "Content-Type: application/json" \ --data '{"sentence": "I feel happy."}' \ --http2-prior-knowledge \ http://localhost:3000/api/connectrpc.eliza.v1.ElizaService/Say ``` -------------------------------- ### Make Connect RPC Request with Node.js (gRPC-web) Source: https://github.com/connectrpc/connect-es/blob/main/packages/connect-next/README.md Example of making a Connect RPC request from a Node.js environment using the gRPC-web protocol. This requires creating a transport and a client. ```typescript import { createClient } from "@connectrpc/connect"; import { createGrpcWebTransport } from "@connectrpc/connect-node"; import { ElizaService } from "./gen/eliza_connect.js"; const transport = createGrpcWebTransport({ baseUrl: "http://localhost:3000/api", httpVersion: "1.1", }); const client = createClient(ElizaService, transport); const { sentence } = await client.say({ sentence: "I feel happy." }); console.log(sentence); // you said: I feel happy. ``` -------------------------------- ### Get method idempotency using say.idempotency Source: https://github.com/connectrpc/connect-es/blob/main/MIGRATING.md The `idempotency` property now uses `MethodOptions_IdempotencyLevel` from `@bufbuild/protobuf/wkt` instead of the deprecated `MethodIdempotency` enum. ```diff - import { MethodIdempotency } from "@bufbuild/protobuf"; + import { MethodOptions_IdempotencyLevel } from "@bufbuild/protobuf/wkt"; - say.idempotency; // MethodIdempotency.NoSideEffects + say.idempotency; // MethodOptions_IdempotencyLevel.NoSideEffects ``` -------------------------------- ### Previous TypeScript Behavior (Allowing Superset Types) Source: https://github.com/connectrpc/connect-es/blob/main/MIGRATING.md In Connect v1, TypeScript allowed passing MessageB to client.requestA, which was an unintended bug. This example shows the old behavior. ```ts client.requestA(new MessageA()); client.requestA(new MessageB()); ``` -------------------------------- ### Get method kind using say.methodKind Source: https://github.com/connectrpc/connect-es/blob/main/MIGRATING.md The `kind` property for method kind has been replaced by `methodKind`. Use `say.methodKind` which returns a string like "unary" instead of the `MethodKind` enum. ```diff - import { MethodKind } from "@bufbuild/protobuf"; - say.kind; // MethodKind.Unary + say.methodKind; // "unary" ``` -------------------------------- ### Run Connect Migration with Help Source: https://github.com/connectrpc/connect-es/blob/main/packages/connect-migrate/README.md Execute the migration tool in your project root and view available flags. ```shell npx @connectrpc/connect-migrate --help ``` -------------------------------- ### Run Connect Migration Source: https://github.com/connectrpc/connect-es/blob/main/packages/connect-migrate/README.md Execute the migration tool in your project root. Add the --help flag for more options. ```shell npx @connectrpc/connect-migrate ``` -------------------------------- ### Run Conformance Tests Source: https://github.com/connectrpc/connect-es/blob/main/packages/connect-web/conformance/README.md Execute conformance tests for a specific environment and client flavor. Use the `--openBrowser` flag to launch a browser with network inspector access. ```bash npx turbo run conformance:: ``` ```bash npx turbo run conformance:chrome:promise -- --openBrowser ``` -------------------------------- ### Run Connect-Node Conformance Tests Source: https://github.com/connectrpc/connect-es/blob/main/packages/connect-node/conformance/README.md Execute the conformance tests for both client and server components of @connectrpc/connect-node using Turbo. Ensure you are in the correct directory before running. ```bash cd packages/connect-node npx turbo run conformance:server conformance:client ``` -------------------------------- ### Run Cloudflare Conformance Tests Source: https://github.com/connectrpc/connect-es/blob/main/packages/connect-cloudflare/README.md Execute both client and server conformance tests for the @connectrpc/connect-cloudflare package using Turbo. ```bash npx turbo run --filter @connectrpc/connect-cloudflare conformance:client conformance:server ``` -------------------------------- ### Set up Local Development Certificates Source: https://github.com/connectrpc/connect-es/blob/main/packages/example/README.md Generate locally-trusted development certificates using mkcert for secure local development. The NODE_EXTRA_CA_CERTS environment variable must be set. ```shell brew install mkcert mkcert -install mkcert localhost 127.0.0.1 ::1 export NODE_EXTRA_CA_CERTS="$(mkcert -CAROOT)/rootCA.pem" ``` -------------------------------- ### Create Node.js HTTP Server with Connect Adapter Source: https://github.com/connectrpc/connect-es/blob/main/packages/connect-node/README.md Set up a Node.js HTTP server using `connectNodeAdapter`. This adapter handles incoming RPC requests and routes them to your defined services. It supports `http`, `https`, and `http2` modules and allows for interceptors like validation. ```typescript // server.ts import * as http2 from "http2"; import routes from "connect"; import { connectNodeAdapter } from "@connectrpc/connect-node"; import { createValidateInterceptor } from "@connectrpc/validate"; http2.createServer( connectNodeAdapter({ // Validation via Protovalidate is almost always recommended interceptors: [createValidateInterceptor()], routes }) ).listen(8080); ``` -------------------------------- ### Call Connect RPC Service from Node.js Client Source: https://github.com/connectrpc/connect-es/blob/main/README.md Create a client with the server's URL and call a method. Ensure the baseUrl matches your server's address. Handle potential errors with a try-catch block. ```typescript import { createClient } from "@connectrpc/connect"; import { createConnectTransport } from "@connectrpc/connect-node"; import { ElizaService } from "./gen/eliza_pb.js"; const client = createClient( ElizaService, createConnectTransport({ httpVersion: "1.1", baseUrl: "http://localhost:8080", }) ); try { const res = await client.say({sentence: "Hello, world!"}) console.log(res.sentence) } catch (err) { console.error(err); } ``` -------------------------------- ### Generate Code with protoc Source: https://github.com/connectrpc/connect-es/blob/main/packages/example/README.md Alternatively, generate code using protoc with the connect-es plugin. This command specifies the proto file, output directory, and target language. ```bash protoc -I . eliza.proto \ --plugin=protoc-gen-es=../../node_modules/.bin/protoc-gen-es \ --es_out src/gen \ --es_opt target=ts ``` -------------------------------- ### Run Node.js Client Source: https://github.com/connectrpc/connect-es/blob/main/packages/example/README.md Execute the CLI client implemented in src/client.ts using npm. This client interacts with the running Connect server. ```shell npm run client ``` -------------------------------- ### Run Browserstack Tests Locally Source: https://github.com/connectrpc/connect-es/blob/main/packages/connect-web/browserstack/README.md Execute the cross-browser tests for connect-web using Browserstack. Ensure your Browserstack username and access key are provided as environment variables. ```bash BROWSERSTACK_USERNAME= BROWSERSTACK_ACCESS_KEY= npx turbo run test-browserstack ``` -------------------------------- ### Make RPC Request with cURL Source: https://github.com/connectrpc/connect-es/blob/main/packages/connect/README.md Demonstrates how to make an RPC request using cURL. This is useful for testing or interacting with services outside of generated clients. ```shell curl \ --header 'Content-Type: application/json' \ --data '{"sentence": "I feel happy."}' \ https://demo.connectrpc.com/connectrpc.eliza.v1.ElizaService/Say ``` -------------------------------- ### Set up Next.js API Route for Connect Source: https://github.com/connectrpc/connect-es/blob/main/packages/connect-next/README.md Configure a catch-all API route in Next.js to handle Connect RPC requests. Ensure the Next.js body parser is disabled to allow Connect to parse request bodies. ```typescript import { nextJsApiRouter } from "@connectrpc/connect-next"; import { createValidateInterceptor } from "@connectrpc/validate"; import routes from "../../connect"; const { handler } = nextJsApiRouter({ interceptors: [createValidateInterceptor()], routes, }); export default handler; // Required: Connect parses request bodies itself, so the Next.js body // parser must be disabled. Without this, RPCs will fail. export const config = { api: { bodyParser: false, }, }; ``` -------------------------------- ### Create Protobuf messages using schema and create function Source: https://github.com/connectrpc/connect-es/blob/main/MIGRATING.md Instead of using the 'new' keyword with message classes, create messages by calling the 'create' function with the message schema and an object containing the message fields. This applies to both client and server code. ```diff - import { SayRequest } from "./gen/eliza_connect.js"; + import { SayRequestSchema } from "./gen/eliza_pb.js"; + import { create } from "@bufbuild/protobuf"; - const sayRequest = new SayRequest({ + const sayRequest = create(SayRequestSchema, { sentence: "Hello", }); ``` -------------------------------- ### connectNodeAdapter() Source: https://github.com/connectrpc/connect-es/blob/main/packages/connect-node/README.md Provides an adapter to run Connect RPCs on Node.js `http`, `https`, or `http2` modules, enabling server-side handling of requests. ```APIDOC ## connectNodeAdapter() ### Description Run your Connect RPCs on the Node.js `http`, `https`, or `http2` modules. ### Server Implementation Example ```typescript // connect.ts import { ConnectRouter } from "@connectrpc/connect"; export default function (router: ConnectRouter) { // implement rpc Say(SayRequest) returns (SayResponse) router.rpc(ElizaService, ElizaService.methods.say, async (req) => ({ sentence: `you said: ${req.sentence}`, })); } ``` ### Server Setup Example ```typescript // server.ts import * as http2 from "http2"; import routes from "connect"; import { connectNodeAdapter } from "@connectrpc/connect-node"; import { createValidateInterceptor } from "@connectrpc/validate"; http2.createServer( connectNodeAdapter({ // Validation via Protovalidate is almost always recommended interceptors: [createValidateInterceptor()], routes }) ).listen(8080); ``` ### Example `buf curl` Request (gRPC Protocol) ```bash buf curl --schema buf.build/connectrpc/eliza \ --protocol grpc --http2-prior-knowledge \ -d '{"sentence": "I feel happy."}' \ http://localhost:8080/connectrpc.eliza.v1.ElizaService/Say ``` ### Example `curl` Request (Connect Protocol) ```bash curl \ --header "Content-Type: application/json" \ --data '{"sentence": "I feel happy."}' \ --http2-prior-knowledge \ http://localhost:8080/connectrpc.eliza.v1.ElizaService/Say ``` ``` -------------------------------- ### Generate Code with buf Source: https://github.com/connectrpc/connect-es/blob/main/packages/example/README.md Re-generate service definitions and message types from eliza.proto using 'npx buf generate'. The output is placed in src/gen. ```shell npx buf generate ``` -------------------------------- ### Add import_extension=js Plugin Option Source: https://github.com/connectrpc/connect-es/blob/main/MIGRATING.md Configure the protoc-gen-es plugin with 'import_extension=js' in buf.gen.yaml if using Node16 module resolution and requiring .js extensions on imports. ```diff # buf.gen.yaml version: v2 plugins: - local: protoc-gen-es out: src/gen include_imports: true opt: - target=ts + - import_extension=js ``` -------------------------------- ### Registry Creation with Schema Objects Source: https://github.com/connectrpc/connect-es/blob/main/MIGRATING.md When creating a registry for serializing well-known types like google.protobuf.Any, pass schema objects (e.g., SayRequestSchema) instead of message classes. ```diff import { createRegistry } from "@bufbuild/protobuf"; - import { SayRequest, SayResponse } from "./gen/eliza_pb"; + import { SayRequestSchema, SayResponseSchema } from "./gen/eliza_pb"; const registry = createRegistry( - SayRequest, SayResponse, + SayRequestSchema, SayResponseSchema, ); ``` -------------------------------- ### Make gRPC Requests from Node.js Client Source: https://github.com/connectrpc/connect-es/blob/main/packages/connect-node/README.md Demonstrates how to make a gRPC request from a Node.js client to a locally running Connect server. This uses `createGrpcTransport` and the `ElizaService`. ```typescript import { createClient } from "@connectrpc/connect"; import { createGrpcTransport } from "@connectrpc/connect-node"; import { ElizaService } from "./gen/eliza_connect.js"; const transport = createGrpcTransport({ baseUrl: "http://localhost:8080", }); const client = createClient(ElizaService, transport); const { sentence } = await client.say({ sentence: "I feel happy." }); console.log(sentence); // you said: I feel happy. ``` -------------------------------- ### Create Connect Transport for Web Clients Source: https://github.com/connectrpc/connect-es/blob/main/packages/connect-web/README.md Use this to enable web browser clients to communicate with a server using the Connect protocol. Ensure you have the necessary imports and configure the baseUrl. ```typescript import { createClient } from "@connectrpc/connect"; import { createConnectTransport } from "@connectrpc/connect-web"; import { ElizaService } from "./gen/eliza_connect.js"; // A transport for clients using the Connect protocol with fetch() const transport = createConnectTransport({ baseUrl: "https://demo.connectrpc.com", }); const client = createClient(ElizaService, transport); const { sentence } = await client.say({ sentence: "I feel happy." }); console.log(sentence) // you said: I feel happy. ``` -------------------------------- ### Create gRPC-Web Transport for Web Clients Source: https://github.com/connectrpc/connect-es/blob/main/packages/connect-web/README.md This transport allows web browser clients to communicate with a server using the gRPC-web protocol. Import the necessary function and set the baseUrl. ```typescript import { createClient } from "@connectrpc/connect"; import { createGrpcWebTransport } from "@connectrpc/connect-web"; import { ElizaService } from "./gen/eliza_connect.js"; // A transport for clients using the Connect protocol with fetch() const transport = createGrpcWebTransport({ baseUrl: "https://demo.connectrpc.com", }); const client = createClient(ElizaService, transport); const { sentence } = await client.say({ sentence: "I feel happy." }); console.log(sentence) // you said: I feel happy. ``` -------------------------------- ### Implement Connect Service in Node.js Source: https://github.com/connectrpc/connect-es/blob/main/README.md Use connectNodeAdapter to turn RPC routes into a Node.js request handler. The createValidateInterceptor is recommended for validation. ```proto service ElizaService { rpc Say(SayRequest) returns (SayResponse) {} } ``` ```typescript import * as http from "node:http"; import type { ConnectRouter } from "@connectrpc/connect"; import { connectNodeAdapter } from "@connectrpc/connect-node"; import { createValidateInterceptor } from "@connectrpc/validate"; import type { SayRequest } from "./gen/eliza_pb.js"; import { ElizaService } from "./gen/eliza_pb.js"; // The adapter turns our RPC routes into a Node.js request handler. const handler = connectNodeAdapter({ // Validation via Protovalidate is almost always recommended interceptors: [createValidateInterceptor()], routes: (router: ConnectRouter) => { router.service(ElizaService, { say(req: SayRequest) { return { sentence: `You said "${req.sentence}"`, }; }, }); }, }); http.createServer(handler).listen(8080) ``` -------------------------------- ### Call RPC with buf curl Source: https://github.com/connectrpc/connect-es/blob/main/packages/example/README.md Utilize buf curl to invoke the Introduce RPC. This command specifies the gRPC protocol, schema location, and data payload. ```shell npx buf curl --protocol grpc --schema . -d '{"name": "John"}' \ https://localhost:8443/connectrpc.eliza.v1.ElizaService/Introduce ``` -------------------------------- ### Update Dependencies in package.json Source: https://github.com/connectrpc/connect-es/blob/main/MIGRATING.md Remove the old protoc-gen-connect-es plugin and ensure compatible versions of Connect and Protobuf-ES dependencies are used. ```diff "dependencies": { "@bufbuild/protobuf": "^2.2.0", "@bufbuild/protoc-gen-es": "^2.2.0", "@connectrpc/connect": "^2.0.0", - "@connectrpc/protoc-gen-connect-es": "^1.0.0", "@connectrpc/connect-web": "^2.0.0", "@connectrpc/connect-node": "^2.0.0", "@connectrpc/connect-next": "^2.0.0", "@connectrpc/connect-fastify": "^2.0.0", "@connectrpc/connect-express": "^2.0.0", "@connectrpc/connect-query": "^2.0.0", "@connectrpc/protoc-gen-connect-query": "^2.0.0", "@connectrpc/connect-playwright": "^0.6.0" } ``` -------------------------------- ### Protobuf Import for Validation Source: https://github.com/connectrpc/connect-es/blob/main/MIGRATING.md Protobuf messages using validation rules now generate corresponding ECMAScript imports. Ensure validation proto files are available. ```protobuf import "buf/validate/validate.proto"; ``` -------------------------------- ### Register Connect RPCs in Next.js Source: https://github.com/connectrpc/connect-es/blob/main/packages/connect-next/README.md Implement your Connect RPCs by defining them in a connect.ts file. This function registers RPC methods with the Connect router. ```typescript import { ConnectRouter } from "@connectrpc/connect"; export default function (router: ConnectRouter) { // implement rpc Say(SayRequest) returns (SayResponse) router.rpc(ElizaService, ElizaService.methods.say, async (req) => ({ sentence: `you said: ${req.sentence}`, })); } ``` -------------------------------- ### Enable Include Imports in buf.gen.yaml Source: https://github.com/connectrpc/connect-es/blob/main/MIGRATING.md To include generated imports for Protobuf options like validation, set `include_imports: true` in your buf.gen.yaml configuration. ```yaml version: v2 plugins: - local: protoc-gen-es out: src/gen include_imports: true ``` -------------------------------- ### Replace createPromiseClient with createClient Source: https://github.com/connectrpc/connect-es/blob/main/MIGRATING.md Update calls from `createPromiseClient` to `createClient` as promise clients are now the default. The `createPromiseClient` function has been removed. ```diff - import { createPromiseClient } from "@connectrpc/connect-node"; + import { createClient } from "@connectrpc/connect-node"; - createPromiseClient(ElizaService, transport); + createClient(ElizaService, transport); ``` -------------------------------- ### Access service method by name using ElizaService.method.say Source: https://github.com/connectrpc/connect-es/blob/main/MIGRATING.md The `methods` property for accessing service methods has been renamed to `method`. Use `ElizaService.method.say` instead of `ElizaService.methods.say`. ```diff - const say = ElizaService.methods.say; + const say = ElizaService.method.say; ``` -------------------------------- ### Update import paths for Connect-ES v2 generated SDKs Source: https://github.com/connectrpc/connect-es/blob/main/MIGRATING.md Modify import statements to reflect the new package name and file structure for v2 generated SDKs. ```diff - import { ByteStream } from "@buf/googleapis_googleapis.connectrpc_es/google/bytestream_connect.js"; + import { ByteStream } from "@buf/googleapis_googleapis.bufbuild_es/google/bytestream_pb.js"; ``` -------------------------------- ### Define Service Schema Source: https://github.com/connectrpc/connect-es/blob/main/packages/connect/README.md Define your API schema using Protocol Buffers. This schema is used for code generation to produce servers and clients. ```proto service ElizaService { rpc Say(SayRequest) returns (SayResponse) {} } ``` -------------------------------- ### Uninstall old Connect-ES generated SDK Source: https://github.com/connectrpc/connect-es/blob/main/MIGRATING.md Remove the v1 Connect-ES generated SDK dependency from package.json using npm remove. ```shell npm remove @buf/googleapis_googleapis.connectrpc_es ``` -------------------------------- ### Remove import_extension=none Plugin Option Source: https://github.com/connectrpc/connect-es/blob/main/MIGRATING.md Remove the 'import_extension=none' plugin option from buf.gen.yaml as it is now the default behavior. ```diff # buf.gen.yaml version: v2 plugins: - local: protoc-gen-es out: src/gen include_imports: true opt: - target=ts - - import_extension=none ``` -------------------------------- ### Customize fetch credentials with fetch override Source: https://github.com/connectrpc/connect-es/blob/main/MIGRATING.md The `credentials` option has been removed from transports. To set fetch options like `credentials`, provide a fetch override in the transport configuration. ```diff createConnectTransport({ baseUrl: "/", - credentials: "include", + fetch: (input, init) => fetch(input, { ...init, credentials: "include" }), }); ``` -------------------------------- ### createGrpcWebTransport() Source: https://github.com/connectrpc/connect-es/blob/main/packages/connect-node/README.md Creates a transport for Node.js clients to communicate with servers using the gRPC-web protocol over the Node.js `http` module. ```APIDOC ## createGrpcWebTransport() ### Description Lets your clients running on Node.js talk to a server with the gRPC-web protocol. ### Usage ```typescript import { createClient } from "@connectrpc/connect"; import { createGrpcWebTransport } from "@connectrpc/connect-node"; import { ElizaService } from "./gen/eliza_connect.js"; // A transport for clients using the Connect protocol with Node.js `http` module const transport = createGrpcWebTransport({ baseUrl: "https://demo.connectrpc.com", httpVersion: "1.1" }); const client = createClient(ElizaService, transport); const { sentence } = await client.say({ sentence: "I feel happy." }); console.log(sentence) // you said: I feel happy. ``` ``` -------------------------------- ### Call RPC with curl Source: https://github.com/connectrpc/connect-es/blob/main/packages/example/README.md Use curl to call the Connect RPC endpoint. This demonstrates making a POST request with JSON payload to the Say procedure. ```shell curl \ --header 'Content-Type: application/json' \ --data '{"sentence": "I feel happy."}' \ https://localhost:8443/connectrpc.eliza.v1.ElizaService/Say ``` -------------------------------- ### Create gRPC-Web Transport for Node.js Clients Source: https://github.com/connectrpc/connect-es/blob/main/packages/connect-node/README.md Use `createGrpcWebTransport` to enable Node.js clients to communicate with servers using the gRPC-web protocol. Specify the `baseUrl` and optionally the `httpVersion`. ```typescript import { createClient } from "@connectrpc/connect"; import { createGrpcWebTransport } from "@connectrpc/connect-node"; import { ElizaService } from "./gen/eliza_connect.js"; // A transport for clients using the Connect protocol with Node.js `http` module const transport = createGrpcWebTransport({ baseUrl: "https://demo.connectrpc.com", httpVersion: "1.1" }); const client = createClient(ElizaService, transport); const { sentence } = await client.say({ sentence: "I feel happy." }); console.log(sentence) // you said: I feel happy. ``` -------------------------------- ### Make a gRPC-Web Request from Node.js Source: https://github.com/connectrpc/connect-es/blob/main/packages/connect-express/README.md Client-side code in Node.js to make a gRPC-Web request to a Connect RPC service. Uses createGrpcWebTransport. ```typescript import { createClient } from "@connectrpc/connect"; import { createGrpcWebTransport } from "@connectrpc/connect-node"; import { ElizaService } from "./gen/eliza_connect.js"; const transport = createGrpcWebTransport({ baseUrl: "http://localhost:8080", httpVersion: "1.1", }); const client = createClient(ElizaService, transport); const { sentence } = await client.say({ sentence: "I feel happy." }); console.log(sentence); // you said: I feel happy. ``` -------------------------------- ### Correct TypeScript Usage with Create Function Source: https://github.com/connectrpc/connect-es/blob/main/MIGRATING.md In Connect v2, use the `create` function with the specific schema for the expected message type to ensure type safety. ```ts client.requestA(create(MessageASchema)); ``` -------------------------------- ### Metro Import Resolution Error Source: https://github.com/connectrpc/connect-es/blob/main/MIGRATING.md If Metro or Expo fails to resolve imports from Connect-ES or Protobuf-ES, ensure package exports are enabled in your Metro configuration. ```text Metro error: Unable to resolve module @bufbuild/protobuf/codegenv1 ``` -------------------------------- ### Constructing error details with Protobuf messages Source: https://github.com/connectrpc/connect-es/blob/main/MIGRATING.md In v2, error details are provided as an object with 'desc' (message schema) and 'value' (initialization object), replacing v1's direct message instances. ```diff - import { LocalizedMessage } from "./gen/google/rpc/error_details_pb"; - const details = [ - new LocalizedMessage({ - locale: "fr-CH", - message: "Je n\'ai plus de mots.", - }), - ]; + import { LocalizedMessageSchema } from "./gen/google/rpc/error_details_pb"; + const details = [ + { + desc: LocalizedMessageSchema, + value: { + locale: "fr-CH", + message: "Je n\'ai plus de mots.", + } + }, + ]; const metadata = new Headers({ "words-left": "none" }); throw new ConnectError( "I have no words anymore.", Code.ResourceExhausted, metadata, details, ); ``` -------------------------------- ### Enable Clean Option in buf.gen.yaml Source: https://github.com/connectrpc/connect-es/blob/main/MIGRATING.md Add the 'clean: true' option to buf.gen.yaml to automatically remove old generated files before new ones are generated. ```diff # buf.gen.yaml version: v2 +clean: true plugins: - local: protoc-gen-es out: src/gen include_imports: true ``` -------------------------------- ### Helper Function Import Change Source: https://github.com/connectrpc/connect-es/blob/main/MIGRATING.md Helpers for well-known types, like timestampDate, are now standalone functions exported from '@bufbuild/protobuf/wkt'. ```diff - import type { Timestamp } from "@bufbuild/protobuf"; + import type { Timestamp } from "@bufbuild/protobuf/wkt"; + import { timestampDate } from "@bufbuild/protobuf/wkt"; const timestamp: Timestamp = ... - const date: Date = timestamp.toDate(); + const date: Date = timestampDate(timestamp); ``` -------------------------------- ### Update Remote Plugin in buf.gen.yaml Source: https://github.com/connectrpc/connect-es/blob/main/MIGRATING.md Modify buf.gen.yaml to use the remote Protobuf-ES v2 plugin and remove the Connect plugin. ```diff # buf.gen.yaml version: v2 plugins: - - remote: buf.build/bufbuild/es:v1.10.0 + - remote: buf.build/bufbuild/es:v2.2.0 out: src/gen include_imports: true opt: target=ts - - remote: buf.build/connectrpc/es - out: src/gen - opt: target=ts ``` -------------------------------- ### Generated ECMAScript Import for Validation Source: https://github.com/connectrpc/connect-es/blob/main/MIGRATING.md The new Protobuf-ES plugin generates ECMAScript imports for validation protos. This import is not generated by default and requires `include_imports: true` in buf.gen.yaml. ```diff + import { file_buf_validate_validate } from "./buf/validate/validate_pb"; ``` -------------------------------- ### Directly Run jscodeshift Transform Source: https://github.com/connectrpc/connect-es/blob/main/packages/connect-migrate/README.md Manually invoke the jscodeshift CLI with the migration transform. This is useful if you encounter parsing errors with the default execution. ```shell npx jscodeshift -t ./node_modules/@connectrpc/connect-migrate/dist/cjs/migrations/v0.13.1-transform.js . ``` -------------------------------- ### createConnectTransport() Source: https://github.com/connectrpc/connect-es/blob/main/packages/connect-node/README.md Creates a transport for Node.js clients to communicate with servers using the Connect protocol over the Node.js `http` module. ```APIDOC ## createConnectTransport() ### Description Lets your clients running on Node.js talk to a server with the Connect protocol. ### Usage ```typescript import { createClient } from "@connectrpc/connect"; import { createConnectTransport } from "@connectrpc/connect-node"; import { ElizaService } from "./gen/eliza_connect.js"; // A transport for clients using the Connect protocol with Node.js `http` module const transport = createConnectTransport({ baseUrl: "https://demo.connectrpc.com", httpVersion: "1.1" }); const client = createClient(ElizaService, transport); const { sentence } = await client.say({ sentence: "I feel happy." }); console.log(sentence) // you said: I feel happy. ``` ``` -------------------------------- ### Register fastifyConnectPlugin Source: https://github.com/connectrpc/connect-es/blob/main/packages/connect-fastify/README.md Register the fastifyConnectPlugin in your Fastify server, optionally including validation interceptors and your defined routes. ```typescript // server.ts import { fastify } from "fastify"; import routes from "connect"; import { fastifyConnectPlugin } from "@connectrpc/connect-fastify"; import { createValidateInterceptor } from "@connectrpc/validate"; const server = fastify({ http2: true, }); await server.register(fastifyConnectPlugin, { // Validation via Protovalidate is almost always recommended interceptors: [createValidateInterceptor()], routes }); await server.listen({ host: "localhost", port: 8080, }); ``` -------------------------------- ### Create Connect Transport for Node.js Clients Source: https://github.com/connectrpc/connect-es/blob/main/packages/connect-node/README.md Use `createConnectTransport` to enable Node.js clients to communicate with servers using the Connect protocol. Specify the `baseUrl` and optionally the `httpVersion`. ```typescript import { createClient } from "@connectrpc/connect"; import { createConnectTransport } from "@connectrpc/connect-node"; import { ElizaService } from "./gen/eliza_connect.js"; // A transport for clients using the Connect protocol with Node.js `http` module const transport = createConnectTransport({ baseUrl: "https://demo.connectrpc.com", httpVersion: "1.1" }); const client = createClient(ElizaService, transport); const { sentence } = await client.say({ sentence: "I feel happy." }); console.log(sentence) // you said: I feel happy. ``` -------------------------------- ### createGrpcTransport() Source: https://github.com/connectrpc/connect-es/blob/main/packages/connect-node/README.md Creates a transport for Node.js clients to communicate with servers using the gRPC protocol over the Node.js `http2` module. ```APIDOC ## createGrpcTransport() ### Description Lets your clients running on Node.js talk to a server with the gRPC protocol. ### Usage ```typescript import { createClient } from "@connectrpc/connect"; import { createGrpcTransport } from "@connectrpc/connect-node"; import { ElizaService } from "./gen/eliza_connect.js"; // A transport for clients using the gRPC protocol with Node.js `http2` module const transport = createGrpcTransport({ baseUrl: "https://demo.connectrpc.com", }); const client = createClient(ElizaService, transport); const { sentence } = await client.say({ sentence: "I feel happy." }); console.log(sentence) // you said: I feel happy. ``` ``` -------------------------------- ### Update ConnectRouter.rpc signature Source: https://github.com/connectrpc/connect-es/blob/main/MIGRATING.md The 'service' argument has been removed from ConnectRouter.rpc. Update call-sites to only pass the method and implementation. ```diff const routes = ({rpc}: ConnectRouter) => { - rpc(ElizaService, ElizaService.say, impl); + rpc(ElizaService.say, impl); } ``` -------------------------------- ### Update import paths for Protobuf messages Source: https://github.com/connectrpc/connect-es/blob/main/MIGRATING.md Change import paths from *_connect.ts to *_pb.ts when referencing Protobuf messages. ```diff - import { ElizaService } from "./gen/eliza_connect"; + import { ElizaService } from "./gen/eliza_pb"; ``` -------------------------------- ### Reflection API Change: Field List Source: https://github.com/connectrpc/connect-es/blob/main/MIGRATING.md Reflection API for accessing fields has changed. Use SayRequestSchema.fields instead of SayRequest.fields.list() to find field information. ```diff - SayRequest.fields.list() + SayRequestSchema.fields .find((f) => f.localName === "sentence") ?.jsonName; ``` -------------------------------- ### Remove Local Plugin from buf.gen.yaml Source: https://github.com/connectrpc/connect-es/blob/main/MIGRATING.md Update buf.gen.yaml to remove the local protoc-gen-connect-es plugin when using local plugins. ```diff # buf.gen.yaml version: v2 plugins: - local: protoc-gen-es out: src/gen include_imports: true opt: target=ts - - local: protoc-gen-connect-es - out: src/gen - opt: target=ts ``` -------------------------------- ### Convert message to binary using toBinary function Source: https://github.com/connectrpc/connect-es/blob/main/MIGRATING.md Replace the message instance's toBinary() method with the standalone toBinary function, passing the message schema and the message instance. ```diff import { toBinary } from "@bufbuild/protobuf"; import { SayRequestSchema } from "./gen/eliza_pb"; - sayRequest.toBinary(); + toBinary(SayRequestSchema, sayRequest); ``` -------------------------------- ### Remove httpVersion from createGrpcTransport Source: https://github.com/connectrpc/connect-es/blob/main/MIGRATING.md The gRPC Transport now requires HTTP/2. Remove the `httpVersion` property from `createGrpcTransport` calls to use the default HTTP/2. ```diff import { createGrpcTransport } from "@connectrpc/connect-node"; createGrpcTransport({ baseUrl: "https://demo.connectrpc.com", - httpVersion: "2", }); ``` -------------------------------- ### Parcel Import Resolution Error Source: https://github.com/connectrpc/connect-es/blob/main/MIGRATING.md If Parcel fails to resolve imports from Connect-ES or Protobuf-ES, ensure package exports are enabled in your Parcel configuration. ```text @parcel/core: Failed to resolve '@bufbuild/protobuf/codegenv1' ``` -------------------------------- ### Add Connect Express Middleware to an Express Server Source: https://github.com/connectrpc/connect-es/blob/main/packages/connect-express/README.md Integrate the expressConnectMiddleware into your Express application. It's recommended to include validation interceptors. ```typescript // server.ts import http from "http"; import express from "express"; import routes from "connect"; import { expressConnectMiddleware } from "@connectrpc/connect-express"; import { createValidateInterceptor } from "@connectrpc/validate"; const app = express(); app.use(expressConnectMiddleware({ // Validation via Protovalidate is almost always recommended interceptors: [createValidateInterceptor()], routes })); http.createServer(app).listen(8080); ``` -------------------------------- ### Create gRPC Transport for Node.js Clients Source: https://github.com/connectrpc/connect-es/blob/main/packages/connect-node/README.md Use `createGrpcTransport` to enable Node.js clients to communicate with servers using the gRPC protocol. This transport typically uses the Node.js `http2` module. ```typescript import { createClient } from "@connectrpc/connect"; import { createGrpcTransport } from "@connectrpc/connect-node"; import { ElizaService } from "./gen/eliza_connect.js"; // A transport for clients using the gRPC protocol with Node.js `http2` module const transport = createGrpcTransport({ baseUrl: "https://demo.connectrpc.com", }); const client = createClient(ElizaService, transport); const { sentence } = await client.say({ sentence: "I feel happy." }); console.log(sentence) // you said: I feel happy. ``` -------------------------------- ### Rename JSON option emitDefaultValues to alwaysEmitImplicit Source: https://github.com/connectrpc/connect-es/blob/main/MIGRATING.md The `emitDefaultValues` option for JSON serialization has been renamed to `alwaysEmitImplicit` in Protobuf-ES v2. Update calls to use the new option name. ```diff await server.register( fastifyConnectPlugin, { routes, jsonOptions: { - emitDefaultValues: true, + alwaysEmitImplicit: true, }, }, ); ``` -------------------------------- ### Workaround for Passing Different Message Types Source: https://github.com/connectrpc/connect-es/blob/main/MIGRATING.md To pass a message as another with similar fields, use object destructuring to remove `$typeName` and then create a new message with the extracted properties. ```ts const messageA: MessageA = ...; const { $typeName: _, ...properties } = messageA; const messageB = create(MessageBSchema, properties); ``` -------------------------------- ### Access Protobuf message type name Source: https://github.com/connectrpc/connect-es/blob/main/MIGRATING.md Access the qualified Protobuf name of a message using the $typeName property instead of the getType().typeName method. ```diff - sayRequest.getType().typeName; // "connectrpc.eliza.v1.SayRequest" + sayRequest.$typeName; // "connectrpc.eliza.v1.SayRequest" ``` -------------------------------- ### Rename JSON option typeRegistry to registry Source: https://github.com/connectrpc/connect-es/blob/main/MIGRATING.md The `typeRegistry` option for JSON serialization has been renamed to `registry` in Protobuf-ES v2. Ensure to use the new option name when configuring transports or server plugins. ```diff import { createRegistry } from "@bufbuild/protobuf"; import { createConnectTransport } from "@connectrpc/connect-web"; - import { SayRequest } from "./gen/eliza_pb"; + import { SayRequestSchema } from "./gen/eliza_pb"; const transport = createConnectTransport({ baseUrl: "https://demo.connectrpc.com", jsonOptions: { - typeRegistry: createRegistry(SayRequest), + registry: createRegistry(SayRequestSchema), }, }); ``` -------------------------------- ### Define Protobuf messages without PlainMessage type Source: https://github.com/connectrpc/connect-es/blob/main/MIGRATING.md Remove the PlainMessage type and directly use the message type for defining message objects. Ensure the $typeName property is included to identify the message. ```diff - import type { PlainMessage } from "@bufbuild/protobuf"; import type { SayRequest } from "./gen/eliza_pb"; - const sayRequest: PlainMessage = { + const sayRequest: SayRequest = { $typeName: "connectrpc.eliza.v1.SayRequest", sentence: "Hello", }; ``` -------------------------------- ### Well-known Type Import Change Source: https://github.com/connectrpc/connect-es/blob/main/MIGRATING.md All well-known types have moved to the subpath export '@bufbuild/protobuf/wkt'. This change affects how you import types like Timestamp. ```diff - import type { Timestamp } from "@bufbuild/protobuf"; + import type { Timestamp } from "@bufbuild/protobuf/wkt"; ``` -------------------------------- ### Identify Protobuf messages using isMessage with schema Source: https://github.com/connectrpc/connect-es/blob/main/MIGRATING.md When checking if a variable is a Protobuf message, use the isMessage function with the message schema instead of the message class. ```diff - import { SayRequest } from "./gen/eliza_connect.js"; + import { SayRequestSchema } from "./gen/eliza_pb.js"; import { isMessage } from "@bufbuild/protobuf"; - if (isMessage(x, SayRequest)) { + if (isMessage(x, SayRequestSchema)) { x.sentence; } ``` -------------------------------- ### Struct Field Change to JsonObject Source: https://github.com/connectrpc/connect-es/blob/main/MIGRATING.md The google.protobuf.Struct well-known type is now generated as JsonObject when used as a message field, simplifying its usage. ```diff /** * @generated from field: google.protobuf.Struct struct = 1; */ - struct?: Struct; + struct?: JsonObject; ``` -------------------------------- ### Proto2 Field Generation Change Source: https://github.com/connectrpc/connect-es/blob/main/MIGRATING.md Proto2 fields with default values are no longer generated as optional properties. You can now rely on the default value instead of handling undefined. ```diff /** * @generated from field: required int32 num = 3; */ - num?: number; + num: number; ``` -------------------------------- ### TypeScript Type Error with MessageB in RequestA Source: https://github.com/connectrpc/connect-es/blob/main/MIGRATING.md Connect v2 enforces stricter type checking. Passing a superset type (MessageB) to a function expecting a subset (MessageA) will now result in a TypeScript error. ```ts client.requestA(create(MessageBSchema)); // Type Error: Argument of type MessageBSchema is not assignable to parameter of type MessageInit ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.