From 0ecd42b9156dad9f8b81e17a8b0319d63f4378e1 Mon Sep 17 00:00:00 2001 From: Eric Anderson Date: Wed, 29 May 2024 08:52:56 -0400 Subject: [PATCH] Generate ontology in ts 5.5-beta safe way (#355) * Generate ontology in ts 5.5-beta safe way * Add changeset --- .changeset/plenty-peaches-scream.md | 5 ++ .../sdk/src/generatedNoCheck/Ontology.ts | 52 +++++++++++++------ .../src/generatedNoCheck/Ontology.ts | 31 ++++++++--- .../todoapp/src/generatedNoCheck2/Ontology.ts | 23 +++++--- .../generateClientSdkVersionTwoPointZero.ts | 37 +++++++++++-- 5 files changed, 116 insertions(+), 32 deletions(-) create mode 100644 .changeset/plenty-peaches-scream.md diff --git a/.changeset/plenty-peaches-scream.md b/.changeset/plenty-peaches-scream.md new file mode 100644 index 000000000..5c31e6b24 --- /dev/null +++ b/.changeset/plenty-peaches-scream.md @@ -0,0 +1,5 @@ +--- +"@osdk/generator": minor +--- + +Generates 2.0's Ontology.ts in a typescript 5.5.0-beta safe way diff --git a/examples-extra/basic/sdk/src/generatedNoCheck/Ontology.ts b/examples-extra/basic/sdk/src/generatedNoCheck/Ontology.ts index e8efe849f..0c031324a 100644 --- a/examples-extra/basic/sdk/src/generatedNoCheck/Ontology.ts +++ b/examples-extra/basic/sdk/src/generatedNoCheck/Ontology.ts @@ -4,7 +4,42 @@ import * as Actions from './ontology/actions/index.js'; import * as Interfaces from './ontology/interfaces.js'; import * as Objects from './ontology/objects.js'; -const _Ontology = { +export interface Ontology + extends OntologyDefinition< + | 'BoundariesUsState' + | 'BuilderDeploymentState' + | 'Employee' + | 'ObjectTypeWithAllPropertyTypes' + | 'Person' + | 'Todo' + | 'Venture' + | 'WeatherStation' + > { + metadata: OntologyMetadata; + objects: { + BoundariesUsState: Objects.BoundariesUsState; + BuilderDeploymentState: Objects.BuilderDeploymentState; + Employee: Objects.Employee; + ObjectTypeWithAllPropertyTypes: Objects.ObjectTypeWithAllPropertyTypes; + Person: Objects.Person; + Todo: Objects.Todo; + Venture: Objects.Venture; + WeatherStation: Objects.WeatherStation; + }; + actions: { + actionTakesAllParameterTypes: typeof Actions.actionTakesAllParameterTypes; + assignEmployee1: typeof Actions.assignEmployee1; + createTodo: typeof Actions.createTodo; + }; + queries: { + // TODO + }; + interfaces: { + FooInterface: Interfaces.FooInterface; + }; +} + +export const Ontology: Ontology = { metadata: OntologyMetadata, objects: { BoundariesUsState: Objects.BoundariesUsState, @@ -27,17 +62,4 @@ const _Ontology = { interfaces: { FooInterface: Interfaces.FooInterface, }, -} satisfies OntologyDefinition< - | 'BoundariesUsState' - | 'BuilderDeploymentState' - | 'Employee' - | 'ObjectTypeWithAllPropertyTypes' - | 'Person' - | 'Todo' - | 'Venture' - | 'WeatherStation' ->; - -type _Ontology = typeof _Ontology; -export interface Ontology extends _Ontology {} -export const Ontology = _Ontology as Ontology; +}; diff --git a/examples-extra/docs_example/src/generatedNoCheck/Ontology.ts b/examples-extra/docs_example/src/generatedNoCheck/Ontology.ts index df2e63520..8f2706dc4 100644 --- a/examples-extra/docs_example/src/generatedNoCheck/Ontology.ts +++ b/examples-extra/docs_example/src/generatedNoCheck/Ontology.ts @@ -3,7 +3,30 @@ import { OntologyMetadata } from './OntologyMetadata'; import * as Actions from './ontology/actions/index'; import * as Objects from './ontology/objects'; -const _Ontology = { +export interface Ontology extends OntologyDefinition<'Employee' | 'equipment' | 'Office' | 'Todo'> { + metadata: OntologyMetadata; + objects: { + Employee: Objects.Employee; + equipment: Objects.equipment; + Office: Objects.Office; + Todo: Objects.Todo; + }; + actions: { + completeTodo: typeof Actions.completeTodo; + createOffice: typeof Actions.createOffice; + createOfficeAndEmployee: typeof Actions.createOfficeAndEmployee; + createTodo: typeof Actions.createTodo; + moveOffice: typeof Actions.moveOffice; + promoteEmployee: typeof Actions.promoteEmployee; + promoteEmployeeObject: typeof Actions.promoteEmployeeObject; + }; + queries: { + // TODO + }; + interfaces: {}; +} + +export const Ontology: Ontology = { metadata: OntologyMetadata, objects: { Employee: Objects.Employee, @@ -24,8 +47,4 @@ const _Ontology = { // TODO }, interfaces: {}, -} satisfies OntologyDefinition<'Employee' | 'equipment' | 'Office' | 'Todo'>; - -type _Ontology = typeof _Ontology; -export interface Ontology extends _Ontology {} -export const Ontology = _Ontology as Ontology; +}; diff --git a/examples-extra/todoapp/src/generatedNoCheck2/Ontology.ts b/examples-extra/todoapp/src/generatedNoCheck2/Ontology.ts index a8074c08e..49852e4f9 100644 --- a/examples-extra/todoapp/src/generatedNoCheck2/Ontology.ts +++ b/examples-extra/todoapp/src/generatedNoCheck2/Ontology.ts @@ -3,7 +3,22 @@ import { OntologyMetadata } from './OntologyMetadata'; import * as Actions from './ontology/actions/index'; import * as Objects from './ontology/objects'; -const _Ontology = { +export interface Ontology extends OntologyDefinition<'Todo'> { + metadata: OntologyMetadata; + objects: { + Todo: Objects.Todo; + }; + actions: { + completeTodo: typeof Actions.completeTodo; + createTodo: typeof Actions.createTodo; + }; + queries: { + // TODO + }; + interfaces: {}; +} + +export const Ontology: Ontology = { metadata: OntologyMetadata, objects: { Todo: Objects.Todo, @@ -16,8 +31,4 @@ const _Ontology = { // TODO }, interfaces: {}, -} satisfies OntologyDefinition<'Todo'>; - -type _Ontology = typeof _Ontology; -export interface Ontology extends _Ontology {} -export const Ontology = _Ontology as Ontology; +}; diff --git a/packages/generator/src/v2.0/generateClientSdkVersionTwoPointZero.ts b/packages/generator/src/v2.0/generateClientSdkVersionTwoPointZero.ts index a190630ea..f607edf79 100644 --- a/packages/generator/src/v2.0/generateClientSdkVersionTwoPointZero.ts +++ b/packages/generator/src/v2.0/generateClientSdkVersionTwoPointZero.ts @@ -78,7 +78,37 @@ export async function generateClientSdkVersionTwoPointZero( import * as Interfaces from "./ontology/interfaces${importExt}"; import { OntologyMetadata } from "./OntologyMetadata${importExt}"; - const _Ontology = { + export interface Ontology extends OntologyDefinition<${ + stringUnionFrom(objectNames) + }> { + metadata: OntologyMetadata, + objects: { + ${ + objectNames.map((objectName) => { + return `${objectName}: Objects.${objectName}`; + }).join(",\n") + } + }, + actions: { + ${ + actionNames.map((actionName) => { + return `${actionName}: typeof Actions.${actionName}`; + }).join(",\n") + } + }, + queries: { + // TODO + }, + interfaces: { + ${ + interfaceNames.map((objectName) => { + return `${objectName}: Interfaces.${objectName}`; + }).join(",\n") + } + } + } + + export const Ontology: Ontology = { metadata: OntologyMetadata, objects: { ${ @@ -106,11 +136,8 @@ export async function generateClientSdkVersionTwoPointZero( } } - } satisfies OntologyDefinition<${stringUnionFrom(objectNames)}>; + }; - type _Ontology = typeof _Ontology; - export interface Ontology extends _Ontology {} - export const Ontology = _Ontology as Ontology; `, ), );