Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
12 changes: 6 additions & 6 deletions app/(auth)/adapter.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
import { Ok, Err, Result } from 'ts-results-es';
import { Ok, Err, type Result } from 'ts-results-es';

import config from '@/config';
import { extractErrorMessageOrDefault } from '@/lib/utils';

import {
import type {
ApiCreateProjectResponse,
ApiCreateWorkspaceResponse,
ApiListAllProjectsResponse,
ApiListAllWorkspacesResponse,
} from './types';

const patternCoreEndpoint = process.env.PATTERN_CORE_ENDPOINT;
if (!patternCoreEndpoint) {
throw new Error('PATTERN_CORE_ENDPOINT is not set');
}
const {
patternCoreEndpoint: { value: patternCoreEndpoint },
} = config;

/**
* Get all workspaces
Expand Down
13 changes: 8 additions & 5 deletions app/(auth)/auth.config.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { type SIWESession } from '@reown/appkit-siwe';
import type { SIWESession } from '@reown/appkit-siwe';
import type { NextAuthConfig } from 'next-auth';

import config from '@/config';

import { fetchSessionPrerequisites } from './service';

declare module 'next-auth' {
Expand All @@ -22,10 +24,11 @@ declare module 'next-auth/jwt' {
}
}

export const nextAuthSecret = process.env.NEXTAUTH_SECRET;
if (!nextAuthSecret) {
throw new Error('NEXTAUTH_SECRET is not set');
}
const {
nextAuth: {
secret: { value: nextAuthSecret },
},
} = config;

export const authConfig = {
secret: nextAuthSecret,
Expand Down
18 changes: 5 additions & 13 deletions app/(auth)/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,14 @@ import NextAuth from 'next-auth';
import 'next-auth/jwt';
import credentialsProvider from 'next-auth/providers/credentials';

import config from '@/config';

import { authConfig } from './auth.config';

/**
* TODO: Move all configs into a validated configs module to avoid duplication
*
* https://github.com/pattern-tech/pattern-app/issues/3
*/
export const nextAuthSecret = process.env.NEXTAUTH_SECRET;
if (!nextAuthSecret) {
throw new Error('NEXTAUTH_SECRET is not set');
}
const {
patternCoreEndpoint: { value: patternCoreEndpoint },
} = config;

const patternCoreEndpoint = process.env.PATTERN_CORE_ENDPOINT;
if (!patternCoreEndpoint) {
throw new Error('PATTERN_CORE_ENDPOINT is not set');
}
const siweVerificationApi = `${patternCoreEndpoint}/auth/verify`;

const providers = [
Expand Down
7 changes: 1 addition & 6 deletions app/(auth)/service.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Err, Ok, Result } from 'ts-results-es';
import { Err, Ok, type Result } from 'ts-results-es';

import {
createProjectInWorkspace,
Expand All @@ -7,11 +7,6 @@ import {
getAllWorkspaces,
} from './adapter';

const patternCoreEndpoint = process.env.PATTERN_CORE_ENDPOINT;
if (!patternCoreEndpoint) {
throw new Error('PATTERN_CORE_ENDPOINT is not set');
}

/**
* Checks if the default workspace and project exist, if not, creates them
* @param accessToken
Expand Down
8 changes: 4 additions & 4 deletions app/(chat)/adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ import type {
ApiGetAllConversationsResponse,
ApiRenameConversationResponse,
} from '@/app/(chat)/types';
import config from '@/config';
import { extractErrorMessageOrDefault } from '@/lib/utils';

const patternCoreEndpoint = process.env.PATTERN_CORE_ENDPOINT;
if (!patternCoreEndpoint) {
throw new Error('PATTERN_CORE_ENDPOINT is not set');
}
const {
patternCoreEndpoint: { value: patternCoreEndpoint },
} = config;

/**
* Get a conversation
Expand Down
8 changes: 5 additions & 3 deletions app/config/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,11 @@ import {
import { getCsrfToken, getSession, signIn, signOut } from 'next-auth/react';
import { getAddress } from 'viem';

export const walletConnectProjectId =
process.env.NEXT_PUBLIC_WALLET_CONNECT_PROJECT_ID;
if (!walletConnectProjectId) throw new Error('Project ID is not defined');
import config from '@/config/config-client-only';

export const {
walletConnectProjectId: { value: walletConnectProjectId },
} = config;

export const metadata = {
name: 'Pattern',
Expand Down
14 changes: 6 additions & 8 deletions app/context/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,15 @@ import { QueryClient, QueryClientProvider } from '@tanstack/react-query';
import React, { type ReactNode } from 'react';
import { type State, WagmiProvider } from 'wagmi';

import {
chains,
metadata,
walletConnectProjectId,
siweConfig,
wagmiAdapter,
} from '../config';
import config from '@/config/config-client-only';

import { chains, metadata, siweConfig, wagmiAdapter } from '../config';

const queryClient = new QueryClient();

if (!walletConnectProjectId) throw new Error('Project ID is not defined');
const {
walletConnectProjectId: { value: walletConnectProjectId },
} = config;

createAppKit({
adapters: [wagmiAdapter],
Expand Down
13 changes: 13 additions & 0 deletions config/BooleanString.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
export class BooleanString {
private constructor(public value: boolean) {}

static parse(value: string | undefined): BooleanString {
if (value === 'true') {
return new BooleanString(true);
}
if (value === 'false') {
return new BooleanString(false);
}
throw new Error(`Cannot parse "${value}" as BooleanString`);
}
}
10 changes: 10 additions & 0 deletions config/NonEmptyString.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
export class NonEmptyString {
private constructor(public value: string) {}

static parse(value: string | undefined): NonEmptyString {
if (value) {
return new NonEmptyString(value);
}
throw new Error(`Cannot parse "${value}" as NonEmptyString`);
}
}
15 changes: 15 additions & 0 deletions config/PatternCoreEndpoint.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
export class PatternCoreEndpoint {
private constructor(public value: string) {}

static parse(value: string | undefined): PatternCoreEndpoint {
if (!value) {
throw new Error(`Cannot parse "${value}" as PatternCoreEndpoint`);
}
try {
new URL(value);
return new PatternCoreEndpoint(value);
} catch (error) {
throw new Error(`Cannot parse "${value}" as PatternCoreEndpoint`);
}
}
}
16 changes: 16 additions & 0 deletions config/config-client-only.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { NonEmptyString } from './NonEmptyString';

interface PatternUiConfigClientOnly {
walletConnectProjectId: NonEmptyString;
}

const NEXT_PUBLIC_WALLET_CONNECT_PROJECT_ID =
process.env.NEXT_PUBLIC_WALLET_CONNECT_PROJECT_ID;

const config: PatternUiConfigClientOnly = {
walletConnectProjectId: NonEmptyString.parse(
NEXT_PUBLIC_WALLET_CONNECT_PROJECT_ID,
),
};

export default config;
25 changes: 25 additions & 0 deletions config/config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import 'server-only';

import { BooleanString } from './BooleanString';
import { NonEmptyString } from './NonEmptyString';
import { PatternCoreEndpoint } from './PatternCoreEndpoint';

interface PatternUiConfig {
nextAuth: {
secret: NonEmptyString;
trustHost: BooleanString;
};
patternCoreEndpoint: PatternCoreEndpoint;
}

const { NEXTAUTH_SECRET, PATTERN_CORE_ENDPOINT, AUTH_TRUST_HOST } = process.env;

const config: PatternUiConfig = {
nextAuth: {
secret: NonEmptyString.parse(NEXTAUTH_SECRET),
trustHost: BooleanString.parse(AUTH_TRUST_HOST),
},
patternCoreEndpoint: PatternCoreEndpoint.parse(PATTERN_CORE_ENDPOINT),
};

export default config;
1 change: 1 addition & 0 deletions config/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { default } from './config';
5 changes: 0 additions & 5 deletions lib/ai/pattern-model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,6 @@ import type {

import { extractErrorMessageOrDefault } from '../utils';

export const patternCoreEndpoint = process.env.PATTERN_CORE_ENDPOINT;
if (!patternCoreEndpoint) {
throw new Error('PATTERN_CORE_ENDPOINT is not set');
}

const textDecoder = new TextDecoder();

export class PatternModel implements LanguageModelV1 {
Expand Down