Skip to content

Commit

Permalink
Add interface support for foundry-sdk-generator (#484)
Browse files Browse the repository at this point in the history
* Speed up build, fix dts generation for all, primitive --beta

* Add empty changeset

* more beta work, packagejson needs revision

* package json work

* small refactor

* add changesets

* spell check fix

---------

Co-authored-by: Eric Anderson <[email protected]>
  • Loading branch information
ssanjay1 and ericanderson authored Jul 18, 2024
1 parent 02c34a9 commit fee51a3
Show file tree
Hide file tree
Showing 9 changed files with 106 additions and 29 deletions.
5 changes: 5 additions & 0 deletions .changeset/funny-days-sneeze.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@osdk/foundry-sdk-generator": minor
---

Add interface support
1 change: 1 addition & 0 deletions packages/foundry-sdk-generator/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
"dependencies": {
"@osdk/api": "workspace:~",
"@osdk/client": "workspace:~",
"@osdk/client.api": "workspace:~",
"@osdk/gateway": "workspace:~",
"@osdk/generator": "workspace:~",
"@osdk/legacy-client": "workspace:~",
Expand Down
6 changes: 6 additions & 0 deletions packages/foundry-sdk-generator/src/__e2e_tests__/testSetup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,9 @@ export async function setup() {
"twoDimensionalAggregationFunction",
"threeDimensionalAggregationFunction",
],
interfaceTypes: [
"FooInterface",
],
linkTypes: ["employee.peeps", "employee.lead", "employee.officeLink"],
palantirOnlyTest: true,
_: [],
Expand Down Expand Up @@ -95,6 +98,9 @@ export async function setup() {
"twoDimensionalAggregationFunction",
"threeDimensionalAggregationFunction",
],
interfaceTypes: [
"FooInterface",
],
beta: true,
linkTypes: ["employee.peeps", "employee.lead", "employee.officeLink"],
palantirOnlyTest: true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ export interface generatePackageCommandArgs {
actionTypes?: string[];
queryTypes?: string[];
linkTypes?: string[];
interfaceTypes?: string[];
experimentalFeatures?: string[];
packageName: string;
packageVersion: string;
Expand Down Expand Up @@ -118,6 +119,16 @@ export class GeneratePackageCommand
defaultDescription:
`By default, no arguments will not load any query type.`,
})
.options("interfaceTypes", {
array: true,
string: true,
demandOption: false,
description:
`The API Names of the interface types to generate. Example Usage: --interfaceTypes Geolocatable`,
default: undefined,
defaultDescription:
`By default, no arguments will not load any interface type.`,
})
.options("experimentalFeatures", {
array: true,
string: true,
Expand Down Expand Up @@ -164,6 +175,7 @@ export class GeneratePackageCommand
objectTypesApiNamesToLoad: transformArrayArg(args.objectTypes),
actionTypesApiNamesToLoad: transformArrayArg(args.actionTypes),
queryTypesApiNamesToLoad: transformArrayArg(args.queryTypes),
interfaceTypesApiNamesToLoad: transformArrayArg(args.interfaceTypes),
linkTypesApiNamesToLoad: transformArrayArg(args.linkTypes),
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,23 @@ import { compileInMemory } from "./compileInMemory.js";
import { generatePackageJson } from "./generatePackageJson.js";

declare const __OSDK_LEGACY_CLIENT_VERSION__: string | undefined;
declare const __OSDK_CLIENT_API_VERSION__: string | undefined;
declare const __OSDK_API_VERSION__: string | undefined;
const dependencies: { [key: string]: string | undefined } = {
"@osdk/legacy-client": typeof __OSDK_LEGACY_CLIENT_VERSION__ !== "undefined"
? __OSDK_LEGACY_CLIENT_VERSION__
: undefined,
};

const betaDependencies: { [key: string]: string | undefined } = {
"@osdk/client.api": typeof __OSDK_CLIENT_API_VERSION__ !== "undefined"
? __OSDK_CLIENT_API_VERSION__
: undefined,
"@osdk/api": typeof __OSDK_API_VERSION__ !== "undefined"
? __OSDK_API_VERSION__
: undefined,
};

export async function generatePackage(
ontology: WireOntologyDefinition,
options: {
Expand All @@ -47,12 +58,14 @@ export async function generatePackage(

const packagePath = join(options.outputDir, options.packageName);
const resolvedDependencies = await Promise.all(
Object.keys(dependencies).map(async dependency => {
return {
dependencyName: dependency,
dependencyVersion: await getDependencyVersion(dependency),
};
}),
Object.keys(options.beta ? betaDependencies : dependencies).map(
async dependency => {
return {
dependencyName: dependency,
dependencyVersion: await getDependencyVersion(dependency),
};
},
),
);
await mkdir(packagePath, { recursive: true });

Expand Down Expand Up @@ -139,6 +152,7 @@ export async function generatePackage(
packagePath,
packageVersion: options.packageVersion,
dependencies: resolvedDependencies,
beta: options.beta,
}),
]);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,32 +22,52 @@ export async function generatePackageJson(options: {
packageVersion: string;
packagePath: string;
dependencies?: Array<{ dependencyName: string; dependencyVersion: string }>;
beta: boolean;
}) {
const packageDeps = options.dependencies?.reduce((acc, value) => {
acc[value.dependencyName] = value.dependencyVersion;
return acc;
}, {} as { [dependencyName: string]: string });

// Note that any "default" conditions _must_ be last in their block otherwise it will crash at runtime
const packageJson = {
name: options.packageName,
version: options.packageVersion,
main: "./index.js",
types: "./index.d.ts",
exports: {
".": {
types: "./index.d.ts",
script: {
types: "./dist/bundle/index.d.ts",
default: "./dist/bundle/index.esm.js",
const packageJson = options.beta
? {
name: options.packageName,
version: options.packageVersion,
main: "./index.js",
types: "./index.d.ts",
exports: {
".": {
types: "./index.d.ts",
script: {
default: "./dist/bundle/index.esm.js",
},
default: "./index.js",
},
default: "./index.js",
},
"./ontology/objects": {
types: "./ontology/objects/index.d.ts",
default: "./ontology/objects/index.js",
dependencies: packageDeps,
}
: {
name: options.packageName,
version: options.packageVersion,
main: "./index.js",
types: "./index.d.ts",
exports: {
".": {
types: "./index.d.ts",
script: {
types: "./dist/bundle/index.d.ts",
default: "./dist/bundle/index.esm.js",
},
default: "./index.js",
},
"./ontology/objects": {
types: "./ontology/objects/index.d.ts",
default: "./ontology/objects/index.js",
},
},
},
dependencies: options.dependencies?.reduce((acc, value) => {
acc[value.dependencyName] = value.dependencyVersion;
return acc;
}, {} as { [dependencyName: string]: string }),
};
dependencies: packageDeps,
};

await writeFile(
join(options.packagePath, "package.json"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ export class OntologyMetadataResolver {
objectTypes: Set<string>;
queryTypes: Set<string>;
actionTypes: Set<string>;
interfaceTypes: Set<string>;
},
): components.OntologyFullMetadata {
const filteredObjectTypes = Object.fromEntries(
Expand All @@ -51,6 +52,12 @@ export class OntologyMetadataResolver {
) => expectedEntities.objectTypes.has(objectTypeApiName.toLowerCase())),
);

const filteredInterfaceTypes = Object.fromEntries(
Object.entries(ontologyFullMetadata.interfaceTypes).filter((
[interfaceApiName],
) => expectedEntities.interfaceTypes.has(interfaceApiName.toLowerCase())),
);

Object.values(filteredObjectTypes).forEach(objectType => {
const linkTypesToKeep = expectedEntities.linkTypes.get(
objectType.objectType.apiName.toLowerCase(),
Expand Down Expand Up @@ -91,7 +98,7 @@ export class OntologyMetadataResolver {
objectTypes: filteredObjectTypes,
actionTypes: filteredActionTypes,
queryTypes: filteredQueryTypes,
interfaceTypes: {},
interfaceTypes: filteredInterfaceTypes,
sharedPropertyTypes: {},
};
}
Expand All @@ -102,6 +109,7 @@ export class OntologyMetadataResolver {
actionTypesApiNamesToLoad?: string[];
objectTypesApiNamesToLoad?: string[];
queryTypesApiNamesToLoad?: string[];
interfaceTypesApiNamesToLoad?: string[];
linkTypesApiNamesToLoad?: string[];
},
): Promise<Result<WireOntologyDefinition, string[]>> {
Expand Down Expand Up @@ -147,6 +155,12 @@ export class OntologyMetadataResolver {
),
);

const interfaceTypes = new Set(
entities.interfaceTypesApiNamesToLoad?.map(interfaceName =>
interfaceName.toLowerCase()
),
);

for (const linkType of entities.linkTypesApiNamesToLoad ?? []) {
const [objectTypeApiName, linkTypeApiName] = linkType.toLowerCase().split(
".",
Expand All @@ -162,6 +176,7 @@ export class OntologyMetadataResolver {
linkTypes,
actionTypes,
queryTypes,
interfaceTypes,
});

const validData: Result<{}, string[]> = this.validateLoadedOntologyMetadata(
Expand Down
3 changes: 2 additions & 1 deletion packages/monorepo.cspell/dict.normal-dev-words.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,5 @@ typesafety
uncapitalize
unevaluable
unioned
webapi
webapi
Geolocatable
3 changes: 3 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit fee51a3

Please sign in to comment.