@kasko/fe-mock-api-lib
v1.2.0
Published
KASKO platform API request mocking made easy.
Downloads
9
Maintainers
Keywords
Readme
Frontend Mock API Library
KASKO platform API request mocking made easy.
Usage
- Install the dependency
yarn add -D @kasko/fe-mock-api-lib
- Use it in webpack
+ const mockApi = require('@kasko/fe-mock-api-lib');
module.exports = {
mode: 'development',
devServer: {
open: true,
port: 3000,
+ before(app) {
+ mockApi(app, path.resolve('./mock-api/api.js'));
+ },
+
+ // Optional: reload page after changes are made
+ contentBase: [
+ path.resolve('./src'),
+ path.resolve('./mock-api'),
+ ],
+ watchContentBase: true,
},
};
- Implement the local API overrides in
mock-api
folder:
mock-api/api.js
const path = require('path');
const mockApi = require('@kasko/fe-mock-api-lib');
const item = require('./resources/item.json');
const itemContents = mockApi.parseCsvFile(path.resolve(__dirname, './resources/item-contents.csv'));
const itemFieldDefinitions = require('./resources/item-field-definitions.json');
const itemManifest = require('./resources/item-webapp-manifest.json');
module.exports = mockApi.constructRoutes({
'GET /touchpoints/:touchpoint/items/:item': async (req, res) => {
item.content.data = await itemContents;
item.webapp_manifest = itemManifest;
item.field_definition = itemFieldDefinitions;
return res.json(item);
},
});
mock-api/resources/item-contents.csv
"hello.world","Hello!"
mock-api/resources/item-field-definitions.json
{
"policy": [
{
"name": "salutation",
"validation": "required|string|in:mr,ms"
}
],
"quote": [
{
"name": "duration",
"validation": "required|string|in:P1Y,P2Y,P3Y"
}
]
}
mock-api/resources/item-webapp-manifest.json
{
"version": "1.0",
"schema": {
"config": {
"initial_screen": "start",
"default_values": {
"duration": "P1Y"
},
"requests": [
{
"type": "quote"
},
{
"type": "policy"
},
{
"type": "payment"
}
],
"content_key": {
"insurer_name": "insurer_name"
}
},
"flow": [
// ...
]
}
}
mock-api/resources/item.json
{
"id": "id_hash",
"object": "item",
"currency": "eur",
"currency_profile_id": "id_hash",
"field_definition": {},
"config": {
"tax_rate": 0.19
},
"webapp_manifest": {},
"tags": [],
"payment_methods": [
{
"payment_method": "creditcard",
"provider": "stripe",
"keys": {
"public_key": "pk_test_123"
}
}
],
"subscription_plans": [],
"content": {
"id": "id_hash",
"object": "content",
"data": {}
},
"assets": []
}
Development
yarn install
Testing
yarn test
yarn lint