-
-
Notifications
You must be signed in to change notification settings - Fork 40
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7896b95
commit fc2c7f9
Showing
8 changed files
with
92 additions
and
48 deletions.
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
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
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,51 @@ | ||
import { | ||
AnyQueryProcedure, | ||
AnyRouter, | ||
DeepPartial, | ||
inferProcedureInput, | ||
} from '@trpc/server'; | ||
import { hash } from 'ohash' | ||
import { DecorateProcedure } from './types'; | ||
|
||
export type GetQueryParams< | ||
TProcedureOrRouter extends AnyQueryProcedure, | ||
TProcedureInput = inferProcedureInput<TProcedureOrRouter>, | ||
> = DeepPartial<TProcedureInput>; | ||
|
||
type GetParams< | ||
TProcedureOrRouter extends AnyQueryProcedure, | ||
> = [ | ||
procedureOrRouter: DecorateProcedure<TProcedureOrRouter, AnyRouter> | string, | ||
params: GetQueryParams<TProcedureOrRouter>, | ||
]; | ||
|
||
type GetQueryKeyParams< | ||
TProcedureOrRouter extends AnyQueryProcedure, | ||
> = GetParams<TProcedureOrRouter>; | ||
|
||
export function getQueryKey< | ||
TProcedure extends AnyQueryProcedure, | ||
>(..._params: GetQueryKeyParams<TProcedure>): string { | ||
const [procedure, input] = _params; | ||
|
||
if (typeof procedure === 'string') { | ||
// TODO: Warn here if string is passed that it will be deprecated in the future. | ||
return getQueryKeyInternal(procedure, input); | ||
} | ||
|
||
// @ts-expect-error: we don't expose _def on the type layer | ||
const path = procedure._def().path as string[]; | ||
const dotPath = path.join('.'); | ||
|
||
return getQueryKeyInternal(dotPath, input) | ||
} | ||
|
||
/** | ||
* @internal | ||
*/ | ||
export function getQueryKeyInternal ( | ||
path: string, | ||
input: unknown | ||
): string { | ||
return input === undefined ? path : `${path}-${hash(input || '')}` | ||
} |
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