diff --git a/CHANGELOG.md b/CHANGELOG.md index 05f99ce..72d2d77 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,7 +1,14 @@ -# 1.0.0-alpha.0 (2024-05-02) +# [1.1.0](https://github.com/shijistar/swagger-api-hub/compare/v1.0.0...v1.1.0) (2024-05-04) ### Features -- add commitlint ([fba1adf](https://github.com/shijistar/swagger-api-hub/commit/fba1adfc3d440c7b5b2c24eb2514053ae8d4556b)) -- add husky and lint-staged ([d58e331](https://github.com/shijistar/swagger-api-hub/commit/d58e3318e48df67dcbe63df4e9287d810d531bae)) -- add main implementation of swagger-api-hub ([63af1d3](https://github.com/shijistar/swagger-api-hub/commit/63af1d3b545d1f403928067130d3db7b077561ad)) +- config file support reading from npm module ([a738538](https://github.com/shijistar/swagger-api-hub/commit/a738538c9abc0c26d0cee1d9ba4c63a65e1e87e4)) + +# 1.0.0 (2024-05-03) + +### Features + +- add commitlint ([d2fe275](https://github.com/shijistar/swagger-api-hub/commit/d2fe275d8296913a395eefdd53974722aa74af90)) +- add husky and lint-staged ([9bc1c7e](https://github.com/shijistar/swagger-api-hub/commit/9bc1c7ef913ee079a7acff05ef6e50abc9b9016c)) +- add main implementation of swagger-api-hub ([37c71fc](https://github.com/shijistar/swagger-api-hub/commit/37c71fc1a934ad901c6de7da68bc145e2d12817d)) +- change name to swagger-api-hub ([38f735c](https://github.com/shijistar/swagger-api-hub/commit/38f735cf19f903ddb47d331a1d11df2e9fc4e8b2)) diff --git a/README.md b/README.md index a78b87b..c645238 100644 --- a/README.md +++ b/README.md @@ -30,7 +30,9 @@ $ swagger-api-hub $ swagger-api-hub config/services.ts ``` -The configuration file should export an array of `ServiceConfig` objects. If you export only one object, it will generate the code directly without prompting. The config file extension can be `.ts`, `.js`, `.json`, or any other module types supported by Node.js. +The configuration file should export an array of `ServiceConfig` objects. If you export only one object, it will generate the code directly without prompting. + +The config file can be either local path or npm module path, and the file extension can be `.ts`, `.js`, `.json`, or any other module types supported by Node.js. The file should look like this: @@ -61,7 +63,7 @@ The `ServiceConfig` object has the following properties: - `id`: _[required]_ A unique identifier for the service. - `name`: A human-readable name for the service. -- `url`: _[required]_ The URL of the OpenAPI specification file. If you have a local file, you can use `input` instead of `url` to specify the file path. Or you can even use `spec` to provide the OpenAPI specification object directly. Either `url`, `input`, or `spec` is required. +- `url`: _[required]_ The URL of the OpenAPI specification file. If you have a local file, you can use `input` instead of `url` to specify the file path. Or you can even use `spec` to provide the OpenAPI specification object directly. Either url, input, or spec is required. - `apiBase`: The base path of all API endpoints. The service may be hosted on a subpath of the main domain, e.g., _/public-api/iam/v3_, then the apiBase is _/public-api_ in this case. If the original api path from the OpenAPI specification is acceptable, you don't need this field. - `crossOrigin`: Whether to use the absolute api path when calling the service. This is useful when the service is hosted on a different domain and you need to set the `Access-Control-Allow-Origin` header. Default is `false`. - `output`: The output directory for the generated code. The default is `./src/api/{id}`. diff --git a/cli/index.ts b/cli/index.ts new file mode 100644 index 0000000..02a86a5 --- /dev/null +++ b/cli/index.ts @@ -0,0 +1,2 @@ +export * from './generate'; +export type * from './types'; diff --git a/cli/repl.ts b/cli/repl.ts index eaefa89..0b4e84d 100644 --- a/cli/repl.ts +++ b/cli/repl.ts @@ -14,21 +14,24 @@ import type { ServiceConfig } from './types'; const program = new commander.Command(); -process.title = 'swagger-api-hub-cli'; - program .name(Object.keys(packageJson.bin)[0]) .description('Generate front-end interface code to interact with OpenAPI-based backend services') .usage('[options] [config-path]') .argument( '[config-path]', - 'Path to the configuration file, if not specified, the default configuration file will be used', + 'Path to the configuration file, if not specified, the default file path will be used', './swagger-api-hub.config.ts' ) .helpOption('-h, --help') .version(packageJson.version, '-v, --version') .action(async (configPath) => { - const absPath = resolve(configPath); + let absPath: string; + try { + absPath = require.resolve(configPath); + } catch (error) { + absPath = resolve(configPath); + } if (!existsSync(absPath)) { signale.fatal('Config file not found:', configPath); process.exit(1); @@ -36,9 +39,8 @@ program let config: ServiceConfig[] | ServiceConfig; try { - // support both module files and json files // eslint-disable-next-line @typescript-eslint/no-var-requires - const originalConfig = require(resolve(configPath)); + const originalConfig = require(absPath); config = originalConfig?.default ?? originalConfig; } catch (error) { signale.fatal(`Config file parse error (${configPath}):`, error); diff --git a/cli/types.ts b/cli/types.ts index 6a00ff9..5e0e512 100644 --- a/cli/types.ts +++ b/cli/types.ts @@ -1,9 +1,12 @@ import type { GenerateApiParams } from 'swagger-typescript-api'; +/** + * The configuration of a service. + */ export type ServiceConfig = GenerateApiParams & { - /** the unique id of service */ + /** The unique id of service */ id: string; - /** the friendly name of service */ + /** The friendly name of service */ name?: string; /** * The base path of all API endpoints. diff --git a/index.ts b/index.ts deleted file mode 100644 index db3debd..0000000 --- a/index.ts +++ /dev/null @@ -1,2 +0,0 @@ -export * from './cli/generate'; -export type * from './cli/types'; \ No newline at end of file diff --git a/package.json b/package.json index 3181921..0897ef3 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "swagger-api-hub", - "version": "1.0.0", + "version": "1.1.0", "description": "Generate front-end access code for swagger backend services in one click", "keywords": [ "OpenApi", @@ -18,7 +18,8 @@ }, "license": "MIT", "author": "李凤宝 ", - "main": "index.js", + "main": "lib/index.js", + "types": "lib/index.d.ts", "bin": { "swagger-api-hub": "./bin/swagger-api-hub.js" }, diff --git a/sample/swagger-api-hub.config.ts b/sample/swagger-api-hub.config.ts index e9abeb7..5fc86be 100644 --- a/sample/swagger-api-hub.config.ts +++ b/sample/swagger-api-hub.config.ts @@ -5,14 +5,12 @@ const services: ServiceConfig[] = [ id: 'iam', name: 'User Management Service', url: 'https://api.example.com/iam/swagger/v3', - output: './src/api/iam', }, { id: 'asset', name: 'Asset Management Service', url: 'https://api.example.com/asset/swagger/v3', apiBase: '/asset', - output: './src/api/asset', }, ]; export default services; diff --git a/templates/http-clients/axios-http-client.ejs b/templates/http-clients/axios-http-client.ejs index cc4189a..a7b76dc 100644 --- a/templates/http-clients/axios-http-client.ejs +++ b/templates/http-clients/axios-http-client.ejs @@ -42,7 +42,6 @@ export enum ContentType { export class HttpClient { public instance: AxiosInstance; - private axiosConfig: AxiosRequestConfig; private securityData: SecurityDataType | null = null; private securityWorker?: ApiConfig["securityWorker"]; private secure?: boolean; @@ -50,7 +49,6 @@ export class HttpClient { constructor(axiosInstance?: AxiosInstance, { securityWorker, secure, format, ...axiosConfig }: ApiConfig = {}) { this.instance = axiosInstance ?? axios.create(axiosConfig); - this.axiosConfig = axiosConfig; this.secure = secure; this.format = format; this.securityWorker = securityWorker; @@ -72,7 +70,7 @@ export class HttpClient { ...(params1.headers || {}), ...((params2 && params2.headers) || {}), }, - baseURL: params2?.baseURL || params1?.baseURL || this.axiosConfig?.baseURL, + baseURL: params2?.baseURL || params1?.baseURL || this.instance.defaults.baseURL, }; } diff --git a/tsconfig.json b/tsconfig.json index d00ebf9..3b9894a 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -10,6 +10,7 @@ "jsx": "react-jsx", "strict": true, "skipLibCheck": true, + "declaration": true, "esModuleInterop": true, "allowSyntheticDefaultImports": true, "forceConsistentCasingInFileNames": false,