[adone] is, net

* [net] add basic http.client
* [is] improve type guards
This commit is contained in:
s0m3on3
2018-02-11 17:47:51 +02:00
parent fa96818c30
commit 6f797b3721
8 changed files with 886 additions and 23 deletions

View File

@@ -99,15 +99,34 @@ namespace isTests {
}
}
{ const a: boolean = is.json({}); }
{ const a: boolean = is.object({}); }
{ const a: boolean = is.plainObject({}); }
{
const a: boolean = is.object({});
const b: any = 2;
if (is.object(b)) {
const c: object = b;
}
}
{
const a: boolean = is.plainObject({});
const b: any = 2;
if (is.plainObject(b)) {
const c: object = b;
}
}
{ const a: boolean = is.class({}); }
{ const a: boolean = is.emptyObject({}); }
{ const a: boolean = is.propertyOwned({}, "a"); }
{ const a: boolean = is.propertyDefined({}, "a"); }
{ const a: boolean = is.conforms({}, {}); }
{ const a: boolean = is.conforms({}, {}, true); }
{ const a: boolean = is.arrayLikeObject({}); }
{
const a: boolean = is.arrayLikeObject({});
const b: any = {};
if (is.arrayLikeObject(b)) {
const c: number = b.length;
b[0];
}
}
{ const a: boolean = is.inArray(1, [1, 2, 3]); }
{ const a: boolean = is.inArray(1, [1, 2, 3], 0); }
{ const a: boolean = is.inArray(1, [1, 2, 3], 0, (a, b) => a === b); }
@@ -167,7 +186,13 @@ namespace isTests {
}
}
{ const a: boolean = is.callback({}); }
{ const a: boolean = is.generator({}); }
{
const a: boolean = is.generator({});
const b: any = 2;
if (is.generator(b)) {
b().next();
}
}
{ const a: boolean = is.nan({}); }
{
const a: boolean = is.finite({});
@@ -238,13 +263,51 @@ namespace isTests {
}
}
{ const a: boolean = is.transform({}); }
{ const a: boolean = is.subsystem({}); }
{ const a: boolean = is.application({}); }
{
const a: boolean = is.subsystem({});
const b: any = 2;
if (is.subsystem(b)) {
b.configureSubsystems().then(() => {});
}
}
{
const a: boolean = is.application({});
const b: any = 2;
if (is.application(b)) {
b.exitOnSignal("SIGINT");
}
}
{ const a: boolean = is.logger({}); }
{ const a: boolean = is.coreStream({}); }
{ const a: boolean = is.fastStream({}); }
{ const a: boolean = is.fastLocalStream({}); }
{ const a: boolean = is.fastLocalMapStream({}); }
{
const a: boolean = is.coreStream({});
const b: any = 2;
if (is.coreStream(b)) {
b.map((x) => 2).forEach((t: number) => {
//
});
}
}
{
const a: boolean = is.fastStream({});
const b: any = 2;
if (is.fastStream(b)) {
b.compress("deflate");
}
}
{
const a: boolean = is.fastLocalStream({});
const b: any = 2;
if (is.fastLocalStream(b)) {
b.compress("gz");
}
}
{
const a: boolean = is.fastLocalMapStream({});
const b: any = 2;
if (is.fastLocalMapStream(b)) {
b.decompress("gz");
}
}
{ const a: boolean = is.genesisNetron({}); }
{ const a: boolean = is.genesisPeer({}); }
{ const a: boolean = is.netronAdapter({}); }
@@ -260,7 +323,14 @@ namespace isTests {
{ const a: boolean = is.netronStub({}); }
{ const a: boolean = is.netronRemoteStub({}); }
{ const a: boolean = is.netronStream({}); }
{ const a: boolean = is.iterable({}); }
{
const a: boolean = is.iterable({});
const b: any = 2;
if (is.iterable(b)) {
const it = b[Symbol.iterator]();
it.next();
}
}
{ const a: boolean = is.windows; }
{ const a: boolean = is.linux; }
{ const a: boolean = is.freebsd; }

View File

@@ -0,0 +1,499 @@
namespace adoneTests.net.http.client {
const {
net: {
http: {
client
}
}
} = adone;
let bool: boolean;
let num: number;
let str: string;
let buf: Buffer;
let stream: nodestd.stream.Readable;
namespace Client {
const {
Client
} = client;
new Client();
new Client({});
new Client({
auth: {
username: "a"
}
});
new Client({
auth: {
password: "a"
}
});
new Client({
cancelToken: new client.CancelToken(() => {})
});
new Client({
data: Buffer.from("hello")
});
new Client({
data: "hello"
});
new Client({
data: adone.fs.createReadStream(__filename)
});
new Client({
data: new ArrayBuffer(10)
});
new Client({
formData: {
a: "string"
}
});
new Client({
formData: {
a: ["string"]
}
});
new Client({
formData: {
a: [adone.fs.createReadStream(__filename)]
}
});
new Client({
formData: {
a: {
value: "string",
options: {}
}
}
});
new Client({
formData: {
a: {
value: "string",
options: {
contentType: "a"
}
}
}
});
new Client({
formData: {
a: {
value: "string",
options: {
filename: "a"
}
}
}
});
new Client({
formData: {
a: {
value: "string",
options: {
header: "a"
}
}
}
});
new Client({
formData: {
a: {
value: "string",
options: {
header: {
a: "a"
}
}
}
}
});
new Client({
formData: {
a: {
value: "string",
options: {
knownLength: 100500
}
}
}
});
new Client({
formData: {
a: [{
value: "string",
options: {
knownLength: 100500
}
}]
}
});
new Client({
headers: {
"User-Agent": "true"
}
});
new Client({
httpAgent: {}
});
new Client({
httpsAgent: {}
});
new Client({
maxContentLength: 100500
});
new Client({
maxRedirects: 100500
});
new Client({
method: "GET"
});
new Client({
onUploadProgress(event) {
bool = event.lengthComputable;
num = event.loaded;
num = event.total;
}
});
new Client({
params: {
a: 1
}
});
new Client({
paramsSerializer(params: object) {
return "asd";
}
});
new Client({
proxy: "socks5://localhost"
});
new Client({
proxy: {
protocol: "socks5",
host: "127.0.0.1",
port: 9000,
auth: {
username: "hello",
password: "world"
}
}
});
new Client({
rejectUnauthorized: true
});
new Client({
responseEncoding: "CP1251"
});
new Client({
responseType: "stream"
});
new Client({
timeout: 100
});
new Client({
transformRequest: [(data, headers, config) => {
return data;
}]
});
new Client({
transformResponse: [(data, headers, config) => {
return data;
}]
});
new Client({
transport: adone.std.http
});
new Client({
validateStatus(status) {
return status > 100;
}
});
new Client({
xsrfCookieName: "a"
});
new Client({
xsrfHeaderName: "a"
});
const req = new Client();
namespace request {
req.request({
method: "GET"
}).then((resp) => {
num = resp.status;
str = resp.statusText;
const len = resp.headers["content-length"];
if (!adone.is.undefined(len)) {
str = len;
}
const data = resp.data;
if (!adone.is.object(data)) {
str = data;
}
});
req.request({
method: "GET",
responseType: "string"
}).then((resp) => {
num = resp.status;
str = resp.statusText;
str = resp.data;
});
req.request({
method: "GET",
responseType: "buffer"
}).then((resp) => {
num = resp.status;
str = resp.statusText;
buf = resp.data;
});
req.request({
method: "GET",
responseType: "stream"
}).then((resp) => {
num = resp.status;
str = resp.statusText;
stream = resp.data;
});
}
namespace get {
req.get("url").then((resp) => {
num = resp.status;
str = resp.statusText;
const data = resp.data;
if (!adone.is.object(data)) {
str = data;
}
});
req.get("url", {
responseType: "string"
}).then((resp) => {
num = resp.status;
str = resp.statusText;
str = resp.data;
});
req.get("url", {
responseType: "buffer"
}).then((resp) => {
num = resp.status;
str = resp.statusText;
buf = resp.data;
});
req.get("url", {
responseType: "stream"
}).then((resp) => {
num = resp.status;
str = resp.statusText;
stream = resp.data;
});
}
namespace post {
req.post("url", {}).then((resp) => {
num = resp.status;
str = resp.statusText;
const data = resp.data;
if (!adone.is.object(data)) {
str = data;
}
});
req.post("url", {}, {
responseType: "string"
}).then((resp) => {
num = resp.status;
str = resp.statusText;
str = resp.data;
});
req.post("url", {}, {
responseType: "buffer"
}).then((resp) => {
num = resp.status;
str = resp.statusText;
buf = resp.data;
});
req.post("url", {}, {
responseType: "stream"
}).then((resp) => {
num = resp.status;
str = resp.statusText;
stream = resp.data;
});
}
namespace put {
req.put("url", {}).then((resp) => {
num = resp.status;
str = resp.statusText;
const data = resp.data;
if (!adone.is.object(data)) {
str = data;
}
});
req.put("url", {}, {
responseType: "string"
}).then((resp) => {
num = resp.status;
str = resp.statusText;
str = resp.data;
});
req.put("url", {}, {
responseType: "buffer"
}).then((resp) => {
num = resp.status;
str = resp.statusText;
buf = resp.data;
});
req.put("url", {}, {
responseType: "stream"
}).then((resp) => {
num = resp.status;
str = resp.statusText;
stream = resp.data;
});
}
namespace patch {
req.patch("url", {}).then((resp) => {
num = resp.status;
str = resp.statusText;
const data = resp.data;
if (!adone.is.object(data)) {
str = data;
}
});
req.patch("url", {}, {
responseType: "string"
}).then((resp) => {
num = resp.status;
str = resp.statusText;
str = resp.data;
});
req.patch("url", {}, {
responseType: "buffer"
}).then((resp) => {
num = resp.status;
str = resp.statusText;
buf = resp.data;
});
req.patch("url", {}, {
responseType: "stream"
}).then((resp) => {
num = resp.status;
str = resp.statusText;
stream = resp.data;
});
}
namespace _delete {
req.delete("url").then((resp) => {
num = resp.status;
str = resp.statusText;
const data = resp.data;
if (!adone.is.object(data)) {
str = data;
}
});
req.delete("url", {
responseType: "string"
}).then((resp) => {
num = resp.status;
str = resp.statusText;
str = resp.data;
});
req.delete("url", {
responseType: "buffer"
}).then((resp) => {
num = resp.status;
str = resp.statusText;
buf = resp.data;
});
req.delete("url", {
responseType: "stream"
}).then((resp) => {
num = resp.status;
str = resp.statusText;
stream = resp.data;
});
}
namespace options {
req.options("url").then((resp) => {
num = resp.status;
str = resp.statusText;
const data = resp.data;
if (!adone.is.object(data)) {
str = data;
}
});
req.options("url", {
responseType: "string"
}).then((resp) => {
num = resp.status;
str = resp.statusText;
str = resp.data;
});
req.options("url", {
responseType: "buffer"
}).then((resp) => {
num = resp.status;
str = resp.statusText;
buf = resp.data;
});
req.options("url", {
responseType: "stream"
}).then((resp) => {
num = resp.status;
str = resp.statusText;
stream = resp.data;
});
}
}
namespace request {
client.request.get("htllo").then((x) => {
num = x.status;
});
}
namespace create {
client.create();
client.create({});
const req = client.create({
headers: {
"User-Agent": "Hello"
}
});
req.get("hello world").then((x) => {
num = x.status;
});
}
namespace cancel {
const {
Cancel,
CancelToken,
isCancel
} = client;
new CancelToken((s: string) => 123).promise.then((x) => {
str = x.message;
});
new CancelToken((s: string) => 123).reason;
CancelToken.source().token.promise;
CancelToken.source().cancel("hello");
new Cancel("hello").message;
const a: any = 2;
if (isCancel(a)) {
str = a.message;
}
}
}