Last active
May 22, 2019 11:52
-
-
Save LeezQ/1abaef8535f59b276193226674fe1418 to your computer and use it in GitHub Desktop.
api.gen.js
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
function config(api, sdkDir) { | |
const tmp = sdkDir.split('/'); | |
const namespace = tmp[tmp.length - 1]; | |
return { | |
api, | |
sdkDir, | |
namespace: `${namespace[0].toUpperCase()}${namespace.substr(1)}API`, | |
camelCase: 'lower', | |
templatePath: './src/services/sdk.njk', | |
interfaceTemplatePath: './src/services/interface.njk', | |
requestLib: false, | |
hook: { | |
customClassName: tagName => tagName.replace('Controller', ''), | |
// 根据 api url 生成 service 函数名 | |
// customFunctionName: (data) => { | |
// return data.path | |
// .replace(/\//g, '_') | |
// .substr(1) | |
// .replace(/_(\w)/g, function (_all, letter) { | |
// return letter.toUpperCase(); | |
// }) + `Using${data.method.toUpperCase()}` | |
// } | |
}, | |
}; | |
} | |
module.exports = [ | |
config('http://xxx/v2/api-docs', './src/services/atpayment'), | |
]; |
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
// tslint:disable | |
export namespace {{ namespace }} { | |
{% for type in list -%} | |
{%- if type.props.length %} | |
export interface {{ type.typeName | safe }} { | |
{%- for prop in type.props %} | |
{%- if prop.desc %} | |
/** {{ prop.desc }} */ | |
{%- endif %} | |
{{ prop.name }}{{ '' if prop.required else '?' }}: {{ prop.type | safe }}; | |
{%- endfor %} | |
} | |
{%- else %} | |
export type {{ type.typeName | safe }} = {{ type.type }}; | |
{%- endif %} | |
{% endfor %} | |
} |
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
{ "openapi-generator": "^0.1.20" } |
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
import request from '@/utils/request'; | |
// @ts-ignore | |
import { {{namespace}} } from './typings'; | |
{% for api in list -%} | |
/** {{ api.desc }} */ | |
export async function {{ api.functionName }}( | |
{%- if api.params %} | |
params | |
{%- if genType === "ts" -%} | |
: { | |
{# query 入参 -#} | |
{% if api.params.query -%} | |
{% for param in api.params.query -%} | |
{% if param.description -%} | |
/** {{ param.description }} */ | |
{% endif -%} | |
{{ param.name }} | |
{{- "?" if not param.required }} | |
{{- (": " + param.type + ";") | safe }} | |
{% endfor -%} | |
{% endif -%} | |
{# header 入参 -#} | |
{% if api.params.header -%} | |
{% for param in api.params.header -%} | |
{% if param.description -%} | |
/** {{ param.description }} */ | |
{% endif -%} | |
{{ param.name }} | |
{{- "?" if not param.required }} | |
{{- (": " + param.type + ";") | safe }} | |
{% endfor -%} | |
{% endif -%} | |
{# path 入参 -#} | |
{% if api.params.path -%} | |
{% for param in api.params.path -%} | |
{% if param.description -%} | |
/** {{ param.description }} */ | |
{% endif -%} | |
{{ param.name }} | |
{{- "?" if not param.required }} | |
{{- (": " + param.type + ";") | safe }} | |
{% endfor -%} | |
{% endif -%} | |
} | |
{%- endif -%} | |
{{ "," if api.body }} | |
{%- endif -%} | |
{%- if api.body %} | |
body | |
{%- if genType === "ts" -%} | |
: {% if api.body.propertiesList %}{ | |
{%- for prop in api.body.propertiesList %} | |
{% if prop.schema.description -%} | |
/** {{ prop.schema.description }} */ | |
{% endif -%} | |
{{ prop.key }}{{ "?" if not prop.schema.requird }}: {{ prop.schema.type }}, | |
{%- endfor %} | |
} | |
{%- else -%} | |
{{ api.body.type }} | |
{%- endif -%} | |
{%- endif -%} | |
{%- endif %} | |
){{ (": Promise<" + api.response.type + ">") | safe if genType === "ts" }} { | |
{% if api.params and api.params.path -%} | |
const { {% for param in api.params.path %}{{ param.name }}, {% endfor %} } = params; | |
{% endif -%} | |
return request(`{{ api.path | safe }}`, { | |
method: `{{ api.method | upper }}`, | |
{%- if api.params %} | |
params, | |
{%- endif %} | |
{%- if api.body %} | |
body, | |
{%- endif %} | |
{%- if api.method | upper !== 'GET' %} | |
headers: { | |
'Content-Type': '{{ api.body.mediaType | safe if api.body.mediaType else "application/x-www-form-urlencoded" | safe }}', | |
}, | |
{%- endif %} | |
}); | |
} | |
{% endfor -%} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment