Skip to content
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

Ensure createconnectQueryKey validates input types #493

Merged
merged 3 commits into from
Dec 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions packages/connect-query-core/src/connect-query-key.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,4 +122,15 @@ describe("createConnectQueryKey", () => {
expect(key[1].serviceName).toBe(ElizaService.typeName);
expect(key[1].methodName).toBeUndefined();
});

it("cannot except invalid input", () => {
createConnectQueryKey({
schema: ElizaService.method.say,
input: {
// @ts-expect-error(2322) cannot create a key with invalid input
sentence: 1,
},
cardinality: undefined,
});
});
});
91 changes: 48 additions & 43 deletions packages/connect-query-core/src/connect-query-key.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@
// limitations under the License.

import type {
DescMessage,
DescMethod,
DescMethodUnary,
DescService,
MessageInitShape,
} from "@bufbuild/protobuf";
Expand Down Expand Up @@ -72,46 +74,46 @@ export type ConnectQueryKey = [
},
];

type KeyParams<Desc extends DescMethod | DescService> = Desc extends DescMethod
? {
/**
* Set `serviceName` and `methodName` in the key.
*/
schema: Desc;
/**
* Set `input` in the key:
* - If a SkipToken is provided, `input` is "skipped".
* - If an init shape is provided, `input` is set to a message key.
* - If omitted or undefined, `input` is not set in the key.
*/
input?: MessageInitShape<Desc["input"]> | SkipToken | undefined;
/**
* Set `transport` in the key.
*/
transport?: Transport;
/**
* Set `cardinality` in the key - undefined is used for filters to match both finite and infinite queries.
*/
cardinality: "finite" | "infinite" | undefined;
/**
* If omit the field with this name from the key for infinite queries.
*/
pageParamKey?: keyof MessageInitShape<Desc["input"]>;
}
: {
/**
* Set `serviceName` in the key, and omit `methodName`.
*/
schema: Desc;
/**
* Set `transport` in the key.
*/
transport?: Transport;
/**
* Set `cardinality` in the key - undefined is used for filters to match both finite and infinite queries.
*/
cardinality: "finite" | "infinite" | undefined;
};
type KeyParamsForMethod<Desc extends DescMethod> = {
/**
* Set `serviceName` and `methodName` in the key.
*/
schema: Desc;
/**
* Set `input` in the key:
* - If a SkipToken is provided, `input` is "skipped".
* - If an init shape is provided, `input` is set to a message key.
* - If omitted or undefined, `input` is not set in the key.
*/
input?: MessageInitShape<Desc["input"]> | SkipToken | undefined;
/**
* Set `transport` in the key.
*/
transport?: Transport;
/**
* Set `cardinality` in the key - undefined is used for filters to match both finite and infinite queries.
*/
cardinality: "finite" | "infinite" | undefined;
/**
* If omit the field with this name from the key for infinite queries.
*/
pageParamKey?: keyof MessageInitShape<Desc["input"]>;
};

type KeyParamsForService<Desc extends DescService> = {
/**
* Set `serviceName` in the key, and omit `methodName`.
*/
schema: Desc;
/**
* Set `transport` in the key.
*/
transport?: Transport;
/**
* Set `cardinality` in the key - undefined is used for filters to match both finite and infinite queries.
*/
cardinality: "finite" | "infinite" | undefined;
};

/**
* TanStack Query manages query caching for you based on query keys. In Connect Query, keys are structured, and can easily be created using this factory function.
Expand Down Expand Up @@ -151,9 +153,12 @@ type KeyParams<Desc extends DescMethod | DescService> = Desc extends DescMethod
* @see ConnectQueryKey for information on the components of Connect-Query's keys.
*/
export function createConnectQueryKey<
Desc extends DescMethod | DescService,
Params extends KeyParams<Desc>,
>(params: Params): ConnectQueryKey {
I extends DescMessage,
O extends DescMessage,
Desc extends DescService,
>(
params: KeyParamsForMethod<DescMethodUnary<I, O>> | KeyParamsForService<Desc>,
): ConnectQueryKey {
const props: ConnectQueryKey[1] =
params.schema.kind == "rpc"
? {
Expand Down
13 changes: 13 additions & 0 deletions packages/connect-query-core/src/create-query-options.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,17 @@ describe("createQueryOptions", () => {
);
expect(opt.queryKey).toStrictEqual(want);
});

it("ensures type safety of parameters", () => {
// @ts-expect-error(2322) cannot provide invalid parameters
createQueryOptions(
sayMethodDescriptor,
{
sentence: 1,
},
{
transport: mockedElizaTransport,
},
);
});
});
Loading