Toggle navigation
Toggle navigation
This project
Loading...
Sign in
arcs
/
arcs.js-components
Go to a project
Toggle navigation
Toggle navigation pinning
Projects
Groups
Snippets
Help
Project
Activity
Repository
Pipelines
Graphs
Issues
0
Merge Requests
0
Wiki
Network
Create a new issue
Builds
Commits
Authored by
Jean-Yves Didier
2023-04-17 10:51:26 +0200
Browse Files
Options
Browse Files
Download
Email Patches
Plain Diff
Commit
a9853e3e03f6193e8adfd7a63c14f33c95d57994
a9853e3e
1 parent
77e9db5a
added rest component
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
89 additions
and
0 deletions
components/rest.js
components/rest.js
0 → 100644
View file @
a9853e3
import
ARCS
from
'../engine/arcs.js'
;
let
RestWrapper
;
/**
* @class RestWrapper
* @classdesc A wrapper for REST requests
*
*/
RestWrapper
=
ARCS
.
Component
.
create
(
function
()
{
const
request
=
async
function
(
method
,
url
,
obj
)
{
const
requestOptions
=
{
method
:
method
,
headers
:
{
'Content-Type'
:
'application/json'
},
};
if
(
obj
)
requestOptions
.
body
=
JSON
.
stringify
(
obj
);
let
response
=
await
fetch
(
url
,
requestOptions
);
if
(
!
response
.
ok
)
return
null
;
let
data
=
await
response
.
json
();
if
(
data
===
null
)
return
response
.
status
;
return
data
;
};
/**
* Read REST request (GET)
* This idea is to obtain a resource through a specific URL.
* The resource is then sent through the <b>send</b> signal.
* @slot
* @emits send
* @param {string} url
*/
this
.
read
=
async
function
(
url
)
{
let
output
=
await
request
(
'GET'
,
url
);
this
.
emit
(
'send'
,
output
);
};
/**
* Create REST request (PUT)
* The passed object is created through the PUT request
* @slot
* @param {string} url
* @param {Object} obj
*/
this
.
create
=
async
function
(
url
,
obj
)
{
let
output
=
await
request
(
'PUT'
,
url
,
obj
);
};
/**
* Update REST request (POST)
* The passed object is updated through the POST request
* @slot
* @param {string} url
* @param {Object} obj
*/
this
.
update
=
async
function
(
url
,
obj
)
{
let
output
=
await
request
(
'POST'
,
url
,
obj
);
};
/**
* Delete REST request (DELETE)
* The resource is destroyed through the request
* @param {string} url
*/
this
.
delete
=
async
function
(
url
)
{
let
output
=
await
request
(
'DELETE'
,
url
);
};
},
[
'create'
,
'read'
,
'update'
,
'delete'
],
[
'send'
]
/*
Reminder of REST API:
- GET: read
- PUT: create
- POST: update
- DELETE: delete
*/
);
export
default
{
RestWrapper
};
\ No newline at end of file
Please
register
or
login
to post a comment