Skip to content

Commit

Permalink
fix(statement): unionize method names
Browse files Browse the repository at this point in the history
  • Loading branch information
johngeorgewright committed Oct 22, 2024
1 parent 4f980ce commit 55e4c91
Showing 1 changed file with 11 additions and 7 deletions.
18 changes: 11 additions & 7 deletions src/query/statement.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,14 @@ interface Rval<T> {

type ServiceMethod<S extends string> = `get${Capitalize<S>}ByStatementAsync`

type GetByStatementMethodNames<C extends Client> = keyof {
[Key in keyof C as Key extends `get${infer T}ByStatementAsync`
? Parameters<C[Key]> extends [GetByStatement, ...unknown[]]
? Uncapitalize<T>
: never
: never]: unknown
}

/**
* Query a service wth PQL options.
*
Expand Down Expand Up @@ -75,16 +83,12 @@ type ServiceMethod<S extends string> = `get${Capitalize<S>}ByStatementAsync`
*/
export function getByStatement<
C extends Client,
S extends string,
S extends GetByStatementMethodNames<C>,
V extends StatementValue<string> = StatementValue<never>,
Args extends unknown[] = [],
>(
client: C,
service: ServiceMethod<S> extends keyof C
? Parameters<C[ServiceMethod<S>]> extends [GetByStatement, ...unknown[]]
? S
: never
: never,
service: S,
query: ServiceMethod<S> extends keyof C
? PQLOptions<
GetByStatementResponseResult<C[ServiceMethod<S>]>,
Expand All @@ -95,7 +99,7 @@ export function getByStatement<
...args: Args
): ServiceMethod<S> extends keyof C ? ReturnType<C[ServiceMethod<S>]> : never {
return client[
`get${service[0].toUpperCase()}${service.slice(1)}ByStatementAsync`
`get${(service as string)[0].toUpperCase()}${(service as string).slice(1)}ByStatementAsync`
](
{
filterStatement: {
Expand Down

0 comments on commit 55e4c91

Please sign in to comment.