Skip to content

Commit

Permalink
fix: validate typesense environment variables
Browse files Browse the repository at this point in the history
  • Loading branch information
stefanprobst authored and kevinstadler committed Oct 28, 2024
1 parent 0b780d2 commit 34e95aa
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 24 deletions.
9 changes: 9 additions & 0 deletions .env.local.example
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,12 @@ ENV_VALIDATION="enabled"
# NEXT_PUBLIC_GOOGLE_SITE_VERIFICATION=
NEXT_PUBLIC_MATOMO_BASE_URL="https://matomo.acdh.oeaw.ac.at"
# NEXT_PUBLIC_MATOMO_ID=

# -------------------------------------------------------------------------------------------------
# typesense
# -------------------------------------------------------------------------------------------------
NEXT_PUBLIC_TYPESENSE_API_KEY=
NEXT_PUBLIC_TYPESENSE_HOST="typesense.acdh-dev.oeaw.ac.at"
NEXT_PUBLIC_TYPESENSE_PORT="443"
NEXT_PUBLIC_TYPESENSE_PROTOCOL="https"

13 changes: 5 additions & 8 deletions app/search/instantsearch.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,18 @@ import { InstantSearchNext } from "react-instantsearch-nextjs";
import TypesenseInstantSearchAdapter, { type SearchClient } from "typesense-instantsearch-adapter";

import { ClickablePublicationThumbnail } from "@/components/publication-cover";
import { env } from "@/config/env.config";
import { collectionName } from "@/lib/data";
import type { Publication } from "@/lib/model";

const typesenseInstantsearchAdapter = new TypesenseInstantSearchAdapter({
server: {
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
apiKey: process.env.NEXT_PUBLIC_TYPESENSE_API_KEY!,
apiKey: env.NEXT_PUBLIC_TYPESENSE_API_KEY,
nodes: [
{
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
host: process.env.NEXT_PUBLIC_TYPESENSE_HOST!,
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
port: Number(process.env.NEXT_PUBLIC_TYPESENSE_PORT!),
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
protocol: process.env.NEXT_PUBLIC_TYPESENSE_PROTOCOL!,
host: env.NEXT_PUBLIC_TYPESENSE_HOST,
port: env.NEXT_PUBLIC_TYPESENSE_PORT,
protocol: env.NEXT_PUBLIC_TYPESENSE_PROTOCOL,
},
],
},
Expand Down
13 changes: 5 additions & 8 deletions components/instantsearch.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { Configure, SearchBox } from "react-instantsearch";
import { InstantSearchNext } from "react-instantsearch-nextjs";
import TypesenseInstantSearchAdapter, { type SearchClient } from "typesense-instantsearch-adapter";

import { env } from "@/config/env.config";
import { collectionName } from "@/lib/data";

import { InfiniteScroll } from "./instantsearch-infinitescroll";
Expand All @@ -24,16 +25,12 @@ interface InstantSearchProps {

const typesenseInstantsearchAdapter = new TypesenseInstantSearchAdapter({
server: {
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
apiKey: process.env.NEXT_PUBLIC_TYPESENSE_API_KEY!,
apiKey: env.NEXT_PUBLIC_TYPESENSE_API_KEY,
nodes: [
{
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
host: process.env.NEXT_PUBLIC_TYPESENSE_HOST!,
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
port: Number(process.env.NEXT_PUBLIC_TYPESENSE_PORT!),
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
protocol: process.env.NEXT_PUBLIC_TYPESENSE_PROTOCOL!,
host: env.NEXT_PUBLIC_TYPESENSE_HOST,
port: env.NEXT_PUBLIC_TYPESENSE_PORT,
protocol: env.NEXT_PUBLIC_TYPESENSE_PROTOCOL,
},
],
},
Expand Down
14 changes: 14 additions & 0 deletions config/env.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,25 @@ export const env = createEnv({
v.integer(),
v.minValue(1),
),
NEXT_PUBLIC_TYPESENSE_API_KEY: v.pipe(v.string(), v.nonEmpty()),
NEXT_PUBLIC_TYPESENSE_HOST: v.pipe(v.string(), v.nonEmpty()),
NEXT_PUBLIC_TYPESENSE_PORT: v.pipe(
v.string(),
v.transform(Number),
v.number(),
v.integer(),
v.minValue(1),
),
NEXT_PUBLIC_TYPESENSE_PROTOCOL: v.pipe(v.string(), v.nonEmpty()),
});

return v.parse(Schema, input);
},
environment: {
NEXT_PUBLIC_TYPESENSE_API_KEY: process.env.NEXT_PUBLIC_TYPESENSE_API_KEY,
NEXT_PUBLIC_TYPESENSE_HOST: process.env.NEXT_PUBLIC_TYPESENSE_HOST,
NEXT_PUBLIC_TYPESENSE_PORT: process.env.NEXT_PUBLIC_TYPESENSE_PORT,
NEXT_PUBLIC_TYPESENSE_PROTOCOL: process.env.NEXT_PUBLIC_TYPESENSE_PROTOCOL,
BUILD_MODE: process.env.BUILD_MODE,
BUNDLE_ANALYZER: process.env.BUNDLE_ANALYZER,
NEXT_PUBLIC_APP_BASE_URL: process.env.NEXT_PUBLIC_APP_BASE_URL,
Expand Down
13 changes: 5 additions & 8 deletions lib/data.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,18 @@
import { Client } from "typesense";
import type { SearchResponse } from "typesense/lib/Typesense/Documents";

import { env } from "@/config/env.config";
import type { Publication } from "@/lib/model";

export const client = new Client({
nodes: [
{
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
host: process.env.NEXT_PUBLIC_TYPESENSE_HOST!,
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
port: Number(process.env.NEXT_PUBLIC_TYPESENSE_PORT!),
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
protocol: process.env.NEXT_PUBLIC_TYPESENSE_PROTOCOL!,
host: env.NEXT_PUBLIC_TYPESENSE_HOST,
port: env.NEXT_PUBLIC_TYPESENSE_PORT,
protocol: env.NEXT_PUBLIC_TYPESENSE_PROTOCOL,
},
],
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
apiKey: process.env.NEXT_PUBLIC_TYPESENSE_API_KEY!,
apiKey: env.NEXT_PUBLIC_TYPESENSE_API_KEY,
connectionTimeoutSeconds: 2,
});

Expand Down

0 comments on commit 34e95aa

Please sign in to comment.