@benup/bensdk 1.6.2 → 1.8.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/bin/src/cli/templates/bensdk-cli/generate.ts +92 -19
- package/bin/src/cli/templates/bensdk-cli/templates/action.test.template.ts +34 -0
- package/bin/src/cli/templates/bensdk-cli/templates/utils.template.ts +1 -0
- package/bin/src/cli/templates/bensdk-local-server/app.ts +3 -1
- package/bin/src/cli/templates/package.template.json +2 -2
- package/package.json +1 -1
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
import fs from 'fs';
|
|
2
|
+
import { promises as fsPromises } from 'fs';
|
|
2
3
|
import { initialStates } from './lib/state-machine';
|
|
3
4
|
import { validateBenefitDefinition } from './validate';
|
|
5
|
+
import inquirer from 'inquirer';
|
|
4
6
|
|
|
5
7
|
async function main() {
|
|
6
8
|
const benefitDefinition = (await import(`${process.cwd()}/src/benefit-definition.ts`)).default;
|
|
@@ -24,32 +26,31 @@ async function main() {
|
|
|
24
26
|
{} as Record<string, string[]>
|
|
25
27
|
);
|
|
26
28
|
|
|
27
|
-
Object.keys(stateMachines)
|
|
29
|
+
for (const action of Object.keys(stateMachines)) {
|
|
28
30
|
const states = stateMachines[action];
|
|
29
31
|
|
|
30
|
-
|
|
32
|
+
for (const state of states) {
|
|
31
33
|
if (initialStates.includes(state)) {
|
|
32
|
-
|
|
34
|
+
continue;
|
|
33
35
|
}
|
|
34
36
|
|
|
35
37
|
const fileName = `${state.toLocaleLowerCase()}.handler.ts`;
|
|
36
38
|
let stateHandler = stateHandlerTemplate;
|
|
37
|
-
const actionSize = action
|
|
38
|
-
.split('_')
|
|
39
|
+
const actionSize = action.split('_');
|
|
39
40
|
let actionInCamelCase = '';
|
|
40
|
-
if(actionSize.length === 1){
|
|
41
|
-
actionInCamelCase = action
|
|
42
|
-
|
|
43
|
-
|
|
41
|
+
if (actionSize.length === 1) {
|
|
42
|
+
actionInCamelCase = action
|
|
43
|
+
.split('')
|
|
44
|
+
.map((word, index) => (index === 0 ? word.toUpperCase() : word.toLowerCase()))
|
|
45
|
+
.join('');
|
|
44
46
|
} else {
|
|
45
47
|
actionInCamelCase = action
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
48
|
+
.split('_')[1]
|
|
49
|
+
.split('')
|
|
50
|
+
.map((word, index) => (index === 0 ? word.toUpperCase() : word.toLowerCase()))
|
|
51
|
+
.join('');
|
|
50
52
|
}
|
|
51
53
|
|
|
52
|
-
|
|
53
54
|
stateHandler = stateHandler.replace(
|
|
54
55
|
'// StateMachine,',
|
|
55
56
|
`const stateMachine = benefitDefinition.stateMachine.${action}.${state}; `
|
|
@@ -60,13 +61,13 @@ async function main() {
|
|
|
60
61
|
`import benefitDefinition from '../benefit-definition';`
|
|
61
62
|
);
|
|
62
63
|
|
|
63
|
-
if(action === 'GRANT' || action === 'REVOKE'){
|
|
64
|
+
if (action === 'GRANT' || action === 'REVOKE') {
|
|
64
65
|
stateHandler = stateHandler.replace(
|
|
65
66
|
'// import {Action}',
|
|
66
67
|
`import { ActionBaseGrantRevoke as Action} from '@benup/bensdk/bin/lib/types/action.types'`
|
|
67
68
|
);
|
|
68
69
|
} else {
|
|
69
|
-
|
|
70
|
+
stateHandler = stateHandler.replace(
|
|
70
71
|
'// import {Action}',
|
|
71
72
|
`import { ActionBase${actionInCamelCase} as Action} from '@benup/bensdk/bin/lib/types/action.types'`
|
|
72
73
|
);
|
|
@@ -86,9 +87,81 @@ async function main() {
|
|
|
86
87
|
if (!fs.existsSync(handlersDir)) {
|
|
87
88
|
fs.mkdirSync(handlersDir, { recursive: true });
|
|
88
89
|
}
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
90
|
+
|
|
91
|
+
if (!fs.existsSync(`${handlersDir}/${fileName}`)) {
|
|
92
|
+
await fsPromises.writeFile(`${handlersDir}/${fileName}`, stateHandler, 'utf-8');
|
|
93
|
+
} else {
|
|
94
|
+
const answer = await inquirer.prompt({
|
|
95
|
+
type: 'list',
|
|
96
|
+
name: 'overwrite',
|
|
97
|
+
message: `The file ${fileName} already exists. Do you want to overwrite it?`,
|
|
98
|
+
choices: ['overwrite', 'skip']
|
|
99
|
+
});
|
|
100
|
+
|
|
101
|
+
if (answer.overwrite === 'overwrite') {
|
|
102
|
+
await fsPromises.writeFile(`${handlersDir}/${fileName}`, stateHandler, 'utf-8');
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
const testsDir = `${process.cwd()}/__tests__`;
|
|
108
|
+
|
|
109
|
+
if (!fs.existsSync(testsDir)) {
|
|
110
|
+
fs.mkdirSync(testsDir, { recursive: true });
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
// Read the action test template
|
|
114
|
+
let actionTestTemplate = await fsPromises.readFile(
|
|
115
|
+
process.cwd() + '/bin/cli/templates/action.test.template.ts',
|
|
116
|
+
'utf-8'
|
|
117
|
+
);
|
|
118
|
+
|
|
119
|
+
// Replaces the action placeholder with the current action name
|
|
120
|
+
actionTestTemplate = actionTestTemplate.replaceAll('CHANGE_ACTION_NAME_PLACEHOLDER', action);
|
|
121
|
+
|
|
122
|
+
const testFileName = `${action.toLowerCase()}.test.ts`;
|
|
123
|
+
if (!fs.existsSync(`${testsDir}/${testFileName}`)) {
|
|
124
|
+
await fsPromises.writeFile(`${testsDir}/${testFileName}`, actionTestTemplate, 'utf-8');
|
|
125
|
+
} else {
|
|
126
|
+
const answer = await inquirer.prompt({
|
|
127
|
+
type: 'list',
|
|
128
|
+
name: 'overwrite',
|
|
129
|
+
message: `The test file ${testFileName} already exists. Do you want to overwrite it?`,
|
|
130
|
+
choices: ['overwrite', 'skip']
|
|
131
|
+
});
|
|
132
|
+
|
|
133
|
+
if (answer.overwrite === 'overwrite') {
|
|
134
|
+
await fsPromises.writeFile(`${testsDir}/${testFileName}`, actionTestTemplate, 'utf-8');
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
/**
|
|
139
|
+
* Creates the utils.ts for the tests if it doesn't exists
|
|
140
|
+
*/
|
|
141
|
+
if (!fs.existsSync(`${testsDir}/utils.ts`)) {
|
|
142
|
+
const utilsTestTemplate = await fsPromises.readFile(
|
|
143
|
+
process.cwd() + '/bin/cli/templates/utils.template.ts',
|
|
144
|
+
'utf-8'
|
|
145
|
+
);
|
|
146
|
+
|
|
147
|
+
await fsPromises.writeFile(`${testsDir}/utils.ts`, utilsTestTemplate, 'utf-8');
|
|
148
|
+
} else {
|
|
149
|
+
const answer = await inquirer.prompt({
|
|
150
|
+
type: 'list',
|
|
151
|
+
name: 'overwrite',
|
|
152
|
+
message: `The utils.ts file already exists. Do you want to overwrite it?`,
|
|
153
|
+
choices: ['overwrite', 'skip']
|
|
154
|
+
});
|
|
155
|
+
|
|
156
|
+
if (answer.overwrite === 'overwrite') {
|
|
157
|
+
const utilsTestTemplate = await fsPromises.readFile(
|
|
158
|
+
process.cwd() + '/bin/cli/templates/utils.template.ts',
|
|
159
|
+
'utf-8'
|
|
160
|
+
);
|
|
161
|
+
await fsPromises.writeFile(`${testsDir}/utils.ts`, utilsTestTemplate, 'utf-8');
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
}
|
|
92
165
|
}
|
|
93
166
|
|
|
94
167
|
main();
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { describe, expect, it } from 'vitest';
|
|
2
|
+
import { app } from '../bin/server/app';
|
|
3
|
+
import { getUrl } from './utils';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Write your state-machine actions tests here!
|
|
7
|
+
* How to do that? This is the test-suite for CHANGE_ACTION_NAME_PLACEHOLDER, We recommend that
|
|
8
|
+
* for each state from the action you write the possible scenarios
|
|
9
|
+
*/
|
|
10
|
+
describe('SM Tests for CHANGE_ACTION_NAME_PLACEHOLDER', () => {
|
|
11
|
+
const action = 'CHANGE_ACTION_NAME_PLACEHOLDER';
|
|
12
|
+
|
|
13
|
+
it('[REQUEST_CHANGE_ACTION_NAME_PLACEHOLDER] should fail and return 500 for invalid payload', async () => {
|
|
14
|
+
const res = await app.request(`${getUrl()}`, {
|
|
15
|
+
method: 'POST',
|
|
16
|
+
headers: { 'Content-Type': 'application/json' },
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* We recommend you to create an utils or some method that create this payload
|
|
20
|
+
*/
|
|
21
|
+
body: JSON.stringify({
|
|
22
|
+
message: {},
|
|
23
|
+
requestPayload: {
|
|
24
|
+
action: 'CHANGE_ACTION_NAME_PLACEHOLDER'
|
|
25
|
+
},
|
|
26
|
+
currentContext: {}
|
|
27
|
+
})
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
const data = await res.text();
|
|
31
|
+
|
|
32
|
+
expect(res.status).toBe(500);
|
|
33
|
+
});
|
|
34
|
+
});
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export const getUrl = () => 'http://localhost:3000/run-action';
|
|
@@ -69,7 +69,7 @@ wss.on('connection', (ws) => {
|
|
|
69
69
|
});
|
|
70
70
|
});
|
|
71
71
|
|
|
72
|
-
|
|
72
|
+
async function startServer() {
|
|
73
73
|
return new Promise((res: any, rej: any) => {
|
|
74
74
|
const server = serve({ fetch: app.fetch, port: 3000 });
|
|
75
75
|
server.on('upgrade', (request, socket, head) => {
|
|
@@ -85,3 +85,5 @@ export async function startServer() {
|
|
|
85
85
|
res();
|
|
86
86
|
});
|
|
87
87
|
}
|
|
88
|
+
|
|
89
|
+
export { app, startServer };
|
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
"main": "index.js",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"scripts": {
|
|
7
|
-
"test": "
|
|
7
|
+
"test": "vitest --run",
|
|
8
8
|
"format": "npx prettier --write .",
|
|
9
9
|
"lint": "npx tsc --noemit && npx prettier --check . && npx eslint .",
|
|
10
10
|
"start": "cd bin/viewer && tsx startup.ts",
|
|
@@ -34,7 +34,7 @@
|
|
|
34
34
|
"pino": "^9.6.0",
|
|
35
35
|
"hono": "^4.7.7",
|
|
36
36
|
"@hono/node-server": "^1.14.1",
|
|
37
|
-
"vitest": "^3.
|
|
37
|
+
"vitest": "^3.2.4",
|
|
38
38
|
"@sveltejs/adapter-auto": "^4.0.0",
|
|
39
39
|
"@sveltejs/kit": "^2.16.0",
|
|
40
40
|
"@sveltejs/vite-plugin-svelte": "^5.0.0",
|