Skip to content

Commit

Permalink
Rest endpoint to load by projectId (#1863)
Browse files Browse the repository at this point in the history
## Description

1. What is this PR about (link the issue and add a short description)
Add REST endpoint to get the latest buildId based on projectId:
`/rest/buildId/<projectId>`

Should return buildId or null (which means the project is not published
yet).

It is required for the new CLI.

## Steps for reproduction

1. click button
3. expect xyz

## Code Review

- [ ] hi @kof, I need you to do
  - conceptual review (architecture, feature-correctness)
  - detailed review (read every line)
  - test it on preview

## Before requesting a review

- [x] made a self-review
- [ ] added inline comments where things may be not obvious (the "why",
not "what")

## Before merging

- [x] tested locally and on preview environment (preview dev login:
5de6)
- [ ] updated [test
cases](https://github.com/webstudio-is/webstudio-builder/blob/main/apps/builder/docs/test-cases.md)
document
- [ ] added tests
- [ ] if any new env variables are added, added them to `.env.example`
and the `builder/env-check.js` if mandatory
  • Loading branch information
andrasbacsai authored Jun 28, 2023
1 parent 7632b05 commit f849cc5
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 0 deletions.
39 changes: 39 additions & 0 deletions apps/builder/app/routes/rest.buildId.$projectId.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { json, type LoaderArgs } from "@remix-run/node";
import { db as projectDb } from "@webstudio-is/project/index.server";
import { sentryException } from "~/shared/sentry";
import { createContext } from "~/shared/context.server";

export const loader = async ({
params,
request,
}: LoaderArgs): Promise<{ buildId: string | null }> => {
try {
const projectId = params.projectId;

if (projectId === undefined) {
throw json("Required project id", { status: 400 });
}

const context = await createContext(request);

const project = await projectDb.project.loadById(projectId, context);
const buildId = project.latestBuild?.buildId ?? null;

return {
buildId,
};
} catch (error) {
// If a Response is thrown, we're rethrowing it for Remix to handle.
// https://remix.run/docs/en/v1/api/conventions#throwing-responses-in-loaders
if (error instanceof Response) {
throw error;
}

sentryException({ error });

// We have no idea what happened, so we'll return a 500 error.
throw json(error instanceof Error ? error.message : String(error), {
status: 500,
});
}
};
4 changes: 4 additions & 0 deletions packages/project/src/db/project.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ export const loadById = async (
},
});

if (data === null) {
throw new Error(`Project ${projectId} not found`);
}

return Project.parse(data);
};

Expand Down

0 comments on commit f849cc5

Please sign in to comment.