Skip to content
This repository has been archived by the owner on Oct 15, 2024. It is now read-only.

Commit

Permalink
refactor(gql-codegen.ts): replace getStringEnv with env.API_LINK and …
Browse files Browse the repository at this point in the history
…env.API_TOKEN to improve semantics

feat(gql-codegen.ts): add support for environment variables to be able to run app on a configurable port
feat(package.json): add zod dependency to validate environment variables
refactor(client.ts): replace getStringEnv with env.BOT_TOKEN to improve semantics
feat(configs/env): add env.config.ts and env.dto.ts to validate environment variables
refactor(github-link-reaction.event.ts): replace getStringEnv with env.GITHUB_TOKEN to improve semantics
refactor(request/graphql.ts): replace getStringEnv with env.API_LINK and env.API_TOKEN to improve semantics
  • Loading branch information
Bluzzi committed May 31, 2023
1 parent cd001b3 commit ee73770
Show file tree
Hide file tree
Showing 9 changed files with 28 additions and 38 deletions.
7 changes: 2 additions & 5 deletions gql-codegen.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
import type { CodegenConfig } from "@graphql-codegen/cli";
import { getStringEnv } from "./src/utils/env-variable";

const apiLink = getStringEnv("API_LINK");
const apiToken = getStringEnv("API_TOKEN");
import { env } from "./src/configs/env";

const config: CodegenConfig = {
schema: [
{ [apiLink]: { headers: { authorization: apiToken } } }
{ [env.API_LINK]: { headers: { authorization: env.API_TOKEN } } }
],
documents: "./src/**/*.ts",
generates: {
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@
"cron": "^2.3.1",
"dayjs": "^1.11.7",
"discord.js": "^14.11.0",
"rustic-error": "^0.2.1"
"rustic-error": "^0.2.1",
"zod": "^3.21.4"
},
"devDependencies": {
"@bluzzi/eslint-config": "^1.1.0",
Expand Down
4 changes: 2 additions & 2 deletions src/client.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Client as DiscordClient, GatewayIntentBits, Partials, Team, User } from "discord.js";
import { logger } from "$core/utils/logger";
import { version, displayName } from "../package.json";
import { getStringEnv } from "./utils/env-variable";
import { env } from "./configs/env/env.config";
import { listener, load as loadCommands, register } from "$core/utils/handler/command";
import { load as loadEvents } from "$core/utils/handler/event";
import { load as loadTasks } from "$core/utils/handler/task";
Expand Down Expand Up @@ -36,7 +36,7 @@ export const getDevTeam = (client: DiscordClient): User[] | null => {


logger.info(`Sarting ${displayName} v${version}...`);
void client.login(getStringEnv("BOT_TOKEN"));
void client.login(env.BOT_TOKEN);
client.on("ready", async client => {
const eventsLoaded = await loadEvents(client, `${__dirname}${sep}events`);

Expand Down
8 changes: 8 additions & 0 deletions src/configs/env/env.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import "dotenv/config";
import { envDTO } from "./env.dto";

const parser = envDTO.safeParse(process.env);

if (!parser.success) throw Error("tgm");

export const env = parser.data;
8 changes: 8 additions & 0 deletions src/configs/env/env.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { z } from "zod";

export const envDTO = z.object({
API_LINK: z.string().url(),
API_TOKEN: z.string().uuid(),
BOT_TOKEN: z.string().nonempty(),
GITHUB_TOKEN: z.string().nonempty()
});
1 change: 1 addition & 0 deletions src/configs/env/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { env } from "./env.config";
4 changes: 2 additions & 2 deletions src/events/github-link-reaction/github-link-reaction.event.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { CodeElement } from "./github-link-reaction.type";
import type { EventExecute, EventName } from "$core/utils/handler/event";
import { githubPermalinkRegex, githubRawUrl } from "./github-link-reaction.const";
import { getStringEnv } from "$core/utils/env-variable";
import { env } from "$core/configs/env";
import { restTextRequest } from "$core/utils/request";
import { logger } from "$core/utils/logger";
import { userWithId } from "$core/utils/user";
Expand Down Expand Up @@ -46,7 +46,7 @@ export const execute: EventExecute<"messageCreate"> = async(message) => {
// Request for get the code :
const response = await restTextRequest("get", githubRawUrl + filePath.join("/"), {
headers: {
authorization: `token ${getStringEnv("GITHUB_TOKEN")}`
authorization: `token ${env.GITHUB_TOKEN}`
}
});

Expand Down
25 changes: 0 additions & 25 deletions src/utils/env-variable.ts

This file was deleted.

6 changes: 3 additions & 3 deletions src/utils/request/graphql/graphql.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import type { TypedDocumentNode } from "@graphql-typed-document-node/core";
import { getStringEnv } from "$core/utils/env-variable";
import { print } from "graphql";
import { logger } from "$core/utils/logger";
import { Result, error, ok } from "rustic-error";
import { env } from "$core/configs/env";

export const gqlRequest = async<D, V>(document: TypedDocumentNode<D, V>, variables?: V): Promise<Result<D, Error>> => {
const response = await fetch(getStringEnv("API_LINK"), {
const response = await fetch(env.API_LINK, {
method: "post",
headers: {
"authorization": getStringEnv("API_TOKEN"),
"authorization": env.API_TOKEN,
"content-type": "application/json"
},
body: JSON.stringify({
Expand Down

0 comments on commit ee73770

Please sign in to comment.