New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

@testlio/discovered-request

Package Overview
Dependencies
Maintainers
16
Versions
4
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@testlio/discovered-request - npm Package Compare versions

Comparing version 1.0.1 to 1.1.0

lib/customPromise.js

2

examples/different-api-location.js
'use strict';
const request = require('../lib/request.js');
request.setPromiseDependency(require('Bluebird'));

@@ -13,2 +14,3 @@ const options = {

};
request(options).then((result) => {

@@ -15,0 +17,0 @@ console.log(result);

9

lib/get-resource.js

@@ -20,7 +20,10 @@ 'use strict';

const _ = require('lodash');
const customPromise = require('./customPromise.js');
function getResourceFromCache(serviceUrl, resourceName) {
return new Promise((resolve, reject) => {
return new customPromise.Promise((resolve, reject) => {
if (!cache[serviceUrl]) {
reject(`Resource ${serviceUrl} is not available`);
} else if (!resourceName) {
resolve(cache[serviceUrl]);
} else if (!cache[serviceUrl].resources || !cache[serviceUrl].resources[resourceName]) {

@@ -56,7 +59,7 @@ reject(`Resource ${resourceName} is not available`);

module.exports = function(apiUrl, path, headers) {
const servicePath = _.initial(path);
const servicePath = _.take(path, 2);
return getService(apiUrl, servicePath).then((serviceUrl) => {
const resourceName = _.last(path);
const resourceName = path.length === 3 ? _.last(path) : undefined;
return getResource(serviceUrl, resourceName, headers);
});
};

@@ -12,6 +12,8 @@ 'use strict';

const request = require('request-promise');
const customPromise = require('./customPromise.js');
let apiCache = undefined;
function getServiceUrlFromApiCache(servicePath) {
return new Promise((resolve, reject) => {
return new customPromise.Promise((resolve, reject) => {
if (!apiCache) {

@@ -18,0 +20,0 @@ reject('API services are not cached');

@@ -13,4 +13,5 @@ 'use strict';

const defaultApiUrl = 'https://api.testlio.com';
const customPromise = require('./customPromise.js');
module.exports = function(options) {
function requestLib(options) {
if (options.servicePath) {

@@ -20,4 +21,9 @@ const servicePath = _.split(options.servicePath, '.');

return getResource(apiUrl, servicePath, options.headers).then((resource) => {
// This is only valid for cases,
// when servicePath is i.e. ['service', 'version'].
// The expected outcome is discovery json.
if (servicePath.length === 2) return customPromise.Promise.resolve(resource);
const resourceOptions = _.assign({ url: resource.href }, options);
delete resourceOptions.api;
delete resourceOptions.api;
delete resourceOptions.servicePath;

@@ -29,2 +35,5 @@ return request(resourceOptions);

}
};
}
requestLib.setPromiseDependency = customPromise.setPromiseDependency;
module.exports = requestLib;
{
"name": "@testlio/discovered-request",
"version": "1.0.1",
"version": "1.1.0",
"description": "Helper library for making http requests through discovery",

@@ -5,0 +5,0 @@ "main": "lib/request.js",

@@ -1,2 +0,2 @@

# request-tools
# Discovered-request
Helper library for making requests through discovery.

@@ -18,3 +18,3 @@

const request = require('../lib/request.js');
const request = require('@testlio/discovered-request');

@@ -41,3 +41,3 @@ const options = {

const request = require('../lib/request.js');
const request = require('@testlio/discovered-request');

@@ -56,2 +56,28 @@ const options = {

## Request only discovery
Returns the result of the resource discovery request of the specified service and version.
```js
'use strict';
const request = require('@testlio/discovered-request');
const options = {
servicePath: 'browser.v1',
json: true,
headers: {
Authorization: 'Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiJtYXJ0QHRlc3RsaW8uY29tIiwibmFtZSI6IkpvaG4gRG9lIiwiYWRtaW4iOnRydWV9.tMRlQfy2e1I0kfqrSJraPbBqgGnmIVrXj8ks-WBEJLg'
}
};
request(options).then((result) => {
console.log(result);
}).catch((err) => {
console.log('Request failed');
console.log(err);
});
```
## Changing API location

@@ -64,3 +90,3 @@

const request = require('../lib/request.js');
const request = require('@testlio/discovered-request');

@@ -83,1 +109,10 @@ const options = {

```
### Changing 'Promise' dependency
If your environment doesn't support `new Promise` you need to define promise dependency by yourself.
```
const request = require('@testlio/discovered-request');
request.setPromisesDependency(require('Bluebird'));
```

@@ -5,2 +5,3 @@ 'use strict';

const proxyquire = require('proxyquire');
const customPromise = require('../lib/customPromise.js');

@@ -68,2 +69,53 @@ let resourceDiscoverHandler = () => {};

tape.test('Request with servicePath without specific resource should return the whole discovery json', (t) => {
const correctPath = ['browser', 'v1'];
const correctHeaders = {
'Authorization': 'key'
};
const expectedResult = {
resources: {
browsers: {
href: 'https://api.testlio/browser/v1/browsers'
},
invalid: {
href: 'http://should-not-be-returned.com'
}
}
};
resourceDiscoverHandler = (path, headers) => {
t.deepEqual(path, correctPath, 'getResource parameter "path" is correct');
t.deepEqual(headers, correctHeaders, 'getResource parameter "headers" is correct');
return expectedResult;
};
const correctRequestOptions = {
headers: { Authorization: 'key' },
queryString: { name: 'test-data' },
url: 'https://api.testlio/browser/v1/'
};
requestPromiseHandler = (requestOptions) => {
t.deepEqual(requestOptions, correctRequestOptions, 'Request options are correct');
return 'success';
};
const options = {
servicePath: 'browser.v1',
headers: {
'Authorization': 'key'
},
queryString: {
name: 'test-data'
}
};
request(options).then((result) => {
t.deepEqual(result, expectedResult, 'Response is correct');
t.end();
}).catch(err => {
t.fail(err);
t.end();
});
});
tape.test('Request without servicePath should make request without discovery', (t) => {

@@ -103,1 +155,8 @@ resourceDiscoverHandler = () => {

});
tape.test('Request.setPromiseDependency should set global dependency', (t) => {
function testPromise() { return 'new'; }
request.setPromiseDependency(testPromise);
t.equal(customPromise.Promise, testPromise, 'Custom promise is correct');
t.end();
});

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc