Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
Monster
Manage
Activity
Members
Plan
Jira
Code
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Analyze
Contributor analytics
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
OSS
Libraries
Javascript
Monster
Commits
dba378ea
Verified
Commit
dba378ea
authored
2 years ago
by
Volker Schukai
Browse files
Options
Downloads
Patches
Plain Diff
fix: error in the status processing
parent
5e4df080
No related branches found
No related tags found
No related merge requests found
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
application/source/data/datasource/server/restapi.mjs
+45
-66
45 additions, 66 deletions
application/source/data/datasource/server/restapi.mjs
development/test/cases/data/datasource/server/restapi.mjs
+12
-11
12 additions, 11 deletions
development/test/cases/data/datasource/server/restapi.mjs
with
57 additions
and
77 deletions
application/source/data/datasource/server/restapi.mjs
+
45
−
66
View file @
dba378ea
...
...
@@ -114,44 +114,17 @@ class RestAPI extends Server {
* @throws {Error} the data cannot be read
*/
read
()
{
const
self
=
this
;
let
response
;
let
init
=
self
.
getOption
(
'
read.init
'
);
if
(
!
isObject
(
init
))
init
=
{};
if
(
!
init
[
'
method
'
])
init
[
'
method
'
]
=
'
GET
'
;
return
new
Promise
((
resolve
,
reject
)
=>
{
fetch
(
self
.
getOption
(
'
read.url
'
),
init
).
then
(
resp
=>
{
response
=
resp
;
const
acceptedStatus
=
self
.
getOption
(
'
read.acceptedStatus
'
,
[
200
]);
if
(
acceptedStatus
.
indexOf
(
resp
.
status
)
===
-
1
)
{
throw
Error
(
'
the data cannot be read (response
'
+
resp
.
status
+
'
)
'
)
}
return
resp
.
text
()
}).
then
(
body
=>
{
let
obj
;
try
{
obj
=
JSON
.
parse
(
body
);
}
catch
(
e
)
{
if
(
body
.
length
>
100
)
{
body
=
body
.
substring
(
0
,
97
)
+
'
...
'
;
}
throw
new
Error
(
'
the response does not contain a valid json (actual:
'
+
body
+
'
).
'
);
}
return
fetchData
.
call
(
this
,
'
read
'
,
(
obj
)
=>
{
self
.
set
(
self
.
transformServerPayload
.
call
(
self
,
obj
));
resolve
(
response
);
}).
catch
(
reject
);
});
})
}
/**
...
...
@@ -159,6 +132,7 @@ class RestAPI extends Server {
* @throws {WriteError} the data cannot be written
*/
write
()
{
const
self
=
this
;
let
init
=
self
.
getOption
(
'
write.init
'
);
...
...
@@ -168,60 +142,65 @@ class RestAPI extends Server {
'
Content-Type
'
:
'
application/json
'
}
}
if
(
!
init
[
'
method
'
])
init
[
'
method
'
]
=
'
POST
'
;
let
obj
=
self
.
prepareServerPayload
(
self
.
get
());
init
[
'
body
'
]
=
JSON
.
stringify
(
obj
);
return
new
Promise
((
resolve
,
reject
)
=>
{
fetch
(
self
.
getOption
(
'
write.url
'
),
init
).
then
(
response
=>
{
const
acceptedStatus
=
self
.
getOption
(
'
write.acceptedStatus
'
,
[
200
,
201
]);
if
(
acceptedStatus
.
indexOf
(
response
.
status
)
>
-
1
)
{
reject
(
response
);
return
;
return
fetchData
.
call
(
this
,
init
,
'
write
'
);
}
response
.
text
().
then
((
body
)
=>
{
let
obj
=
{},
validation
=
{};
try
{
obj
=
JSON
.
parse
(
body
);
/**
* @return {RestAPI}
*/
getClone
()
{
const
self
=
this
;
return
new
RestAPI
(
self
[
internalSymbol
].
getRealSubject
()[
'
options
'
].
read
,
self
[
internalSymbol
].
getRealSubject
()[
'
options
'
].
write
);
}
if
(
reportPath
)
{
validation
=
(
new
Pathfinder
(
obj
)).
getVia
(
reportPath
);
}
}
catch
(
e
)
{
function
fetchData
(
init
,
key
,
callback
)
{
if
(
body
.
length
>
100
)
{
body
=
body
.
substring
(
0
,
97
)
+
'
...
'
;
}
const
self
=
this
;
let
response
;
reject
(
new
Error
(
'
the response does not contain a valid json (actual:
'
+
body
+
'
).
'
));
return
;
}
reject
(
new
WriteError
(
'
the data cannot be written (response
'
+
response
.
status
+
'
)
'
,
response
,
validation
))
return
;
return
fetch
(
self
.
getOption
(
key
+
'
.url
'
),
init
).
then
(
resp
=>
{
response
=
resp
;
const
acceptedStatus
=
self
.
getOption
(
key
+
'
.acceptedStatus
'
,
[
200
]);
if
(
acceptedStatus
.
indexOf
(
resp
.
status
)
===
-
1
)
{
throw
Error
(
'
the data cannot be
'
+
key
+
'
(response
'
+
resp
.
status
+
'
)
'
)
}
}).
catch
(
reject
);
return
resp
.
text
()
}).
then
(
body
=>
{
let
obj
;
}).
catch
(
reject
);
try
{
obj
=
JSON
.
parse
(
body
);
}
)
}
catch
(
e
)
{
if
(
body
.
length
>
100
)
{
body
=
body
.
substring
(
0
,
97
)
+
'
...
'
;
}
throw
new
Error
(
'
the response does not contain a valid json (actual:
'
+
body
+
'
).
'
);
}
/**
* @return {RestAPI}
*/
getClone
()
{
const
self
=
this
;
return
new
RestAPI
(
self
[
internalSymbol
].
getRealSubject
()[
'
options
'
].
read
,
self
[
internalSymbol
].
getRealSubject
()[
'
options
'
].
write
);
if
(
callback
&&
isFunction
(
callback
))
{
callback
(
obj
);
}
return
response
;
});
}
...
...
This diff is collapsed.
Click to expand it.
development/test/cases/data/datasource/server/restapi.mjs
+
12
−
11
View file @
dba378ea
...
...
@@ -28,9 +28,6 @@ describe('RestAPI', function () {
a
:
"
test
"
}));
});
},
status
:
returnStatus
});
...
...
@@ -55,19 +52,23 @@ describe('RestAPI', function () {
});
it
(
'
write should
'
,
function
(
done
)
{
const
ds
=
new
RestAPI
({
url
:
'
https://monsterjs.org/assets/world.json
'
},
{
const
ds
=
new
RestAPI
({
read
:
{
url
:
'
https://monsterjs.org/assets/world.json
'
},
write
:
{
url
:
'
https://monsterjs.org/assets/world.json
'
,
acceptedStatus
:
[
99
]
})
}
}
)
ds
.
write
().
then
(
data
=>
{
done
(
"
should not be here
"
);
}).
catch
(
e
=>
done
());
});
});
})
describe
(
'
rw with errors
'
,
function
()
{
it
(
'
read should throw exception
'
,
function
(
done
)
{
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment