Skip to content

Commit

Permalink
Remove getMethods in templates (#27)
Browse files Browse the repository at this point in the history
  • Loading branch information
tzyl authored Feb 5, 2024
1 parent c511447 commit c96590e
Show file tree
Hide file tree
Showing 16 changed files with 106 additions and 120 deletions.
3 changes: 3 additions & 0 deletions packages/create-app/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Override root lib/ ignore
!templates/template-next-static-export/src/lib/
!**/__snapshots__/expected-template-next-static-export/src/lib/
5 changes: 5 additions & 0 deletions packages/create-app/changelog/@unreleased/pr-27.v2.yml
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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";

Expand All @@ -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 (
<div>
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Original file line number Diff line number Diff line change
@@ -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;
Original file line number Diff line number Diff line change
@@ -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 (
<Layout>
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
<script setup lang="ts">
import client from "./client";
import getMethods from "./getMethods";
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);
</script>

<template>
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -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";

Expand All @@ -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 (
<div>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { FoundryClient, PublicClientAuth } from "{{osdkPackage}}";

/**
* 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;
Original file line number Diff line number Diff line change
@@ -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;
7 changes: 3 additions & 4 deletions packages/create-app/templates/template-react/src/Home.tsx.hbs
Original file line number Diff line number Diff line change
@@ -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 (
<Layout>
Expand Down
24 changes: 0 additions & 24 deletions packages/create-app/templates/template-react/src/getMethods.ts

This file was deleted.

7 changes: 3 additions & 4 deletions packages/create-app/templates/template-vue/src/Home.vue.hbs
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
<script setup lang="ts">
import client from "./client";
import getMethods from "./getMethods";
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);
</script>

<template>
Expand Down
24 changes: 0 additions & 24 deletions packages/create-app/templates/template-vue/src/getMethods.ts

This file was deleted.

0 comments on commit c96590e

Please sign in to comment.