Skip to content

Commit

Permalink
Replace env-var with zod
Browse files Browse the repository at this point in the history
  • Loading branch information
spencerpogo committed Jan 8, 2022
1 parent c322c8f commit 8dd5ebe
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 49 deletions.
4 changes: 2 additions & 2 deletions api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
"apollo-server-express": "^2.25.2",
"class-validator": "^0.13.1",
"connect-redis": "^6.0.0",
"env-var": "^7.0.1",
"express": "^4.17.1",
"express-session": "^1.17.2",
"graphql": "^15.5.1",
Expand All @@ -36,6 +35,7 @@
"reflect-metadata": "^0.1.13",
"type-graphql": "^1.1.1",
"typeorm": "^0.2.34",
"uuid": "^8.3.2"
"uuid": "^8.3.2",
"zod": "^3.11.6"
}
}
37 changes: 13 additions & 24 deletions api/src/config.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,14 @@
import * as env from "env-var";
import { z } from "zod";

export const HOST: string = env.get("HOST").default("127.0.0.1").asString();
export const PORT: number = env.get("PORT").default(8080).asPortNumber();
export const SESSION_SECRET = env.get("SESSION_SECRET").required().asString();
export const DATABASE_URL: string = env
.get("DATABASE_URL")
.required()
.asUrlString();
export const REDIS_HOST: string = env
.get("REDIS_HOST")
.default("127.0.0.1")
.asString();
export const REDIS_PORT: number = env
.get("REDIS_PORT")
.default(6379)
.asPortNumber();
export const GITHUB_CLIENT_ID: string = env
.get("GITHUB_CLIENT_ID")
.required()
.asString();
export const GITHUB_CLIENT_SECRET: string = env
.get("GITHUB_CLIENT_SECRET")
.required()
.asString();
const PORT_REGEX = /^[0-9]+$/;
const schema = z.object({
HOST: z.string(),
PORT: z.string().regex(PORT_REGEX),
SESSION_SECRET: z.string(),
DATABASE_URL: z.string(),
REDIS_HOST: z.string(),
REDIS_PORT: z.string().regex(PORT_REGEX),
GITHUB_CLIENT_ID: z.string(),
GITHUB_CLIENT_SECRET: z.string(),
});
export const CONFIG = schema.parse(process.env);
8 changes: 4 additions & 4 deletions api/src/githubAuth.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import fetch, { Response } from "node-fetch";
import { stringify as querystringify } from "querystring";
import { version as serverVersion } from "../package.json";
import { GITHUB_CLIENT_ID, GITHUB_CLIENT_SECRET } from "./config.js";
import { CONFIG } from "./config.js";

const GITHUB_URL = "https://github.com";
const GITHUB_API_URL = "https://api.github.com";
Expand Down Expand Up @@ -42,8 +42,8 @@ export async function tokenFromCode(
...UA,
},
body: JSON.stringify({
client_id: GITHUB_CLIENT_ID,
client_secret: GITHUB_CLIENT_SECRET,
client_id: CONFIG.GITHUB_CLIENT_ID,
client_secret: CONFIG.GITHUB_CLIENT_SECRET,
code: githubCode,
}),
});
Expand Down Expand Up @@ -76,7 +76,7 @@ export function genGithubLoginURL(redirectUri: string, state: string) {
return (
`${GITHUB_URL}/login/oauth/authorize?` +
querystringify({
client_id: GITHUB_CLIENT_ID,
client_id: CONFIG.GITHUB_CLIENT_ID,
redirect_uri: redirectUri,
scope: "", // no scope gives access to public information
state,
Expand Down
23 changes: 9 additions & 14 deletions api/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,7 @@ import path from "path";
import "reflect-metadata";
import { buildSchema } from "type-graphql";
import { createConnection } from "typeorm";
import {
DATABASE_URL,
HOST,
PORT,
REDIS_HOST,
REDIS_PORT,
SESSION_SECRET,
} from "./config";
import { CONFIG } from "./config.js";
import { User } from "./models/User";
import { HelloResolver } from "./resolvers/hello";
import { UserResolver } from "./resolvers/user";
Expand All @@ -26,22 +19,22 @@ async function main() {

const conn = await createConnection({
type: "postgres",
url: DATABASE_URL,
url: CONFIG.DATABASE_URL,
logging: true,
migrations: [path.join(__dirname, "migrations", "*")],
entities: [User],
});
await conn.runMigrations();

const redis = new IORedis({
host: REDIS_HOST,
port: REDIS_PORT,
host: CONFIG.REDIS_HOST,
port: Number(CONFIG.REDIS_PORT),
});
app.use(
session({
store: new RedisStore({ client: redis }),
saveUninitialized: false,
secret: SESSION_SECRET,
secret: CONFIG.SESSION_SECRET,
resave: false, // redis supports touch
cookie: {
maxAge: 1000 * 60 * 60 * 24 * 30, // 30 days
Expand All @@ -60,8 +53,10 @@ async function main() {
});
server.applyMiddleware({ app });

app.listen({ host: HOST, port: PORT }, () =>
console.log(`Listening on http://${HOST}:${PORT}${server.graphqlPath}`)
app.listen({ host: CONFIG.HOST, port: CONFIG.PORT }, () =>
console.log(
`Listening on http://${CONFIG.HOST}:${CONFIG.PORT}${server.graphqlPath}`
)
);
}

Expand Down
10 changes: 5 additions & 5 deletions api/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1068,11 +1068,6 @@ end-of-stream@^1.1.0:
dependencies:
once "^1.4.0"

env-var@^7.0.1:
version "7.1.1"
resolved "https://registry.yarnpkg.com/env-var/-/env-var-7.1.1.tgz#c1697f8dfded0b54418dc9bbd78a82119910e57c"
integrity sha512-4+vvlq+wwGQNwY/nI3/+Ojc1MKHCmITRJ6VWkQzDtMD6fAEb60ACRUCnlIAonMKW9YzqYmYxbyVu9vTb++yNRg==

es-abstract@^1.19.1:
version "1.19.1"
resolved "https://registry.yarnpkg.com/es-abstract/-/es-abstract-1.19.1.tgz#d4885796876916959de78edaa0df456627115ec3"
Expand Down Expand Up @@ -2806,3 +2801,8 @@ [email protected], zen-observable@^0.8.0:
version "0.8.15"
resolved "https://registry.yarnpkg.com/zen-observable/-/zen-observable-0.8.15.tgz#96415c512d8e3ffd920afd3889604e30b9eaac15"
integrity sha512-PQ2PC7R9rslx84ndNBZB/Dkv8V8fZEpk83RLgXtYd0fwUgEjseMn1Dgajh2x6S8QbZAFa9p2qVCEuYZNgve0dQ==

zod@^3.11.6:
version "3.11.6"
resolved "https://registry.yarnpkg.com/zod/-/zod-3.11.6.tgz#e43a5e0c213ae2e02aefe7cb2b1a6fa3d7f1f483"
integrity sha512-daZ80A81I3/9lIydI44motWe6n59kRBfNzTuS2bfzVh1nAXi667TOTWWtatxyG+fwgNUiagSj/CWZwRRbevJIg==

0 comments on commit 8dd5ebe

Please sign in to comment.