0
Fork 0
mirror of https://github.com/verdaccio/verdaccio.git synced 2024-12-23 22:27:34 -05:00
verdaccio/test/lib/request.ts
Juan Picado 69d7df20d8
build: enable pnp yarn2 (#2253)
* chore: enable pnp yarn

* chore: ignore pnp

* fix type issues on run eslint

* add missing dependency and fix some errors

* fix most of the errors

some were just disabled, already fixed in master

* add missing jest-config

* update jest@26 align with other deps

* add missing @babel/register

* clean up

* use yarn node

* use yarn node on release

* chore: add husky 6

* chore: add husky 6

* chore: lint-stage

* chore: test

* chore: add hook git

* chore: test

* chore: test

* update deps

* chore: fix commit lint

* fix docker run

* update git ignore
2021-05-13 23:13:57 +02:00

128 lines
3.1 KiB
TypeScript

import assert from 'assert';
import _ from 'lodash';
import request from 'request';
import { IRequestPromise } from '../types';
const requestData = Symbol('smart_request_data');
export class PromiseAssert extends Promise<any> implements IRequestPromise {
public constructor(options: any) {
super(options);
}
public status(expected: number) {
const selfData = this[requestData];
return injectResponse(
this,
this.then(function (body) {
try {
assert.equal(selfData.response.statusCode, expected);
} catch (err) {
selfData.error.message = err.message;
throw selfData.error;
}
return body;
})
);
}
public body_ok(expected: any) {
const selfData = this[requestData];
return injectResponse(
this,
this.then(function (body) {
try {
if (_.isRegExp(expected)) {
assert(body.ok.match(expected), "'" + body.ok + "' doesn't match " + expected);
} else {
assert.equal(body.ok, expected);
}
assert.equal(body.error, null);
} catch (err) {
selfData.error.message = err.message;
throw selfData.error;
}
return body;
})
);
}
public body_error(expected: any) {
// $FlowFixMe
const selfData = this[requestData];
return injectResponse(
this,
this.then(function (body) {
try {
if (_.isRegExp(expected)) {
assert(body.error.match(expected), body.error + " doesn't match " + expected);
} else {
assert.equal(body.error, expected);
}
assert.equal(body.ok, null);
} catch (err) {
selfData.error.message = err.message;
throw selfData.error;
}
return body;
})
);
}
public request(callback: any) {
callback(this[requestData].request);
return this;
}
public response(cb: any) {
const selfData = this[requestData];
return injectResponse(
this,
this.then(function (body) {
cb(selfData.response);
return body;
})
);
}
public send(data: any) {
this[requestData].request.end(data);
return this;
}
}
function injectResponse(smartObject: any, promise: Promise<any>): Promise<any> {
// $FlowFixMe
promise[requestData] = smartObject[requestData];
return promise;
}
function smartRequest(options: any): Promise<any> {
const smartObject: any = {};
smartObject[requestData] = {};
smartObject[requestData].error = Error();
Error.captureStackTrace(smartObject[requestData].error, smartRequest);
const promiseResult: Promise<any> = new PromiseAssert(function (resolve, reject) {
// store request reference on symbol
smartObject[requestData].request = request(options, function (err, res, body) {
if (err) {
return reject(err);
}
// store the response on symbol
smartObject[requestData].response = res;
resolve(body);
});
});
return injectResponse(smartObject, promiseResult);
}
export default smartRequest;