Skip to content

Commit

Permalink
Demo dev-console-snippets
Browse files Browse the repository at this point in the history
  • Loading branch information
ericanderson committed Oct 24, 2024
1 parent 76a85ef commit 9ef7201
Show file tree
Hide file tree
Showing 10 changed files with 265 additions and 0 deletions.
1 change: 1 addition & 0 deletions packages/dev-console-snippets/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/src/generatedNoCheck/
42 changes: 42 additions & 0 deletions packages/dev-console-snippets/codegen.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#!/usr/bin/env node
/*
* Copyright 2024 Palantir Technologies, Inc. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import Handlebars from "handlebars";
import * as fs from "node:fs";
import * as templates from "./templates.mjs";

let indexContents = "";

fs.mkdirSync("./src/generatedNoCheck", { recursive: true });

for (const c of ["loadObjectPageGuide"]) {
const h = Handlebars.compile(templates[c]);

fs.writeFileSync(
`./src/generatedNoCheck/${c}.ts`,
`import {client} from "../client.js";\n`
+ h({
objectType: "Employee",
packageName: "@osdk/e2e.generated.catchall",
titleProperty: "firstName",
}),
);

indexContents = `export const ${c} = ${JSON.stringify(templates[c])}\n\n`;
}

fs.writeFileSync(`./src/generatedNoCheck/index.ts`, indexContents);
54 changes: 54 additions & 0 deletions packages/dev-console-snippets/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
{
"name": "@osdk/dev-console-snippets",
"version": "0.0.0",
"license": "Apache-2.0",
"repository": {
"type": "git",
"url": "https://github.com/palantir/osdk-ts.git"
},
"exports": {
".": {
"browser": "./build/browser/index.js",
"import": "./build/esm/index.js"
},
"./*": {
"browser": "./build/browser/public/*.js",
"import": "./build/esm/public/*.js"
}
},
"scripts": {
"check-attw": "monorepo.tool.attw esm",
"check-spelling": "cspell --quiet .",
"clean": "rm -rf lib dist types build tsconfig.tsbuildinfo",
"codegen": "node codegen.mjs",
"fix-lint": "eslint . --fix && dprint fmt --config $(find-up dprint.json)",
"lint": "eslint . && dprint check --config $(find-up dprint.json)",
"transpile": "monorepo.tool.transpile"
},
"dependencies": {},
"devDependencies": {
"@osdk/client": "workspace:*",
"@osdk/e2e.generated.catchall": "workspace:*",
"@osdk/monorepo.api-extractor": "workspace:~",
"@osdk/monorepo.tsconfig": "workspace:~",
"@osdk/monorepo.tsup": "workspace:~",
"handlebars": "^4.7.8",
"outdent": "^0.8.0",
"typescript": "~5.5.4"
},
"publishConfig": {
"access": "public"
},
"files": [
"build/cjs",
"build/esm",
"build/browser",
"CHANGELOG.md",
"package.json",
"templates",
"*.d.ts"
],
"module": "./build/esm/index.js",
"types": "./build/esm/index.d.ts",
"type": "module"
}
18 changes: 18 additions & 0 deletions packages/dev-console-snippets/src/client.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/*
* Copyright 2024 Palantir Technologies, Inc. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import { createClient } from "@osdk/client";
export const client = createClient("", "", () => Promise.resolve(""));
17 changes: 17 additions & 0 deletions packages/dev-console-snippets/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/*
* Copyright 2024 Palantir Technologies, Inc. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

export { loadObjectPageGuide } from "./generatedNoCheck/index.js";
43 changes: 43 additions & 0 deletions packages/dev-console-snippets/templates.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* Copyright 2024 Palantir Technologies, Inc. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import { outdent } from "outdent";

export const loadObjectPageGuide = outdent`
import { {{objectType}} } from "{{{packageName}}}";
import { isOk, Osdk, PageResult, Result } from "@osdk/client";
const firstPage: Result<PageResult<Osdk.Instance<{{objectType}}>>>
= await client({{objectType}}).fetchPageWithErrors({ $pageSize: 30 });
if (isOk(firstPage)) {
const secondPage: Result<PageResult<Osdk.Instance<{{objectType}}, never, "{{titleProperty}}">>>
// You can also down select properties to only get the properties you need from the object
= await client({{objectType}}).fetchPageWithErrors({ $select: ["{{titleProperty}}"], $pageSize: 30, $nextPageToken: firstPage.value.nextPageToken });
const objects = isOk(secondPage) ? [...firstPage.value.data, ...secondPage.value.data] : firstPage.value.data;
const object = objects[0];
}
// To fetch a page without a result wrapper, use fetchPage with a try/catch instead
try {
const firstPage: PageResult<Osdk.Instance<{{objectType}}>>
= await client({{objectType}}).fetchPage({ $pageSize: 30 });
const secondPage: PageResult<Osdk.Instance<{{objectType}}>>
= await client({{objectType}}).fetchPage({ $pageSize: 30, $nextPageToken: firstPage.nextPageToken });
const objects = [...firstPage.data, ...secondPage.data];
const object = objects[0];
}
catch (e) {
console.error(e);
}
`;
11 changes: 11 additions & 0 deletions packages/dev-console-snippets/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"extends": "@osdk/monorepo.tsconfig/base.json",
"compilerOptions": {
"rootDir": "src",
"outDir": "build/esm"
},
"include": [
"./src/**/*"
],
"references": []
}
23 changes: 23 additions & 0 deletions packages/dev-console-snippets/tsup.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
* Copyright 2023 Palantir Technologies, Inc. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import { defineConfig } from "tsup";

export default defineConfig(async (options) =>
(await import("@osdk/monorepo.tsup")).default(options, {
esmOnly: true,
})
);
24 changes: 24 additions & 0 deletions packages/dev-console-snippets/vitest.config.mts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
* Copyright 2023 Palantir Technologies, Inc. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import { configDefaults, defineConfig } from "vitest/config";

export default defineConfig({
test: {
pool: "forks",
exclude: [...configDefaults.exclude, "**/build/**/*"],
},
});
32 changes: 32 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 9ef7201

Please sign in to comment.