Comparing version 0.1.2 to 0.1.3
/// <reference types="node" /> | ||
/// <reference types="node" /> | ||
import { Response, Config as RawConfig } from "deep-http-client"; | ||
import { Response as RawResponse, Config as RawConfig } from "deep-http-client"; | ||
import { URLSearchParams } from "url"; | ||
import FormData from "form-data"; | ||
type Response = Omit<RawResponse, "body"> & { | ||
body: JSON | string | Buffer; | ||
req: ReqPeek; | ||
}; | ||
type ReqPeek = { | ||
headers: Record<string, string>; | ||
}; | ||
type JSONValues = string | number | boolean | null; | ||
@@ -10,6 +18,8 @@ type JSONMap = { | ||
type JSON = JSONMap | JSONValues; | ||
type RequestBody = JSON | string | Buffer | FormData | URLSearchParams; | ||
type Config = RawConfig & { | ||
cookieStore: boolean; | ||
body?: JSON | string | Buffer | FormData | URLSearchParams; | ||
body?: RequestBody; | ||
ua?: string; | ||
responseType?: "json" | "text" | "buffer"; | ||
}; | ||
@@ -24,4 +34,4 @@ type PartialConfig = Partial<Config>; | ||
get(url: string, cfg: PartialConfig): Promise<Response>; | ||
post(url: string, cfg: PartialConfig): Promise<Response>; | ||
put(url: string, cfg: PartialConfig): Promise<Response>; | ||
post(url: string, body: RequestBody, cfg: PartialConfig): Promise<Response>; | ||
put(url: string, body: RequestBody, cfg: PartialConfig): Promise<Response>; | ||
delete(url: string, cfg: PartialConfig): Promise<Response>; | ||
@@ -32,6 +42,6 @@ } | ||
declare const get: (url: string, cfg: PartialConfig) => Promise<Response>; | ||
declare const post: (url: string, cfg: PartialConfig) => Promise<Response>; | ||
declare const put: (url: string, cfg: PartialConfig) => Promise<Response>; | ||
declare const post: (url: string, body: RequestBody, cfg: PartialConfig) => Promise<Response>; | ||
declare const put: (url: string, body: RequestBody, cfg: PartialConfig) => Promise<Response>; | ||
declare const del: (url: string, cfg: PartialConfig) => Promise<Response>; | ||
declare function create(cfg: PartialConfig): Client; | ||
export { get, post, put, del as delete, create, }; | ||
export { get, post, put, del as delete, create }; |
@@ -34,2 +34,5 @@ "use strict"; | ||
}; | ||
var __importDefault = (this && this.__importDefault) || function (mod) { | ||
return (mod && mod.__esModule) ? mod : { "default": mod }; | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
@@ -40,4 +43,5 @@ exports.create = exports.delete = exports.put = exports.post = exports.get = void 0; | ||
const url_1 = require("url"); | ||
const form_data_1 = __importDefault(require("form-data")); | ||
function defaultConfig() { | ||
return Object.assign(Object.assign({}, (0, deep_http_client_1.defaultConfig)()), { cookieStore: true, ua: "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 " }); | ||
return Object.assign(Object.assign({}, (0, deep_http_client_1.defaultConfig)()), { cookieStore: true, ua: "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 ", responseType: "text" }); | ||
} | ||
@@ -55,3 +59,3 @@ class Client { | ||
if (cookies.length > 0) { | ||
config.headers = Object.assign(Object.assign({}, config.headers), { cookie: cookies.map(x => x.value).join("; ") }); | ||
config.headers = Object.assign(Object.assign({}, config.headers), { cookie: cookies.map((x) => x.value).join("; ") }); | ||
} | ||
@@ -61,5 +65,5 @@ } | ||
const body = config.body; | ||
if (body instanceof FormData) { | ||
if (body instanceof form_data_1.default) { | ||
config.headers = Object.assign(Object.assign({}, config.headers), { "Content-Type": "multipart/form-data" }); | ||
// change config.body | ||
config.body = body.getBuffer(); | ||
} | ||
@@ -78,8 +82,8 @@ else if (body instanceof url_1.URLSearchParams) { | ||
} | ||
const response = yield (0, deep_http_client_1.request)(config); | ||
if (response instanceof Error) { | ||
throw response; | ||
const rwresponse = yield (0, deep_http_client_1.request)(config); | ||
if (rwresponse instanceof Error) { | ||
throw rwresponse; | ||
} | ||
if (config.cookieStore) { | ||
const c = response.headers["Set-Cookie"]; | ||
const c = rwresponse.headers["Set-Cookie"]; | ||
if (c) { | ||
@@ -89,2 +93,13 @@ this.cookieStore.setCookieSync(c, config.url); | ||
} | ||
const response = Object.assign(Object.assign({}, rwresponse), { req: { | ||
headers: config.headers, | ||
} }); | ||
if (cfg.responseType) { | ||
if (cfg.responseType === "json") { | ||
response.body = JSON.parse(rwresponse.body.toString()); | ||
} | ||
else if (cfg.responseType === "text") { | ||
response.body = rwresponse.body.toString(); | ||
} | ||
} | ||
return response; | ||
@@ -107,5 +122,5 @@ }); | ||
} | ||
post(url, cfg) { | ||
post(url, body, cfg) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
const config = Object.assign(Object.assign(Object.assign(Object.assign({}, this.cfg), { url }), cfg), { method: "POST" }); | ||
const config = Object.assign(Object.assign(Object.assign(Object.assign({}, this.cfg), { url }), cfg), { method: "POST", body }); | ||
const response = yield this.request(config); | ||
@@ -115,5 +130,5 @@ return response; | ||
} | ||
put(url, cfg) { | ||
put(url, body, cfg) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
const config = Object.assign(Object.assign(Object.assign(Object.assign({}, this.cfg), { url }), cfg), { method: "PUT" }); | ||
const config = Object.assign(Object.assign(Object.assign(Object.assign({}, this.cfg), { url }), cfg), { method: "PUT", body }); | ||
const response = yield this.request(config); | ||
@@ -120,0 +135,0 @@ return response; |
{ | ||
"name": "deep-http", | ||
"version": "0.1.2", | ||
"version": "0.1.3", | ||
"main": "dist/index.js", | ||
@@ -5,0 +5,0 @@ "types": "dist/index.d.ts", |
21534
296