Skip to content

Commit d8bd272

Browse files
author
williamd5
authored
Merge branch 'main' into build-test
2 parents 76c3472 + 3e0429f commit d8bd272

File tree

5 files changed

+51
-41
lines changed

5 files changed

+51
-41
lines changed

browser/Cloudnode.js

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -60,20 +60,22 @@ class Cloudnode {
6060
const response = await fetch(url.toString(), options);
6161
if (response.status === 204)
6262
return undefined;
63+
const text = await response.text();
64+
let data;
6365
if (response.headers.get("Content-Type")?.startsWith("application/json")) {
64-
const json = await response.json();
65-
if (response.ok)
66-
return json;
67-
else
68-
throw json;
69-
}
70-
else {
71-
const text = await response.text();
72-
if (response.ok)
73-
return text;
74-
else
75-
throw text;
66+
data = JSON.parse(text, (key, value) => {
67+
// parse dates
68+
if (/^\d{4}-\d{2}-\d{2}T(?:\d{2}:){2}\d{2}(?:\.\d+)?(?:[a-zA-Z]+|\+\d{2}:\d{2})?$/.test(value))
69+
return new Date(value);
70+
return value;
71+
});
7672
}
73+
else
74+
data = text;
75+
if (response.ok)
76+
return data;
77+
else
78+
throw data;
7779
};
7880
newsletter = {
7981
/**

browser/Cloudnode.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

gen/templates/main.mustache

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -64,16 +64,19 @@ class {{config.name}} {
6464
options.credentials = "include";
6565
const response = await fetch(url.toString(), options);
6666
if (response.status === 204) return undefined as any;
67+
const text = await response.text();
68+
let data: T;
6769
if (response.headers.get("Content-Type")?.startsWith("application/json")) {
68-
const json = await response.json();
69-
if (response.ok) return json as T;
70-
else throw json;
71-
}
72-
else {
73-
const text = await response.text();
74-
if (response.ok) return text as any;
75-
else throw text;
70+
data = JSON.parse(text, (key, value) => {
71+
// parse dates
72+
if (/^\d{4}-\d{2}-\d{2}T(?:\d{2}:){2}\d{2}(?:\.\d+)?(?:[a-zA-Z]+|\+\d{2}:\d{2})?$/.test(value))
73+
return new Date(value);
74+
return value;
75+
});
7676
}
77+
else data = text as any;
78+
if (response.ok) return data;
79+
else throw data;
7780
}
7881

7982
{{#namespaces}}

src/Cloudnode.js

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -60,20 +60,22 @@ class Cloudnode {
6060
const response = await fetch(url.toString(), options);
6161
if (response.status === 204)
6262
return undefined;
63+
const text = await response.text();
64+
let data;
6365
if (response.headers.get("Content-Type")?.startsWith("application/json")) {
64-
const json = await response.json();
65-
if (response.ok)
66-
return json;
67-
else
68-
throw json;
69-
}
70-
else {
71-
const text = await response.text();
72-
if (response.ok)
73-
return text;
74-
else
75-
throw text;
66+
data = JSON.parse(text, (key, value) => {
67+
// parse dates
68+
if (/^\d{4}-\d{2}-\d{2}T(?:\d{2}:){2}\d{2}(?:\.\d+)?(?:[a-zA-Z]+|\+\d{2}:\d{2})?$/.test(value))
69+
return new Date(value);
70+
return value;
71+
});
7672
}
73+
else
74+
data = text;
75+
if (response.ok)
76+
return data;
77+
else
78+
throw data;
7779
};
7880
newsletter = {
7981
/**

src/Cloudnode.ts

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -64,16 +64,19 @@ class Cloudnode {
6464
options.credentials = "include";
6565
const response = await fetch(url.toString(), options);
6666
if (response.status === 204) return undefined as any;
67+
const text = await response.text();
68+
let data: T;
6769
if (response.headers.get("Content-Type")?.startsWith("application/json")) {
68-
const json = await response.json();
69-
if (response.ok) return json as T;
70-
else throw json;
71-
}
72-
else {
73-
const text = await response.text();
74-
if (response.ok) return text as any;
75-
else throw text;
70+
data = JSON.parse(text, (key, value) => {
71+
// parse dates
72+
if (/^\d{4}-\d{2}-\d{2}T(?:\d{2}:){2}\d{2}(?:\.\d+)?(?:[a-zA-Z]+|\+\d{2}:\d{2})?$/.test(value))
73+
return new Date(value);
74+
return value;
75+
});
7676
}
77+
else data = text as any;
78+
if (response.ok) return data;
79+
else throw data;
7780
}
7881

7982
public newsletter = {

0 commit comments

Comments
 (0)