Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions .eslintrc.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,28 @@ module.exports = defineConfig({
},

overrides: [
{
files: ['*.cjs'],
extends: [
//
'eslint:recommended',
],
rules: {
'prettier/prettier': 'error',
},
},
{
files: ['*.mjs'],
parserOptions: {
sourceType: 'module',
},
extends: [
//
'eslint:recommended',
],
rules: {
'prettier/prettier': 'error',
},
},
{
files: ['*.ts'],
Expand Down
2 changes: 1 addition & 1 deletion .husky/pre-commit
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/env sh
. "$(dirname -- "$0")/_/husky.sh"

npx lint-staged
npx lint-staged --allow-empty
5 changes: 3 additions & 2 deletions .lintstagedrc.cjs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
module.exports = {
'*.ts': 'eslint --fix',
'*': 'prettier --ignore-unknown --write',
'(src|test)/*.{cjs,mjs,ts,tsx}': 'eslint --fix',
'*.{cjs,mjs,ts,tsx,html,css,scss}': 'prettier --write',
'(package|tsconfig*).json': 'prettier --write',
};
26 changes: 13 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -131,22 +131,22 @@ generate({

# 配置

| 参数名 | 类型 | 可选性 | 描述 | 默认值 |
| -------------------- | ----------------- | ------- | --------------------------------------------------------------------------------- | ----------------------------------------------- |
| `cwd` | `string` | `false` | 当前工作路径 | `process.cwd()` |
| `dest` | `string` | `false` | 目标目录 | `src/apis` |
| `axiosImport` | `string` | `false` | axios 导入内容 | 默认从官方 Axios 导入,可以使用自己实现的客户端 |
| `unwrapResponseData` | `boolean` | `false` | 是否取消对 axios response 的包裹(即直接返回 ResponseData,而不是 AxiosResponse) | `false` |
| `apis` | `OpenapiConfig[]` | `false` | OpenAPI 列表 | `[]` |
| 参数名 | 类型 | 可选性 | 描述 | 默认值 |
|----------------------|-------------------|---------|----------------------------------------------------------------|-----------------------------|
| `cwd` | `string` | `false` | 当前工作路径 | `process.cwd()` |
| `dest` | `string` | `false` | 目标目录 | `src/apis` |
| `axiosImport` | `string` | `false` | axios 导入内容 | 默认从官方 Axios 导入,可以使用自己实现的客户端 |
| `unwrapResponseData` | `boolean` | `false` | 是否取消对 axios response 的包裹(即直接返回 ResponseData,而不是 AxiosResponse) | `false` |
| `apis` | `OpenapiConfig[]` | `false` | OpenAPI 列表 | `[]` |

`OpenapiConfig` 签名:

| 名称 | 类型 | 可选项 | 描述 | 默认值 |
| -------------------- | --------- | ------------ | --------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------- | ----------- |
| `name` | `string` | 必须 | openapi 的名称,将会生成 ${name}.ts 文件 | `undefined` |
| `axiosImport` | `string` | 可选 | axios 导入内容,优先级更高 | 无 |
| `unwrapResponseData` | `boolean` | 可选 | 是否取消对 axios response 的包裹,优先级更高(即直接返回 ResponseData,而不是 AxiosResponse) | `false` |
| `schema` | `string | OpenApiSpec` | 必须 | openapi 的 schema,可以是一个链接地址,也可以是本地路径,也可以是一个对象 | `undefined` |
| 名称 | 类型 | 可选 | 描述 | 默认值 |
|----------------------|-----------|--------------|----------------------------------------------------------------------|----------------------------------------------|
| `name` | `string` | 必须 | openapi 的名称,将会生成 ${name}.ts 文件 | `undefined` |
| `axiosImport` | `string` | 可选 | axios 导入内容,优先级更高 | 无 |
| `unwrapResponseData` | `boolean` | 可选 | 是否取消对 axios response 的包裹,优先级更高(即直接返回 ResponseData,而不是 AxiosResponse) | `false` |
| `schema` | `string | OpenApiSpec` | 必须 | openapi 的 schema,可以是一个链接地址,也可以是本地路径,也可以是一个对象 | `undefined` |

# 鸣谢

Expand Down
115 changes: 113 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 6 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"description": "OpenAPI ➡️ Axios",
"scripts": {
"prepare": "husky install",
"lint": "eslint src/**/*.ts test/**/*.ts && tsc --project tsconfig.json --noEmit",
"lint": "eslint --ext=.cjs,.mjs,.ts,.tsx src/ test/ && tsc --project tsconfig.json --noEmit",
"test": "vitest run",
"test:coverage": "vitest run --coverage",
"build:types": "rm -rf dist-dts && tsc --project tsconfig.types.json",
Expand Down Expand Up @@ -51,18 +51,21 @@
"OAS",
"axios"
],
"author": "publish-node-package-action",
"author": "云淡然 <hi@ydr.me> (https://ydr.me)",
"homepage": "https://github.com/FrontEndDev-org/openapi-axios",
"repository": "https://github.com/FrontEndDev-org/openapi-axios",
"license": "MIT",
"dependencies": {
"chalk": "^4.1.2",
"swagger-typescript-api": "^12.0.4"
"swagger-typescript-api": "^12.0.4",
"try-flatten": "^1.0.1"
},
"devDependencies": {
"@commitlint/cli": "^17.4.4",
"@commitlint/config-conventional": "^17.4.4",
"@commitlint/types": "^17.4.4",
"@rollup/plugin-node-resolve": "^15.0.1",
"@rollup/plugin-replace": "^5.0.2",
"@rollup/plugin-typescript": "^11.0.0",
"@types/node": "^18.15.7",
"@types/prettier": "^2.7.2",
Expand Down
10 changes: 10 additions & 0 deletions rollup.config.mjs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import nodeResolve from '@rollup/plugin-node-resolve';
import replace from '@rollup/plugin-replace';
import typescript from '@rollup/plugin-typescript';
import pkg from './package.json' assert { type: 'json' };

/** @type {import('rollup').RollupOptions} */
export default {
Expand All @@ -19,7 +22,14 @@ export default {
sourcemap: true,
},
],
external: Object.keys(pkg.dependencies),
plugins: [
nodeResolve(),
replace({
preventAssignment: true,
'process.env.PKG_NAME': JSON.stringify(pkg.name),
'process.env.PKG_VERSION': JSON.stringify(pkg.version),
}),
typescript({
tsconfig: 'tsconfig.json',
}),
Expand Down
8 changes: 5 additions & 3 deletions src/commands.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import chalk from 'chalk';
import path from 'path';
import * as path from 'path';
import * as process from 'process';
import { normalizeError, tryFlatten } from 'try-flatten';
import { defineConfig } from './configure';
import { generate } from './generator';
import { UserConfig } from './types';
import { isFile, isString, isUrl, normalizeError, syncPromise, tryCatch } from './utils';
import { isFile } from './utils/fs2';
import { isString, isUrl } from './utils/type-is';

interface StartConfig {
// 指定配置文件绝对路径
Expand Down Expand Up @@ -41,7 +43,7 @@ export async function start(startConfig?: StartConfig) {
}

// eslint-disable-next-line @typescript-eslint/no-var-requires
const [err, config] = await tryCatch<UserConfig>(syncPromise(() => require(configPath)));
const [err, config] = tryFlatten(() => require(configPath) as UserConfig | undefined);

if (err) {
throw new Error(err.message);
Expand Down
5 changes: 4 additions & 1 deletion src/const.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import path from 'path';
import * as path from 'path';

export const pkgName = process.env.PKG_NAME!;
export const pkgVersion = process.env.PKG_VERSION!;

const dirname = __dirname;

Expand Down
6 changes: 3 additions & 3 deletions src/generator.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import fs from 'fs/promises';
import path from 'path';
import * as fs from 'fs/promises';
import * as path from 'path';
import { generateApi, GenerateApiParams } from 'swagger-typescript-api';
import { axiosImportDefault, helpersImport, templatesDir } from './const';
import { Generated, GeneratedCallback, OpenapiConfig, StrictConfig } from './types';
import { isBoolean, isString, isUrl } from './utils';
import { isBoolean, isString, isUrl } from './utils/type-is';

export function generateParams(openapiConfig: OpenapiConfig, config: StrictConfig): GenerateApiParams {
const { name, schema, unwrapResponseData: unwrapResponseDataScope } = openapiConfig;
Expand Down
2 changes: 1 addition & 1 deletion src/helpers.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { ContentKind } from './types';
import { isBoolean, isDate, isNumber, isObject, isString } from './utils';
import { isBoolean, isDate, isNumber, isObject, isString } from './utils/type-is';

/**
* 格式化请求头
Expand Down
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export { pkgName, pkgVersion } from './const';
export * from './generator';
export * from './configure';
export * from './types';
Expand Down
9 changes: 9 additions & 0 deletions src/types/env.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
declare module 'process' {
interface ProcessEnv {
// package name
PKG_NAME: string;

// package version
PKG_VERSION: string;
}
}
Loading