75 lines
2.1 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import { Logger } from '@fesjs/compiler';
import { readFileSync } from 'fs';
import { join } from 'path';
import { name } from '../package.json';
const logger = new Logger('fes:plugin-request');
export default (api) => {
api.addRuntimePluginKey(() => 'request');
// 配置
api.describe({
key: 'request',
config: {
schema(joi) {
return joi.object({
dataField: joi
.string()
.pattern(/^[a-zA-Z]*$/)
.allow(''),
base: joi.string().allow(''),
});
},
default: {
base: '',
dataField: '',
},
},
});
const namespace = 'plugin-request';
const absoluteFilePath = `${namespace}/request.js`;
const requestTemplate = readFileSync(join(__dirname, 'template', 'request.js'), 'utf-8');
api.onGenerateFiles(() => {
// 文件写出
const { dataField = '', base = '' } = api.config.request;
if (base) {
// DEPRECATED
logger.warn('[DEPRECATED]: reqeust base 即将废弃,建议使用 axios baseURL代替https://github.com/axios/axios');
}
api.writeTmpFile({
path: absoluteFilePath,
content: requestTemplate
.replace('REPLACE_DATA_FIELD', JSON.stringify(dataField))
.replace('REPLACE_BASE', base || '')
.replace('AXIOS_PATH', 'axios'),
});
});
let generatedOnce = false;
api.onGenerateFiles(() => {
if (generatedOnce) return;
generatedOnce = true;
api.copyTmpFiles({
namespace,
path: join(__dirname, 'template'),
ignore: ['request.js'],
});
});
api.addPluginExports(() => [
{
exportAll: true,
source: absoluteFilePath,
},
]);
api.addConfigType(() => ({
source: name,
runtime: ['RequestRuntimeConfig'],
build: ['RequestBuildConfig'],
}));
};