Refactor code structure for improved readability and maintainability
This commit is contained in:
@@ -1,13 +1,20 @@
|
||||
import { app, db, schema } from '@/app.ts';
|
||||
import { App, AppData } from '../module/app-drizzle.ts';
|
||||
import { AppDomain, AppDomainHelper } from '../module/app-domain-drizzle.ts';
|
||||
import { eq, and } from 'drizzle-orm';
|
||||
import { randomUUID } from 'crypto';
|
||||
import z from 'zod';
|
||||
|
||||
app
|
||||
.route({
|
||||
path: 'app',
|
||||
key: 'getDomainApp',
|
||||
description: '根据域名获取应用信息',
|
||||
metadata: {
|
||||
args: {
|
||||
data: z.object({
|
||||
domain: z.string().describe('域名'),
|
||||
})
|
||||
}
|
||||
}
|
||||
})
|
||||
.define(async (ctx) => {
|
||||
const { domain } = ctx.query.data;
|
||||
@@ -39,7 +46,7 @@ app
|
||||
if (!domain || !appId) {
|
||||
ctx.throw(400, 'domain and appId are required');
|
||||
}
|
||||
const newDomains = await db.insert(schema.kvAppDomain).values({ id: randomUUID(), domain, appId, uid }).returning();
|
||||
const newDomains = await db.insert(schema.kvAppDomain).values({ domain, appId, uid }).returning();
|
||||
const domainInfo = newDomains[0];
|
||||
ctx.body = domainInfo;
|
||||
return ctx;
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
import { app, db, schema } from '@/app.ts';
|
||||
import { AppDomain, AppDomainHelper } from '../module/app-domain-drizzle.ts';
|
||||
import { eq } from 'drizzle-orm';
|
||||
import { randomUUID } from 'crypto';
|
||||
import z from 'zod';
|
||||
|
||||
app
|
||||
@@ -78,7 +77,7 @@ app
|
||||
try {
|
||||
if (!domainInfo) {
|
||||
await checkAppId();
|
||||
const newDomains = await db.insert(schema.kvAppDomain).values({ id: randomUUID(), domain, data: {}, ...rest }).returning();
|
||||
const newDomains = await db.insert(schema.kvAppDomain).values({ domain, data: {}, ...rest }).returning();
|
||||
domainInfo = newDomains[0];
|
||||
} else {
|
||||
if (rest.status && domainInfo.status !== rest.status) {
|
||||
|
||||
@@ -7,7 +7,6 @@ import { setExpire } from './revoke.ts';
|
||||
import { User } from '@/models/user.ts';
|
||||
import { callDetectAppVersion } from './export.ts';
|
||||
import { eq, and, desc } from 'drizzle-orm';
|
||||
import { randomUUID } from 'crypto';
|
||||
import { z } from 'zod';
|
||||
app
|
||||
.route({
|
||||
@@ -70,7 +69,6 @@ app
|
||||
}
|
||||
if (!appListModel && create) {
|
||||
const newApps = await db.insert(schema.kvAppList).values({
|
||||
id: randomUUID(),
|
||||
key,
|
||||
version,
|
||||
uid: tokenUser.id,
|
||||
@@ -84,7 +82,6 @@ app
|
||||
const appModel = appModels[0];
|
||||
if (!appModel) {
|
||||
await db.insert(schema.kvApp).values({
|
||||
id: randomUUID(),
|
||||
key,
|
||||
uid: tokenUser.id,
|
||||
user: tokenUser.username,
|
||||
@@ -145,7 +142,7 @@ app
|
||||
if (!rest.key) {
|
||||
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({ data, ...rest, uid: tokenUser.id }).returning();
|
||||
ctx.body = newApps[0];
|
||||
return ctx;
|
||||
})
|
||||
@@ -233,7 +230,6 @@ app
|
||||
if (!am) {
|
||||
appIsNew = true;
|
||||
const newAms = await db.insert(schema.kvApp).values({
|
||||
id: randomUUID(),
|
||||
user: userPrefix,
|
||||
key: appKey,
|
||||
uid,
|
||||
@@ -255,7 +251,6 @@ app
|
||||
let app = apps[0];
|
||||
if (!app) {
|
||||
const newApps = await db.insert(schema.kvAppList).values({
|
||||
id: randomUUID(),
|
||||
key: appKey,
|
||||
version,
|
||||
uid: uid,
|
||||
@@ -436,7 +431,6 @@ app
|
||||
let appList = appLists[0];
|
||||
if (!appList) {
|
||||
const newAppLists = await db.insert(schema.kvAppList).values({
|
||||
id: randomUUID(),
|
||||
key: appKey,
|
||||
version,
|
||||
uid,
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
import { app, db, schema } from '@/app.ts';
|
||||
import { randomUUID } from 'crypto';
|
||||
import { oss } from '@/app.ts';
|
||||
import { User } from '@/models/user.ts';
|
||||
import { customAlphabet } from 'nanoid';
|
||||
@@ -10,9 +9,7 @@ const number = '0123456789';
|
||||
const randomId = customAlphabet(letter + number, 16);
|
||||
const getShareUser = async () => {
|
||||
const shareUser = await User.findOne({
|
||||
where: {
|
||||
username: 'share',
|
||||
},
|
||||
username: 'share',
|
||||
});
|
||||
return shareUser?.id || '';
|
||||
};
|
||||
@@ -64,7 +61,6 @@ app
|
||||
},
|
||||
];
|
||||
const appModels = await db.insert(schema.kvApp).values({
|
||||
id: randomUUID(),
|
||||
title,
|
||||
description,
|
||||
version,
|
||||
@@ -82,7 +78,6 @@ app
|
||||
}).returning();
|
||||
const appModel = appModels[0];
|
||||
const appVersionModels = await db.insert(schema.kvAppList).values({
|
||||
id: randomUUID(),
|
||||
data: {
|
||||
files: files,
|
||||
},
|
||||
|
||||
@@ -3,6 +3,7 @@ import { app, db, schema } from '@/app.ts';
|
||||
import { setExpire } from './revoke.ts';
|
||||
import { deleteFileByPrefix } from '../file/index.ts';
|
||||
import { eq, and, desc } from 'drizzle-orm';
|
||||
import z from 'zod';
|
||||
|
||||
app
|
||||
.route({
|
||||
@@ -42,6 +43,14 @@ app
|
||||
key: 'get',
|
||||
middleware: ['auth'],
|
||||
description: '获取用户应用,可以指定id或者key',
|
||||
metadata: {
|
||||
args: {
|
||||
id: z.string().optional(),
|
||||
data: z.object({
|
||||
key: z.string().optional(),
|
||||
}).optional(),
|
||||
}
|
||||
}
|
||||
})
|
||||
.define(async (ctx) => {
|
||||
const tokenUser = ctx.state.tokenUser;
|
||||
@@ -79,6 +88,20 @@ app
|
||||
key: 'update',
|
||||
middleware: ['auth'],
|
||||
description: '创建或更新用户应用,参数在data中传入',
|
||||
metadata: {
|
||||
args: {
|
||||
data: z.object({
|
||||
id: z.string().optional(),
|
||||
key: z.string().optional(),
|
||||
title: z.string().optional(),
|
||||
description: z.string().optional(),
|
||||
version: z.string().optional(),
|
||||
proxy: z.boolean().optional(),
|
||||
share: z.boolean().optional(),
|
||||
status: z.enum(['running', 'stopped']).optional(),
|
||||
}),
|
||||
}
|
||||
}
|
||||
})
|
||||
.define(async (ctx) => {
|
||||
const tokenUser = ctx.state.tokenUser;
|
||||
@@ -138,6 +161,12 @@ app
|
||||
key: 'delete',
|
||||
middleware: ['auth'],
|
||||
description: '删除用户应用,可以指定id,参数:deleteFile表示是否删除文件,默认不删除',
|
||||
metadata: {
|
||||
args: {
|
||||
id: z.string().optional().describe('应用id'),
|
||||
deleteFile: z.boolean().optional().describe('是否删除文件, 默认不删除'),
|
||||
}
|
||||
}
|
||||
})
|
||||
.define(async (ctx) => {
|
||||
const tokenUser = ctx.state.tokenUser;
|
||||
@@ -172,6 +201,11 @@ app
|
||||
path: 'user-app',
|
||||
key: 'test',
|
||||
description: '对user-app的数据进行测试, 获取版本的信息',
|
||||
metadata: {
|
||||
args: {
|
||||
id: z.string().optional().describe('应用id'),
|
||||
}
|
||||
}
|
||||
})
|
||||
.define(async (ctx) => {
|
||||
const id = ctx.query.id;
|
||||
|
||||
Reference in New Issue
Block a user