From c96590e0e5798096f85a0f1b0f2ab917e37089bf Mon Sep 17 00:00:00 2001 From: Tim Leung Date: Mon, 5 Feb 2024 10:00:39 +0000 Subject: [PATCH] Remove getMethods in templates (#27) --- packages/create-app/.gitignore | 3 +++ .../changelog/@unreleased/pr-27.v2.yml | 5 ++++ .../src/app/page.tsx | 7 +++--- .../src/lib/client.ts | 15 +++++++++++ .../src/lib/useAuthenticated.ts | 25 +++++++++++++++++++ .../expected-template-react/src/Home.tsx | 7 +++--- .../expected-template-react/src/getMethods.ts | 24 ------------------ .../expected-template-vue/src/Home.vue | 7 +++--- .../expected-template-vue/src/getMethods.ts | 24 ------------------ .../src/app/page.tsx.hbs | 7 +++--- .../src/lib/client.ts.hbs | 15 +++++++++++ .../src/lib/useAuthenticated.ts | 25 +++++++++++++++++++ .../templates/template-react/src/Home.tsx.hbs | 7 +++--- .../template-react/src/getMethods.ts | 24 ------------------ .../templates/template-vue/src/Home.vue.hbs | 7 +++--- .../templates/template-vue/src/getMethods.ts | 24 ------------------ 16 files changed, 106 insertions(+), 120 deletions(-) create mode 100644 packages/create-app/.gitignore create mode 100644 packages/create-app/changelog/@unreleased/pr-27.v2.yml create mode 100644 packages/create-app/src/__tests__/__snapshots__/expected-template-next-static-export/src/lib/client.ts create mode 100644 packages/create-app/src/__tests__/__snapshots__/expected-template-next-static-export/src/lib/useAuthenticated.ts delete mode 100644 packages/create-app/src/__tests__/__snapshots__/expected-template-react/src/getMethods.ts delete mode 100644 packages/create-app/src/__tests__/__snapshots__/expected-template-vue/src/getMethods.ts create mode 100644 packages/create-app/templates/template-next-static-export/src/lib/client.ts.hbs create mode 100644 packages/create-app/templates/template-next-static-export/src/lib/useAuthenticated.ts delete mode 100644 packages/create-app/templates/template-react/src/getMethods.ts delete mode 100644 packages/create-app/templates/template-vue/src/getMethods.ts diff --git a/packages/create-app/.gitignore b/packages/create-app/.gitignore new file mode 100644 index 000000000..7fb49a280 --- /dev/null +++ b/packages/create-app/.gitignore @@ -0,0 +1,3 @@ +# Override root lib/ ignore +!templates/template-next-static-export/src/lib/ +!**/__snapshots__/expected-template-next-static-export/src/lib/ diff --git a/packages/create-app/changelog/@unreleased/pr-27.v2.yml b/packages/create-app/changelog/@unreleased/pr-27.v2.yml new file mode 100644 index 000000000..4104d49c4 --- /dev/null +++ b/packages/create-app/changelog/@unreleased/pr-27.v2.yml @@ -0,0 +1,5 @@ +type: improvement +improvement: + description: Remove getMethods in templates now that generated methods are enumerable + links: + - https://github.com/palantir/osdk-ts/pull/27 diff --git a/packages/create-app/src/__tests__/__snapshots__/expected-template-next-static-export/src/app/page.tsx b/packages/create-app/src/__tests__/__snapshots__/expected-template-next-static-export/src/app/page.tsx index c60fc211d..283f3de1a 100644 --- a/packages/create-app/src/__tests__/__snapshots__/expected-template-next-static-export/src/app/page.tsx +++ b/packages/create-app/src/__tests__/__snapshots__/expected-template-next-static-export/src/app/page.tsx @@ -1,6 +1,5 @@ "use client"; import client from "@/lib/client"; -import getMethods from "@/lib/getMethods"; import useAuthenticated from "@/lib/useAuthenticated"; import css from "./page.module.css"; @@ -10,9 +9,9 @@ function Home() { return null; } - const objectApiNames = getMethods(client.ontology.objects); - const actionApiNames = getMethods(client.ontology.actions); - const queryApiNames = getMethods(client.ontology.queries); + const objectApiNames = Object.keys(client.ontology.objects); + const actionApiNames = Object.keys(client.ontology.actions); + const queryApiNames = Object.keys(client.ontology.queries); return (
diff --git a/packages/create-app/src/__tests__/__snapshots__/expected-template-next-static-export/src/lib/client.ts b/packages/create-app/src/__tests__/__snapshots__/expected-template-next-static-export/src/lib/client.ts new file mode 100644 index 000000000..c13e8ba36 --- /dev/null +++ b/packages/create-app/src/__tests__/__snapshots__/expected-template-next-static-export/src/lib/client.ts @@ -0,0 +1,15 @@ +import { FoundryClient, PublicClientAuth } from "@fake/sdk"; + +/** + * Initialize the client to interact with the Ontology SDK + */ +const client = new FoundryClient({ + url: process.env.NEXT_PUBLIC_FOUNDRY_API_URL!, + auth: new PublicClientAuth({ + clientId: process.env.NEXT_PUBLIC_FOUNDRY_CLIENT_ID!, + url: process.env.NEXT_PUBLIC_FOUNDRY_API_URL!, + redirectUrl: process.env.NEXT_PUBLIC_FOUNDRY_REDIRECT_URL!, + }), +}); + +export default client; diff --git a/packages/create-app/src/__tests__/__snapshots__/expected-template-next-static-export/src/lib/useAuthenticated.ts b/packages/create-app/src/__tests__/__snapshots__/expected-template-next-static-export/src/lib/useAuthenticated.ts new file mode 100644 index 000000000..8f182ef70 --- /dev/null +++ b/packages/create-app/src/__tests__/__snapshots__/expected-template-next-static-export/src/lib/useAuthenticated.ts @@ -0,0 +1,25 @@ +import { useRouter } from "next/navigation"; +import { useEffect, useState } from "react"; +import client from "./client"; + +function useAuthenticated() { + const router = useRouter(); + const [token, setToken] = useState(client.auth.token); + useEffect(() => { + if (client.auth.token == null || client.auth.token.isExpired) { + client.auth + .refresh() + .then(() => { + setToken(client.auth.token); + }) + .catch(() => { + // If we cannot refresh the token (i.e. the user is not logged in) we redirect to the login page + router.push("/login"); + }); + } + }, [router]); + + return token != null && !token.isExpired; +} + +export default useAuthenticated; diff --git a/packages/create-app/src/__tests__/__snapshots__/expected-template-react/src/Home.tsx b/packages/create-app/src/__tests__/__snapshots__/expected-template-react/src/Home.tsx index 2d7829e9f..2812b20a5 100644 --- a/packages/create-app/src/__tests__/__snapshots__/expected-template-react/src/Home.tsx +++ b/packages/create-app/src/__tests__/__snapshots__/expected-template-react/src/Home.tsx @@ -1,12 +1,11 @@ import client from "./client"; -import getMethods from "./getMethods"; import css from "./Home.module.css"; import Layout from "./Layout"; function Home() { - const objectApiNames = getMethods(client.ontology.objects); - const actionApiNames = getMethods(client.ontology.actions); - const queryApiNames = getMethods(client.ontology.queries); + const objectApiNames = Object.keys(client.ontology.objects); + const actionApiNames = Object.keys(client.ontology.actions); + const queryApiNames = Object.keys(client.ontology.queries); return ( diff --git a/packages/create-app/src/__tests__/__snapshots__/expected-template-react/src/getMethods.ts b/packages/create-app/src/__tests__/__snapshots__/expected-template-react/src/getMethods.ts deleted file mode 100644 index dfc6bc707..000000000 --- a/packages/create-app/src/__tests__/__snapshots__/expected-template-react/src/getMethods.ts +++ /dev/null @@ -1,24 +0,0 @@ -// Note this doesn't work with TypeScript OSDK 1.1 -// https://stackoverflow.com/a/67975040 -function getMethods(obj: object): string[] { - const isGetter = (x: object, name: string) => - Boolean((Object.getOwnPropertyDescriptor(x, name) ?? {})?.get); - const isFunction = (x: object, name: string) => - typeof (x as { [key: string]: unknown })[name] === "function"; - const deepFunctions = (x: object): string[] => - (x - && x !== Object.prototype - && Object.getOwnPropertyNames(x) - .filter((name) => isGetter(x, name) || isFunction(x, name)) - .concat(deepFunctions(Object.getPrototypeOf(x)) || [])) - || []; - - const distinctDeepFunctions = (x: object) => - Array.from(new Set(deepFunctions(x))); - - return distinctDeepFunctions(obj).filter( - (name) => name !== "constructor" && !~name.indexOf("__"), - ); -} - -export default getMethods; diff --git a/packages/create-app/src/__tests__/__snapshots__/expected-template-vue/src/Home.vue b/packages/create-app/src/__tests__/__snapshots__/expected-template-vue/src/Home.vue index 323ca4aab..1538d87ee 100644 --- a/packages/create-app/src/__tests__/__snapshots__/expected-template-vue/src/Home.vue +++ b/packages/create-app/src/__tests__/__snapshots__/expected-template-vue/src/Home.vue @@ -1,10 +1,9 @@