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};氧化。 //將使用`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
的許多現有技術。您可能會注意到一些類似的邏輯,這是預期的。