Skip to content

Commit

Permalink
feat: Added feature toggle variants
Browse files Browse the repository at this point in the history
  • Loading branch information
elhoyos authored and ivarconr committed Feb 8, 2019
1 parent e459d33 commit 8c12ead
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 4 deletions.
18 changes: 15 additions & 3 deletions docs/api/admin/feature-toggles-api.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,14 @@ This endpoint is the one all admin ui should use to fetch all available feature
"name": "default",
"parameters": {}
}
],
"variants": [
{
"name": "variant1",
},
{
"name": "variant2",
}
]
},
{
Expand All @@ -43,7 +51,8 @@ This endpoint is the one all admin ui should use to fetch all available feature
"percentage": "10"
}
}
]
],
"variants": []
}
]
}
Expand All @@ -63,7 +72,8 @@ Used to fetch details about a specific featureToggle. This is mostly provded to
"name": "default",
"parameters": {}
}
]
],
"variants": []
}
```

Expand Down Expand Up @@ -107,7 +117,8 @@ Returns 200-respose if the feature toggle was created successfully.
"name": "default",
"parameters": {}
}
]
],
"variants": []
}
```

Expand Down Expand Up @@ -145,6 +156,7 @@ Used to fetch list of archived feature toggles
"parameters": {}
}
],
"variants": [],
"strategy": "default",
"parameters": {}
}
Expand Down
3 changes: 3 additions & 0 deletions lib/db/feature-toggle-store.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ const FEATURE_COLUMNS = [
'description',
'enabled',
'strategies',
'variants',
'created_at',
];
const TABLE = 'features';
Expand Down Expand Up @@ -85,6 +86,7 @@ class FeatureToggleStore {
description: row.description,
enabled: row.enabled > 0,
strategies: row.strategies,
variants: row.variants,
createdAt: row.created_at,
};
}
Expand All @@ -96,6 +98,7 @@ class FeatureToggleStore {
enabled: data.enabled ? 1 : 0,
archived: data.archived ? 1 : 0,
strategies: JSON.stringify(data.strategies),
variants: data.variants ? JSON.stringify(data.variants) : null,
created_at: data.createdAt, // eslint-disable-line
};
}
Expand Down
2 changes: 1 addition & 1 deletion lib/options.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ const DEFAULT_OPTIONS = {
enableRequestLogger: isDev(),
secret: 'UNLEASH-SECRET',
sessionAge: THIRTY_DAYS,
adminAuthentication: 'unsecure',
adminAuthentication: 'none',
};

module.exports = {
Expand Down
9 changes: 9 additions & 0 deletions lib/routes/admin-api/feature-schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ const strategiesSchema = joi.object().keys({
parameters: joi.object(),
});

const variantsSchema = joi.object().keys({
name: nameType,
percentage: joi.number(),
});

const featureShema = joi
.object()
.keys({
Expand All @@ -21,6 +26,10 @@ const featureShema = joi
.required()
.min(1)
.items(strategiesSchema),
variants: joi
.array()
.optional()
.items(variantsSchema),
})
.options({ allowUnknown: false, stripUnknown: true });

Expand Down
15 changes: 15 additions & 0 deletions migrations/20190123204125-add-variants-to-features.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
'use strict';

exports.up = function(db, callback) {
db.runSql(
`
ALTER TABLE features ADD "variants" json;
ALTER TABLE features ALTER COLUMN "variants" SET DEFAULT '[]';
`,
callback
);
};

exports.down = function(db, callback) {
db.runSql(`ALTER TABLE features DROP COLUMN "variants";`, callback);
};
20 changes: 20 additions & 0 deletions test/e2e/api/admin/feature.e2e.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,26 @@ test.serial('creates new feature toggle', async t => {
.then(destroy);
});

test.serial('creates new feature toggle with variants', async t => {
t.plan(1);
const { request, destroy } = await setupApp('feature_api_serial');
await request
.post('/api/admin/features')
.send({
name: 'com.test.variants',
enabled: false,
strategies: [{ name: 'default' }],
variants: [{ name: 'variant1' }, { name: 'variant2' }],
})
.set('Content-Type', 'application/json');
await request
.get('/api/admin/features/com.test.variants')
.expect(res => {
t.true(res.body.variants.length === 2);
})
.then(destroy);
});

test.serial('creates new feature toggle with createdBy unknown', async t => {
t.plan(1);
const { request, destroy } = await setupApp('feature_api_serial');
Expand Down

0 comments on commit 8c12ead

Please sign in to comment.