openapi-ts-request 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,54 @@
1
+ /* eslint-disable */
2
+ // @ts-ignore
3
+ {% for type in list -%}
4
+ {%- if type.props.length %}
5
+ export type {{ type.typeName | safe }} =
6
+ {%- for prop in type.props %}
7
+ {%- if prop.length > 1 %}
8
+ {
9
+ {%- endif %}
10
+ {%- if prop.length == 1 %}
11
+ {%- if not prop[0].$ref or prop[0].name %}
12
+ {
13
+ {%- endif %}
14
+ {%- endif %}
15
+ {%- for p in prop %}
16
+ {%- if p.desc %}
17
+ /** {{ p.desc }} */
18
+ {%- endif %}
19
+ {%- if p["$ref"] and not p.name %}
20
+ // {{ p.$ref }}
21
+ {{ p.type | safe }}
22
+ {%- else %}
23
+ {%- if nullable %}
24
+ '{{ p.name }}': {{ p.type | safe }}{{'' if p.required else '| null'}};
25
+ {%- else %}
26
+ '{{ p.name }}'{{ '' if p.required else '?' }}: {{ p.type | safe }};
27
+ {%- endif %}
28
+ {%- endif %}
29
+ {%- endfor %}
30
+ {%- if prop.length > 1 %}
31
+ }
32
+ {%- endif %}
33
+ {%- if prop.length == 1 %}
34
+ {%- if not prop[0].$ref or prop[0].name %}
35
+ }
36
+ {%- endif %}
37
+ {%- endif %}
38
+ {%- if prop.length == 0 %}
39
+ {}
40
+ {%- endif %}
41
+ {{ '' if loop.last === true else ' & ' }}
42
+ {%- endfor %}
43
+ {%- else %}
44
+ {%- if type.isEnum %}
45
+ export enum {{ type.typeName | safe }} {{ type.type }};
46
+
47
+ export type I{{ type.typeName | safe }} = keyof typeof {{ type.typeName }}
48
+ {%- else %}
49
+ export type {{ type.typeName | safe }} = {{ type.type }};
50
+ {%- endif %}
51
+ {%- endif %}
52
+ {% endfor %}
53
+
54
+ export default {}
@@ -0,0 +1,168 @@
1
+ /* eslint-disable */
2
+ // @ts-ignore
3
+ {{ requestImportStatement }}
4
+ import * as {{ namespace }} from './{{ interfaceFileName }}';
5
+
6
+ {% for api in list -%}
7
+ /** {{ api.desc if api.desc else '此处后端没有提供注释' }} {{api.method | upper}} {{ api.pathInComment | safe }} */
8
+ export async function {{ api.functionName }}(
9
+ {%- if api.params and api.hasParams %}
10
+ // 叠加生成的Param类型 (非body参数openapi默认没有生成对象)
11
+ params
12
+ {%- if genType === "ts" -%}
13
+ : {{namespace}}.{{api.typeName}}
14
+ {# header 入参 -#}
15
+ {% if api.params.header -%}
16
+ & { // header
17
+ {% for param in api.params.header -%}
18
+ {% if param.description -%}
19
+ /** {{ param.description }} */
20
+ {% endif -%}
21
+ '{{ param.name }}'
22
+ {{- "?" if not param.required }}
23
+ {{- (": " + param.type + ";") | safe }}
24
+ {% endfor -%}
25
+ }
26
+ {%- endif -%}
27
+ {%- endif -%}
28
+ {%- if api.hasParams -%}
29
+ {{ "," if api.body or api.file}}
30
+ {%- endif -%}
31
+ {%- endif -%}
32
+
33
+
34
+
35
+ {%- if api.body -%}
36
+ body
37
+ {%- if genType === "ts" -%}
38
+
39
+ : {% if api.body.propertiesList %}{
40
+ {%- for prop in api.body.propertiesList %}
41
+ {% if prop.schema.description -%}
42
+ /** {{ prop.schema.description }} */
43
+ {% endif -%}
44
+ {{ prop.key }}{{ "?" if not prop.schema.required }}: {{ prop.schema.type }},
45
+ {%- endfor %}
46
+ }
47
+ {%- else -%}
48
+ {{ api.body.type }}
49
+ {%- endif -%}
50
+
51
+ {%- endif -%}
52
+ {{ "," if api.file }}
53
+ {%- endif %}
54
+
55
+
56
+
57
+ {%- if api.file -%}
58
+ {%- for file in api.file -%}
59
+ {{file.title | safe}}
60
+ {%- if genType === "ts" -%}
61
+ {{- "?" if not api.file.required -}}
62
+ : File {{ "[]" if file.multiple }}
63
+ {%- endif -%}
64
+ {{"," if not loop.last }}
65
+ {%- endfor -%}
66
+ {%- endif -%}
67
+
68
+
69
+
70
+ {{ "," if api.body or api.hasParams or api.file }}
71
+ options {{ ("?: " + requestOptionsType) if genType === "ts" }}
72
+ ) {
73
+ {% if api.params and api.params.path -%}
74
+ const { {% for param in api.params.path %}'{{ param.name }}': {{ param.alias }}, {% endfor %}
75
+ {% if api.params.path -%}
76
+ ...queryParams
77
+ {% endif -%}
78
+ } = params;
79
+ {% endif -%}
80
+
81
+
82
+
83
+ {% if api.hasFormData -%}
84
+ const formData = new FormData();
85
+
86
+ {% if api.file -%}
87
+ {% for file in api.file %}
88
+ if({{file.title | safe}}){
89
+ {% if file.multiple %}
90
+ {{file.title | safe}}.forEach(f => formData.append('{{file.title | safe}}', f || ''));
91
+ {% else %}
92
+ formData.append('{{file.title | safe}}', {{file.title | safe}})
93
+ {% endif %}
94
+ }
95
+ {% endfor %}
96
+ {%- endif -%}
97
+
98
+ {% if api.body %}
99
+ Object.keys(body).forEach(ele => {
100
+ {% if genType === "ts" %}
101
+ const item = (body as any)[ele];
102
+ {% else %}
103
+ const item = body[ele];
104
+ {% endif %}
105
+ if (item !== undefined && item !== null) {
106
+ {% if genType === "ts" %}
107
+ if (typeof item === 'object' && !(item instanceof File)) {
108
+ if (item instanceof Array) {
109
+ item.forEach((f) => formData.append(ele, f || ''));
110
+ } else {
111
+ formData.append(ele, JSON.stringify(item));
112
+ }
113
+ } else {
114
+ formData.append(ele, item);
115
+ }
116
+ {% else %}
117
+ formData.append(ele, typeof item === 'object' ? JSON.stringify(item) : item);
118
+ {% endif %}
119
+ }
120
+ });
121
+ {% endif %}
122
+ {% endif -%}
123
+
124
+
125
+
126
+ {% if api.hasPathVariables or api.hasApiPrefix -%}
127
+ return request{{ ("<" + api.response.type + ">") | safe if genType === "ts" }}(`{{ api.path | safe }}`, {
128
+ {% else -%}
129
+ return request{{ ("<" + api.response.type + ">") | safe if genType === "ts" }}('{{ api.path }}', {
130
+ {% endif -%}
131
+ method: '{{ api.method | upper }}',
132
+
133
+ {%- if api.hasHeader and api.body.mediaType %}
134
+ headers: {
135
+ {%- if api.body.mediaType %}
136
+ 'Content-Type': '{{ api.body.mediaType | safe }}',
137
+ {%- endif %}
138
+ },
139
+ {%- endif %}
140
+
141
+ {%- if api.params and api.hasParams %}
142
+ params: {
143
+ {%- for query in api.params.query %}
144
+ {% if query.schema.default -%}
145
+ // {{query.name | safe}} has a default value: {{ query.schema.default | safe }}
146
+ '{{query.name | safe}}': '{{query.schema.default | safe}}',
147
+ {%- endif -%}
148
+ {%- endfor -%}
149
+ ...{{ 'queryParams' if api.params and api.params.path else 'params' }},
150
+ {%- for query in api.params.query %}
151
+ {%- if query.isComplexType %}
152
+ '{{query.name | safe}}': undefined,
153
+ ...{{ 'queryParams' if api.params and api.params.path else 'params' }}['{{query.name | safe}}'],
154
+ {%- endif %}
155
+ {%- endfor -%}
156
+ },
157
+ {%- endif %}
158
+
159
+ {%- if api.hasFormData %}
160
+ data: formData,
161
+ {%- elseif api.body %}
162
+ data: body,
163
+ {%- endif %}
164
+ ...(options || {{api.options | dump}}),
165
+ });
166
+ }
167
+
168
+ {% endfor -%}
@@ -0,0 +1,13 @@
1
+ /* eslint-disable */
2
+ // @ts-ignore
3
+ import * as {{ namespace }} from './{{ interfaceFileName }}';
4
+ {% for api in list -%}
5
+ import * as {{ api.controllerName }} from './{{ api.fileName }}';
6
+ {% endfor -%}
7
+
8
+ export default {
9
+ {{ namespace }},
10
+ {% for api in list -%}
11
+ {{ api.controllerName }},
12
+ {% endfor -%}
13
+ }