temp
This commit is contained in:
@@ -1,6 +1,5 @@
|
|||||||
import { App, CustomError } from '@kevisual/router';
|
import { App as AppType, AppList, AppData } from './module/app-drizzle.ts';
|
||||||
import { App as AppType, AppList, AppData, AppHelper } from './module/app-drizzle.ts';
|
import { app, db, schema } from '@/app.ts';
|
||||||
import { app, redis, db, schema } from '@/app.ts';
|
|
||||||
import { uniqBy } from 'es-toolkit';
|
import { uniqBy } from 'es-toolkit';
|
||||||
import { getUidByUsername, prefixFix } from './util.ts';
|
import { getUidByUsername, prefixFix } from './util.ts';
|
||||||
import { deleteFiles, getMinioListAndSetToAppList } from '../file/index.ts';
|
import { deleteFiles, getMinioListAndSetToAppList } from '../file/index.ts';
|
||||||
@@ -9,18 +8,26 @@ import { User } from '@/models/user.ts';
|
|||||||
import { callDetectAppVersion } from './export.ts';
|
import { callDetectAppVersion } from './export.ts';
|
||||||
import { eq, and, desc } from 'drizzle-orm';
|
import { eq, and, desc } from 'drizzle-orm';
|
||||||
import { randomUUID } from 'crypto';
|
import { randomUUID } from 'crypto';
|
||||||
|
import { z } from 'zod';
|
||||||
app
|
app
|
||||||
.route({
|
.route({
|
||||||
path: 'app',
|
path: 'app',
|
||||||
key: 'list',
|
key: 'list',
|
||||||
middleware: ['auth'],
|
middleware: ['auth'],
|
||||||
description: '获取应用列表,根据key进行过滤',
|
description: '获取应用列表,根据key进行过滤',
|
||||||
|
metadata: {
|
||||||
|
args: {
|
||||||
|
data: z.object({
|
||||||
|
key: z.string().describe('应用的唯一标识')
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
})
|
})
|
||||||
.define(async (ctx) => {
|
.define(async (ctx) => {
|
||||||
const tokenUser = ctx.state.tokenUser;
|
const tokenUser = ctx.state.tokenUser;
|
||||||
const data = ctx.query.data || {};
|
const data = ctx.query.data || {};
|
||||||
if (!data.key) {
|
if (!data.key) {
|
||||||
throw new CustomError('key is required');
|
ctx.throw('key is required');
|
||||||
}
|
}
|
||||||
const list = await db.select()
|
const list = await db.select()
|
||||||
.from(schema.kvAppList)
|
.from(schema.kvAppList)
|
||||||
@@ -47,7 +54,7 @@ app
|
|||||||
const id = ctx.query.id;
|
const id = ctx.query.id;
|
||||||
const { key, version, create = false } = ctx.query?.data || {};
|
const { key, version, create = false } = ctx.query?.data || {};
|
||||||
if (!id && (!key || !version)) {
|
if (!id && (!key || !version)) {
|
||||||
throw new CustomError('id is required');
|
ctx.throw('id is required');
|
||||||
}
|
}
|
||||||
let appListModel: AppList | undefined;
|
let appListModel: AppList | undefined;
|
||||||
if (id) {
|
if (id) {
|
||||||
@@ -130,13 +137,13 @@ app
|
|||||||
ctx.body = newApp;
|
ctx.body = newApp;
|
||||||
setExpire(newApp.id, 'test');
|
setExpire(newApp.id, 'test');
|
||||||
} else {
|
} else {
|
||||||
throw new CustomError('app not found');
|
ctx.throw('app not found');
|
||||||
}
|
}
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!rest.key) {
|
if (!rest.key) {
|
||||||
throw new CustomError('key is required');
|
ctx.throw('key is required');
|
||||||
}
|
}
|
||||||
const newApps = await db.insert(schema.kvAppList).values({ id: randomUUID(), data, ...rest, uid: tokenUser.id }).returning();
|
const newApps = await db.insert(schema.kvAppList).values({ id: randomUUID(), data, ...rest, uid: tokenUser.id }).returning();
|
||||||
ctx.body = newApps[0];
|
ctx.body = newApps[0];
|
||||||
@@ -155,12 +162,12 @@ app
|
|||||||
const id = ctx.query.id;
|
const id = ctx.query.id;
|
||||||
const deleteFile = !!ctx.query.deleteFile; // 是否删除文件, 默认不删除
|
const deleteFile = !!ctx.query.deleteFile; // 是否删除文件, 默认不删除
|
||||||
if (!id) {
|
if (!id) {
|
||||||
throw new CustomError('id is required');
|
ctx.throw('id is required');
|
||||||
}
|
}
|
||||||
const apps = await db.select().from(schema.kvAppList).where(eq(schema.kvAppList.id, id)).limit(1);
|
const apps = await db.select().from(schema.kvAppList).where(eq(schema.kvAppList.id, id)).limit(1);
|
||||||
const app = apps[0];
|
const app = apps[0];
|
||||||
if (!app) {
|
if (!app) {
|
||||||
throw new CustomError('app not found');
|
ctx.throw('app not found');
|
||||||
}
|
}
|
||||||
const ams = await db.select().from(schema.kvApp).where(and(
|
const ams = await db.select().from(schema.kvApp).where(and(
|
||||||
eq(schema.kvApp.key, app.key),
|
eq(schema.kvApp.key, app.key),
|
||||||
@@ -168,10 +175,10 @@ app
|
|||||||
)).limit(1);
|
)).limit(1);
|
||||||
const am = ams[0];
|
const am = ams[0];
|
||||||
if (!am) {
|
if (!am) {
|
||||||
throw new CustomError('app not found');
|
ctx.throw('app not found');
|
||||||
}
|
}
|
||||||
if (am.version === app.version) {
|
if (am.version === app.version) {
|
||||||
throw new CustomError('app is published');
|
ctx.throw('app is published');
|
||||||
}
|
}
|
||||||
const appData = app.data as AppData;
|
const appData = app.data as AppData;
|
||||||
const files = appData.files || [];
|
const files = appData.files || [];
|
||||||
@@ -197,10 +204,10 @@ app
|
|||||||
const tokenUser = ctx.state.tokenUser;
|
const tokenUser = ctx.state.tokenUser;
|
||||||
const { appKey, files, version, username, description } = ctx.query.data;
|
const { appKey, files, version, username, description } = ctx.query.data;
|
||||||
if (!appKey) {
|
if (!appKey) {
|
||||||
throw new CustomError('appKey is required');
|
ctx.throw('appKey is required');
|
||||||
}
|
}
|
||||||
if (!files || !files.length) {
|
if (!files || !files.length) {
|
||||||
throw new CustomError('files is required');
|
ctx.throw('files is required');
|
||||||
}
|
}
|
||||||
let uid = tokenUser.id;
|
let uid = tokenUser.id;
|
||||||
let userPrefix = tokenUser.username;
|
let userPrefix = tokenUser.username;
|
||||||
@@ -214,7 +221,7 @@ app
|
|||||||
}
|
}
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.log('getUserByToken error', e);
|
console.log('getUserByToken error', e);
|
||||||
throw new CustomError('user not found');
|
ctx.throw('user not found');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
const ams = await db.select().from(schema.kvApp).where(and(
|
const ams = await db.select().from(schema.kvApp).where(and(
|
||||||
@@ -276,7 +283,7 @@ app
|
|||||||
ctx.body = prefixFix(res, userPrefix);
|
ctx.body = prefixFix(res, userPrefix);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.log('update error', e);
|
console.log('update error', e);
|
||||||
throw new CustomError(e.message);
|
ctx.throw(e.message);
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
.addTo(app);
|
.addTo(app);
|
||||||
@@ -292,7 +299,7 @@ app
|
|||||||
const tokenUser = ctx.state.tokenUser;
|
const tokenUser = ctx.state.tokenUser;
|
||||||
const { id, username, appKey, version, detect } = ctx.query.data;
|
const { id, username, appKey, version, detect } = ctx.query.data;
|
||||||
if (!id && !appKey) {
|
if (!id && !appKey) {
|
||||||
throw new CustomError('id or appKey is required');
|
ctx.throw('id or appKey is required');
|
||||||
}
|
}
|
||||||
|
|
||||||
const uid = await getUidByUsername(app, ctx, username);
|
const uid = await getUidByUsername(app, ctx, username);
|
||||||
@@ -380,10 +387,10 @@ app
|
|||||||
)).limit(1);
|
)).limit(1);
|
||||||
app = apps[0];
|
app = apps[0];
|
||||||
} else {
|
} else {
|
||||||
throw new CustomError('user or key is required');
|
ctx.throw('user or key is required');
|
||||||
}
|
}
|
||||||
if (!app) {
|
if (!app) {
|
||||||
throw new CustomError('app not found');
|
ctx.throw('app not found');
|
||||||
}
|
}
|
||||||
ctx.body = app;
|
ctx.body = app;
|
||||||
})
|
})
|
||||||
@@ -400,7 +407,7 @@ app
|
|||||||
const tokenUser = ctx.state.tokenUser;
|
const tokenUser = ctx.state.tokenUser;
|
||||||
const { key, version } = ctx.query?.data || {};
|
const { key, version } = ctx.query?.data || {};
|
||||||
if (!key || !version) {
|
if (!key || !version) {
|
||||||
throw new CustomError('key and version are required');
|
ctx.throw('key and version are required');
|
||||||
}
|
}
|
||||||
const files = await getMinioListAndSetToAppList({ username: tokenUser.username, appKey: key, version });
|
const files = await getMinioListAndSetToAppList({ username: tokenUser.username, appKey: key, version });
|
||||||
ctx.body = files;
|
ctx.body = files;
|
||||||
@@ -418,7 +425,7 @@ app
|
|||||||
const tokenUser = ctx.state.tokenUser;
|
const tokenUser = ctx.state.tokenUser;
|
||||||
let { appKey, version, username } = ctx.query?.data || {};
|
let { appKey, version, username } = ctx.query?.data || {};
|
||||||
if (!appKey || !version) {
|
if (!appKey || !version) {
|
||||||
throw new CustomError('appKey and version are required');
|
ctx.throw('appKey and version are required');
|
||||||
}
|
}
|
||||||
const uid = await getUidByUsername(app, ctx, username);
|
const uid = await getUidByUsername(app, ctx, username);
|
||||||
const appLists = await db.select().from(schema.kvAppList).where(and(
|
const appLists = await db.select().from(schema.kvAppList).where(and(
|
||||||
|
|||||||
Reference in New Issue
Block a user