11import { json , type LoaderFunctionArgs } from "@remix-run/server-runtime" ;
22import { type GetProjectEnvResponse } from "@trigger.dev/core/v3" ;
3- import { type RuntimeEnvironment } from "@trigger.dev/database" ;
43import { z } from "zod" ;
5- import { prisma } from "~/db.server" ;
64import { env as processEnv } from "~/env.server" ;
7- import { logger } from "~/services/logger.server" ;
8- import { authenticateApiRequestWithPersonalAccessToken } from "~/services/personalAccessToken.server" ;
5+ import {
6+ authenticatedEnvironmentForAuthentication ,
7+ authenticateRequest ,
8+ } from "~/services/apiAuth.server" ;
99
1010const ParamsSchema = z . object ( {
1111 projectRef : z . string ( ) ,
@@ -15,14 +15,6 @@ const ParamsSchema = z.object({
1515type ParamsSchema = z . infer < typeof ParamsSchema > ;
1616
1717export async function loader ( { request, params } : LoaderFunctionArgs ) {
18- logger . info ( "projects get env" , { url : request . url } ) ;
19-
20- const authenticationResult = await authenticateApiRequestWithPersonalAccessToken ( request ) ;
21-
22- if ( ! authenticationResult ) {
23- return json ( { error : "Invalid or Missing Access Token" } , { status : 401 } ) ;
24- }
25-
2618 const parsedParams = ParamsSchema . safeParse ( params ) ;
2719
2820 if ( ! parsedParams . success ) {
@@ -31,117 +23,24 @@ export async function loader({ request, params }: LoaderFunctionArgs) {
3123
3224 const { projectRef, env } = parsedParams . data ;
3325
34- const project = await prisma . project . findFirst ( {
35- where : {
36- externalRef : projectRef ,
37- organization : {
38- members : {
39- some : {
40- userId : authenticationResult . userId ,
41- } ,
42- } ,
43- } ,
44- } ,
45- } ) ;
26+ const authenticationResult = await authenticateRequest ( request ) ;
4627
47- if ( ! project ) {
48- return json ( { error : "Project not found" } , { status : 404 } ) ;
49- }
50-
51- const envResult = await getEnvironmentFromEnv ( {
52- projectId : project . id ,
53- userId : authenticationResult . userId ,
54- env,
55- } ) ;
56-
57- if ( ! envResult . success ) {
58- return json ( { error : envResult . error } , { status : 404 } ) ;
28+ if ( ! authenticationResult ) {
29+ return json ( { error : "Invalid or Missing API key" } , { status : 401 } ) ;
5930 }
6031
61- const runtimeEnv = envResult . environment ;
32+ const environment = await authenticatedEnvironmentForAuthentication (
33+ authenticationResult ,
34+ projectRef ,
35+ env
36+ ) ;
6237
6338 const result : GetProjectEnvResponse = {
64- apiKey : runtimeEnv . apiKey ,
65- name : project . name ,
39+ apiKey : environment . apiKey ,
40+ name : environment . project . name ,
6641 apiUrl : processEnv . API_ORIGIN ?? processEnv . APP_ORIGIN ,
67- projectId : project . id ,
42+ projectId : environment . project . id ,
6843 } ;
6944
7045 return json ( result ) ;
7146}
72-
73- async function getEnvironmentFromEnv ( {
74- projectId,
75- userId,
76- env,
77- } : {
78- projectId : string ;
79- userId : string ;
80- env : ParamsSchema [ "env" ] ;
81- } ) : Promise <
82- | {
83- success : true ;
84- environment : RuntimeEnvironment ;
85- }
86- | {
87- success : false ;
88- error : string ;
89- }
90- > {
91- if ( env === "dev" ) {
92- const environment = await prisma . runtimeEnvironment . findFirst ( {
93- where : {
94- projectId,
95- orgMember : {
96- userId : userId ,
97- } ,
98- } ,
99- } ) ;
100-
101- if ( ! environment ) {
102- return {
103- success : false ,
104- error : "Dev environment not found" ,
105- } ;
106- }
107-
108- return {
109- success : true ,
110- environment,
111- } ;
112- }
113-
114- let slug : "stg" | "prod" | "preview" = "prod" ;
115- switch ( env ) {
116- case "staging" :
117- slug = "stg" ;
118- break ;
119- case "prod" :
120- slug = "prod" ;
121- break ;
122- case "preview" :
123- slug = "preview" ;
124- break ;
125- default :
126- break ;
127- }
128-
129- const environment = await prisma . runtimeEnvironment . findFirst ( {
130- where : {
131- projectId,
132- slug,
133- } ,
134- } ) ;
135-
136- if ( ! environment ) {
137- return {
138- success : false ,
139- error : `${ env === "staging" ? "Staging" : "Production" } environment not found` ,
140- } ;
141- }
142-
143- return {
144- success : true ,
145- environment,
146- } ;
147- }
0 commit comments