-
Notifications
You must be signed in to change notification settings - Fork 57
Expand file tree
/
Copy pathserviceController.njk
More file actions
164 lines (158 loc) · 5.68 KB
/
serviceController.njk
File metadata and controls
164 lines (158 loc) · 5.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
{% if genType === "ts" %}
/* eslint-disable */
// @ts-ignore
{% endif -%}
{{ requestImportStatement }}
{% if genType === "ts" %}
import * as {{ namespace }} from './{{ interfaceFileName }}';
{% endif %}
{% for api in list -%}
{% if api.customTemplate %}
{{ api.data }}
{% else %}
/** {{ api.desc if api.desc else '此处后端没有提供注释' }} {{ api.method | upper }} {{ api.pathInComment | safe }}{{ ' ' if api.apifoxRunLink else '' }}{{ api.apifoxRunLink }} */
export function {{ api.functionName }}({
{%- if api.params and api.hasParams %}
params
{%- if api.hasParams -%}
{{ "," if api.body or api.file }}
{%- endif -%}
{%- endif -%}
{%- if api.body -%}
body
{{ "," if api.file }}
{%- endif %}
{%- if api.file -%}
{%- for file in api.file -%}
{{ file.title | safe }}
{{ "," if not loop.last }}
{%- endfor -%}
{%- endif -%}
{{ "," if api.body or api.hasParams or api.file }}
options
}
{%- if genType === "ts" -%}
: {
{%- if api.params and api.hasParams %}
// 叠加生成的Param类型 (非body参数openapi默认没有生成对象)
params: {{ namespace }}.{{ api.typeName }}
{# header 入参 -#}
{% if api.params.header -%}
& { // header
{% for param in api.params.header -%}
{% if param.description -%}
/** {{ param.description }} */
{% endif -%}
'{{ param.name }}'
{{- "?" if not param.required }}
{{- (": " + param.type + ";") | safe }}
{% endfor -%}
}
{%- endif -%}
{%- if api.hasParams -%}
{{ ";" if api.body or api.file }}
{%- endif -%}
{%- endif -%}
{%- if api.body -%}
body: {{ api.body.type }}
{{ ";" if api.file }}
{%- endif %}
{%- if api.file -%}
{%- for file in api.file -%}
{{ file.title | safe }}
{{- "?" if not api.file.required -}}
: globalThis.File {{ "[]" if file.multiple }}
{{ ";" if not loop.last }}
{%- endfor -%}
{%- endif -%}
{{ ";" if api.body or api.hasParams or api.file }}
options?: {{ requestOptionsType }}
}
{%- endif -%}
)
{
{% if api.params and api.params.path %}
const { {% for param in api.params.path %}'{{ param.name }}': {{ param.alias }}, {% endfor %}
{% if api.params.path -%}
...queryParams
{% endif -%}
} = params;
{% endif %}
{%- if api.hasFormData -%}
const formData = new FormData();
{% if api.file -%}
{% for file in api.file %}
if({{ file.title | safe }}) {
{% if file.multiple %}
{{ file.title | safe }}.forEach(f => formData.append('{{ file.title | safe }}', f || ''));
{% else %}
formData.append('{{ file.title | safe }}', {{ file.title | safe }})
{% endif %}
}
{% endfor %}
{%- endif -%}
{% if api.body %}
Object.keys(body).forEach(ele => {
{% if genType === "ts" %}
const item = (body as { [key: string]: any })[ele];
{% else %}
const item = body[ele];
{% endif %}
if (item !== undefined && item !== null) {
{% if genType === "ts" %}
if (typeof item === 'object' && !(item instanceof globalThis.File)) {
if (item instanceof Array) {
item.forEach((f) => formData.append(ele, f || ''));
} else {
formData.append(ele, JSON.stringify(item));
}
} else {
formData.append(ele, item);
}
{% else %}
formData.append(ele, typeof item === 'object' ? JSON.stringify(item) : item);
{% endif %}
}
});
{% endif %}
{% endif %}
{% if api.hasPathVariables or api.hasApiPrefix -%}
return request{{ ("<" + api.response.type + ">") | safe if genType === "ts" }}(`{{ api.path | safe }}`, {
{% else -%}
return request{{ ("<" + api.response.type + ">") | safe if genType === "ts" }}('{{ api.path }}', {
{% endif -%}
method: '{{ api.method | upper }}',
{%- if api.hasHeader and api.body.mediaType %}
headers: {
{%- if api.body.mediaType %}
'Content-Type': '{{ api.body.mediaType | safe }}',
{%- endif %}
},
{%- endif %}
{%- if api.params and api.hasParams %}
params: {
{%- for query in api.params.query %}
{% if query.schema.default -%}
// {{ query.name | safe }} has a default value: {{ query.schema.default | safe }}
'{{ query.name | safe }}': '{{query.schema.default | safe}}',
{%- endif -%}
{%- endfor -%}
...{{ 'queryParams' if api.params and api.params.path else 'params' }},
{%- for query in api.params.query %}
{%- if query.isComplexType %}
'{{ query.name | safe }}': undefined,
...{{ 'queryParams' if api.params and api.params.path else 'params' }}['{{ query.name | safe }}'],
{%- endif %}
{%- endfor -%}
},
{%- endif %}
{%- if api.hasFormData %}
data: formData,
{%- elseif api.body %}
data: body,
{%- endif %}
...(options || {{ api.options | dump }}),
});
}
{% endif %}
{% endfor -%}