oxid
1.0.0
oxid
是基于 rxjs observable 的同构 http 请求模块。
它具有rxjs@6
的对等依赖项,也必须安装它
npm 安装氧化
Oxid 导出默认实例oxid
,还公开Oxid
类来构造单独的实例。
从'氧化'导入{氧化,氧化};氧化.get('url',选项).subscribe();const anotherInstance = new Oxid(customConfigurations);anotherInstance.get('url',选项).subscribe();
所有 oxy 的接口返回Observable<HttpEvent<T>>
允许订阅从基本request
函数到 http 方法函数。
类型 requestMethodType = <T>(url: string, config?: RequestConfig) => Observable<HttpEvent<T>>;type requestMethodWithDataType = <T, U>(url: string, data?: T, config?: RequestConfig) = > Observable<HttpEvent<U>>;类氧化{ 公共只读删除:requestMethodType; 公共只读获取:requestMethodType; 公共只读头:requestMethodType; 公共只读选项:requestMethodType; 公共只读帖子:requestMethodWithDataType; 公共只读放置:requestMethodWithDataType; 公共只读补丁:requestMethodWithDataType; 公共请求 <T 扩展对象 | string = any>(url: string, config?: RequestConfig): Observable<HttpEvent<T>>; 公共请求 <T 扩展对象 | string = any>(url: string): Observable<HttpEvent<T>>; 公共请求 <T 扩展对象 | string = any>(config: RequestConfig): Observable<HttpEvent<T>>; 公共请求 <T 扩展对象 |字符串=任意>(urlOrConfig:RequestConfig |字符串,配置?:RequestConfig ): Observable<HttpEvent<T>> {
Oxid 包含一组默认配置值。当使用请求实例oxid
时将使用该值。通过类创建新实例时,默认情况下不包含任何选项值,必须通过类构造函数指定。尽管如此,单个方法( request()
和rest)单独接受RequestConfig
,它将在创建实例时合并到配置中。
接口请求配置库{ 网址?:字符串; 方法?: 方法; 基本URL?:字符串; 转换请求?:变压器 |数组<变压器>; 变换响应?: 变压器 |数组<变压器>; 标题?:任何; 参数?:任何; paramsSerializer?: (params: 任意) => 字符串; 数据?:任何; 适配器?: 适配器; auth?:基本凭证; 响应类型?:响应类型; 响应编码?:字符串; xsrfCookieName?:字符串; xsrfHeaderName?:字符串; 最大内容长度?:数字; validateStatus?: (状态: 数字) => 布尔值; maxRedirects?:数量; 套接字路径?:字符串|无效的; proxy?: ProxyConfig;}接口RequestConfigNode扩展RequestConfigBase { /** * 在节点 http 请求中使用的自定义代理。 */ httpAgent?:任何; /** * 在节点 https 请求中使用的自定义代理。 */ httpsAgent?:任何; Transport?: { request: typeof import('http').request };}接口 RequestConfigBrowser 扩展 RequestConfigBase { /** * 发出 xhr 请求的进度事件。 */ 报告进度?:布尔值; withCredentials?: 布尔值;}
从“氧化”导入{氧化,氧化,defaultOptions};氧化。get(url); //将使用`defaultOptions`oxyd.get({url, withCredentials: false}); //将使用`defaultOptions`,覆盖`withCredentials`const another = new Oxid(); //无基本配置 const anotherWithConfig = new oxy({withCredendials: false}) //设置基本配置anotherWithConfig.get({url, withCredentials: false}) //将在创建实例时使用配置,覆盖`withCredentials`
注意defaultOptions
对象是不可变的。更改、重新分配值到现有的默认配置值将不起作用,而应该构建新的配置对象。
Oxid本身没有写日志的机制。相反,它公开了一个函数来连接应用程序中使用的任何记录器。
函数enableLogger(记录器:logFunctionType):void;函数enableLogger(记录器:Partial<Logger>):void;
它可以是单个函数,也可以是具有调试、信息、警告、错误等日志级别的对象。注意enableLogger
是影响任何氧化实例的全局函数,并且仅在调用enableLogger
后才开始发出日志一次。
import {enableLogger, oxy } from 'oxy';// 日志不被发出 oxy.get().subscribe();enableLogger(console.log.bind(console));// 现在内部日志将通过 consoleoxy.get 发出()。订阅();
构建/测试代码支持很少的 npm 脚本。
build
:将代码转换为dist
。
build:clean
:清理现有构建。
test
:运行单元测试。执行测试之前不需要build
。
lint
:对所有代码库运行 lint。
虽然这个模块没有正式附属,但它依赖于axios
和@angular/http
的许多现有技术。您可能会注意到一些类似的逻辑,这是预期的。