Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: upgrade dependencies to latest and add node 14 check #215

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,15 @@ language: node_js
node_js:
- 10
- 12
- 14

cache:
yarn: true
directories:
- node_modules

script:
- sh ./scripts/run-test.sh
- yarn test:ci

deploy:
provider: pages
Expand Down
39 changes: 20 additions & 19 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,12 @@
"jest": "jest --coverage test/jest/*.js",
"predocs": "rimraf ./jsdocs && mkdir jsdocs/ && ./utils/make-jsdoc-readme.js > ./jsdocs/jsdoc-temp.md",
"docs": "./node_modules/.bin/jsdoc src/ -c ./docs/config.json -d ./jsdocs/ -P ./package.json -R ./jsdocs/jsdoc-temp.md -r",
"test:ci": "yarn test:unit && test:integration",
"test:integration": "yarn test:integration:oauth && yarn test:integration:ssws",
"test:integration:ssws": "OKTA_CLIENT_AUTHORIZATIONMODE=SSWS nyc --reporter=text --reporter=html mocha test/it/*.js --no-timeouts",
"test:integration:oauth": "OKTA_CLIENT_AUTHORIZATIONMODE=PrivateKey nyc --reporter=text --reporter=html mocha test/it/user-get.js --no-timeouts",
"test:unit": "nyc --reporter=text --reporter=html mocha test/unit/*.js --no-timeouts",
"test": "npm run eslint && npm run test:unit && npm run test:integration && npm run jest"
"test:integration:ssws": "OKTA_CLIENT_AUTHORIZATIONMODE=SSWS nyc --reporter=text --reporter=html mocha test/it/*.js --no-timeouts --colors",
"test:integration:oauth": "OKTA_CLIENT_AUTHORIZATIONMODE=PrivateKey nyc --reporter=text --reporter=html mocha test/it/user-get.js --no-timeouts --colors",
"test:unit": "nyc --reporter=text --reporter=html mocha test/unit/*.js --no-timeouts --colors",
"test": "yarn eslint && yarn test:unit && yarn test:integration && yarn jest"
},
"keywords": [],
"license": "Apache-2.0",
Expand All @@ -31,28 +32,28 @@
"deep-copy": "^1.4.2",
"flat": "^5.0.2",
"isomorphic-fetch": "^3.0.0",
"js-yaml": "^3.13.0",
"lodash": "^4.17.14",
"js-yaml": "^3.14.0",
"lodash": "^4.17.20",
"njwt": "^1.0.0",
"parse-link-header": "1.0.0",
"parse-link-header": "^1.0.1",
"rasha": "^1.2.5"
},
"devDependencies": {
"@okta/openapi": "^2.1.6",
"chai": "^3.5.0",
"eslint": "^3.19.0",
"eslint-plugin-jest": "^21.17.0",
"@okta/openapi": "^2.2.2",
"chai": "^4.2.0",
"eslint": "^7.13.0",
"eslint-plugin-jest": "^24.1.3",
"fake-fs": "^0.5.0",
"faker": "^4.1.0",
"globby": "^6.1.0",
"faker": "^5.1.0",
"globby": "^11.0.1",
"ink-docstrap": "^1.3.0",
"jest": "^23.1.0",
"jest": "^26.6.3",
"jest-date-mock": "^1.0.8",
"jsdoc": "^3.4.0",
"mocha": "^3.4.1",
"nyc": "^10.3.2",
"jsdoc": "^3.6.6",
"mocha": "^8.2.1",
"nyc": "^15.1.0",
"rimraf": "^3.0.2",
"sinon": "^7.3.1",
"sinon": "^9.2.1",
"speakeasy": "^2.0.0"
},
"jest": {
Expand All @@ -62,4 +63,4 @@
"jest-date-mock"
]
}
}
}
12 changes: 0 additions & 12 deletions scripts/run-test.sh

This file was deleted.

104 changes: 52 additions & 52 deletions src/collection.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,12 @@
* See the License for the specific language governing permissions and limitations under the License.
*/


const parseLinkHeader = require('parse-link-header');

/**
* Provides an interface to iterate over all objects in a collection that has pagination via Link headers
*/
class Collection {

/**
* Creates an instance of Collection.
* @param {ApiClient} client A reference to the top-level api client
Expand Down Expand Up @@ -56,35 +54,37 @@ class Collection {
return;
}

self.getNextPage()
.then(collection => {
self.currentItems = collection;
return nextItem();
})
.catch(reject);
self
.getNextPage()
.then((collection) => {
self.currentItems = collection;
return nextItem();
})
.catch(reject);
});
}

[Symbol.asyncIterator]() {
return {
next: () => this.next()
next: () => this.next(),
};
}

getNextPage() {
return this.client.http.http(this.nextUri, this.request, {isCollection: true})
.then(res => {
const link = res.headers.get('link');
if (link) {
const parsed = parseLinkHeader(link);
if (parsed.next) {
this.nextUri = parsed.next.url;
return res.json();
return this.client.http
.http(this.nextUri, this.request, { isCollection: true })
.then((res) => {
const link = res.headers.get('link');
if (link) {
const parsed = parseLinkHeader(link);
if (parsed.next) {
this.nextUri = parsed.next.url;
return res.json();
}
}
}
this.nextUri = undefined;
return res.json();
});
this.nextUri = undefined;
return res.json();
});
}

/**
Expand All @@ -95,26 +95,25 @@ class Collection {
each(iterator) {
const self = this;
function nextItem() {
return self.next()
.then(nextResult => {
return self.next().then((nextResult) => {
if (!nextResult.value) {
return;
}
const result = iterator(nextResult.value);

// if it's a Promise
if (result && result.then) {
return result.then(shouldContinue => {
return result.then((shouldContinue) => {
if (shouldContinue !== false && !nextResult.done) {
return nextItem();
}
});

// if they want to short-circuit
// if they want to short-circuit
} else if (result === false) {
return;

// if there are no more items
// if there are no more items
} else if (nextResult.done) {
return;
}
Expand Down Expand Up @@ -153,31 +152,32 @@ class Collection {
if (closed) {
return;
}
return self.next()
.then(nextResult => {
if (closed) {
return;
}

if (!nextResult.value) {
return setTimeout(nextItem, interval);
}
const result = config.next(nextResult.value);

// if it's a Promise
if (result && result.then) {
return result.then(() => nextItem());
} else {
nextItem();
}
})
.catch(err => {
const errResult = config.error(err);
if (errResult && errResult.then) {
return errResult.then(() => nextItem());
}
return nextItem();
});
return self
.next()
.then((nextResult) => {
if (closed) {
return;
}

if (!nextResult.value) {
return setTimeout(nextItem, interval);
}
const result = config.next(nextResult.value);

// if it's a Promise
if (result && result.then) {
return result.then(() => nextItem());
} else {
nextItem();
}
})
.catch((err) => {
const errResult = config.error(err);
if (errResult && errResult.then) {
return errResult.then(() => nextItem());
}
return nextItem();
});
}

nextItem();
Expand All @@ -186,7 +186,7 @@ class Collection {
unsubscribe() {
closed = true;
config.complete();
}
},
};
}
}
Expand Down
62 changes: 32 additions & 30 deletions src/default-cache-middleware.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,49 +13,51 @@
const _ = require('lodash');

module.exports = function defaultCacheMiddleware(ctx, next) {
let cacheCheck, cacheHit = false;
let cacheCheck,
cacheHit = false;
if (ctx.req.method.toLowerCase() === 'get' && !ctx.isCollection) {
// TODO: use ctx.req.url as cache key and add json() method to cached result: https://oktainc.atlassian.net/browse/OKTA-280090
cacheCheck = ctx.cacheStore.get(ctx.req.uri)
.then(body => {
cacheCheck = ctx.cacheStore.get(ctx.req.uri).then((body) => {
if (body) {
cacheHit = true;
ctx.res = {
status: 200,
text() {
return Promise.resolve(body);
}
},
};
}
});
}
return Promise.resolve(cacheCheck)
.then(() => next())
.then(() => {
if (cacheHit || ctx.isCollection) {
return;
}
if (ctx.req.method.toLowerCase() === 'get') {
// store response in cache
return ctx.res.clone().text()
.then(text => {
try {
const selfHref = _.get(JSON.parse(text), '_links.self.href');
if (selfHref) {
ctx.cacheStore.set(selfHref, text);
}
} catch (e) {
// TODO: add custom logger
.then(() => next())
.then(() => {
if (cacheHit || ctx.isCollection) {
return;
}
if (ctx.req.method.toLowerCase() === 'get') {
// store response in cache
return ctx.res
.clone()
.text()
.then((text) => {
try {
const selfHref = _.get(JSON.parse(text), '_links.self.href');
if (selfHref) {
ctx.cacheStore.set(selfHref, text);
}
} catch (e) {
// TODO: add custom logger
}
});
} else {
// clear cache for affected resources
const affected = [];
const resources = ctx.resources || [];
for (let resource of resources) {
affected.push(ctx.cacheStore.delete(resource));
}
});
} else {
// clear cache for affected resources
const affected = [];
const resources = ctx.resources || [];
for (let resource of resources) {
affected.push(ctx.cacheStore.delete(resource));
return Promise.all(affected);
}
return Promise.all(affected);
}
});
});
};
Loading