File tree Expand file tree Collapse file tree 5 files changed +51
-41
lines changed
Expand file tree Collapse file tree 5 files changed +51
-41
lines changed Original file line number Diff line number Diff 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 - z A - 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 /**
Original file line number Diff line number Diff 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} }
Original file line number Diff line number Diff 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 - z A - 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 /**
Original file line number Diff line number Diff 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 - z A - 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 = {
You can’t perform that action at this time.
0 commit comments