@25sprout/react-starter
Version:
25sprout web starter with React
58 lines (54 loc) • 2.2 kB
JavaScript
import path from 'path';
import camelCase from 'camelcase';
function mapEntries(obj, mapCallbackFn) {
return Object.fromEntries(Object.entries(obj).map(mapCallbackFn));
}
export default {
name: 'api.js',
input: path.join(process.cwd(), 'src', 'util', 'api', 'apiSchema.json'),
output: path.join(process.cwd(), 'src', 'util', 'api', 'swaggerApi'),
templates: path.join(__dirname, '.', 'api-templates'),
modular: true,
toJS: true,
defaultResponseType: 'void',
// generateClient: false, // 預設 true, false 意即僅產生 data-contract (response) type
singleHttpClient: true, // 客製化 http-client
moduleNameFirstTag: false, // 根據 tag 作檔案分類
extractRequestParams: true,
extractRequestBody: true,
unwrapResponseData: true, // 將 response 降階
generateUnionEnums: true,
cleanOutput: true,
extractingOptions: {
requestBodySuffix: ['RequestPayload'],
requestParamsSuffix: ['RequestParams'],
responseBodySuffix: ['Data', 'Result', 'Output'],
responseErrorSuffix: ['Error', 'Fail', 'Fails', 'ErrorData', 'HttpError', 'BadResponse'],
},
hooks: {
// 將 output 全部改為 camel 命名 ref: https://github.com/acacode/swagger-typescript-api/issues/328
onCreateComponent: component => {
// 全改 snake naming 會導致 body, params 與 search request 這種狀況相依,所以採取下述作法:
// 為方便引用 request payload,設定 'extractRequestBody' 'extractRequestParams' 後,將後綴有特定字詞(預計會送回後端)的 interface 略過
if (typeof component.$ref === 'string') {
const suffix = component.$ref.slice(-14);
const ignore = suffix.includes('RequestPayload') || suffix.includes('RequestParams');
if (ignore) {
return component;
}
}
if (component.rawTypeData.properties) {
component.rawTypeData.properties = mapEntries(
component.rawTypeData.properties,
([propName, value]) => [camelCase(propName), value],
);
}
if (component.rawTypeData.required && Array.isArray(component.rawTypeData.required)) {
component.rawTypeData.required = component.rawTypeData.required.map(propName =>
camelCase(propName),
);
}
return component;
},
},
};