Skip to content

Commit

Permalink
Introduce connect-query-core package
Browse files Browse the repository at this point in the history
  • Loading branch information
paul-sachs committed Oct 21, 2024
1 parent 1d5e7c9 commit 349ce3d
Show file tree
Hide file tree
Showing 42 changed files with 964 additions and 27 deletions.
3 changes: 3 additions & 0 deletions packages/connect-query-core/.eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
dist
coverage
src/gen
3 changes: 3 additions & 0 deletions packages/connect-query-core/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# @connectrpc/connect-query-core

This package provides the core functionality for the Connect Query API. It exposes all the necessary functions to use with the different variants of the tanstack/query packages. Documentation for these APIs can be found in the main repo readme at https://github.com/connectrpc/connect-query-es and covers any non-hook functions (anything that doesn't start with `use`).
12 changes: 12 additions & 0 deletions packages/connect-query-core/buf.gen.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# buf.gen.yaml defines a local generation template.
# For details, see https://buf.build/docs/configuration/v2/buf-gen-yaml
version: v2
inputs:
- directory: proto
# Deletes the directories specified in the `out` field for all plugins before running code generation.
clean: true
plugins:
- local: protoc-gen-es
out: src/gen
opt:
- target=ts
47 changes: 47 additions & 0 deletions packages/connect-query-core/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
{
"name": "@connectrpc/connect-query-core",
"version": "1.3.1",
"description": "Core of connect-query, framework agnostic helpers for typescript safe queries.",
"license": "Apache-2.0",
"repository": {
"type": "git",
"url": "https://github.com/connectrpc/connect-query-es.git",
"directory": "packages/connect-query-core"
},
"scripts": {
"clean": "rm -rf ./dist/*",
"build": "npm run build:cjs && npm run build:esm",
"build:cjs": "tsc --project tsconfig.build.json --module commonjs --moduleResolution node10 --verbatimModuleSyntax false --outDir ./dist/cjs --declarationDir ./dist/cjs && echo >./dist/cjs/package.json '{\"type\":\"commonjs\"}'",
"build:esm": "tsc --project tsconfig.build.json",
"generate": "buf generate",
"test": "vitest --run",
"test:watch": "vitest --watch",
"format": "prettier . --write --ignore-path ./.eslintignore && eslint . --fix && license-header",
"attw": "attw --pack"
},
"type": "module",
"sideEffects": false,
"main": "./dist/cjs/index.js",
"exports": {
".": {
"import": "./dist/esm/index.js",
"require": "./dist/cjs/index.js"
}
},
"devDependencies": {
"@arethetypeswrong/cli": "^0.15.2",
"@bufbuild/buf": "1.43.0",
"@bufbuild/jest-environment-jsdom": "^0.1.1",
"@bufbuild/protobuf": "^2.2.0",
"@bufbuild/protoc-gen-es": "^2.2.0",
"@connectrpc/connect": "^2.0.0-rc.1",
"@connectrpc/connect-web": "^2.0.0-rc.1",
"@testing-library/react": "^16.0.1",
"typescript": "^5.5.4"
},
"peerDependencies": {
"@bufbuild/protobuf": "2.x",
"@connectrpc/connect": "^2.0.0-rc.1",
"@tanstack/query-core": "5.x"
}
}
30 changes: 30 additions & 0 deletions packages/connect-query-core/proto/bigint.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// Copyright 2021-2023 The Connect Authors
//
// 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";

import "google/protobuf/empty.proto";

service BigIntService {
rpc Count(CountRequest) returns (CountResponse);
rpc GetCount(google.protobuf.Empty) returns (CountResponse);
}

message CountRequest {
int64 add = 1;
}

message CountResponse {
int64 count = 1;
}
38 changes: 38 additions & 0 deletions packages/connect-query-core/proto/eliza.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// Copyright 2021-2023 The Connect Authors
//
// 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 connectrpc.eliza.v1;

// ElizaService provides a way to talk to Eliza, a port of the DOCTOR script
// for Joseph Weizenbaum's original ELIZA program. Created in the mid-1960s at
// the MIT Artificial Intelligence Laboratory, ELIZA demonstrates the
// superficiality of human-computer communication. DOCTOR simulates a
// psychotherapist, and is commonly found as an Easter egg in emacs
// distributions.
service ElizaService {
// Say is a unary RPC. Eliza responds to the prompt with a single sentence.
rpc Say(SayRequest) returns (SayResponse) {}
}

// SayRequest is a single-sentence request.
message SayRequest {
string sentence = 1;
}

// SayResponse is a single-sentence response.
message SayResponse {
string sentence = 1;
}
30 changes: 30 additions & 0 deletions packages/connect-query-core/proto/list.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// Copyright 2021-2023 The Connect Authors
//
// 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";

service ListService {
rpc List(ListRequest) returns (ListResponse);
}

message ListRequest {
int64 page = 1;
bool preview = 2;
}

message ListResponse {
int64 page = 1;
repeated string items = 2;
}

21 changes: 21 additions & 0 deletions packages/connect-query-core/proto/proto2.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// Copyright 2021-2023 The Connect Authors
//
// 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 = "proto2";
package test;

message Proto2Message {
optional string string_field = 1;
optional int32 int32_field = 3;
}
49 changes: 49 additions & 0 deletions packages/connect-query-core/proto/proto3.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// Copyright 2021-2023 The Connect Authors
//
// 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 test;

// Note: We do not exhaust all field types
message Proto3Message {
string string_field = 1;
bytes bytes_field = 2;
int32 int32_field = 3;
int64 int64_field = 4;
double double_field = 5;
bool bool_field = 6;
Proto3Enum enum_field = 7;
Proto3Message message_field = 8;

optional string optional_string_field = 9;

repeated string repeated_string_field = 17;
repeated Proto3Message repeated_message_field = 18;
repeated Proto3Enum repeated_enum_field = 19;

oneof either {
string oneof_string_field = 31;
int32 oneof_int32_field = 33;
}

map<string, int64> map_string_int64_field = 39;
map<string, Proto3Message> map_string_message_field = 40;
map<string, Proto3Enum> map_string_enum_field = 41;
}

enum Proto3Enum {
PROTO3_ENUM_UNSPECIFIED = 0;
PROTO3_ENUM_YES = 1;
PROTO3_ENUM_NO = 2;
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@

import { create } from "@bufbuild/protobuf";
import type { Transport } from "@connectrpc/connect";
import { skipToken } from "@tanstack/react-query";
import { describe, expect, it } from "vitest";

import { createConnectQueryKey } from "./connect-query-key.js";
import { ElizaService, SayRequestSchema } from "./gen/eliza_pb.js";
import { ListRequestSchema, ListService } from "./gen/list_pb.js";
import { skipToken } from "./index.js";
import { createMessageKey } from "./message-key.js";
import { createTransportKey } from "./transport-key.js";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import type {
MessageInitShape,
} from "@bufbuild/protobuf";
import type { Transport } from "@connectrpc/connect";
import type { SkipToken } from "@tanstack/react-query";
import type { SkipToken } from "@tanstack/query-core";

import { createMessageKey } from "./message-key.js";
import { createTransportKey } from "./transport-key.js";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ import type {
QueryFunction,
QueryKey,
SkipToken,
} from "@tanstack/react-query";
import { skipToken } from "@tanstack/react-query";
} from "@tanstack/query-core";
import { skipToken } from "@tanstack/query-core";

import { callUnaryMethod } from "./call-unary-method.js";
import {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@
// See the License for the specific language governing permissions and
// limitations under the License.

import { skipToken } from "@tanstack/react-query";
import { describe, expect, it } from "vitest";

import { createConnectQueryKey } from "./connect-query-key.js";
import { createQueryOptions } from "./create-query-options.js";
import { ElizaService } from "./gen/eliza_pb.js";
import { skipToken } from "./index.js";
import { mockEliza } from "./test/test-utils.js";

// TODO: maybe create a helper to take a service and method and generate this.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ import type {
} from "@bufbuild/protobuf";
import { create } from "@bufbuild/protobuf";
import type { Transport } from "@connectrpc/connect";
import type { QueryFunction, SkipToken } from "@tanstack/react-query";
import { skipToken } from "@tanstack/react-query";
import type { QueryFunction, SkipToken } from "@tanstack/query-core";
import { skipToken } from "@tanstack/query-core";

import { callUnaryMethod } from "./call-unary-method.js";
import type { ConnectQueryKey } from "./connect-query-key.js";
Expand Down
87 changes: 87 additions & 0 deletions packages/connect-query-core/src/gen/bigint_pb.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
// Copyright 2021-2023 The Connect Authors
//
// 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.

// @generated by protoc-gen-es v2.2.0 with parameter "target=ts"
// @generated from file bigint.proto (syntax proto3)
/* eslint-disable */

import type { GenFile, GenMessage, GenService } from "@bufbuild/protobuf/codegenv1";
import { fileDesc, messageDesc, serviceDesc } from "@bufbuild/protobuf/codegenv1";
import type { EmptySchema } from "@bufbuild/protobuf/wkt";
import { file_google_protobuf_empty } from "@bufbuild/protobuf/wkt";
import type { Message } from "@bufbuild/protobuf";

/**
* Describes the file bigint.proto.
*/
export const file_bigint: GenFile = /*@__PURE__*/
fileDesc("CgxiaWdpbnQucHJvdG8iGwoMQ291bnRSZXF1ZXN0EgsKA2FkZBgBIAEoAyIeCg1Db3VudFJlc3BvbnNlEg0KBWNvdW50GAEgASgDMmsKDUJpZ0ludFNlcnZpY2USJgoFQ291bnQSDS5Db3VudFJlcXVlc3QaDi5Db3VudFJlc3BvbnNlEjIKCEdldENvdW50EhYuZ29vZ2xlLnByb3RvYnVmLkVtcHR5Gg4uQ291bnRSZXNwb25zZWIGcHJvdG8z", [file_google_protobuf_empty]);

/**
* @generated from message CountRequest
*/
export type CountRequest = Message<"CountRequest"> & {
/**
* @generated from field: int64 add = 1;
*/
add: bigint;
};

/**
* Describes the message CountRequest.
* Use `create(CountRequestSchema)` to create a new message.
*/
export const CountRequestSchema: GenMessage<CountRequest> = /*@__PURE__*/
messageDesc(file_bigint, 0);

/**
* @generated from message CountResponse
*/
export type CountResponse = Message<"CountResponse"> & {
/**
* @generated from field: int64 count = 1;
*/
count: bigint;
};

/**
* Describes the message CountResponse.
* Use `create(CountResponseSchema)` to create a new message.
*/
export const CountResponseSchema: GenMessage<CountResponse> = /*@__PURE__*/
messageDesc(file_bigint, 1);

/**
* @generated from service BigIntService
*/
export const BigIntService: GenService<{
/**
* @generated from rpc BigIntService.Count
*/
count: {
methodKind: "unary";
input: typeof CountRequestSchema;
output: typeof CountResponseSchema;
},
/**
* @generated from rpc BigIntService.GetCount
*/
getCount: {
methodKind: "unary";
input: typeof EmptySchema;
output: typeof CountResponseSchema;
},
}> = /*@__PURE__*/
serviceDesc(file_bigint, 0);

Loading

0 comments on commit 349ce3d

Please sign in to comment.