@penkov/swagger-code-gen 1.14.0 → 1.15.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.
@@ -88,13 +88,39 @@ function objectToFormWwwEncoded(o: Record<string, any>): string {
88
88
 
89
89
  type ResponseParseMode = 'json' | 'text';
90
90
 
91
+ function normaliseCharset(charset: string): string {
92
+ const normalized = charset.trim().toLowerCase();
93
+ if (normalized === 'cp1251') {
94
+ return 'windows-1251';
95
+ }
96
+ return normalized;
97
+ }
98
+
99
+ function extractCharset(contentType: string | null): string {
100
+ if (contentType == null) {
101
+ return 'utf-8';
102
+ }
103
+ const match = contentType.match(/charset\s*=\s*("?)([^";\s]+)\1/i);
104
+ return normaliseCharset(match?.[2] || 'utf-8');
105
+ }
106
+
107
+ async function readResponseText(response: Response): Promise<string> {
108
+ const payload = await response.arrayBuffer();
109
+ const charset = extractCharset(response.headers.get('content-type'));
110
+ try {
111
+ return new TextDecoder(charset).decode(payload);
112
+ } catch {
113
+ return new TextDecoder('utf-8').decode(payload);
114
+ }
115
+ }
116
+
91
117
  async function requestImpl<T>(request: Request, requestOptions: RequestOptions, parseMode: ResponseParseMode = 'json'): Promise<T> {
92
118
  const preProcessed = requestOptions.preProcessRequest ? await requestOptions.preProcessRequest(request) : request;
93
119
  const resp = await fetch(preProcessed);
94
120
  const postProcessed = requestOptions.postProcessResponse ? await requestOptions.postProcessResponse(preProcessed, resp) : resp;
95
121
  if (postProcessed.ok) {
96
122
  if (parseMode === 'text') {
97
- return (await postProcessed.text()) as T;
123
+ return (await readResponseText(postProcessed)) as T;
98
124
  }
99
125
 
100
126
  let json: any = null;
@@ -102,10 +128,10 @@ async function requestImpl<T>(request: Request, requestOptions: RequestOptions,
102
128
  const ctLent = postProcessed.headers.get('content-length');
103
129
  const ct = ctLent != null ? parseInt(ctLent): 0;
104
130
  if (ct > 0) {
105
- json = await postProcessed.json()
131
+ json = JSON.parse((await readResponseText(postProcessed)).replace(/^\uFEFF/, ''))
106
132
  }
107
133
  } else {
108
- json = await postProcessed.json()
134
+ json = JSON.parse((await readResponseText(postProcessed)).replace(/^\uFEFF/, ''))
109
135
  }
110
136
  return json as T;
111
137
  } else {
@@ -145,4 +171,3 @@ methods.foreach(method => {
145
171
  <%- include('scats-api-client.ejs'); %>
146
172
 
147
173
  <% } %>
148
-
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@penkov/swagger-code-gen",
3
- "version": "1.14.0",
3
+ "version": "1.15.0",
4
4
  "type": "module",
5
5
  "bin": {
6
6
  "generate-client": "dist/cli.mjs"