Skip to content

Commit

Permalink
Merge pull request #142 from palantir/ssanjay/deprecateapi
Browse files Browse the repository at this point in the history
Deprecate __apiName, __rid, and __primaryKey
  • Loading branch information
ssanjay1 authored Mar 26, 2024
2 parents 8da7dd8 + 6d04685 commit c22cce1
Show file tree
Hide file tree
Showing 17 changed files with 120 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,12 @@ import type {
* A type with all property types
*/
export interface ObjectTypeWithAllPropertyTypes extends OntologyObject {
/** @deprecated please migrate to $apiName instead */
readonly __apiName: 'ObjectTypeWithAllPropertyTypes';
/** @deprecated please migrate to $primaryKey instead */
readonly __primaryKey: number;
readonly $apiName: 'ObjectTypeWithAllPropertyTypes';
readonly $primaryKey: number;
readonly attachment: Attachment | undefined;
readonly attachmentArray: Attachment[] | undefined;
readonly boolean: boolean | undefined;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,12 @@ import type { Todo } from './Todo';
* A person
*/
export interface Person extends OntologyObject {
/** @deprecated please migrate to $apiName instead */
readonly __apiName: 'Person';
/** @deprecated please migrate to $primaryKey instead */
readonly __primaryKey: string;
readonly $apiName: 'Person';
readonly $primaryKey: string;
readonly email: string | undefined;
readonly Todos: MultiLink<Todo>;
readonly Friends: MultiLink<Person>;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,12 @@ import type { Person } from './Person';
* Its a todo item.
*/
export interface Todo extends OntologyObject {
/** @deprecated please migrate to $apiName instead */
readonly __apiName: 'Todo';
/** @deprecated please migrate to $primaryKey instead */
readonly __primaryKey: number;
readonly $apiName: 'Todo';
readonly $primaryKey: number;
/**
* The text of the todo
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,12 @@ import type { OntologyObject } from '@osdk/legacy-client';
* Its a todo item.
*/
export interface Todo extends OntologyObject {
/** @deprecated please migrate to $apiName instead */
readonly __apiName: 'Todo';
/** @deprecated please migrate to $primaryKey instead */
readonly __primaryKey: string;
readonly $apiName: 'Todo';
readonly $primaryKey: string;
readonly id: string | undefined;
readonly isComplete: boolean | undefined;
/**
Expand Down
48 changes: 25 additions & 23 deletions examples-extra/todoapp/src/useTodos.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { ActionValidationError } from "@osdk/client";
import { useCallback, useEffect } from "react";
import useSWR from "swr";
import { foundryClient, foundryClient2 } from "./foundryClient";
import { isOk, ReturnEditsMode, type Result } from "./generatedNoCheck";
import { isOk, type Result, ReturnEditsMode } from "./generatedNoCheck";
import type { Todo } from "./generatedNoCheck/ontology/objects";
import { ActionValidationError } from "@osdk/client";
import * as MyOsdk from "./generatedNoCheck2";

function orThrow<T, E>(result: Result<T, E>) {
Expand All @@ -18,7 +18,7 @@ export function useTodos() {
const { data, isLoading, error, isValidating, mutate } = useSWR(
"/todos",
async () => orThrow(await foundryClient.ontology.objects.Todo.all()),
{ keepPreviousData: true, revalidateOnFocus: false }
{ keepPreviousData: true, revalidateOnFocus: false },
);

useEffect(() => {
Expand All @@ -36,15 +36,15 @@ export function useTodos() {
} else {
byApiNameByPK.set(
object.$apiName,
new Map([[object.$primaryKey, object]])
new Map([[object.$primaryKey, object]]),
);
}
}

// get the new version of an object that has changed, removing it from the list of updates
const getUpdate = (
apiName: (typeof objects)[0]["$apiName"],
primaryKey: (typeof objects)[0]["$primaryKey"]
primaryKey: (typeof objects)[0]["$primaryKey"],
) => {
const byPk = byApiNameByPK.get(apiName);
if (byPk) {
Expand All @@ -58,14 +58,13 @@ export function useTodos() {

mutate((data) => {
// update any Todos that we got a new version for
const updated =
data?.map((object) => {
const updateObject = getUpdate(
object.__apiName,
object.__primaryKey
);
return updateObject ?? object;
}) ?? [];
const updated = data?.map((object) => {
const updateObject = getUpdate(
object.__apiName,
object.__primaryKey,
);
return updateObject ?? object;
}) ?? [];

// add any new Todos to the bottom
for (const byPk of byApiNameByPK.values()) {
Expand All @@ -91,7 +90,7 @@ export function useTodos() {
}, [mutate]);

const toggleComplete = useCallback(
async function (todo: Todo) {
async function(todo: Todo) {
const b = !todo.isComplete;
await mutate(
async () => {
Expand All @@ -111,8 +110,8 @@ export function useTodos() {
is_complete: b,
Todo: todo,
},
{ returnEdits: ReturnEditsMode.ALL }
)
{ returnEdits: ReturnEditsMode.ALL },
),
);
}

Expand All @@ -121,10 +120,10 @@ export function useTodos() {
{
optimisticData: updateTodo.bind(undefined, todo.id!, b),
rollbackOnError: true,
}
},
);
},
[mutate]
[mutate],
);

const createTodoMutator = useCallback(
Expand All @@ -151,7 +150,7 @@ export function useTodos() {
if (constraint.type === "stringLength") {
if (constraint.gte != null && constraint.lte != null) {
setError?.(
`Todo must be between ${constraint.gte}-${constraint.lte} characters`
`Todo must be between ${constraint.gte}-${constraint.lte} characters`,
);
}
}
Expand All @@ -169,11 +168,11 @@ export function useTodos() {
optimisticData: (todos = []) => [...todos, createFauxTodo(title)],
rollbackOnError: true,
throwOnError: true,
}
},
);
return undefined;
},
[mutate]
[mutate],
);

return {
Expand All @@ -194,21 +193,24 @@ function createFauxTodo(title: string): Todo {
__primaryKey: title,
__apiName: "Todo",
__rid: "",
$primaryKey: title,
$apiName: "Todo",
$rid: "",
};
}

function updateTodo(
id: string,
isComplete: boolean,
todos: Todo[] | undefined
todos: Todo[] | undefined,
) {
return updateOne(todos, id, (todo) => ({ ...todo, isComplete })) ?? [];
}

function updateOne<T extends { __primaryKey: Q }, Q>(
things: T[] | undefined,
primaryKey: Q,
update: (thing: T) => T
update: (thing: T) => T,
) {
return things?.map((thing) => {
if (thing.__primaryKey === primaryKey) {
Expand Down
5 changes: 5 additions & 0 deletions packages/generator/changelog/@unreleased/pr-142.v2.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
type: deprecation
deprecation:
description: Deprecate __apiName, __rid, and __primaryKey
links:
- https://github.com/palantir/osdk-ts/pull/142
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,12 @@ describe("wireObjectTypeV2ToObjectInterfaceStringV1", () => {
* Its a todo item.
*/
export interface Todo extends OntologyObject {
/** @deprecated please migrate to \$apiName instead */
readonly __apiName: 'Todo';
/** @deprecated please migrate to \$primaryKey instead */
readonly __primaryKey: number;
readonly $apiName: 'Todo';
readonly $primaryKey: number;
/**
* The text of the todo
*/
Expand Down Expand Up @@ -78,8 +82,12 @@ describe("wireObjectTypeV2ToObjectInterfaceStringV1", () => {
"import type { OntologyObject, SingleLink } from '@osdk/legacy-client';
export interface Todo extends OntologyObject {
/** @deprecated please migrate to \$apiName instead */
readonly __apiName: 'Todo';
/** @deprecated please migrate to \$primaryKey instead */
readonly __primaryKey: number;
readonly $apiName: 'Todo';
readonly $primaryKey: number;
readonly break: number | undefined;
/** @deprecated please migrate to 'break' instead */
readonly break_: number | undefined;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,22 @@ ${
${getDescriptionIfPresent(objectTypeWithLinks.objectType.description)}
export interface ${objectTypeWithLinks.objectType.apiName} extends OntologyObject {
/** \@deprecated please migrate to \$apiName instead */
readonly __apiName: "${objectTypeWithLinks.objectType.apiName}";
/** \@deprecated please migrate to \$primaryKey instead */
readonly __primaryKey: ${
wirePropertyTypeV2ToTypeScriptType(
objectTypeWithLinks.objectType
.properties[objectTypeWithLinks.objectType.primaryKey].dataType,
)
};
readonly \$apiName: "${objectTypeWithLinks.objectType.apiName}";
readonly \$primaryKey: ${
wirePropertyTypeV2ToTypeScriptType(
objectTypeWithLinks.objectType
.properties[objectTypeWithLinks.objectType.primaryKey].dataType,
)
};
${
Object.entries(objectTypeWithLinks.objectType.properties).sort((a, b) =>
a[0].localeCompare(b[0])
Expand Down
5 changes: 5 additions & 0 deletions packages/legacy-client/changelog/@unreleased/pr-142.v2.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
type: deprecation
deprecation:
description: Deprecate __apiName, __rid, and __primaryKey
links:
- https://github.com/palantir/osdk-ts/pull/142
11 changes: 9 additions & 2 deletions packages/legacy-client/src/client/baseTypes/OntologyObject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,19 @@ export interface OntologyObject<
T extends string = string,
P extends NonNullable<ParameterValue> = NonNullable<ParameterValue>,
> {
/** @deprecated use $rid */
__rid: string;
/** @deprecated use $apiName */
__apiName: T;
/** @deprecated use $primaryKey */
__primaryKey: P;
$rid: string;
$apiName: T;
$primaryKey: P;
}

export function isOntologyObject(obj: any): obj is OntologyObject {
return obj && typeof obj === "object" && typeof obj.__apiName === "string"
&& "__primaryKey" in obj;
return obj && typeof obj === "object" && ((typeof obj.__apiName === "string"
&& "__primaryKey" in obj) || (typeof obj.$apiName === "string"
&& "$primaryKey" in obj));
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,12 @@ export type FilteredPropertiesTerminalOperations<
> = {
all(): Promise<
Result<
Array<Pick<T, V[number] | "__apiName" | "__primaryKey">>,
Array<
Pick<
T,
V[number] | "$apiName" | "$primaryKey" | "__apiName" | "__primaryKey"
>
>,
LoadObjectSetError
>
>;
Expand All @@ -34,7 +39,12 @@ export type FilteredPropertiesTerminalOperations<
pageToken?: string;
}): Promise<
Result<
Page<Pick<T, V[number] | "__apiName" | "__primaryKey">>,
Page<
Pick<
T,
V[number] | "$apiName" | "$primaryKey" | "__apiName" | "__primaryKey"
>
>,
LoadObjectSetError
>
>;
Expand All @@ -45,8 +55,14 @@ export type FilteredPropertiesTerminalOperationsWithGet<
V extends Array<keyof T>,
> = FilteredPropertiesTerminalOperations<T, V> & {
get(
primaryKey: T["__primaryKey"],
primaryKey: T["$primaryKey"],
): Promise<
Result<Pick<T, V[number] | "__apiName" | "__primaryKey">, GetObjectError>
Result<
Pick<
T,
V[number] | "$apiName" | "$primaryKey" | "__apiName" | "__primaryKey"
>,
GetObjectError
>
>;
};
4 changes: 2 additions & 2 deletions packages/legacy-client/src/client/interfaces/baseObjectSet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,13 @@ export type BaseObjectSet<O extends OntologyObject> =
& ObjectSet<O>;

export type BaseObjectSetOperations<O extends OntologyObject> = {
apiName: O["__apiName"];
apiName: O["$apiName"];

description: string;

properties: Properties<O>;

get(primaryKey: O["__primaryKey"]): Promise<Result<O, GetObjectError>>;
get(primaryKey: O["$primaryKey"]): Promise<Result<O, GetObjectError>>;

select<T extends keyof SelectableProperties<O>>(
properties: readonly T[],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,17 @@
import type { IsLink } from "./IsLink";

export declare type OmitMetadataProperties<T> = {
[K in keyof Omit<T, "__apiName" | "__rid" | "__primaryKey">]: T[K];
[
K in keyof Omit<
T,
| "__apiName"
| "__rid"
| "__primaryKey"
| "$apiName"
| "$rid"
| "$primaryKey"
>
]: T[K];
};

export declare type OmitLinksProperties<T> = {
Expand Down
6 changes: 3 additions & 3 deletions packages/legacy-client/src/client/net/getObject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ import { wrapResult } from "./util/wrapResult";
import type { WireOntologyObjectV2 } from "./WireOntologyObjectV2";

export async function getObject<T extends OntologyObject>(
client: ClientContext<OntologyDefinition<T["__apiName"]>>,
client: ClientContext<OntologyDefinition<T["$apiName"]>>,
objectApiName: string,
primaryKey: T["__primaryKey"],
primaryKey: T["$primaryKey"],
selectedProperties: ReadonlyArray<keyof T> = [],
): Promise<Result<T, GetObjectError>> {
return wrapResult(async () => {
Expand All @@ -41,7 +41,7 @@ export async function getObject<T extends OntologyObject>(
{
select: selectedProperties.map(x => x.toString()),
},
) as WireOntologyObjectV2<T["__apiName"]>;
) as WireOntologyObjectV2<T["$apiName"]>;

return convertWireToOsdkObject(
client,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ export function createBaseOsdkObjectSet<
};

const objectSet: BaseObjectSetOperations<OsdkLegacyObjectFrom<O, K>> = {
apiName: apiName as string as OsdkLegacyObjectFrom<O, K>["__apiName"],
apiName: apiName as string as OsdkLegacyObjectFrom<O, K>["$apiName"],

description: client.ontology.objects[apiName].description ?? "",

Expand Down
3 changes: 3 additions & 0 deletions packages/legacy-client/src/util/test/TaskObject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ import type { Todo } from "./TodoObject";
export interface Task extends OntologyObject {
readonly __apiName: "Task";
readonly __primaryKey: number;
readonly $apiName: "Task";
readonly $primaryKey: number;
readonly id: number | undefined;
readonly $rid: string;
readonly linkedTodos: MultiLink<Todo>;
}
Loading

0 comments on commit c22cce1

Please sign in to comment.