Skip to content

Commit fb07c2e

Browse files
authored
Merge pull request #18 from pattern-tech/refactor/configs
refactor: move all configs to a validated configs module
2 parents 2071861 + 196670b commit fb07c2e

File tree

14 files changed

+115
-50
lines changed

14 files changed

+115
-50
lines changed

app/(auth)/adapter.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
1-
import { Ok, Err, Result } from 'ts-results-es';
1+
import { Ok, Err, type Result } from 'ts-results-es';
22

3+
import config from '@/config';
34
import { extractErrorMessageOrDefault } from '@/lib/utils';
45

5-
import {
6+
import type {
67
ApiCreateProjectResponse,
78
ApiCreateWorkspaceResponse,
89
ApiListAllProjectsResponse,
910
ApiListAllWorkspacesResponse,
1011
} from './types';
1112

12-
const patternCoreEndpoint = process.env.PATTERN_CORE_ENDPOINT;
13-
if (!patternCoreEndpoint) {
14-
throw new Error('PATTERN_CORE_ENDPOINT is not set');
15-
}
13+
const {
14+
patternCoreEndpoint: { value: patternCoreEndpoint },
15+
} = config;
1616

1717
/**
1818
* Get all workspaces

app/(auth)/auth.config.ts

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1-
import { type SIWESession } from '@reown/appkit-siwe';
1+
import type { SIWESession } from '@reown/appkit-siwe';
22
import type { NextAuthConfig } from 'next-auth';
33

4+
import config from '@/config';
5+
46
import { fetchSessionPrerequisites } from './service';
57

68
declare module 'next-auth' {
@@ -22,10 +24,11 @@ declare module 'next-auth/jwt' {
2224
}
2325
}
2426

25-
export const nextAuthSecret = process.env.NEXTAUTH_SECRET;
26-
if (!nextAuthSecret) {
27-
throw new Error('NEXTAUTH_SECRET is not set');
28-
}
27+
const {
28+
nextAuth: {
29+
secret: { value: nextAuthSecret },
30+
},
31+
} = config;
2932

3033
export const authConfig = {
3134
secret: nextAuthSecret,

app/(auth)/auth.ts

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,22 +6,14 @@ import NextAuth from 'next-auth';
66
import 'next-auth/jwt';
77
import credentialsProvider from 'next-auth/providers/credentials';
88

9+
import config from '@/config';
10+
911
import { authConfig } from './auth.config';
1012

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

21-
const patternCoreEndpoint = process.env.PATTERN_CORE_ENDPOINT;
22-
if (!patternCoreEndpoint) {
23-
throw new Error('PATTERN_CORE_ENDPOINT is not set');
24-
}
2517
const siweVerificationApi = `${patternCoreEndpoint}/auth/verify`;
2618

2719
const providers = [

app/(auth)/service.ts

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Err, Ok, Result } from 'ts-results-es';
1+
import { Err, Ok, type Result } from 'ts-results-es';
22

33
import {
44
createProjectInWorkspace,
@@ -7,11 +7,6 @@ import {
77
getAllWorkspaces,
88
} from './adapter';
99

10-
const patternCoreEndpoint = process.env.PATTERN_CORE_ENDPOINT;
11-
if (!patternCoreEndpoint) {
12-
throw new Error('PATTERN_CORE_ENDPOINT is not set');
13-
}
14-
1510
/**
1611
* Checks if the default workspace and project exist, if not, creates them
1712
* @param accessToken

app/(chat)/adapter.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,12 @@ import type {
99
ApiGetAllConversationsResponse,
1010
ApiRenameConversationResponse,
1111
} from '@/app/(chat)/types';
12+
import config from '@/config';
1213
import { extractErrorMessageOrDefault } from '@/lib/utils';
1314

14-
const patternCoreEndpoint = process.env.PATTERN_CORE_ENDPOINT;
15-
if (!patternCoreEndpoint) {
16-
throw new Error('PATTERN_CORE_ENDPOINT is not set');
17-
}
15+
const {
16+
patternCoreEndpoint: { value: patternCoreEndpoint },
17+
} = config;
1818

1919
/**
2020
* Get a conversation

app/config/index.tsx

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,11 @@ import {
1616
import { getCsrfToken, getSession, signIn, signOut } from 'next-auth/react';
1717
import { getAddress } from 'viem';
1818

19-
export const walletConnectProjectId =
20-
process.env.NEXT_PUBLIC_WALLET_CONNECT_PROJECT_ID;
21-
if (!walletConnectProjectId) throw new Error('Project ID is not defined');
19+
import config from '@/config/config-client-only';
20+
21+
export const {
22+
walletConnectProjectId: { value: walletConnectProjectId },
23+
} = config;
2224

2325
export const metadata = {
2426
name: 'Pattern',

app/context/index.tsx

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,15 @@ import { QueryClient, QueryClientProvider } from '@tanstack/react-query';
55
import React, { type ReactNode } from 'react';
66
import { type State, WagmiProvider } from 'wagmi';
77

8-
import {
9-
chains,
10-
metadata,
11-
walletConnectProjectId,
12-
siweConfig,
13-
wagmiAdapter,
14-
} from '../config';
8+
import config from '@/config/config-client-only';
9+
10+
import { chains, metadata, siweConfig, wagmiAdapter } from '../config';
1511

1612
const queryClient = new QueryClient();
1713

18-
if (!walletConnectProjectId) throw new Error('Project ID is not defined');
14+
const {
15+
walletConnectProjectId: { value: walletConnectProjectId },
16+
} = config;
1917

2018
createAppKit({
2119
adapters: [wagmiAdapter],

config/BooleanString.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
export class BooleanString {
2+
private constructor(public value: boolean) {}
3+
4+
static parse(value: string | undefined): BooleanString {
5+
if (value === 'true') {
6+
return new BooleanString(true);
7+
}
8+
if (value === 'false') {
9+
return new BooleanString(false);
10+
}
11+
throw new Error(`Cannot parse "${value}" as BooleanString`);
12+
}
13+
}

config/NonEmptyString.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
export class NonEmptyString {
2+
private constructor(public value: string) {}
3+
4+
static parse(value: string | undefined): NonEmptyString {
5+
if (value) {
6+
return new NonEmptyString(value);
7+
}
8+
throw new Error(`Cannot parse "${value}" as NonEmptyString`);
9+
}
10+
}

config/PatternCoreEndpoint.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
export class PatternCoreEndpoint {
2+
private constructor(public value: string) {}
3+
4+
static parse(value: string | undefined): PatternCoreEndpoint {
5+
if (!value) {
6+
throw new Error(`Cannot parse "${value}" as PatternCoreEndpoint`);
7+
}
8+
try {
9+
new URL(value);
10+
return new PatternCoreEndpoint(value);
11+
} catch (error) {
12+
throw new Error(`Cannot parse "${value}" as PatternCoreEndpoint`);
13+
}
14+
}
15+
}

0 commit comments

Comments
 (0)