Skip to content

Commit

Permalink
more progress
Browse files Browse the repository at this point in the history
  • Loading branch information
ssanjay1 committed Jul 14, 2024
1 parent 383de83 commit 3f0008c
Show file tree
Hide file tree
Showing 14 changed files with 254 additions and 101 deletions.
64 changes: 58 additions & 6 deletions packages/client.api/src/OsdkObjectFrom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,10 @@ export type ConvertProps<
: TO extends ObjectTypeDefinition<any> ? (
(
UnionIfTrue<
TO["interfaceMap"][ApiNameAsString<FROM>][
NonNullable<TO["interfaceMap"]>[ApiNameAsString<FROM>][
P extends "$all" ? (
keyof FROM["properties"] extends
keyof TO["interfaceMap"][ApiNameAsString<FROM>]
NonNullable<keyof TO["interfaceMap"]>[ApiNameAsString<FROM>]
? keyof FROM["properties"]
: never
)
Expand All @@ -70,9 +70,10 @@ export type ConvertProps<
: UnionIfTrue<
TO extends InterfaceDefinition<any> ? P extends "$all" ? "$all"
: FROM extends ObjectTypeDefinition<any>
? DropDollarOptions<P> extends keyof FROM["inverseInterfaceMap"][
ApiNameAsString<TO>
] ? FROM["inverseInterfaceMap"][ApiNameAsString<TO>][
? DropDollarOptions<P> extends
keyof NonNullable<FROM["inverseInterfaceMap"]>[
ApiNameAsString<TO>
] ? NonNullable<FROM["inverseInterfaceMap"]>[ApiNameAsString<TO>][
DropDollarOptions<P>
]
: never
Expand All @@ -82,6 +83,57 @@ export type ConvertProps<
"$notStrict"
>;

// type TO = InterfaceDefinition<string, unknown>;
// type FROM = FetchedObjectTypeDefinition<any, unknown>;
// type P = string;
// type Z = ValidToFrom<FROM>;

// type huh2 = TO["apiName"]["__Unbranded"];
// type huh3 = ApiNameAsString<Z>;
// type huh22 = FROM["inverseInterfaceMap"];

// type huhtest<TO extends ObjectTypeDefinition<any>> = UnionIfTrue<
// TO["interfaceMap"][ApiNameAsString<FROM>][
// P extends "$all" ? (
// keyof FROM["properties"] extends
// keyof TO["interfaceMap"][ApiNameAsString<FROM>]
// ? keyof FROM["properties"]
// : never
// )
// : DropDollarOptions<P>
// ],
// P extends "$notStrict" ? true : false,
// "$notStrict"
// >;

// type huh = UnionIfTrue<
// TO extends InterfaceDefinition<any> ? P extends "$all" ? "$all"
// : FROM extends ObjectTypeDefinition<any>
// ? DropDollarOptions<P> extends keyof NonNullable<FROM["inverseInterfaceMap"]>[
// ApiNameAsString<Z>
// ] ? NonNullable<FROM["inverseInterfaceMap"]>[ApiNameAsString<Z>][
// DropDollarOptions<P>
// ]
// : never
// : never
// : never,
// P extends "$notStrict" ? true : false,
// "$notStrict"
// >;

export const InterfaceDefinitions = Symbol(
process.env.MODE !== "production" ? "InterfaceDefinitions" : undefined,
);
export interface FetchedObjectTypeDefinition<K extends string, N = unknown>
extends ObjectTypeDefinition<K, N>
{
rid: string;

// we keep this here so we can depend on these synchronously
[InterfaceDefinitions]: {
[key: string]: { def: InterfaceDefinition<any> };
};
}
/** DO NOT EXPORT FROM PACKAGE */
export type ValidToFrom<FROM extends ObjectOrInterfaceDefinition> = FROM extends
InterfaceDefinition<any, any>
Expand Down Expand Up @@ -139,7 +191,7 @@ export type Osdk<
/** @deprecated use $primaryKey */
__primaryKey: Q extends ObjectTypeDefinition<any>
? OsdkObjectPrimaryKeyType<Q>
: unknown;
: any;

// $uniqueId: string; // will be dynamic

Expand Down
4 changes: 4 additions & 0 deletions packages/client.api/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
* limitations under the License.
*/

import { TimeseriesDurationMapping } from "./timeseries/timeseries.js";

export type { ActionReturnTypeForOptions } from "./actions/ActionReturnTypeForOptions.js";
export type {
ActionEditResponse,
Expand Down Expand Up @@ -111,7 +113,9 @@ export type { PageResult } from "./PageResult.js";
export type {
TimeSeriesPoint,
TimeSeriesProperty,
TimeSeriesQuery,
} from "./timeseries/timeseries.js";
export { TimeseriesDurationMapping } from "./timeseries/timeseries.js";
export type { LinkedType, LinkNames } from "./util/LinkUtils.js";
export type { NOOP } from "./util/NOOP.js";

Expand Down
2 changes: 1 addition & 1 deletion packages/client.api/src/mapping/PropertyValueMapping.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export interface PropertyValueWireToClient {
string: string;
timestamp: string;

numericTimeseries: unknown;
numericTimeseries: TimeSeriesProperty<number>;
stringTimeseries: TimeSeriesProperty<string>;
}

Expand Down
11 changes: 7 additions & 4 deletions packages/client.api/src/object/FetchPageResult.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,13 @@ export type UnionIfFalse<S extends string, JUST_S_IF_TRUE extends boolean, E> =
: S | E;

/** @internal exposed for a test */
export type UnionIfTrue<S extends string, UNION_IF_TRUE extends boolean, E> =
IsNever<S> extends true ? never
: UNION_IF_TRUE extends true ? S | E
: S;
export type UnionIfTrue<
S extends string,
UNION_IF_TRUE extends boolean,
E extends string,
> = IsNever<S> extends true ? never
: UNION_IF_TRUE extends true ? S | E
: S;

export type FetchPageResult<
Q extends ObjectOrInterfaceDefinition,
Expand Down
39 changes: 34 additions & 5 deletions packages/client.api/src/timeseries/timeseries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,15 @@
* limitations under the License.
*/

export interface TimeSeriesQuery {
when: "BEFORE" | "AFTER";
value: number;
unit: TimeseriesDurationUnits;
}
export type TimeSeriesQuery = {
$before: number;
$after?: never;
$unit: keyof typeof TimeseriesDurationMapping;
} | {
$after: number;
$before?: never;
$unit: keyof typeof TimeseriesDurationMapping;
};

export type TimeseriesDurationUnits =
| "YEARS"
Expand All @@ -30,6 +34,31 @@ export type TimeseriesDurationUnits =
| "SECONDS"
| "MILLISECONDS";

export const TimeseriesDurationMapping = {
"ms": "MILLISECONDS",
"milliseconds": "MILLISECONDS",
"sec": "SECONDS",
"seconds": "SECONDS",
"min": "MINUTES",
"minute": "MINUTES",
"minutes": "MINUTES",
"hr": "HOURS",
"hrs": "HOURS",
"hour": "HOURS",
"hours": "HOURS",
"day": "DAYS",
"days": "DAYS",
"wk": "WEEKS",
"week": "WEEKS",
"weeks": "WEEKS",
"mos": "MONTHS",
"month": "MONTHS",
"months": "MONTHS",
"yr": "YEARS",
"year": "YEARS",
"years": "YEARS",
} satisfies Record<string, TimeseriesDurationUnits>;

export interface TimeSeriesPoint<T extends string | number> {
time: string;
value: T;
Expand Down
63 changes: 45 additions & 18 deletions packages/client/src/createTimeseriesProperty.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,18 @@
* limitations under the License.
*/

import type {
Attachment,
TimeSeriesPoint,
TimeSeriesProperty,
import {
type Attachment,
TimeseriesDurationMapping,
type TimeSeriesPoint,
type TimeSeriesProperty,
type TimeSeriesQuery,
} from "@osdk/client.api";
import type { StreamTimeSeriesPointsRequest } from "@osdk/internal.foundry";
import type {
RelativeTime,
RelativeTimeRange,
StreamTimeSeriesPointsRequest,
} from "@osdk/internal.foundry";
import { Ontologies, OntologiesV2 } from "@osdk/internal.foundry";
import type { MinimalClient } from "./MinimalClientContext.js";
import {
Expand All @@ -32,13 +38,12 @@ export function createTimeseriesProperty<T extends number | string>(
objectApiName: string,
primaryKey: any,
propertyName: string,
body: StreamTimeSeriesPointsRequest,
): TimeSeriesProperty<T> {
return {
async getFirstPoint() {
return OntologiesV2.OntologyObjectsV2.getFirstPoint(
client,
client.ontologyRid,
await client.ontologyRid,
objectApiName,
primaryKey,
propertyName,
Expand All @@ -47,29 +52,29 @@ export function createTimeseriesProperty<T extends number | string>(
async getLastPoint() {
return OntologiesV2.OntologyObjectsV2.getLastPoint(
client,
client.ontologyRid,
await client.ontologyRid,
objectApiName,
primaryKey,
propertyName,
) as Promise<TimeSeriesPoint<T>>;
},
async getAllPoints() {
async getAllPoints(query: TimeSeriesQuery) {
return getAllTimeSeriesPoints(
client,
objectApiName,
primaryKey,
propertyName,
body,
query,
);
},

asyncIterPoints() {
asyncIterPoints(query: TimeSeriesQuery) {
return iterateTimeSeriesPoints(
client,
objectApiName,
primaryKey,
propertyName,
body,
query,
);
},
};
Expand All @@ -80,16 +85,27 @@ async function getAllTimeSeriesPoints<T extends string | number>(
objectApiName: string,
primaryKey: any,
propertyName: string,
body: StreamTimeSeriesPointsRequest,
body: TimeSeriesQuery,
): Promise<Array<TimeSeriesPoint<T>>> {
const relativeTime: RelativeTime = body.$before
? {
when: "BEFORE",
value: body.$before,
unit: TimeseriesDurationMapping[body.$unit],
}
: {
when: "AFTER",
value: body.$after!,
unit: TimeseriesDurationMapping[body.$unit],
};
const streamPointsIterator = await OntologiesV2.OntologyObjectsV2
.streamPoints(
client,
client.ontologyRid,
await client.ontologyRid,
objectApiName,
primaryKey,
propertyName,
body,
{ range: { type: "relative", startTime: relativeTime } },
);

const allPoints: Array<TimeSeriesPoint<T>> = [];
Expand All @@ -111,16 +127,27 @@ async function* iterateTimeSeriesPoints<T extends string | number>(
objectApiName: string,
primaryKey: any,
propertyName: string,
body: StreamTimeSeriesPointsRequest,
body: TimeSeriesQuery,
): AsyncGenerator<TimeSeriesPoint<T>, any, unknown> {
const relativeTime: RelativeTime = body.$before
? {
when: "BEFORE",
value: body.$before,
unit: TimeseriesDurationMapping[body.$unit],
}
: {
when: "AFTER",
value: body.$after!,
unit: TimeseriesDurationMapping[body.$unit],
};
const streamPointsResponse = await OntologiesV2.OntologyObjectsV2
.streamPoints(
client,
client.ontologyRid,
await client.ontologyRid,
objectApiName,
primaryKey,
propertyName,
body,
{ range: { type: "relative", startTime: relativeTime } },
);

const reader = streamPointsResponse.stream().getReader();
Expand Down
4 changes: 2 additions & 2 deletions packages/client/src/object/convertWireToOsdkObjects.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ describe("convertWireToOsdkObjects", () => {
clientCtx,
[objectFromWire],
undefined,
)) as Osdk<Employee>[];
)) as unknown as Osdk<Employee>[];

expect(obj.fullName).toEqual("Steve");
expect(Object.keys(obj).sort()).toEqual([
Expand Down Expand Up @@ -227,7 +227,7 @@ describe("convertWireToOsdkObjects", () => {
clientCtx,
[objectFromWire],
FooInterface.apiName,
)) as Osdk<FooInterface>[];
)) as unknown as Osdk<FooInterface>[];

expect(objAsFoo).toMatchInlineSnapshot(`
{
Expand Down
21 changes: 19 additions & 2 deletions packages/client/src/object/convertWireToOsdkObjects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,11 @@ import type {
ObjectOrInterfaceDefinition,
ObjectTypeDefinition,
} from "@osdk/api";
import type { NullabilityAdherence, Osdk } from "@osdk/client.api";
import type {
ConvertProps,
NullabilityAdherence,
Osdk,
} from "@osdk/client.api";
import type { OntologyObjectV2 } from "@osdk/internal.foundry";
import invariant from "tiny-invariant";
import type { MinimalClient } from "../MinimalClientContext.js";
Expand All @@ -29,6 +33,12 @@ import {
} from "../ontology/OntologyProvider.js";
import { createOsdkObject } from "./convertWireToOsdkObjects/createOsdkObject.js";

type Expand<T> = T extends infer O ? { [K in keyof O]: O[K] } : never;

// expands object types recursively
type ExpandRecursively<T> = T extends object
? T extends infer O ? { [K in keyof O]: ExpandRecursively<O[K]> } : never
: T;
/**
* If interfaceApiName is not undefined, converts the instances of the
* interface into their respective
Expand Down Expand Up @@ -101,7 +111,14 @@ export async function convertWireToOsdkObjects(
} else if (strictNonNull === "drop" && !conforming) {
continue;
}

// type huh = Osdk<InterfaceDefinition<any, any>, string, never>;
// type huh2 = Osdk<FetchedObjectTypeDefinition<any, unknown>, string, never>;
// type expandedhuh = Expand<huh>;
type huhuh = ConvertProps<
FetchedObjectTypeDefinition<any, unknown>,
InterfaceDefinition<any, unknown>,
string
>;
let osdkObject = createOsdkObject(client, objectDef, rawObj);
if (interfaceApiName) osdkObject = osdkObject.$as(interfaceApiName);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,9 +123,8 @@ export function createOsdkObject<
>(
client,
objectDef.apiName,
objectDef.primaryKeyApiName,
target[RawObject][objectDef.primaryKeyApiName as string],
p as string,
undefined as any,
);
}
}
Expand Down
Loading

0 comments on commit 3f0008c

Please sign in to comment.