Skip to content

Commit

Permalink
refactor: provider registration
Browse files Browse the repository at this point in the history
  • Loading branch information
rgwozdz committed Aug 7, 2023
1 parent 41de812 commit c1c2ed4
Show file tree
Hide file tree
Showing 39 changed files with 1,816 additions and 1,919 deletions.
5 changes: 5 additions & 0 deletions .changeset/heavy-pumpkins-wave.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@koopjs/koop-core': patch
---

- refactor provider registration
2 changes: 2 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions packages/koop-core/coverage.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions packages/koop-core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
},
"devDependencies": {
"mocha": "^10.0.0",
"proxyquire": "^2.1.3",
"should": "^13.2.3",
"should-sinon": "0.0.6",
"sinon": "^15.0.0",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@ const hasher = require('@sindresorhus/fnv1a');

const before = (req, callback) => { callback(); };
const after = (req, data, callback) => { callback(null, data); };
const cacheRetrieveNoop = (key, options, callback) => { callback(); };
const cacheInsertNoop = (key, options, data, callback) => { callback(); };

module.exports = function createModel ({ ProviderModel, koop, namespace }, options = {}) {
module.exports = function createModel ({ ProviderModel, namespace, logger, cache, authModule }, options = {}) {
class Model extends ProviderModel {
#cache;
#cacheTtl;
#before;
#after;
Expand All @@ -16,18 +17,18 @@ module.exports = function createModel ({ ProviderModel, koop, namespace }, optio
#getLayer;
#getCatalog;

constructor (koop, options) {
constructor ({ logger, cache }, options) {

super(koop, options);
super({ logger, log: logger }, options);
// Provider constructor's may assign values to this.cache
const modelCache = this.cache || options.cache || cache;
this.#cacheTtl = options.cacheTtl;
this.#cache = this.cache || options.cache || koop.cache;
this.namespace = namespace;
this.logger = koop.log;
this.logger = logger;
this.#before = promisify(options.before || before);
this.#after = promisify(options.after || after);
this.#cacheRetrieve = promisify(this.#cache.retrieve).bind(this.#cache);
this.#cacheInsert = promisify(this.#cache.insert).bind(this.#cache);
this.#cacheRetrieve = promisify(modelCache?.retrieve || cacheRetrieveNoop).bind(modelCache);
this.#cacheInsert = promisify(modelCache?.insert || cacheInsertNoop).bind(modelCache);
this.#getProviderData = promisify(this.getData).bind(this);
this.#getLayer = this.getLayer ? promisify(this.getLayer).bind(this) : undefined;
this.#getCatalog = this.getCatalog ? promisify(this.getCatalog).bind(this) : undefined;
Expand Down Expand Up @@ -81,7 +82,7 @@ module.exports = function createModel ({ ProviderModel, koop, namespace }, optio
const data = await this.#getLayer(req);
const ttl = data.ttl || this.#cacheTtl;
if (ttl) {
this.#cacheInsert(key, data, { ttl });
await this.#cacheInsert(key, data, { ttl });
}
callback(null, data);
} catch (err) {
Expand Down Expand Up @@ -137,18 +138,18 @@ module.exports = function createModel ({ ProviderModel, koop, namespace }, optio
}

// Add auth methods if auth plugin registered with Koop
if (koop._authModule) {
if (authModule) {
const {
authenticationSpecification,
authenticate,
authorize
} = koop._authModule;
} = authModule;

Model.prototype.authenticationSpecification = Object.assign({}, authenticationSpecification(namespace), { provider: namespace });
Model.prototype.authenticate = authenticate;
Model.prototype.authorize = authorize;
}
return new Model(koop, options);
return new Model({ logger, cache }, options);
};


Expand Down
Loading

0 comments on commit c1c2ed4

Please sign in to comment.