Skip to content

Commit

Permalink
feat(core): validate provided feature service versions on register (#252
Browse files Browse the repository at this point in the history
)
  • Loading branch information
fahrradflucht authored and clebert committed Jan 11, 2019
1 parent 0d628c3 commit d6f89e8
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 20 deletions.
6 changes: 3 additions & 3 deletions packages/core/src/__tests__/feature-service-registry.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ describe('FeatureServiceRegistry', () => {
const stateProviderD = {
id: 'd',
optionalDependencies: {a: '1.0'},
create: jest.fn()
create: jest.fn(() => ({}))
};

expect(() =>
Expand Down Expand Up @@ -300,7 +300,7 @@ describe('FeatureServiceRegistry', () => {
const stateProviderDefinitionD = {
id: 'd',
optionalDependencies: {a: ''},
create: jest.fn()
create: jest.fn(() => ({}))
};

expect(() =>
Expand Down Expand Up @@ -343,7 +343,7 @@ describe('FeatureServiceRegistry', () => {
)
).toThrowError(
new Error(
'The required Feature Service "d" in the unsupported version "1.0" could not be bound to consumer "e". The supported versions are ["foo"].'
'The Feature Service "d" could not be registered by consumer "test" because it contains an invalid version.'
)
);
});
Expand Down
38 changes: 21 additions & 17 deletions packages/core/src/feature-service-registry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,10 +144,24 @@ export class FeatureServiceRegistry implements FeatureServiceRegistryLike {
const config = configs && configs[providerId];
const {featureServices} = this.bindFeatureServices(providerDefinition);

this.sharedFeatureServices.set(
providerId,
providerDefinition.create({config, featureServices})
);
const sharedFeatureService = providerDefinition.create({
config,
featureServices
});

for (const version of Object.keys(sharedFeatureService)) {
if (!coerce(version)) {
throw new Error(
`The Feature Service ${JSON.stringify(
providerId
)} could not be registered by consumer ${JSON.stringify(
consumerId
)} because it contains an invalid version.`
);
}
}

this.sharedFeatureServices.set(providerId, sharedFeatureService);

console.info(
`The Feature Service ${JSON.stringify(
Expand Down Expand Up @@ -304,19 +318,9 @@ export class FeatureServiceRegistry implements FeatureServiceRegistryLike {
const version = supportedVersions.find(supportedVersion => {
const actualVersion = coerce(supportedVersion);

if (!actualVersion) {
throw new Error(
createUnsupportedFeatureServiceMessage(
optional,
providerId,
consumerUid,
requiredVersion,
supportedVersions
)
);
}

return satisfies(actualVersion, requiredVersion);
// We already ensure coercebility at service registration time.
// tslint:disable-next-line: no-non-null-assertion
return satisfies(actualVersion!, requiredVersion);
});

const bindFeatureService = version && sharedFeatureService[version];
Expand Down

0 comments on commit d6f89e8

Please sign in to comment.