Skip to content

Commit

Permalink
feat(http): extract http plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
shigma committed May 12, 2024
1 parent 4d49bcc commit 4e2f9b7
Show file tree
Hide file tree
Showing 8 changed files with 83 additions and 8 deletions.
4 changes: 3 additions & 1 deletion packages/koishi/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
]
Expand Down
20 changes: 15 additions & 5 deletions packages/loader/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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')
Expand Down
1 change: 1 addition & 0 deletions plugins/http/index.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = require('@cordisjs/plugin-http')
1 change: 1 addition & 0 deletions plugins/http/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from '@cordisjs/plugin-http'
1 change: 1 addition & 0 deletions plugins/http/index.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from '@cordisjs/plugin-http'
59 changes: 59 additions & 0 deletions plugins/http/package.json
Original file line number Diff line number Diff line change
@@ -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 <[email protected]>",
"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"
}
}
3 changes: 3 additions & 0 deletions plugins/http/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# @koishijs/plugin-http

Fetch-based axios-style HTTP client for [Koishi](https://koishi.chat).
2 changes: 0 additions & 2 deletions plugins/proxy-agent/.npmignore

This file was deleted.

0 comments on commit 4e2f9b7

Please sign in to comment.