Skip to content

Commit

Permalink
feat(http): register ctx.schema
Browse files Browse the repository at this point in the history
  • Loading branch information
shigma committed Jun 3, 2024
1 parent b6b3833 commit d033173
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 25 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@
"shx": "^0.3.4",
"tsx": "^4.7.1",
"typescript": "^5.4.3",
"yakumo": "^1.0.0-beta.14",
"yakumo-esbuild": "^1.0.0-beta.5",
"yakumo": "^1.0.0-beta.16",
"yakumo-esbuild": "^1.0.0-beta.6",
"yakumo-tsc": "^1.0.0-beta.3"
}
}
6 changes: 3 additions & 3 deletions packages/core/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@cordisjs/plugin-http",
"description": "Fetch-based axios-style HTTP client",
"version": "0.4.2",
"version": "0.5.2",
"type": "module",
"main": "lib/index.js",
"types": "lib/index.d.ts",
Expand Down Expand Up @@ -50,11 +50,11 @@
],
"devDependencies": {
"@types/mime-db": "^1.43.5",
"cordis": "^3.14.0",
"cordis": "^3.16.1",
"undici": "^6.10.1"
},
"peerDependencies": {
"cordis": "^3.14.0"
"cordis": "^3.16.1"
},
"dependencies": {
"cosmokit": "^1.6.2",
Expand Down
20 changes: 17 additions & 3 deletions packages/core/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Context, Service } from 'cordis'
import { Context, Schema, Service } from 'cordis'
import { Awaitable, Binary, defineProperty, Dict, isNullable, trimSlash } from 'cosmokit'
import { ClientOptions } from 'ws'
import { loadFile, lookup, WebSocket } from '@cordisjs/plugin-http/adapter'
Expand All @@ -14,7 +14,7 @@ declare module 'cordis' {
}

interface Intercept {
http: HTTP.Config
http: HTTP.Intercept
}

interface Events {
Expand Down Expand Up @@ -84,14 +84,16 @@ export namespace HTTP {
<T = any>(url: string, data?: any, config?: HTTP.RequestConfig): Promise<T>
}

export interface Config {
export interface Intercept {
baseURL?: string
/** @deprecated use `baseURL` instead */
endpoint?: string
headers?: Dict
timeout?: number
}

export interface Config extends Intercept {}

export interface RequestConfig extends Config {
method?: Method
params?: Dict
Expand Down Expand Up @@ -166,6 +168,17 @@ export class HTTP extends Service<HTTP.Config> {
}
}

static Config: Schema<HTTP.Config> = Schema.object({
timeout: Schema.natural().role('ms').description('等待请求的最长时间。'),
keepAlive: Schema.boolean().description('是否保持连接。'),
})

static Intercept: Schema<HTTP.Config> = Schema.object({
baseURL: Schema.string().description('基础 URL。'),
timeout: Schema.natural().role('ms').description('等待请求的最长时间。'),
keepAlive: Schema.boolean().description('是否保持连接。'),
})

public isError = HTTPError.is

private _decoders: Dict = Object.create(null)
Expand All @@ -181,6 +194,7 @@ export class HTTP extends Service<HTTP.Config> {
this.decoder('formdata', (raw) => raw.formData())
this.decoder('stream', (raw) => raw.body as any)
this.ctx.on('http/file', (url, options) => loadFile(url))
this.ctx.schema.extend('service:http', HTTP.Intercept)
}

static mergeConfig = (target: HTTP.Config, source?: HTTP.Config) => ({
Expand Down
8 changes: 4 additions & 4 deletions packages/proxy-agent/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@cordisjs/plugin-proxy-agent",
"description": "Proxy agent support for @cordisjs/plugin-http",
"version": "0.3.0",
"version": "0.3.2",
"type": "module",
"main": "lib/index.js",
"types": "lib/index.d.ts",
Expand Down Expand Up @@ -44,11 +44,11 @@
"plugin"
],
"devDependencies": {
"cordis": "^3.14.0"
"cordis": "^3.16.1"
},
"peerDependencies": {
"@cordisjs/plugin-http": "^0.4.2",
"cordis": "^3.14.0"
"@cordisjs/plugin-http": "^0.5.2",
"cordis": "^3.16.1"
},
"dependencies": {
"http-proxy-agent": "^7.0.2",
Expand Down
32 changes: 19 additions & 13 deletions packages/proxy-agent/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import {} from '@cordisjs/plugin-http'
import * as http from 'node:http'
import { lookup } from 'node:dns/promises'
import { Context, z } from 'cordis'
import { Context, Schema, z } from 'cordis'
import { SocksClient, SocksProxy } from 'socks'
import { Agent, buildConnector, Dispatcher, ProxyAgent } from 'undici'
import { HttpProxyAgent } from 'http-proxy-agent'
Expand All @@ -20,9 +20,7 @@ declare module 'cordis' {

declare module '@cordisjs/plugin-http' {
namespace HTTP {
interface Config {
proxyAgent?: string
}
interface Intercept extends ProxyAgentConfig {}
}
}

Expand Down Expand Up @@ -76,15 +74,23 @@ function socksAgent(result: ParseResult, options: SocksDispatcherOptions = {}) {
export const name = 'proxy-agent'
export const inject = ['http']

export interface Config {}
type ProxyAgentConfig = Config

export interface Config {
proxyAgent: string
}

export const Config: z<Config> = z.object({
proxyAgent: Schema.string().description('代理服务器地址。'),
})

export const Config: z<Config> = z.object({})
export function apply(ctx: Context, _config: Config) {
ctx.schema.extend('service:http', Config)

export function apply(ctx: Context, config: Config) {
ctx.on('http/fetch-init', (url, init, config) => {
if (!config?.proxyAgent) return
const proxy = new URL(config.proxyAgent)
const agent = ctx.bail('http/dispatcher', proxy, url)
const proxy = config?.proxyAgent ?? _config.proxyAgent
if (!proxy) return
const agent = ctx.bail('http/dispatcher', new URL(proxy), url)
if (!agent) throw new Error(`Cannot resolve proxy agent ${url}`)
init['dispatcher'] = agent
})
Expand All @@ -99,9 +105,9 @@ export function apply(ctx: Context, config: Config) {
})

ctx.on('http/websocket-init', (url, init, config) => {
if (!config?.proxyAgent) return
const proxy = new URL(config.proxyAgent)
const agent = ctx.bail('http/legacy-agent', proxy, url)
const proxy = config?.proxyAgent ?? _config.proxyAgent
if (!proxy) return
const agent = ctx.bail('http/legacy-agent', new URL(proxy), url)
if (!agent) throw new Error(`Cannot resolve proxy agent ${url}`)
init.agent = agent
})
Expand Down

0 comments on commit d033173

Please sign in to comment.