diff --git a/package.json b/package.json index 551485f..0c07d30 100644 --- a/package.json +++ b/package.json @@ -46,6 +46,7 @@ "devDependencies": { "@kevisual/ai-center": "^0.0.3", "@kevisual/app-assistant": "workspace:*", + "@kevisual/xhs": "workspace:*", "@kevisual/types": "^0.0.9", "@kevisual/use-config": "^1.0.12", "@types/bun": "^1.2.11", diff --git a/packages/xhs/bun.config.mjs b/packages/xhs/bun.config.mjs index 6afa989..3fafda8 100644 --- a/packages/xhs/bun.config.mjs +++ b/packages/xhs/bun.config.mjs @@ -2,23 +2,19 @@ // https://bun.sh/docs/bundler // @ts-ignore import pkg from './package.json'; -// import { resolvePath as rp } from '@kevisual/use-config/env'; -import path from 'path'; -const rp = (resolvePath) => { - return path.resolve(process.cwd(), resolvePath); -}; +import { resolvePath as rp } from '@kevisual/use-config/env'; + // bun run src/index.ts -- await Bun.build({ target: 'node', format: 'esm', - entrypoints: [rp('src/index.js')], + entrypoints: [rp('src/index.ts')], outdir: rp('./dist'), naming: { entry: 'app.mjs', }, - define: { VERSION: JSON.stringify(pkg.version), }, - env: 'KEVISUAL_*', + external: ['sequelize'], }); diff --git a/packages/xhs/package.json b/packages/xhs/package.json index 69bc05f..66616ce 100644 --- a/packages/xhs/package.json +++ b/packages/xhs/package.json @@ -6,7 +6,7 @@ "types": "app.d.ts", "scripts": { "build": "bun run bun.config.mjs", - "postbuild": "dts -i src/index.js -o app.d.ts", + "postbuild": "dts -i src/index.ts -o app.d.ts", "cmd": "tsx src/test/command.ts ", "dts": "dts -i src/index.js -o app.d.ts" }, @@ -22,10 +22,21 @@ "packageManager": "pnpm@10.10.0", "type": "module", "devDependencies": { + "@kevisual/use-config": "^1.0.14", "@kevisual/xhs-core": "workspace:*", "@types/node": "^22.15.3" }, + "exports": { + ".": { + "import": "./dist/app.mjs", + "types": "./dist/app.d.ts" + }, + "./index": { + "import": "./src/index.ts" + } + }, "dependencies": { + "@kevisual/db": "^0.0.1", "nanoid": "^5.1.5", "sequelize": "^6.37.7", "sqlite3": "^5.1.7" diff --git a/packages/xhs/src/app.ts b/packages/xhs/src/app.ts index 9ea0c52..0ee8eda 100644 --- a/packages/xhs/src/app.ts +++ b/packages/xhs/src/app.ts @@ -1,5 +1,5 @@ -import { QueryRouterServer } from '@kevisual/router'; -import { XhsServices } from '@/services/xhs-services.ts'; +import { QueryRouterServer } from '@kevisual/router/browser'; +import { XhsServices } from '@kevisual/xhs/services/xhs-services.ts'; export const app = new QueryRouterServer(); export const xhsServices = new XhsServices(); diff --git a/packages/xhs/src/libs/xhs.ts b/packages/xhs/src/libs/xhs.ts index a38e3fa..d731176 100644 --- a/packages/xhs/src/libs/xhs.ts +++ b/packages/xhs/src/libs/xhs.ts @@ -49,7 +49,7 @@ export const getSign = async (signInfo: SignInfo, options?: SignOptions): Promis web_session: web_session, }), }).then((res) => res.json()); - return signs; + return signs as SignResponse; } catch (error) { return null; } @@ -161,7 +161,7 @@ export class XhsClient extends XhsClientBase { const url = '/api/sns/web/v1/you/mentions'; const response = await this.get( url, - { num: 20, cursor: '' }, + { num: num, cursor: '' }, { sign: this.sign.bind(this), needSign: true, diff --git a/packages/xhs/src/routes/fans/fans.ts b/packages/xhs/src/routes/fans/fans.ts index fdf51d9..b431559 100644 --- a/packages/xhs/src/routes/fans/fans.ts +++ b/packages/xhs/src/routes/fans/fans.ts @@ -1,5 +1,5 @@ -import { app, xhsServices } from '@/app.ts'; -import { Parse } from '@/libs/parse.ts'; +import { app, xhsServices } from '@kevisual/xhs/app.ts'; +import { Parse } from '@kevisual/xhs/libs/parse.ts'; app .route({ path: 'fans', diff --git a/packages/xhs/src/routes/mentions/mention.ts b/packages/xhs/src/routes/mentions/mention.ts index 3ebf846..37d9841 100644 --- a/packages/xhs/src/routes/mentions/mention.ts +++ b/packages/xhs/src/routes/mentions/mention.ts @@ -1,4 +1,4 @@ -import { app, xhsServices } from '@/app.ts'; +import { app, xhsServices } from '@kevisual/xhs/app.ts'; app .route({ diff --git a/packages/xhs/src/routes/mentions/unread-task.ts b/packages/xhs/src/routes/mentions/unread-task.ts index c8b1e55..e473f9d 100644 --- a/packages/xhs/src/routes/mentions/unread-task.ts +++ b/packages/xhs/src/routes/mentions/unread-task.ts @@ -1,6 +1,6 @@ -import { app, xhsServices } from '@/app.ts'; -import { Mention } from '@/libs/xhs-type/mention.ts'; -import { Parse } from '@/libs/parse.ts'; +import { app, xhsServices } from '@kevisual/xhs/app.ts'; +import { Mention } from '@kevisual/xhs/libs/xhs-type/mention.ts'; +import { Parse } from '@kevisual/xhs/libs/parse.ts'; app .route({ @@ -20,7 +20,7 @@ app path: 'mention', key: 'getMention', payload: { - num: 2, + num: unread_count, }, }); if (mentionRes.code === 200) { diff --git a/packages/xhs/src/services/xhs-db/comment.ts b/packages/xhs/src/services/xhs-db/comment.ts index 5c402b9..1a28643 100644 --- a/packages/xhs/src/services/xhs-db/comment.ts +++ b/packages/xhs/src/services/xhs-db/comment.ts @@ -1,6 +1,6 @@ -import { InitOptions, Model, Sequelize, SyncOptions } from 'sequelize'; +import { DBModel, Sequelize, SyncOptions } from '@kevisual/db'; import { DataTypes } from 'sequelize'; -export class Comment extends Model { +export class Comment extends DBModel { declare id: number; declare content: string; declare note_id: string; @@ -8,7 +8,7 @@ export class Comment extends Model { declare comment_id: string; declare created_at: Date; declare updated_at: Date; - static async initModel(sequelize: Sequelize, initOptions?: InitOptions, opts?: SyncOptions) { + static async initModel(sequelize: Sequelize, opts?: SyncOptions) { Comment.init( { id: { @@ -36,7 +36,6 @@ export class Comment extends Model { { sequelize, // passing the `sequelize` instance is required modelName: 'Comment', // we need to choose the model name - ...initOptions, }, ); await Comment.sync(opts || {}); diff --git a/packages/xhs/src/services/xhs-services.ts b/packages/xhs/src/services/xhs-services.ts index c0dc16f..fe43f95 100644 --- a/packages/xhs/src/services/xhs-services.ts +++ b/packages/xhs/src/services/xhs-services.ts @@ -1,6 +1,6 @@ -import { XhsClient } from '@/libs/xhs.ts'; +import { XhsClient } from '@kevisual/xhs/libs/xhs.ts'; import { Sequelize } from 'sequelize'; -import { createSequelize } from '@/services/xhs-db/db.ts'; +import { createSequelize } from '@kevisual/xhs/services/xhs-db/db.ts'; import path from 'node:path'; import fs from 'node:fs'; @@ -22,6 +22,10 @@ type XhsClientMap = { type XhsServicesOptions = { root?: string; }; +/** + * @description XhsServices is a singleton class that manages the XhsClient instances. + * It is used to create and manage the XhsClient instances. + */ export class XhsServices { map: Map = new Map(); root: string = 'root'; diff --git a/packages/xhs/src/test/db/comment.ts b/packages/xhs/src/test/db/comment.ts index 9a54c49..81b6aa7 100644 --- a/packages/xhs/src/test/db/comment.ts +++ b/packages/xhs/src/test/db/comment.ts @@ -1,4 +1,4 @@ -import { Comment } from '@/services/xhs-db/comment.ts'; +import { Comment } from '@kevisual/xhs/services/xhs-db/comment.ts'; import { Sequelize, Model, DataTypes, InferAttributes, InferCreationAttributes } from 'sequelize'; import path from 'path'; diff --git a/packages/xhs/src/test/run/mention.ts b/packages/xhs/src/test/run/mention.ts index 63b1bf7..d3ac0a1 100644 --- a/packages/xhs/src/test/run/mention.ts +++ b/packages/xhs/src/test/run/mention.ts @@ -1,4 +1,4 @@ -import { app } from '@/index.ts'; +import { app } from '@kevisual/xhs/index.ts'; app.parse({ path: 'mention', diff --git a/packages/xhs/test2.db b/packages/xhs/test2.db deleted file mode 100644 index 024c4a0..0000000 Binary files a/packages/xhs/test2.db and /dev/null differ diff --git a/packages/xhs/tsconfig.json b/packages/xhs/tsconfig.json index bae175b..70f615a 100644 --- a/packages/xhs/tsconfig.json +++ b/packages/xhs/tsconfig.json @@ -5,13 +5,8 @@ "lib": [ "ESNext" ], - "emitDecoratorMetadata": true, - "experimentalDecorators": true, - "types": [ - "node_modules/@types" - ], "paths": { - "@/*": [ + "@kevisual/xhs/*": [ "src/*" ] } diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 6d8d689..2705b91 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -42,6 +42,9 @@ importers: '@kevisual/types': specifier: ^0.0.9 version: 0.0.9 + '@kevisual/xhs': + specifier: workspace:* + version: link:packages/xhs '@types/bun': specifier: ^1.2.11 version: 1.2.11 @@ -109,6 +112,9 @@ importers: packages/xhs: dependencies: + '@kevisual/db': + specifier: ^0.0.1 + version: 0.0.1 nanoid: specifier: ^5.1.5 version: 5.1.5 @@ -119,6 +125,9 @@ importers: specifier: ^5.1.7 version: 5.1.7 devDependencies: + '@kevisual/use-config': + specifier: ^1.0.14 + version: 1.0.14(dotenv@16.5.0) '@kevisual/xhs-core': specifier: workspace:* version: link:../xhs-core @@ -211,6 +220,9 @@ packages: pg: ^8.13.3 sequelize: ^6.37.5 + '@kevisual/db@0.0.1': + resolution: {integrity: sha512-FgDS5FzcU5Um2UV6EYQN4+D4R24M6/A7DANMPpvTpRioS94apKsnthZQWNL65e6gPc4ca3ys8zygrF6rd3ZA7Q==} + '@kevisual/dts@0.0.2': resolution: {integrity: sha512-AFULZR011hH8g6F30djbUlIgbG2y4AbEXml+1jotLC0Mf87apZ7LYlb9amxVSiPqOmtXYKETsI47RDzgihBpLw==} hasBin: true @@ -229,6 +241,11 @@ packages: peerDependencies: dotenv: ^16.4.7 + '@kevisual/use-config@1.0.14': + resolution: {integrity: sha512-U4fmPFJre+Ph+hdg+EKVaRGxdpD4T4ZoOz5QLEyC6o3ekg8NC4n1i7Myo1ektqATyoG9Y0q/bJdOhiJwnLEt4g==} + peerDependencies: + dotenv: ^16.4.7 + '@ljharb/resumer@0.1.3': resolution: {integrity: sha512-d+tsDgfkj9X5QTriqM4lKesCkMMJC3IrbPKHvayP00ELx2axdXvDfWkqjxrLXIzGcQzmj7VAUT1wopqARTvafw==} engines: {node: '>= 0.4'} @@ -2099,6 +2116,8 @@ snapshots: - supports-color - utf-8-validate + '@kevisual/db@0.0.1': {} + '@kevisual/dts@0.0.2(typescript@5.8.3)': dependencies: rollup: 4.40.1 @@ -2122,6 +2141,11 @@ snapshots: '@kevisual/load': 0.0.6 dotenv: 16.5.0 + '@kevisual/use-config@1.0.14(dotenv@16.5.0)': + dependencies: + '@kevisual/load': 0.0.6 + dotenv: 16.5.0 + '@ljharb/resumer@0.1.3': dependencies: '@ljharb/through': 2.3.14 diff --git a/src/services/xhs-task.ts b/src/services/xhs-task.ts new file mode 100644 index 0000000..d5f4b94 --- /dev/null +++ b/src/services/xhs-task.ts @@ -0,0 +1,33 @@ +import { Queue } from 'bullmq'; +import { app } from '@kevisual/xhs/index'; +import { QueryRouterServer } from '@kevisual/router'; + +type XhsTaskOptions = { + queue: Queue; +}; +export class XhsTask { + queue: Queue; + constructor(opts: XhsTaskOptions) { + this.queue = opts.queue; + } + async getUnread() { + const res = await app.call({ + path: 'mention', + key: 'getUnread', + }); + } +} + +const qs = new QueryRouterServer(); + +qs.route({ + path: 'task', + key: 'getUnread', +}) + .define(async (ctx) => { + const unread_count = 0; + if (unread_count > 0) { + + } + }) + .addTo(qs); diff --git a/src/task/common.ts b/src/task/common.ts index 2af73a5..9b1ff9c 100644 --- a/src/task/common.ts +++ b/src/task/common.ts @@ -1,2 +1,3 @@ // https://edith.xiaohongshu.com/api/sns/web/unread_count export const XHS_GET_UNREAD = 'unread_count'; +export const XHS_QUEUE_NAME = 'XHS_QUEUE'; \ No newline at end of file diff --git a/src/task/task.ts b/src/task/task.ts index 109ddce..1412e25 100644 --- a/src/task/task.ts +++ b/src/task/task.ts @@ -1,7 +1,8 @@ import { redis } from '@/modules/redis.ts'; import { Queue } from 'bullmq'; import { nanoid } from 'nanoid'; -const XHS_QUEUE_NAME = 'XHS_QUEUE'; +import { XHS_QUEUE_NAME } from '@/task/common.ts'; + export const queue = new Queue(XHS_QUEUE_NAME, { connection: redis, }); diff --git a/src/test/router.ts b/src/test/router.ts new file mode 100644 index 0000000..f1b3c11 --- /dev/null +++ b/src/test/router.ts @@ -0,0 +1,15 @@ +import { App, QueryRouterServer } from '@kevisual/router'; + +export const app = new App(); +app + .route({ + path: 'k', + key: 'k', + }) + .define(async (ctx) => { + ctx.body = 'hello world'; + }) + .addTo(app); + +const res = await app.router.queryRoute({ path: 'k', key: 'k' }); +console.log('res', res);