Skip to content

Instantly share code, notes, and snippets.

@LeezQ
Last active May 22, 2019 11:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save LeezQ/1abaef8535f59b276193226674fe1418 to your computer and use it in GitHub Desktop.
Save LeezQ/1abaef8535f59b276193226674fe1418 to your computer and use it in GitHub Desktop.
api.gen.js
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'),
];
// 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 %}
}
{ "openapi-generator": "^0.1.20" }
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