-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
402 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
#!/usr/bin/env node | ||
|
||
import { start } from '../dist'; | ||
|
||
start(); |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import { axiosImportDefault } from './const'; | ||
import { Config, UserConfig } from './types'; | ||
|
||
export const defaults: Config = { | ||
cwd: process.cwd(), | ||
dest: 'src/apis', | ||
axiosImport: axiosImportDefault, | ||
list: [], | ||
}; | ||
|
||
export function defineConfig(config: UserConfig) { | ||
return Object.assign({}, defaults, config); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import path from 'node:path'; | ||
|
||
export const templatesDir = path.join(__dirname, '../templates'); | ||
export const axiosImportDefault = `import { Axios } from 'axios'; | ||
const axios = new Axios();`; | ||
export const helpersImport = `import { formatHeaders, formatBody } from 'oas-gen-ts/client'`; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import { cosmiconfig } from 'cosmiconfig'; | ||
import fs from 'node:fs/promises'; | ||
import path from 'node:path'; | ||
import { generateApi } from 'swagger-typescript-api'; | ||
import { axiosImportDefault, helpersImport, templatesDir } from './const'; | ||
import { Config, Oas } from './types'; | ||
|
||
export async function generateItem(oas: Oas, config: Config) { | ||
const { name, url, spec, axiosImport: axiosImportScope } = oas; | ||
const { cwd, dest, axiosImport: axiosImportGlobal } = config; | ||
const axiosImport = axiosImportScope || axiosImportGlobal || axiosImportDefault; | ||
const { files } = await generateApi({ | ||
name, | ||
url, | ||
spec, | ||
output: false, | ||
httpClientType: 'axios', | ||
templates: templatesDir, | ||
}); | ||
|
||
for (const { content, name: filename } of files) { | ||
const contentFinal = [axiosImport, helpersImport, content].join('\n'); | ||
const file = path.join(cwd, dest, filename); | ||
await fs.writeFile(file, contentFinal); | ||
} | ||
} | ||
|
||
export async function generate(config: Config) { | ||
const { list } = config; | ||
|
||
for (const oas of list) { | ||
await generateItem(oas, config); | ||
} | ||
} | ||
|
||
export async function start() { | ||
const explorer = cosmiconfig('oas'); | ||
const result = await explorer.search(); | ||
|
||
if (!result) { | ||
throw new Error('Could not find an oas config file'); | ||
} | ||
|
||
const config = result.config as Config; | ||
await generate(config); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import { ContentKind } from './types'; | ||
|
||
/** | ||
* 格式化请求头 | ||
* @param {ContentKind} contentKind | ||
* @returns {{"content-type": string} | {}} | ||
*/ | ||
export function formatHeaders(contentKind: ContentKind) { | ||
const contentType = { | ||
[ContentKind.JSON]: 'application/json', | ||
[ContentKind.URL_ENCODED]: 'application/x-www-form-urlencoded', | ||
[ContentKind.FORM_DATA]: 'multipart/form-data', | ||
[ContentKind.TEXT]: 'text/plain', | ||
[ContentKind.OTHER]: '', | ||
}[contentKind]; | ||
return contentType ? { 'content-type': contentType } : {}; | ||
} | ||
|
||
/** | ||
* 格式化请求体 | ||
* @param {string} contentKind | ||
* @param data | ||
* @returns {FormData | string} | ||
*/ | ||
export function formatBody(contentKind: ContentKind, data: any) { | ||
switch (contentKind) { | ||
case ContentKind.URL_ENCODED: | ||
return new URLSearchParams(data).toString(); | ||
|
||
case ContentKind.FORM_DATA: { | ||
return Object.keys(data).reduce((fd, key) => { | ||
const val = data[key]; | ||
const isFileType = val instanceof Blob || val instanceof File; | ||
const isString = typeof val === 'string' || typeof val === 'number'; | ||
fd.append(key, isFileType ? val : isString ? String(val) : JSON.stringify(val)); | ||
return fd; | ||
}, new FormData()); | ||
} | ||
|
||
default: | ||
return JSON.stringify(data); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,2 @@ | ||
// | ||
export {}; | ||
export * from './generator'; | ||
export * from './configure'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
interface OasBase { | ||
name: string; | ||
|
||
/** | ||
* 全局导入 axios 客户端,优先级低于每个 oas 配置,默认从 axios 官方导入,导入名称必须为 axios,例如 | ||
* ``` | ||
* import { axios } from '@/utils/axios'; | ||
* ``` | ||
*/ | ||
axiosImport?: string; | ||
} | ||
|
||
interface OasAsUrl extends OasBase { | ||
url: string; | ||
} | ||
|
||
interface OasAsSpec extends OasBase { | ||
spec: import('swagger-schema-official').Spec; | ||
} | ||
|
||
export interface Oas extends OasAsUrl, OasAsSpec {} | ||
|
||
export interface UserConfig { | ||
/** | ||
* 工作目录,默认为 process.cwd() | ||
*/ | ||
cwd?: string; | ||
|
||
/** | ||
* 生成文件目的地,默认为 src/apis | ||
*/ | ||
dest?: string; | ||
|
||
/** | ||
* 导入 axios 客户端,优先级高于全局配置,默认从 axios 官方导入,导入名称必须为 axios,例如 | ||
* ``` | ||
* import { axios } from '@/utils/axios'; | ||
* ``` | ||
*/ | ||
axiosImport?: string; | ||
|
||
/** | ||
* oas 列表 | ||
*/ | ||
list: Oas[]; | ||
} | ||
|
||
export type Config = Required<UserConfig>; | ||
|
||
export enum ContentKind { | ||
JSON = 'JSON', | ||
URL_ENCODED = 'URL_ENCODED', | ||
FORM_DATA = 'FORM_DATA', | ||
TEXT = 'TEXT', | ||
OTHER = 'OTHER', | ||
} |
Oops, something went wrong.