Skip to content

Commit

Permalink
Check client env variables at runtime (#71)
Browse files Browse the repository at this point in the history
  • Loading branch information
tzyl authored Feb 15, 2024
1 parent 6702612 commit 6730932
Show file tree
Hide file tree
Showing 9 changed files with 132 additions and 29 deletions.
5 changes: 5 additions & 0 deletions packages/create-app/changelog/@unreleased/pr-71.v2.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
type: improvement
improvement:
description: Check client env variables at runtime
links:
- https://github.com/palantir/osdk-ts/pull/71
Original file line number Diff line number Diff line change
@@ -1,14 +1,30 @@
import { FoundryClient, PublicClientAuth } from "@fake/sdk";

const url = process.env.NEXT_PUBLIC_FOUNDRY_API_URL;
const clientId = process.env.NEXT_PUBLIC_FOUNDRY_CLIENT_ID;
const redirectUrl = process.env.NEXT_PUBLIC_FOUNDRY_REDIRECT_URL;
checkEnv(url, "NEXT_PUBLIC_FOUNDRY_API_URL");
checkEnv(clientId, "NEXT_PUBLIC_FOUNDRY_CLIENT_ID");
checkEnv(redirectUrl, "NEXT_PUBLIC_FOUNDRY_REDIRECT_URL");

function checkEnv(
value: string | undefined,
name: string,
): asserts value is string {
if (value == null) {
throw new Error(`Missing environment variable: ${name}`);
}
}

/**
* Initialize the client to interact with the Ontology SDK
*/
const client = new FoundryClient({
url: process.env.NEXT_PUBLIC_FOUNDRY_API_URL!,
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!,
clientId,
url,
redirectUrl,
}),
});

Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,30 @@
import { FoundryClient, PublicClientAuth } from "@fake/sdk";

const url = import.meta.env.VITE_FOUNDRY_API_URL;
const clientId = import.meta.env.VITE_FOUNDRY_CLIENT_ID;
const redirectUrl = import.meta.env.VITE_FOUNDRY_REDIRECT_URL;
checkEnv(url, "VITE_FOUNDRY_API_URL");
checkEnv(clientId, "VITE_FOUNDRY_CLIENT_ID");
checkEnv(redirectUrl, "VITE_FOUNDRY_REDIRECT_URL");

function checkEnv(
value: string | undefined,
name: string,
): asserts value is string {
if (value == null) {
throw new Error(`Missing environment variable: ${name}`);
}
}

/**
* Initialize the client to interact with the Ontology SDK
*/
const client = new FoundryClient({
url: import.meta.env.VITE_FOUNDRY_API_URL!,
url,
auth: new PublicClientAuth({
clientId: import.meta.env.VITE_FOUNDRY_CLIENT_ID!,
url: import.meta.env.VITE_FOUNDRY_API_URL!,
redirectUrl: import.meta.env.VITE_FOUNDRY_REDIRECT_URL!,
clientId,
url,
redirectUrl,
}),
});

Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,30 @@
import { FoundryClient, PublicClientAuth } from "@fake/sdk";

const url = import.meta.env.VITE_FOUNDRY_API_URL;
const clientId = import.meta.env.VITE_FOUNDRY_CLIENT_ID;
const redirectUrl = import.meta.env.VITE_FOUNDRY_REDIRECT_URL;
checkEnv(url, "VITE_FOUNDRY_API_URL");
checkEnv(clientId, "VITE_FOUNDRY_CLIENT_ID");
checkEnv(redirectUrl, "VITE_FOUNDRY_REDIRECT_URL");

function checkEnv(
value: string | undefined,
name: string,
): asserts value is string {
if (value == null) {
throw new Error(`Missing environment variable: ${name}`);
}
}

/**
* Initialize the client to interact with the Ontology SDK
*/
const client = new FoundryClient({
url: import.meta.env.VITE_FOUNDRY_API_URL!,
url,
auth: new PublicClientAuth({
clientId: import.meta.env.VITE_FOUNDRY_CLIENT_ID!,
url: import.meta.env.VITE_FOUNDRY_API_URL!,
redirectUrl: import.meta.env.VITE_FOUNDRY_REDIRECT_URL!,
clientId,
url,
redirectUrl,
}),
});

Expand Down
2 changes: 1 addition & 1 deletion packages/create-app/src/generate/generateEnv.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ PUBLIC_FOUNDRY_CLIENT_ID=123

const expectedEnvProductionNoAppUrl = `
PUBLIC_FOUNDRY_API_URL=https://example.palantirfoundry.com
PUBLIC_FOUNDRY_REDIRECT_URL=<Fill in the domain at which you deploy your application>/auth/callback
# PUBLIC_FOUNDRY_REDIRECT_URL=<Fill in the domain at which you deploy your application>/auth/callback
PUBLIC_FOUNDRY_CLIENT_ID=123
`.trimStart();

Expand Down
10 changes: 6 additions & 4 deletions packages/create-app/src/generate/generateEnv.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,7 @@ export function generateEnvProduction({
return generateEnv({
envPrefix,
foundryUrl,
applicationUrl: applicationUrl
?? "<Fill in the domain at which you deploy your application>",
applicationUrl,
clientId,
});
}
Expand All @@ -59,10 +58,13 @@ function generateEnv({
}: {
envPrefix: string;
foundryUrl: string;
applicationUrl: string;
applicationUrl: string | undefined;
clientId: string;
}): string {
return `${envPrefix}FOUNDRY_API_URL=${foundryUrl}\n`
+ `${envPrefix}FOUNDRY_REDIRECT_URL=${applicationUrl}/auth/callback\n`
+ `${applicationUrl == null ? "# " : ""}${envPrefix}FOUNDRY_REDIRECT_URL=${
applicationUrl
?? "<Fill in the domain at which you deploy your application>"
}/auth/callback\n`
+ `${envPrefix}FOUNDRY_CLIENT_ID=${clientId}\n`;
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,30 @@
import { FoundryClient, PublicClientAuth } from "{{osdkPackage}}";

const url = process.env.NEXT_PUBLIC_FOUNDRY_API_URL;
const clientId = process.env.NEXT_PUBLIC_FOUNDRY_CLIENT_ID;
const redirectUrl = process.env.NEXT_PUBLIC_FOUNDRY_REDIRECT_URL;
checkEnv(url, "NEXT_PUBLIC_FOUNDRY_API_URL");
checkEnv(clientId, "NEXT_PUBLIC_FOUNDRY_CLIENT_ID");
checkEnv(redirectUrl, "NEXT_PUBLIC_FOUNDRY_REDIRECT_URL");

function checkEnv(
value: string | undefined,
name: string,
): asserts value is string {
if (value == null) {
throw new Error(`Missing environment variable: ${name}`);
}
}

/**
* Initialize the client to interact with the Ontology SDK
*/
const client = new FoundryClient({
url: process.env.NEXT_PUBLIC_FOUNDRY_API_URL!,
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!,
clientId,
url,
redirectUrl,
}),
});

Expand Down
24 changes: 20 additions & 4 deletions packages/create-app/templates/template-react/src/client.ts.hbs
Original file line number Diff line number Diff line change
@@ -1,14 +1,30 @@
import { FoundryClient, PublicClientAuth } from "{{osdkPackage}}";

const url = import.meta.env.VITE_FOUNDRY_API_URL;
const clientId = import.meta.env.VITE_FOUNDRY_CLIENT_ID;
const redirectUrl = import.meta.env.VITE_FOUNDRY_REDIRECT_URL;
checkEnv(url, "VITE_FOUNDRY_API_URL");
checkEnv(clientId, "VITE_FOUNDRY_CLIENT_ID");
checkEnv(redirectUrl, "VITE_FOUNDRY_REDIRECT_URL");

function checkEnv(
value: string | undefined,
name: string,
): asserts value is string {
if (value == null) {
throw new Error(`Missing environment variable: ${name}`);
}
}

/**
* Initialize the client to interact with the Ontology SDK
*/
const client = new FoundryClient({
url: import.meta.env.VITE_FOUNDRY_API_URL!,
url,
auth: new PublicClientAuth({
clientId: import.meta.env.VITE_FOUNDRY_CLIENT_ID!,
url: import.meta.env.VITE_FOUNDRY_API_URL!,
redirectUrl: import.meta.env.VITE_FOUNDRY_REDIRECT_URL!,
clientId,
url,
redirectUrl,
}),
});

Expand Down
24 changes: 20 additions & 4 deletions packages/create-app/templates/template-vue/src/client.ts.hbs
Original file line number Diff line number Diff line change
@@ -1,14 +1,30 @@
import { FoundryClient, PublicClientAuth } from "{{osdkPackage}}";

const url = import.meta.env.VITE_FOUNDRY_API_URL;
const clientId = import.meta.env.VITE_FOUNDRY_CLIENT_ID;
const redirectUrl = import.meta.env.VITE_FOUNDRY_REDIRECT_URL;
checkEnv(url, "VITE_FOUNDRY_API_URL");
checkEnv(clientId, "VITE_FOUNDRY_CLIENT_ID");
checkEnv(redirectUrl, "VITE_FOUNDRY_REDIRECT_URL");

function checkEnv(
value: string | undefined,
name: string,
): asserts value is string {
if (value == null) {
throw new Error(`Missing environment variable: ${name}`);
}
}

/**
* Initialize the client to interact with the Ontology SDK
*/
const client = new FoundryClient({
url: import.meta.env.VITE_FOUNDRY_API_URL!,
url,
auth: new PublicClientAuth({
clientId: import.meta.env.VITE_FOUNDRY_CLIENT_ID!,
url: import.meta.env.VITE_FOUNDRY_API_URL!,
redirectUrl: import.meta.env.VITE_FOUNDRY_REDIRECT_URL!,
clientId,
url,
redirectUrl,
}),
});

Expand Down

0 comments on commit 6730932

Please sign in to comment.