Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
125 changes: 66 additions & 59 deletions examples/openapi-ts-fetch/src/client/client/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,79 +76,86 @@
// fetch must be assigned here, otherwise it would throw the error:
// TypeError: Failed to execute 'fetch' on 'Window': Illegal invocation
const _fetch = opts.fetch!;
let response = await _fetch(request);

for (const fn of interceptors.response._fns) {
if (fn) {
response = await fn(response, request, opts);
}
let error : unknown;
let response

Check notice

Code scanning / CodeQL

Semicolon insertion Note

Avoid automated semicolon insertion (93% of all statements in
the enclosing function
have an explicit semicolon).
try {
response = await _fetch(request);
}catch (e) {
error = e

Check notice

Code scanning / CodeQL

Semicolon insertion Note

Avoid automated semicolon insertion (93% of all statements in
the enclosing function
have an explicit semicolon).
}

const result = {
request,
response,
};

if (response.ok) {
if (
response.status === 204 ||
response.headers.get('Content-Length') === '0'
) {
return opts.responseStyle === 'data'
? {}
: {
data: {},
...result,
};
if (response) {
for (const fn of interceptors.response._fns) {
if (fn) {
response = await fn(response, request, opts);
}
}

const parseAs =
(opts.parseAs === 'auto'
? getParseAs(response.headers.get('Content-Type'))
: opts.parseAs) ?? 'json';

let data: any;
switch (parseAs) {
case 'arrayBuffer':
case 'blob':
case 'formData':
case 'json':
case 'text':
data = await response[parseAs]();
break;
case 'stream':
const result = {
request,
response,
};

if (response.ok) {
if (
response.status === 204 ||
response.headers.get('Content-Length') === '0'
) {
return opts.responseStyle === 'data'
? response.body
? {}
: {
data: response.body,
data: {},
...result,
};
}
}

if (parseAs === 'json') {
if (opts.responseValidator) {
await opts.responseValidator(data);
const parseAs =
(opts.parseAs === 'auto'
? getParseAs(response.headers.get('Content-Type'))
: opts.parseAs) ?? 'json';

let data: any;
switch (parseAs) {
case 'arrayBuffer':
case 'blob':
case 'formData':
case 'json':
case 'text':
data = await response[parseAs]();
break;
case 'stream':
return opts.responseStyle === 'data'
? response.body
: {
data: response.body,
...result,
};
}

if (opts.responseTransformer) {
data = await opts.responseTransformer(data);
if (parseAs === 'json') {
if (opts.responseValidator) {
await opts.responseValidator(data);
}

if (opts.responseTransformer) {
data = await opts.responseTransformer(data);
}
}
}

return opts.responseStyle === 'data'
? data
: {
data,
...result,
};
}
return opts.responseStyle === 'data'
? data
: {
data,
...result,
};
}

let error = await response.text();
error = await response.text();

try {
error = JSON.parse(error);
} catch {
// noop
try {
error = JSON.parse(error as string);
} catch {
// noop
}
}

let finalError = error;
Expand Down
Loading