1234567891011121314151617181920212223242526272829303132333435363738394041424344 |
- // Next.js API route support: https://nextjs.org/docs/api-routes/introduction
- import type { NextApiRequest, NextApiResponse } from "next";
- import { Low, JSONFile } from "lowdb";
- type IDBData = {
- buildObj: {
- [key: string]: string[];
- };
- };
- const adapter = new JSONFile<IDBData>("./db.json");
- const db = new Low(adapter);
- interface IData {
- buildId: string;
- }
- // SSE 实时安装进度显示,https://www.ruanyifeng.com/blog/2017/05/server-sent_events.html
- export default async function handler(
- req: NextApiRequest,
- res: NextApiResponse<IData>
- ) {
- // Get data submitted in request's body.
- const body = req.body;
- const buildId = randomId();
- await db.read();
- db.data = db.data || { buildObj: {} };
- const { buildObj } = db.data;
- buildObj[buildId] = body.buildList;
- await db.write();
- res.status(200).json({ buildId: buildId });
- }
- function randomId(): string {
- function S4() {
- return (((1 + Math.random()) * 0x10000) | 0).toString(16).substring(1);
- }
- return S4() + new Date().getTime() + S4() + S4();
- }
|