dry-react
Version:
Initialiseur de structure React Native typée et modulaire
99 lines (79 loc) • 3.25 kB
JavaScript
const fs = require('fs');
const path = require('path');
module.exports = function generateService() {
const servicePath = path.join(__dirname, '..', 'src', 'core', 'services');
fs.mkdirSync(servicePath, { recursive: true });
// === dry-api-available.service.tsx ===
const apiAvailableContent = `import { getApiUrl } from '../../config/env.config.tsx';
async function DryApiAvailable(url: string): Promise<boolean> {
const fullUrl = getApiUrl(url);
try {
const response = await fetch(fullUrl, { method: 'HEAD' });
return response.ok;
} catch {
return false;
}
}
export default DryApiAvailable;
`;
fs.writeFileSync(path.join(servicePath, 'dry-api-available.service.tsx'), apiAvailableContent);
// === dry-main.service.tsx ===
const mainServiceContent = `import { getApiUrl } from '../../config/env.config.tsx';
import httpClient from '../interceptors/dry.interceptor.tsx';
import DryApiAvailable from './dry-api-available.service.tsx';
const apiErrorResponse = {
success: false,
data: {},
error: 'Contactez l\\'administrateur',
};
class DryMainService {
private async safeCall<T>(method: () => Promise<T>): Promise<T | typeof apiErrorResponse> {
try {
return await method();
} catch (error) {
console.error('Erreur lors de la requête HTTP :', error);
return { data: '', success: false, error: '' };
}
}
private async withApiGuard<T>(url: string, method: () => Promise<T>) {
const available = await DryApiAvailable(url);
return available ? this.safeCall(method) : apiErrorResponse;
}
find(params: any = {}, url: string = '') {
const fullUrl = getApiUrl(\`\${url}/find\`);
return httpClient.post(fullUrl, params);
}
async getAll(params: any = {}, url: string = '') {
const fullUrl = getApiUrl(url);
return this.withApiGuard(url, () => httpClient.get(fullUrl, { params }));
}
async create(_object: any = {}, url: string = '', params = {}) {
const fullUrl = getApiUrl(url);
let config: any = { params };
if (_object instanceof FormData) {
config.headers = { 'Content-Type': 'multipart/form-data' };
}
return this.withApiGuard(url, () => httpClient.post(fullUrl, _object, config));
}
async update(_object: any = {}, url: string = '') {
const fullUrl = getApiUrl(\`\${url}/\${_object._id}\`);
return this.withApiGuard(url, () => httpClient.put(fullUrl, _object));
}
async delete(id: string = '', url: string = '') {
const fullUrl = getApiUrl(\`\${url}/\${id}\`);
return this.withApiGuard(url, () => httpClient.delete(fullUrl));
}
async getById(id: string = '', url: string = '') {
const fullUrl = getApiUrl(\`\${url}/\${id}\`);
return this.withApiGuard(url, () => httpClient.get(fullUrl));
}
async getOne(url: string = '') {
const fullUrl = getApiUrl(url);
return this.withApiGuard(url, () => httpClient.get(fullUrl));
}
}
export default new DryMainService();
`;
fs.writeFileSync(path.join(servicePath, 'dry-main.service.tsx'), mainServiceContent);
console.log(`🔧 Services générés dans src/core/services avec getApiUrl() intégré.`);
};