Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion core/actions/_lib/runActionInstance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import { logger } from "logger";
import type { ActionSuccess } from "../types";
import type { ClientException, ClientExceptionOptions } from "~/lib/serverActions";
import { db } from "~/kysely/database";
import { env } from "~/lib/env/env.mjs";
import { env } from "~/lib/env/env";
import { hydratePubValues } from "~/lib/fields/utils";
import { createLastModifiedBy } from "~/lib/lastModifiedBy";
import { getPubsWithRelatedValues } from "~/lib/server";
Expand Down
2 changes: 1 addition & 1 deletion core/actions/datacite/run.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { assert, AssertionError, expect } from "utils";
import type { ActionPub } from "../types";
import type { action } from "./action";
import type { components } from "./types";
import { env } from "~/lib/env/env.mjs";
import { env } from "~/lib/env/env";
import { getPubsWithRelatedValues, updatePub } from "~/lib/server";
import { isClientExceptionOptions } from "~/lib/serverActions";
import { defineRun } from "../types";
Expand Down
2 changes: 1 addition & 1 deletion core/actions/googleDriveImport/getGDriveFiles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { JWT } from "google-auth-library";

import { logger } from "logger";

import { env } from "~/lib/env/env.mjs";
import { env } from "~/lib/env/env";

// Load the service account key JSON file
// const keyFilePath = path.join(process.cwd(), 'src/utils/google/keyFile.json');
Expand Down
2 changes: 1 addition & 1 deletion core/app/(user)/magic-link/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import type { AuthTokenType } from "db/public";
import { logger } from "logger";

import { lucia } from "~/lib/authentication/lucia";
import { env } from "~/lib/env/env.mjs";
import { env } from "~/lib/env/env";
import { InvalidTokenError, TokenFailureReason, validateToken } from "~/lib/server/token";

const redirectToURL = (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { runInstancesForEvent } from "~/actions/_lib/runActionInstance";
import { scheduleActionInstances } from "~/actions/_lib/scheduleActionInstance";
import { runActionInstance } from "~/actions/api/server";
import { compareAPIKeys, getBearerToken } from "~/lib/authentication/api";
import { env } from "~/lib/env/env.mjs";
import { env } from "~/lib/env/env";
import { NotFoundError, tsRestHandleErrors } from "~/lib/server";
import { findCommunityBySlug } from "~/lib/server/community";

Expand Down Expand Up @@ -97,4 +97,4 @@ const handler = createNextHandler(
}
);

export { handler as GET, handler as POST, handler as PUT, handler as PATCH, handler as DELETE };
export { handler as DELETE, handler as GET, handler as PATCH, handler as POST, handler as PUT };
2 changes: 1 addition & 1 deletion core/app/c/[communitySlug]/developers/docs/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { redirect } from "next/navigation";

import { getPageLoginData } from "~/lib/authentication/loginData";
import { isCommunityAdmin } from "~/lib/authentication/roles";
import { env } from "~/lib/env/env.mjs";
import { env } from "~/lib/env/env";

export const dynamic = "force-dynamic";

Expand Down
1 change: 1 addition & 0 deletions core/app/c/[communitySlug]/pubs/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import type { Metadata } from "next";
import type { CommunitiesId } from "db/public";

import { getPageLoginData } from "~/lib/authentication/loginData";
import { env } from "~/lib/env/env";
import { findCommunityBySlug } from "~/lib/server/community";
import PubHeader from "./PubHeader";
import { PaginatedPubList } from "./PubList";
Expand Down
2 changes: 1 addition & 1 deletion core/instrumentation.edge.mts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import * as Sentry from "@sentry/nextjs";

import { logger } from "logger";

import { env } from "./lib/env/env.mjs";
import { env } from "./lib/env/env";

logger.info("Running instrumentation hook for edge...");

Expand Down
2 changes: 1 addition & 1 deletion core/instrumentation.node.mts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import * as Sentry from "@sentry/nextjs";

import { logger } from "logger";

import { env } from "./lib/env/env.mjs";
import { env } from "./lib/env/env";

// function hook() {
logger.info("Running instrumentation hook for nodejs...");
Expand Down
2 changes: 1 addition & 1 deletion core/kysely/database.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { env } from "~/lib/env/env.mjs";
import { env } from "~/lib/env/env";
import { createDatabase } from "./database-init";

export const db = createDatabase({
Expand Down
2 changes: 1 addition & 1 deletion core/lib/__tests__/db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { databaseTables } from "db/table-names";
import { logger } from "logger";

import { UpdatedAtPlugin } from "~/kysely/updated-at-plugin";
import { env } from "../env/env.mjs";
import { env } from "../env/env";

const int8TypeId = 20;

Expand Down
2 changes: 1 addition & 1 deletion core/lib/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { initTsrReactQuery } from "@ts-rest/react-query/v5";

import { siteApi } from "contracts";

import { env } from "./env/env.mjs";
import { env } from "./env/env";

export const client = initTsrReactQuery(siteApi, {
baseUrl: typeof window === "undefined" ? env.PUBPUB_URL : window.location.origin,
Expand Down
2 changes: 1 addition & 1 deletion core/lib/authentication/createMagicLink.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { AuthTokenType, UsersId } from "db/public";

import { db } from "~/kysely/database";
import { env } from "../env/env.mjs";
import { env } from "../env/env";
import { createToken } from "../server/token";

type NativeMagicLinkOptions = {
Expand Down
2 changes: 1 addition & 1 deletion core/lib/authentication/lucia.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import { AuthTokenType } from "db/public";
import { logger } from "logger";

import { db } from "~/kysely/database";
import { env } from "~/lib/env/env.mjs";
import { env } from "~/lib/env/env";

type UserWithMembersShips = Omit<Users, "passwordHash"> & {
memberships: (CommunityMemberships & {
Expand Down
99 changes: 73 additions & 26 deletions core/lib/env/env.mjs → core/lib/env/env.ts
Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All of the important changes are in this file!

Original file line number Diff line number Diff line change
@@ -1,19 +1,65 @@
// @ts-check
import type { ZodTypeAny } from "zod";

import { createEnv } from "@t3-oss/env-nextjs";
import { z } from "zod";
import { z, ZodError } from "zod";

/**
* Parameters which are optional if the app is self-hosted
* but we do want checked for our AWS deploys
*
* @template {import("zod").ZodTypeAny} Z
* @param {Z} schema
*/
const selfHostedOptional = (schema) => {
import { actionSchema } from "db/public";

const selfHostedOptional = (schema: ZodTypeAny) => {
return process.env.SELF_HOSTED ? schema.optional() : schema;
};

const flagStateToBoolean = (flagState: string, ctx: z.RefinementCtx) => {
switch (flagState) {
case "on":
case "true":
return true;
case "off":
case "false":
return false;
}
ctx.addIssue({
code: z.ZodIssueCode.custom,
message: "Invalid flag state",
fatal: true,
});
return z.NEVER;
};

const flagSchema = z.union([
z.tuple([
z.literal("disabled-actions"),
z
.string()
.transform((s) => s.split("+"))
.pipe(actionSchema.array()),
]),
z.tuple([
z.literal("invites"),
z.string().transform(flagStateToBoolean).optional().default("on"),
]),
z.tuple([
z.literal("uploads"),
z.string().transform(flagStateToBoolean).optional().default("on"),
]),
]);

type FlagSchema = z.infer<typeof flagSchema>;
type FlagName = FlagSchema[0];
type FlagArgs<F extends FlagName> = Extract<FlagSchema, [F, unknown]>[1];

class Flags {
#flags;
constructor(flags: z.infer<typeof flagSchema>[]) {
this.#flags = new Map(flags as [string, unknown][]);
}
get<F extends FlagName>(flagName: F): FlagArgs<F> {
return (this.#flags.get(flagName) ??
flagSchema.parse([flagName, undefined])) as FlagArgs<F>;
}
}

export const env = createEnv({
shared: {
NODE_ENV: z.enum(["development", "production", "test"]).optional(),
Expand Down Expand Up @@ -52,25 +98,26 @@ export const env = createEnv({
DATACITE_REPOSITORY_ID: z.string().optional(),
DATACITE_PASSWORD: z.string().optional(),
SENTRY_AUTH_TOKEN: z.string().optional(),
DISABLED_ACTIONS: z
FLAGS: z
.string()
.transform((value) => value.split(","))
.pipe(
z
// TODO: Figure out how to get `import {actionSchema} from "db/public"` working
.enum([
"log",
"pdf",
"email",
"pushToV6",
"http",
"move",
"googleDriveImport",
"datacite",
])
.array()
)
.optional(),
.transform((flagStrings, ctx) => {
const parsedFlags: z.infer<typeof flagSchema>[] = [];
for (const flagString of flagStrings) {
try {
const [flagName, flagArgs] = flagString.split(":");
const parsedFlag = flagSchema.parse([flagName, flagArgs]);
parsedFlags.push(parsedFlag);
} catch (error) {
if (error instanceof ZodError) {
error.issues.forEach(ctx.addIssue);
}
}
}
return new Flags(parsedFlags);
})
.optional()
.default(""),
},
client: {},
experimental__runtimeEnv: {
Expand Down
2 changes: 1 addition & 1 deletion core/lib/server/assets.db.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import fs from "fs/promises";
import { beforeAll, describe, expect, it } from "vitest";

import { mockServerCode } from "~/lib/__tests__/utils";
import { env } from "../env/env.mjs";
import { env } from "../env/env";

const { createForEachMockedTransaction } = await mockServerCode();

Expand Down
2 changes: 1 addition & 1 deletion core/lib/server/assets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { getSignedUrl } from "@aws-sdk/s3-request-presigner";
import type { PubsId } from "db/public";
import { logger } from "logger";

import { env } from "../env/env.mjs";
import { env } from "../env/env";

let s3Client: S3Client;

Expand Down
2 changes: 1 addition & 1 deletion core/lib/server/cache/autoRevalidate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { logger } from "logger";

import type { autoCache } from "./autoCache";
import type { AutoRevalidateOptions, DirectAutoOutput, ExecuteFn, QB } from "./types";
import { env } from "~/lib/env/env.mjs";
import { env } from "~/lib/env/env";
import { getCommunitySlug } from "./getCommunitySlug";
import { revalidateTagsForCommunity } from "./revalidate";
import { cachedFindTables, directAutoOutput } from "./sharedAuto";
Expand Down
2 changes: 1 addition & 1 deletion core/lib/server/cache/memoize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { unstable_cache } from "next/cache";
import { logger } from "logger";

import type { CacheTag } from "./cacheTags";
import { env } from "~/lib/env/env.mjs";
import { env } from "~/lib/env/env";
import { ONE_YEAR } from "./constants";

type Callback<Parameters extends unknown[], ReturnType> = (
Expand Down
2 changes: 1 addition & 1 deletion core/lib/server/cache/revalidate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { revalidateTag } from "next/cache";
import { logger } from "logger";

import type { CacheScope } from "./cacheTags";
import { env } from "~/lib/env/env.mjs";
import { env } from "~/lib/env/env";
import { createCommunityCacheTags } from "./cacheTags";
import { getCommunitySlug } from "./getCommunitySlug";

Expand Down
2 changes: 1 addition & 1 deletion core/lib/server/email.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import type { XOR } from "../types";
import type { FormInviteLinkProps } from "./form";
import { db } from "~/kysely/database";
import { createMagicLink } from "~/lib/authentication/createMagicLink";
import { env } from "../env/env.mjs";
import { env } from "../env/env";
import { createFormInviteLink } from "./form";
import { getSmtpClient } from "./mailgun";

Expand Down
2 changes: 1 addition & 1 deletion core/lib/server/jobs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { makeWorkerUtils } from "graphile-worker";
import { logger } from "logger";

import type { ClientException, ClientExceptionOptions } from "../serverActions";
import { env } from "../env/env.mjs";
import { env } from "../env/env";

import "date-fns";

Expand Down
2 changes: 1 addition & 1 deletion core/lib/server/mailgun.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import type SMTPPool from "nodemailer/lib/smtp-pool";

import nodemailer from "nodemailer";

import { env } from "~/lib/env/env.mjs";
import { env } from "~/lib/env/env";

let smtpclient: nodemailer.Transporter;

Expand Down
11 changes: 2 additions & 9 deletions core/lib/server/pub.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,7 @@ import type {
UsersId,
} from "db/public";
import type { LastModifiedBy, StageConstraint } from "db/types";
import {
Capabilities,
CoreSchemaType,
MemberRole,
MembershipType,
OperationType,
pubTypesIdSchema,
} from "db/public";
import { Capabilities, CoreSchemaType, MemberRole, MembershipType, OperationType } from "db/public";
import { NO_STAGE_OPTION } from "db/types";
import { logger } from "logger";
import { assert, expect } from "utils";
Expand All @@ -53,7 +46,7 @@ import type { DefinitelyHas, MaybeHas, XOR } from "../types";
import type { SafeUser } from "./user";
import { db } from "~/kysely/database";
import { isUniqueConstraintError } from "~/kysely/errors";
import { env } from "../env/env.mjs";
import { env } from "../env/env";
import { parseRichTextForPubFieldsAndRelatedPubs } from "../fields/richText";
import { hydratePubValues, mergeSlugsWithFields } from "../fields/utils";
import { parseLastModifiedBy } from "../lastModifiedBy";
Expand Down
2 changes: 1 addition & 1 deletion core/lib/server/render/pub/renderWithPubUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { CoreSchemaType } from "db/public";
import { expect } from "utils";

import { db } from "~/kysely/database";
import { env } from "~/lib/env/env.mjs";
import { env } from "~/lib/env/env";
import { autoCache } from "~/lib/server/cache/autoCache";
import { addMemberToForm, createFormInviteLink } from "../../form";

Expand Down
14 changes: 8 additions & 6 deletions core/next.config.mjs → core/next.config.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
// @ts-check

import type { NextConfig, normalizeConfig } from "next/dist/server/config";

import { PHASE_PRODUCTION_BUILD } from "next/dist/shared/lib/constants.js";
import withPreconstruct from "@preconstruct/next";
import { withSentryConfig } from "@sentry/nextjs";

import { env } from "./lib/env/env.mjs";
import { env } from "./lib/env/env";

// import { PHASE_DEVELOPMENT_SERVER } from "next/constants";

/**
* @type {import("next").NextConfig}
*/
const nextConfig = {
const nextConfig: NextConfig = {
output: "standalone",
typescript: {
// this gets checked in CI already
Expand Down Expand Up @@ -91,6 +90,7 @@ const modifiedConfig = withPreconstruct(
// tunnelRoute: "/monitoring",

// Hides source maps from generated client bundles
// @ts-expect-error
hideSourceMaps: true,

// Automatically tree-shake Sentry logger statements to reduce bundle size
Expand All @@ -102,7 +102,7 @@ const modifiedConfig = withPreconstruct(
})
);

export default (phase, { defaultConfig }) => {
const config: typeof normalizeConfig = async (phase, { defaultConfig }) => {
if (!env.SENTRY_AUTH_TOKEN) {
console.warn("⚠️ SENTRY_AUTH_TOKEN is not set");
}
Expand All @@ -117,3 +117,5 @@ export default (phase, { defaultConfig }) => {
}
return modifiedConfig;
};

export default config;
Loading
Loading