Skip to content

Commit

Permalink
fix: 兼容 JSON 配置文件
Browse files Browse the repository at this point in the history
Closes: #48
  • Loading branch information
cloudcome committed Mar 16, 2023
1 parent 6a36926 commit 1a380fa
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 8 deletions.
6 changes: 3 additions & 3 deletions src/configure.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { axiosImportDefault } from './const';
import { Config, UserConfig } from './types';
import { StrictConfig, UserConfig } from './types';

export const defaults: Config = {
export const defaults: StrictConfig = {
cwd: process.cwd(),
dest: 'src/apis',
axiosImport: axiosImportDefault,
Expand All @@ -10,5 +10,5 @@ export const defaults: Config = {
};

export function defineConfig(config: UserConfig) {
return Object.assign({}, defaults, config);
return Object.assign({}, defaults, config) as StrictConfig;
}
13 changes: 9 additions & 4 deletions src/generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@ import { cosmiconfig } from 'cosmiconfig';
import fs from 'fs/promises';
import path from 'path';
import { generateApi } from 'swagger-typescript-api';
import { defineConfig } from './configure';
import { axiosImportDefault, helpersImport, templatesDir } from './const';
import { Config, Oas } from './types';
import { StrictConfig, Oas, UserConfig } from './types';
import { exitError, normalizeError, tryCatch } from './utils';

export async function generateItem(oas: Oas, config: Config) {
export async function generateItem(oas: Oas, config: StrictConfig) {
const { name, url, spec, axiosImport: axiosImportScope } = oas;
const { cwd, dest, axiosImport: axiosImportGlobal, unwrapResponseData } = config;
const axiosImport = axiosImportScope || axiosImportGlobal || axiosImportDefault;
Expand All @@ -32,7 +33,7 @@ export async function generateItem(oas: Oas, config: Config) {
}
}

export async function generate(config: Config) {
export async function generate(config: StrictConfig) {
const { list } = config;
let step = 0;
const length = list.length;
Expand Down Expand Up @@ -60,7 +61,11 @@ export async function start() {
return exitError('配置文件未找到');
}

const config = result.config as Config;
const config = result.filepath.endsWith('js')
? // js 文件使用 defineConfig,返回的是 StrictConfig
(result.config as StrictConfig)
: // json 文件是纯文本,返回的 UserConfig
defineConfig(result.config as UserConfig);

try {
await generate(config);
Expand Down
2 changes: 1 addition & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ export interface UserConfig {
list: Oas[];
}

export type Config = Required<UserConfig>;
export type StrictConfig = Required<UserConfig>;

export enum ContentKind {
JSON = 'JSON',
Expand Down

0 comments on commit 1a380fa

Please sign in to comment.