Skip to content

Commit

Permalink
Merge pull request #23 from palantir/ssanjay/fixactionparam
Browse files Browse the repository at this point in the history
action params type fix
  • Loading branch information
ssanjay1 authored Feb 2, 2024
2 parents a38d9ca + a19d611 commit 0b9302d
Show file tree
Hide file tree
Showing 7 changed files with 82 additions and 28 deletions.
5 changes: 5 additions & 0 deletions packages/legacy-client/changelog/@unreleased/pr-23.v2.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
type: fix
fix:
description: action params type fix
links:
- https://github.com/palantir/osdk-ts/pull/23
23 changes: 13 additions & 10 deletions packages/legacy-client/src/client/actions/actions.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,13 @@ import {
it,
vi,
} from "vitest";
import {
type ActionError,
ActionExecutionMode,
type ActionExecutionOptions,
type ActionResponseFromOptions,
type Edits,
type Result,
ReturnEditsMode,
import { ActionExecutionMode, ReturnEditsMode } from "../..";
import type {
ActionError,
ActionExecutionOptions,
ActionResponseFromOptions,
Edits,
Result,
} from "../..";
import { USER_AGENT } from "../../USER_AGENT";
import {
Expand All @@ -49,7 +48,7 @@ import {
import { unwrapResultOrThrow } from "../../util/test/resultUtils";
import { createBaseOsdkObjectSet } from "../objectSets/OsdkObjectSet";
import type { OsdkLegacyObjectFrom } from "../OsdkLegacyObject";
import { type Actions } from "./actions";
import type { Actions } from "./actions";
import { createActionProxy } from "./createActionProxy";

describe("Actions", () => {
Expand All @@ -73,7 +72,7 @@ describe("Actions", () => {

describe("type tests", () => {
it("creates proper parameters", async () => {
expectTypeOf<Parameters<typeof actions.createTask>>().toMatchTypeOf<
expectTypeOf<Parameters<typeof actions.createTask>>().toEqualTypeOf<
[
{
id?: number;
Expand All @@ -82,6 +81,10 @@ describe("Actions", () => {
]
>();

expectTypeOf<typeof actions.createTask>()
// @ts-expect-error
.toBeCallableWith([{ id: 1 }]);

expectTypeOf<ReturnType<typeof actions.createTask>>().toMatchTypeOf<
Promise<
Result<
Expand Down
31 changes: 17 additions & 14 deletions packages/legacy-client/src/client/actions/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,20 +46,23 @@ export type ActionArgs<
O extends OntologyDefinition<any>,
A extends keyof O["actions"],
> =
& {
[P in NullableKeys<O["actions"][A]["parameters"]>]?: ActionParameterType<
O,
A,
P
>;
}
& {
[P in NonNullableKeys<O["actions"][A]["parameters"]>]: ActionParameterType<
O,
A,
P
>;
};
& (NonNullableKeys<O["actions"][A]["parameters"]> extends never ? {}
: {
[P in NonNullableKeys<O["actions"][A]["parameters"]>]:
ActionParameterType<
O,
A,
P
>;
})
& (NullableKeys<O["actions"][A]["parameters"]> extends never ? {}
: {
[P in NullableKeys<O["actions"][A]["parameters"]>]?: ActionParameterType<
O,
A,
P
>;
});

export type ActionParameterType<
O extends OntologyDefinition<any>,
Expand Down
14 changes: 14 additions & 0 deletions packages/legacy-client/src/client/queries.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,18 @@ describe("Queries", () => {
LocalDate
>;
}]>();

expectTypeOf<
Parameters<Queries<typeof MockOntology>["queryWithOnlyOptionalArgs"]>
>().toEqualTypeOf<[{ string?: string }]>();

expectTypeOf<
Parameters<Queries<typeof MockOntology>["queryWithOnlyRequiredArgs"]>
>().toEqualTypeOf<[{ string: string }]>();

expectTypeOf<typeof queries.queryWithOnlyOptionalArgs>()
// @ts-expect-error
.toBeCallableWith([{ string: "test" }]);
});
});

Expand All @@ -344,6 +356,8 @@ describe("Queries", () => {
"queryReturnsAggregation",
"queryTakesAllParameterTypes",
"queryTakesNestedObjects",
"queryWithOnlyOptionalArgs",
"queryWithOnlyRequiredArgs",
]
`);
});
Expand Down
8 changes: 4 additions & 4 deletions packages/legacy-client/src/client/queries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,20 +83,20 @@ export type QueryArgs<
O extends OntologyDefinition<any>,
Q extends QueryNamesFrom<O>,
> =
& {
& (NonNullableArgKeys<QueryParameters<O, Q>> extends never ? {} : {
[P in NonNullableArgKeys<QueryParameters<O, Q>>]: QueryDataType<
O,
QueryParameters<O, Q>[P],
false
>;
}
& {
})
& (NullableArgKeys<QueryParameters<O, Q>> extends never ? {} : {
[P in NullableArgKeys<QueryParameters<O, Q>>]?: QueryDataType<
O,
QueryParameters<O, Q>[P],
false
>;
};
});

export type QueryNamesFrom<O extends OntologyDefinition<any>> =
keyof O["queries"];
Expand Down
5 changes: 5 additions & 0 deletions packages/shared.test/changelog/@unreleased/pr-23.v2.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
type: fix
fix:
description: action params type fix
links:
- https://github.com/palantir/osdk-ts/pull/23
24 changes: 24 additions & 0 deletions packages/shared.test/src/mock-ontology/mockOntology.ts
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,30 @@ export const MockOntology = {
},
},
},
queryWithOnlyOptionalArgs: {
apiName: "queryWithOnlyOptionalArgs",
description: "a query that only has optional args",
version: "version",
output: { type: "boolean" },
parameters: {
string: {
type: "string",
nullable: true,
},
},
},
queryWithOnlyRequiredArgs: {
apiName: "queryWithOnlyRequiredArgs",
description: "a query that only has required args",
version: "version",
output: { type: "boolean" },
parameters: {
string: {
type: "string",
nullable: false,
},
},
},
},
} satisfies OntologyDefinition<
| "Task"
Expand Down

0 comments on commit 0b9302d

Please sign in to comment.