-
Notifications
You must be signed in to change notification settings - Fork 422
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
116 additions
and
0 deletions.
There are no files selected for viewing
98 changes: 98 additions & 0 deletions
98
apps/webapp/app/routes/api.v1.projects.$projectRef.envvars.$slug.import.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
import { ActionFunctionArgs, json } from "@remix-run/server-runtime"; | ||
import { ImportEnvironmentVariablesRequestBody } from "@trigger.dev/core/v3"; | ||
import { parse } from "dotenv"; | ||
import { z } from "zod"; | ||
import { prisma } from "~/db.server"; | ||
import { findProjectByRef } from "~/models/project.server"; | ||
import { authenticateApiRequestWithPersonalAccessToken } from "~/services/personalAccessToken.server"; | ||
import { EnvironmentVariablesRepository } from "~/v3/environmentVariables/environmentVariablesRepository.server"; | ||
|
||
const ParamsSchema = z.object({ | ||
projectRef: z.string(), | ||
slug: z.string(), | ||
}); | ||
|
||
export async function action({ params, request }: ActionFunctionArgs) { | ||
const parsedParams = ParamsSchema.safeParse(params); | ||
|
||
if (!parsedParams.success) { | ||
return json({ error: "Invalid params" }, { status: 400 }); | ||
} | ||
|
||
const authenticationResult = await authenticateApiRequestWithPersonalAccessToken(request); | ||
|
||
if (!authenticationResult) { | ||
return json({ error: "Invalid or Missing API key" }, { status: 401 }); | ||
} | ||
|
||
const user = await prisma.user.findUnique({ | ||
where: { | ||
id: authenticationResult.userId, | ||
}, | ||
}); | ||
|
||
if (!user) { | ||
return json({ error: "Invalid or Missing API key" }, { status: 401 }); | ||
} | ||
|
||
const project = await findProjectByRef(parsedParams.data.projectRef, user.id); | ||
|
||
if (!project) { | ||
return json({ error: "Project not found" }, { status: 404 }); | ||
} | ||
|
||
const environment = await prisma.runtimeEnvironment.findFirst({ | ||
where: { | ||
projectId: project.id, | ||
slug: parsedParams.data.slug, | ||
}, | ||
}); | ||
|
||
if (!environment) { | ||
return json({ error: "Environment not found" }, { status: 404 }); | ||
} | ||
|
||
const repository = new EnvironmentVariablesRepository(); | ||
|
||
const body = await parseImportBody(request); | ||
|
||
const result = await repository.create(project.id, user.id, { | ||
overwrite: body.overwrite === true ? true : false, | ||
environmentIds: [environment.id], | ||
variables: Object.entries(body.variables).map(([key, value]) => ({ | ||
key, | ||
value, | ||
})), | ||
}); | ||
|
||
if (result.success) { | ||
return json({ success: true }); | ||
} else { | ||
return json({ error: result.error, variableErrors: result.variableErrors }, { status: 400 }); | ||
} | ||
} | ||
|
||
async function parseImportBody(request: Request): Promise<ImportEnvironmentVariablesRequestBody> { | ||
const contentType = request.headers.get("content-type") ?? "application/json"; | ||
|
||
if (contentType.includes("application/octet-stream")) { | ||
// We have a "dotenv" formatted file uploaded | ||
const buffer = await request.arrayBuffer(); | ||
|
||
const variables = parse(Buffer.from(buffer)); | ||
|
||
const overwrite = request.headers.get("x-overwrite") === "true"; | ||
|
||
return { variables, overwrite }; | ||
} else { | ||
const rawBody = await request.json(); | ||
|
||
const body = ImportEnvironmentVariablesRequestBody.safeParse(rawBody); | ||
|
||
if (!body.success) { | ||
throw json({ error: "Invalid body" }, { status: 400 }); | ||
} | ||
|
||
return body.data; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters