mirror of
https://github.com/YuzuZensai/Pien-Studio.git
synced 2026-09-02 14:18:35 +00:00
♻️ refactor!: massive refactor
This commit is contained in:
@@ -9,7 +9,7 @@
|
||||
"typecheck": "tsc --noEmit"
|
||||
},
|
||||
"dependencies": {
|
||||
"@pien-studio/types": "workspace:*",
|
||||
"@pien-studio/contracts": "workspace:*",
|
||||
"elysia": "^1.1.25",
|
||||
"zod": "^4.4.3"
|
||||
},
|
||||
|
||||
@@ -21,7 +21,7 @@ describe("api endpoints", () => {
|
||||
it("returns token for valid device payload", async () => {
|
||||
const app = createApp();
|
||||
const response = await app.handle(
|
||||
new Request("http://localhost/auth/device", {
|
||||
new Request("http://localhost/v1/auth/device", {
|
||||
method: "POST",
|
||||
headers: { "content-type": "application/json" },
|
||||
body: JSON.stringify({ deviceId: "dev1234", locale: "en" }),
|
||||
|
||||
+34
-11
@@ -1,5 +1,12 @@
|
||||
import { Elysia } from "elysia";
|
||||
import { DeviceSessionSchema } from "@pien-studio/types";
|
||||
import { DeviceSessionRequestSchema } from "@pien-studio/contracts";
|
||||
|
||||
function errorResponse(code: string, message: string, status = 400) {
|
||||
return new Response(JSON.stringify({ error: { code, message } }), {
|
||||
status,
|
||||
headers: { "content-type": "application/json" },
|
||||
});
|
||||
}
|
||||
|
||||
export function createApp() {
|
||||
return new Elysia()
|
||||
@@ -8,25 +15,41 @@ export function createApp() {
|
||||
status: "ok",
|
||||
}))
|
||||
.get("/health", () => ({ ok: true, service: "pien-api" }))
|
||||
.post("/auth/device", ({ body }) => {
|
||||
const parsed = DeviceSessionSchema.safeParse(body);
|
||||
.get("/v1/health", () => ({ ok: true, service: "pien-api" }))
|
||||
.post("/v1/auth/device", ({ body }) => {
|
||||
const parsed = DeviceSessionRequestSchema.safeParse(body);
|
||||
if (!parsed.success) {
|
||||
return new Response(JSON.stringify({ error: "invalid_device_payload" }), {
|
||||
status: 400,
|
||||
});
|
||||
return errorResponse(
|
||||
"invalid_device_payload",
|
||||
"Device payload is invalid",
|
||||
);
|
||||
}
|
||||
return {
|
||||
token: `dev_${parsed.data.deviceId}`,
|
||||
scope: "local-sync",
|
||||
};
|
||||
})
|
||||
.get("/sync/bootstrap", () => ({
|
||||
.get("/v1/sync/bootstrap", () => ({
|
||||
replication: {
|
||||
pull: "/sync/pull",
|
||||
push: "/sync/push",
|
||||
strategy: "couch-compatible",
|
||||
pull: "/v1/sync/pull",
|
||||
push: "/v1/sync/push",
|
||||
strategy: "operation-log",
|
||||
},
|
||||
}));
|
||||
}))
|
||||
.post("/v1/sync/pull", () =>
|
||||
errorResponse(
|
||||
"sync_not_implemented",
|
||||
"Sync pull is not implemented yet",
|
||||
501,
|
||||
),
|
||||
)
|
||||
.post("/v1/sync/push", () =>
|
||||
errorResponse(
|
||||
"sync_not_implemented",
|
||||
"Sync push is not implemented yet",
|
||||
501,
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
if (import.meta.main) {
|
||||
|
||||
Reference in New Issue
Block a user