Skip to content

Commit

Permalink
Merge pull request #638 from koopjs/private-model-props
Browse files Browse the repository at this point in the history
feat: private model props
  • Loading branch information
rgwozdz authored Jul 17, 2023
2 parents 9001437 + ae0ea78 commit 643b304
Show file tree
Hide file tree
Showing 12 changed files with 555 additions and 310 deletions.
7 changes: 7 additions & 0 deletions .changeset/cyan-apricots-tie.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
'@koopjs/koop-core': minor
---

- make 'cache', 'before', and 'after' private model props
- add `cacheSize` option
- add `cacheTtl` option for provider registration
19 changes: 19 additions & 0 deletions demo/provider-data/point-cached.geojson
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"type": "FeatureCollection",
"metadata": {
"ttl": 30
},
"features": [
{
"type": "Feature",
"properties": {},
"geometry": {
"type": "Point",
"coordinates": [
-104.9476,
39.9448
]
}
}
]
}
17 changes: 17 additions & 0 deletions package-lock.json

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

38 changes: 38 additions & 0 deletions packages/koop-core/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,44 @@ const options = {
}
```

### Registering Providers
When registering a provider you can pass an options object that provides some useful functionality:

```js
const Koop = require('@koopjs/koop-core')
const koop = new Koop()

const providerOptions = {
name: 'special-name',
cacheTtl: 600,
before: (req, callback) => {
req.query.myHardCodedParam = 'foo';
callback();
}
after: (req, geojson, callback) => {
geojson.crs = 'my coordinate system';
callback(null, geojson);
}
}

/* Register Koop data providers with options */
const provider = require('@koopjs/provider-github')
koop.register(provider, providerOptions)
```

#### Provider registration options
##### name
Use this param to override the name of the provider. If you supply a value for `name` it will be used in the path for routes registered to this provider.

##### cacheTtl
Use this param to set the default caching time (in seconds) for any data acquired by this provider. It can be overridden on a request-by-request basis by adding a `ttl` property to the root level of the geojson produced by the provider.

##### before
Supply a function that is executed _before_ a provider's `getData` method and has access to the Express request object. This is useful is you want to modify incoming request params before they arrive in a providers `getData` method.

##### after
Supply a function that is executed _after_ a provider's `getData` method and has access to the Express request object. This is useful is you want to modify the GeoJSON produce by the provider before it is passed on to the cache or any output plugin. It's a way of modifying a provider's getData functionality without having to modify the code of the provider itself.

## Issues

Find a bug or want to request a new feature? Please let us know by submitting an [issue](https://github.com/koopjs/koop/issues).
Expand Down
14 changes: 7 additions & 7 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 @@ -22,6 +22,7 @@
"@koopjs/cache-memory": "5.0.0",
"@koopjs/logger": "5.0.0",
"@koopjs/output-geoservices": "7.0.0",
"@sindresorhus/fnv1a": "^2.0.1",
"body-parser": "^1.19.0",
"compression": "^1.7.4",
"config": "^3.3.9",
Expand Down
3 changes: 1 addition & 2 deletions packages/koop-core/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,6 @@ Koop.prototype._registerAuth = function (auth) {
Koop.prototype._registerProvider = function (provider, options = {}) {
const providerRegistration = ProviderRegistration.create({ koop: this, provider, options });
this.providers.push(providerRegistration);
this.log.info('registered provider:', providerRegistration.namespace, providerRegistration.version);
};

/**
Expand All @@ -115,7 +114,7 @@ Koop.prototype._registerProvider = function (provider, options = {}) {
*/
Koop.prototype._registerOutput = function (outputClass, options) {
this.outputs.push({ outputClass, options });
this.log.info('registered output:', outputClass.name, outputClass.version);
this.log.info(`registered output: ${outputClass.name} ${outputClass.version}`);
};

/**
Expand Down
18 changes: 3 additions & 15 deletions packages/koop-core/src/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,12 +71,11 @@ describe('Index tests', function () {
const koop = new Koop({ logLevel: 'error' });
koop.register(provider, { foo: 'bar' });
const registeredProvider = koop.providers.find(provider => { return provider.namespace === 'test-provider'; });
registeredProvider.model.should.have.property('cache');
registeredProvider.model.should.have.property('options');
registeredProvider.model.options.should.have.property('foo', 'bar');
});

it('should register successfully and attach optional custom cache to model', function () {
it('should register successfully and pass optional cache to model', function () {
const koop = new Koop({ logLevel: 'error' });
koop.register(provider, {
cache: {
Expand All @@ -86,8 +85,8 @@ describe('Index tests', function () {
}
});
const registeredProvider = koop.providers.find(provider => { return provider.namespace === 'test-provider'; });
registeredProvider.model.should.have.property('cache');
registeredProvider.model.cache.should.have.property('customFunction').and.be.a.Function();
registeredProvider.model.options.should.have.property('cache');
registeredProvider.model.options.cache.should.have.property('customFunction').and.be.a.Function();
});

it('should reject cache option missing an insert method', function () {
Expand Down Expand Up @@ -128,17 +127,6 @@ describe('Index tests', function () {
}
});

it('should register successfully and attach optional "before" and "after" function to model', function () {
const koop = new Koop({ logLevel: 'error' });
koop.register(provider, {
before: (req, next) => {}, // eslint-disable-line -- this allow validation to occur
after: (req, data, callback) => {} // eslint-disable-line -- this allow validation to occur
});
const registeredProvider = koop.providers.find(provider => { return provider.namespace === 'test-provider'; });
registeredProvider.model.should.have.property('before').and.be.a.Function();
registeredProvider.model.should.have.property('after').and.be.a.Function();
});

it('should reject optional "before" function that does not have correct arity', function () {
const koop = new Koop({ logLevel: 'error' });
try {
Expand Down
Loading

0 comments on commit 643b304

Please sign in to comment.