backend-scaffold-cli 1.4.0 → 1.5.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/cli.js +82 -0
- package/package.json +1 -1
package/bin/cli.js
CHANGED
|
@@ -48,6 +48,12 @@ program
|
|
|
48
48
|
name: 'installDeps',
|
|
49
49
|
message: 'Install dependencies now?',
|
|
50
50
|
default: true
|
|
51
|
+
},
|
|
52
|
+
{
|
|
53
|
+
type: 'confirm',
|
|
54
|
+
name: 'useAxios',
|
|
55
|
+
message: 'Include axios (for external API calls)?',
|
|
56
|
+
default: true
|
|
51
57
|
}
|
|
52
58
|
]);
|
|
53
59
|
|
|
@@ -74,6 +80,9 @@ program
|
|
|
74
80
|
if (answers.useAuth) {
|
|
75
81
|
createUserModel(projectPath);
|
|
76
82
|
}
|
|
83
|
+
if(answers.useAxios){
|
|
84
|
+
createAxiosUtil(projectPath);
|
|
85
|
+
}
|
|
77
86
|
|
|
78
87
|
console.log(chalk.cyan(' Initializing git repository...'));
|
|
79
88
|
execSync('git init', { cwd: projectPath });
|
|
@@ -149,6 +158,7 @@ const createConfigFiles = (projectPath, answers) => {
|
|
|
149
158
|
...(answers.useValidation && { joi: '^17.9.0' }),
|
|
150
159
|
...(answers.useRateLimit && { 'express-rate-limit': '^6.7.0' }),
|
|
151
160
|
...(answers.useFileUpload && { multer: '^2.0.0' }),
|
|
161
|
+
...(answers.useAxios && { axios: '^1.6.0' }),
|
|
152
162
|
|
|
153
163
|
},
|
|
154
164
|
devDependencies: {
|
|
@@ -664,4 +674,76 @@ function createUserModel(projectPath) {
|
|
|
664
674
|
)
|
|
665
675
|
|
|
666
676
|
console.log(chalk.green('User model created'));
|
|
677
|
+
}
|
|
678
|
+
|
|
679
|
+
|
|
680
|
+
|
|
681
|
+
|
|
682
|
+
function createAxiosUtil(projectPath){
|
|
683
|
+
const axiosUtil = ` const axios = require('axios');
|
|
684
|
+
|
|
685
|
+
//create an axios instance with default config
|
|
686
|
+
const axiosInstance = axios.create({
|
|
687
|
+
baseURL: process.env.API_BASE_URL || '',
|
|
688
|
+
timeout: 10000,
|
|
689
|
+
headers:{
|
|
690
|
+
'Content-Type' : 'application/json',
|
|
691
|
+
}
|
|
692
|
+
});
|
|
693
|
+
|
|
694
|
+
//request interceptor
|
|
695
|
+
axiosInstance.interceptors.request.use(
|
|
696
|
+
(config) =>{
|
|
697
|
+
console.log(\`Making request to: \${config.url}\`);;
|
|
698
|
+
return config;
|
|
699
|
+
},
|
|
700
|
+
(error) => Promise.reject(error)
|
|
701
|
+
);
|
|
702
|
+
|
|
703
|
+
//response interceptor
|
|
704
|
+
axiosInstance.interceptors.response.use(
|
|
705
|
+
(response) => response,
|
|
706
|
+
(error) => {
|
|
707
|
+
console.error('API call error: ', error.response ? error.response.data : error.message);
|
|
708
|
+
return Promise.reject(error);
|
|
709
|
+
}
|
|
710
|
+
);
|
|
711
|
+
|
|
712
|
+
//fetch data function
|
|
713
|
+
const fetchData = async (url) => {
|
|
714
|
+
try{
|
|
715
|
+
const response = await axiosInstance.get(url);
|
|
716
|
+
return response.data;
|
|
717
|
+
}catch(error){
|
|
718
|
+
throw new Error(\`Failed to fetch data: \${error.message}\`);
|
|
719
|
+
}
|
|
720
|
+
};
|
|
721
|
+
|
|
722
|
+
//post data function
|
|
723
|
+
const postData = async (url, data) => {
|
|
724
|
+
try{
|
|
725
|
+
const response = await axiosInstance.post(url, data);
|
|
726
|
+
return response.data;
|
|
727
|
+
}catch(error){
|
|
728
|
+
throw new Error(\`Failed to post data: \${error.message}\`);
|
|
729
|
+
}
|
|
730
|
+
};
|
|
731
|
+
|
|
732
|
+
module.exports = {
|
|
733
|
+
axiosInstance,
|
|
734
|
+
fetchData,
|
|
735
|
+
postData,
|
|
736
|
+
};
|
|
737
|
+
|
|
738
|
+
|
|
739
|
+
|
|
740
|
+
`;
|
|
741
|
+
|
|
742
|
+
|
|
743
|
+
fs.writeFileSync(
|
|
744
|
+
path.join(projectPath, 'src/utils/axiosUtil.js'),
|
|
745
|
+
axiosUtil
|
|
746
|
+
);
|
|
747
|
+
|
|
748
|
+
console.log(chalk.green('Axios utility created'));
|
|
667
749
|
}
|