@cordisjs/plugin-http
Advanced tools
Comparing version 0.5.1 to 0.5.2
@@ -1,2 +0,2 @@ | ||
import { Context, Service } from 'cordis'; | ||
import { Context, Schema, Service } from 'cordis'; | ||
import { Awaitable, Dict } from 'cosmokit'; | ||
@@ -11,6 +11,6 @@ import { ClientOptions } from 'ws'; | ||
interface Intercept { | ||
http: HTTP.Config; | ||
http: HTTP.Intercept; | ||
} | ||
interface Events { | ||
'http/file'(this: HTTP, url: string, options: FileConfig): Awaitable<FileResponse | undefined>; | ||
'http/file'(this: HTTP, url: string, options: FileOptions): Awaitable<FileResponse | undefined>; | ||
'http/config'(this: HTTP, config: HTTP.Config): void; | ||
@@ -51,3 +51,3 @@ 'http/fetch-init'(this: HTTP, url: URL, init: RequestInit, config: HTTP.Config): void; | ||
} | ||
interface Config { | ||
interface Intercept { | ||
baseURL?: string; | ||
@@ -59,2 +59,4 @@ /** @deprecated use `baseURL` instead */ | ||
} | ||
interface Config extends Intercept { | ||
} | ||
interface RequestConfig extends Config { | ||
@@ -82,3 +84,3 @@ method?: Method; | ||
} | ||
export interface FileConfig { | ||
export interface FileOptions { | ||
timeout?: number | string; | ||
@@ -112,2 +114,4 @@ } | ||
static [Service.immediate]: boolean; | ||
static Config: Schema<HTTP.Config>; | ||
static Intercept: Schema<HTTP.Config>; | ||
isError: typeof HTTPError.is; | ||
@@ -121,5 +125,5 @@ private _decoders; | ||
}; | ||
baseURL?: string | undefined; | ||
endpoint?: string | undefined; | ||
timeout?: number | undefined; | ||
baseURL?: string; | ||
endpoint?: string; | ||
timeout?: number; | ||
}; | ||
@@ -139,5 +143,5 @@ decoder<K extends keyof HTTP.ResponseTypes>(type: K, decoder: (raw: Response) => Awaitable<HTTP.ResponseTypes[K]>): () => boolean; | ||
ws(url: string | URL, init?: HTTP.Config): WebSocket; | ||
file(this: HTTP, url: string, options?: FileConfig): Promise<FileResponse>; | ||
file(this: HTTP, url: string, options?: FileOptions): Promise<FileResponse>; | ||
isLocal(url: string): Promise<boolean>; | ||
} | ||
export default HTTP; |
@@ -5,3 +5,3 @@ var __defProp = Object.defineProperty; | ||
// src/index.ts | ||
import { Context, Service } from "cordis"; | ||
import { Context, Schema, Service } from "cordis"; | ||
import { Binary, defineProperty, isNullable, trimSlash } from "cosmokit"; | ||
@@ -164,2 +164,11 @@ import { loadFile, lookup, WebSocket } from "@cordisjs/plugin-http/adapter"; | ||
} | ||
static Config = Schema.object({ | ||
timeout: Schema.natural().role("ms").description("等待请求的最长时间。"), | ||
keepAlive: Schema.boolean().description("是否保持连接。") | ||
}); | ||
static Intercept = Schema.object({ | ||
baseURL: Schema.string().description("基础 URL。"), | ||
timeout: Schema.natural().role("ms").description("等待请求的最长时间。"), | ||
keepAlive: Schema.boolean().description("是否保持连接。") | ||
}); | ||
isError = HTTPError.is; | ||
@@ -176,2 +185,3 @@ _decoders = /* @__PURE__ */ Object.create(null); | ||
this.ctx.on("http/file", (url, options) => loadFile(url)); | ||
this.ctx.schema.extend("service:http", _HTTP.Intercept); | ||
} | ||
@@ -178,0 +188,0 @@ static mergeConfig = (target, source) => ({ |
{ | ||
"name": "@cordisjs/plugin-http", | ||
"description": "Fetch-based axios-style HTTP client", | ||
"version": "0.5.1", | ||
"version": "0.5.2", | ||
"type": "module", | ||
@@ -53,7 +53,7 @@ "main": "lib/index.js", | ||
"@types/mime-db": "^1.43.5", | ||
"cordis": "^3.15.1", | ||
"cordis": "^3.16.1", | ||
"undici": "^6.10.1" | ||
}, | ||
"peerDependencies": { | ||
"cordis": "^3.15.1" | ||
"cordis": "^3.16.1" | ||
}, | ||
@@ -60,0 +60,0 @@ "dependencies": { |
@@ -1,2 +0,2 @@ | ||
import { Context, Service } from 'cordis' | ||
import { Context, Schema, Service } from 'cordis' | ||
import { Awaitable, Binary, defineProperty, Dict, isNullable, trimSlash } from 'cosmokit' | ||
@@ -17,7 +17,7 @@ import { ClientOptions } from 'ws' | ||
interface Intercept { | ||
http: HTTP.Config | ||
http: HTTP.Intercept | ||
} | ||
interface Events { | ||
'http/file'(this: HTTP, url: string, options: FileConfig): Awaitable<FileResponse | undefined> | ||
'http/file'(this: HTTP, url: string, options: FileOptions): Awaitable<FileResponse | undefined> | ||
'http/config'(this: HTTP, config: HTTP.Config): void | ||
@@ -88,3 +88,3 @@ 'http/fetch-init'(this: HTTP, url: URL, init: RequestInit, config: HTTP.Config): void | ||
export interface Config { | ||
export interface Intercept { | ||
baseURL?: string | ||
@@ -97,2 +97,4 @@ /** @deprecated use `baseURL` instead */ | ||
export interface Config extends Intercept {} | ||
export interface RequestConfig extends Config { | ||
@@ -124,3 +126,3 @@ method?: Method | ||
export interface FileConfig { | ||
export interface FileOptions { | ||
timeout?: number | string | ||
@@ -173,2 +175,13 @@ } | ||
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 | ||
@@ -189,2 +202,3 @@ | ||
this.ctx.on('http/file', (url, options) => loadFile(url)) | ||
this.ctx.schema.extend('service:http', HTTP.Intercept) | ||
} | ||
@@ -392,3 +406,3 @@ | ||
async file(this: HTTP, url: string, options: FileConfig = {}): Promise<FileResponse> { | ||
async file(this: HTTP, url: string, options: FileOptions = {}): Promise<FileResponse> { | ||
const task = await this[Context.origin].serial(this, 'http/file', url, options) | ||
@@ -395,0 +409,0 @@ if (task) return task |
Sorry, the diff of this file is not supported yet
62608
1644