UNPKG

dry-react

Version:

Initialiseur de structure React Native typée et modulaire

40 lines (32 loc) 1.23 kB
const fs = require('fs'); const path = require('path'); module.exports = function generateInterceptor() { const interceptorPath = path.join(__dirname, '..', 'src', 'core', 'interceptors'); fs.mkdirSync(interceptorPath, { recursive: true }); const content = `import axios from 'axios'; import AsyncStorage from '@react-native-async-storage/async-storage'; import { ENV_CONFIG } from '../../config/env.config.tsx'; const BASE_URL = ENV_CONFIG.baseUrl; const httpClient = axios.create({ baseURL: BASE_URL, headers: { 'Content-Type': 'application/json', }, }); // ✅ Interceptor pour injecter le token automatiquement httpClient.interceptors.request.use( async (config: any) => { const userJson = await AsyncStorage.getItem('currentUser'); const user = userJson ? JSON.parse(userJson) : null; if (user?.token) { config.headers.Authorization = \`Bearer \${user.token}\`; } return config; }, (error: any) => Promise.reject(error) ); export default httpClient; `; fs.writeFileSync(path.join(interceptorPath, 'dry.interceptor.tsx'), content); console.log(`🛡️ Interceptor généré dans src/core/interceptors/dry.interceptor.tsx`); };