From 4e2f9b7decc9ce521f42631f6c6060b210268dd2 Mon Sep 17 00:00:00 2001 From: Shigma Date: Sun, 12 May 2024 16:06:50 +0800 Subject: [PATCH] feat(http): extract http plugin --- packages/koishi/package.json | 4 ++- packages/loader/src/index.ts | 20 +++++++++--- plugins/http/index.cjs | 1 + plugins/http/index.d.ts | 1 + plugins/http/index.mjs | 1 + plugins/http/package.json | 59 ++++++++++++++++++++++++++++++++++ plugins/http/readme.md | 3 ++ plugins/proxy-agent/.npmignore | 2 -- 8 files changed, 83 insertions(+), 8 deletions(-) create mode 100644 plugins/http/index.cjs create mode 100644 plugins/http/index.d.ts create mode 100644 plugins/http/index.mjs create mode 100644 plugins/http/package.json create mode 100644 plugins/http/readme.md delete mode 100644 plugins/proxy-agent/.npmignore diff --git a/packages/koishi/package.json b/packages/koishi/package.json index ead6d77d68..dc8306d651 100644 --- a/packages/koishi/package.json +++ b/packages/koishi/package.json @@ -47,16 +47,18 @@ "dependencies": { "@koishijs/core": "4.17.5", "@koishijs/loader": "4.5.5", + "@koishijs/plugin-http": "^0.4.2", "@koishijs/plugin-proxy-agent": "^0.3.0", "@koishijs/plugin-server": "^3.2.1", "@koishijs/utils": "^7.2.1", - "@satorijs/satori": "^3.7.0", + "@satorijs/core": "^4.0.0", "cac": "^6.7.14", "kleur": "^4.1.5" }, "yakumo": { "tsc": { "ignore": [ + "@koishijs/plugin-http", "@koishijs/plugin-proxy-agent", "@koishijs/plugin-server" ] diff --git a/packages/loader/src/index.ts b/packages/loader/src/index.ts index 4f715eea94..3b60f0c173 100644 --- a/packages/loader/src/index.ts +++ b/packages/loader/src/index.ts @@ -45,15 +45,25 @@ export default class NodeLoader extends Loader { async migrate() { try { let isDirty = false - const manifest = JSON.parse(await fs.readFile('package.json', 'utf8')) + const meta = JSON.parse(await fs.readFile('package.json', 'utf8')) const require = createRequire(__filename) const deps = require('koishi/package.json').dependencies function addDep(name: string) { - manifest.dependencies[name] = deps[name] + meta.dependencies[name] = deps[name] isDirty = true } - if (!manifest.dependencies['@koishijs/plugin-proxy-agent']) { + if (!meta.dependencies['@koishijs/plugin-http']) { + const { request = {} } = this.config as any + delete this.config['request'] + this.config.plugins = { + http: request, + ...this.config.plugins, + } + addDep('@koishijs/plugin-http') + } + + if (!meta.dependencies['@koishijs/plugin-proxy-agent']) { this.config.plugins = { 'proxy-agent': {}, ...this.config.plugins, @@ -75,8 +85,8 @@ export default class NodeLoader extends Loader { } if (isDirty) { - manifest.dependencies = Object.fromEntries(Object.entries(manifest.dependencies).sort(([a], [b]) => a.localeCompare(b))) - await fs.writeFile('package.json', JSON.stringify(manifest, null, 2) + '\n') + meta.dependencies = Object.fromEntries(Object.entries(meta.dependencies).sort(([a], [b]) => a.localeCompare(b))) + await fs.writeFile('package.json', JSON.stringify(meta, null, 2) + '\n') } } catch (error) { logger.warn('failed to migrate manifest') diff --git a/plugins/http/index.cjs b/plugins/http/index.cjs new file mode 100644 index 0000000000..36107851ed --- /dev/null +++ b/plugins/http/index.cjs @@ -0,0 +1 @@ +module.exports = require('@cordisjs/plugin-http') diff --git a/plugins/http/index.d.ts b/plugins/http/index.d.ts new file mode 100644 index 0000000000..c64e6abdc1 --- /dev/null +++ b/plugins/http/index.d.ts @@ -0,0 +1 @@ +export * from '@cordisjs/plugin-http' diff --git a/plugins/http/index.mjs b/plugins/http/index.mjs new file mode 100644 index 0000000000..c64e6abdc1 --- /dev/null +++ b/plugins/http/index.mjs @@ -0,0 +1 @@ +export * from '@cordisjs/plugin-http' diff --git a/plugins/http/package.json b/plugins/http/package.json new file mode 100644 index 0000000000..6d0b853d1b --- /dev/null +++ b/plugins/http/package.json @@ -0,0 +1,59 @@ +{ + "name": "@koishijs/plugin-http", + "description": "HTTP and WebSocket client for Koishi", + "version": "0.4.2", + "type": "module", + "main": "index.cjs", + "module": "index.mjs", + "types": "index.d.ts", + "exports": { + ".": { + "require": "./index.cjs", + "import": "./index.mjs", + "types": "./index.d.ts" + }, + "./package.json": "./package.json" + }, + "files": [ + "index.cjs", + "index.mjs", + "index.d.ts" + ], + "author": "Shigma ", + "license": "MIT", + "repository": { + "type": "git", + "url": "git+https://github.com/cordiverse/http.git", + "directory": "packages/core" + }, + "bugs": { + "url": "https://github.com/cordiverse/http/issues" + }, + "homepage": "https://github.com/cordiverse/http", + "keywords": [ + "http", + "fetch", + "axios", + "http", + "undici", + "client", + "request", + "cordis", + "plugin" + ], + "koishi": { + "description": { + "en": "HTTP and WebSocket client for Koishi", + "zh": "HTTP 与 WebSocket 基础服务" + } + }, + "peerDependencies": { + "koishi": "^4.17.6" + }, + "devDependencies": { + "koishi": "^4.17.6" + }, + "dependencies": { + "@cordisjs/plugin-http": "^0.4.2" + } +} diff --git a/plugins/http/readme.md b/plugins/http/readme.md new file mode 100644 index 0000000000..7fe60d0759 --- /dev/null +++ b/plugins/http/readme.md @@ -0,0 +1,3 @@ +# @koishijs/plugin-http + +Fetch-based axios-style HTTP client for [Koishi](https://koishi.chat). diff --git a/plugins/proxy-agent/.npmignore b/plugins/proxy-agent/.npmignore deleted file mode 100644 index 7e5fcbc18b..0000000000 --- a/plugins/proxy-agent/.npmignore +++ /dev/null @@ -1,2 +0,0 @@ -.DS_Store -tsconfig.tsbuildinfo