Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
C
coopwire-hypothesis
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
孙灵跃 Leon Sun
coopwire-hypothesis
Commits
540184c1
Commit
540184c1
authored
May 16, 2023
by
Alejandro Celaya
Committed by
Alejandro Celaya
May 16, 2023
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Migrate APIRoutesService to TS
parent
15d64695
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
69 additions
and
0 deletions
+69
-0
api-routes.ts
src/sidebar/services/api-routes.ts
+69
-0
No files found.
src/sidebar/services/api-routes.
j
s
→
src/sidebar/services/api-routes.
t
s
View file @
540184c1
import
type
{
IndexResponse
,
LinksResponse
,
RouteMap
,
RouteMetadata
,
}
from
'../../types/api'
;
import
type
{
SidebarSettings
}
from
'../../types/config'
;
import
{
fetchJSON
}
from
'../util/fetch'
;
import
{
retryPromiseOperation
}
from
'../util/retry'
;
/**
* Fetch an API metadata file, retrying the operation if it fails.
*
* @param {string} url
*/
function
getJSON
(
url
)
{
return
retryPromiseOperation
(()
=>
// nb. The `/api/` and `/api/links` routes are fetched without specifying
// any additional headers/config so that we can use `<link rel="preload">` in
// the `/app.html` response to fetch them early, while the client JS app
// is loading.
fetchJSON
(
url
)
function
getJSON
<
T
>
(
url
:
string
):
Promise
<
T
>
{
return
retryPromiseOperation
(
()
=>
// nb. The `/api/` and `/api/links` routes are fetched without specifying
// any additional headers/config so that we can use `<link rel="preload">` in
// the `/app.html` response to fetch them early, while the client JS app
// is loading.
fetchJSON
(
url
)
as
Promise
<
T
>
);
}
/**
* @typedef {import('../../types/api').IndexResponse} IndexResponse
* @typedef {import('../../types/api').LinksResponse} LinksResponse
* @typedef {import('../../types/api').RouteMap} RouteMap
* @typedef {import('../../types/api').RouteMetadata} RouteMetadata
* @typedef {import('../../types/config').SidebarSettings} SidebarSettings
*/
/**
* A service which fetches and caches API route metadata.
*/
// @inject
export
class
APIRoutesService
{
/**
* @param {SidebarSettings} settings
*/
constructor
(
settings
)
{
this
.
_apiURL
=
settings
.
apiUrl
;
private
_apiURL
:
SidebarSettings
[
'apiUrl'
];
private
_routeCache
:
Promise
<
RouteMap
>
|
null
;
private
_linkCache
:
Promise
<
LinksResponse
>
|
null
;
/** @type {Promise<RouteMap>|null} */
constructor
(
settings
:
SidebarSettings
)
{
this
.
_apiURL
=
settings
.
apiUrl
;
this
.
_routeCache
=
null
;
/** @type {Promise<LinksResponse>|null} */
this
.
_linkCache
=
null
;
}
...
...
@@ -48,29 +43,25 @@ export class APIRoutesService {
* Routes are fetched without any authentication and therefore assumed to be
* the same regardless of whether the user is authenticated or not.
*
* @return
{Promise<RouteMap>} -
Map of routes to route metadata.
* @return Map of routes to route metadata.
*/
routes
()
{
routes
()
:
Promise
<
RouteMap
>
{
if
(
!
this
.
_routeCache
)
{
this
.
_routeCache
=
getJSON
(
this
.
_apiURL
).
then
(
result
=>
{
const
index
=
/** @type {IndexResponse} */
(
result
);
return
index
.
links
;
});
this
.
_routeCache
=
getJSON
<
IndexResponse
>
(
this
.
_apiURL
).
then
(
result
=>
result
.
links
);
}
return
this
.
_routeCache
;
}
/**
* Fetch and cache service page links from the API.
*
* @return {Promise<LinksResponse>}
*/
links
()
{
links
()
:
Promise
<
LinksResponse
>
{
if
(
!
this
.
_linkCache
)
{
this
.
_linkCache
=
this
.
routes
().
then
(
async
routes
=>
{
const
linksRoute
=
/** @type {RouteMetadata} */
(
routes
.
links
);
const
links
=
await
getJSON
(
linksRoute
.
url
);
return
/** @type {LinksResponse} */
(
links
);
const
linksRoute
=
routes
.
links
as
RouteMetadata
;
return
getJSON
<
LinksResponse
>
(
linksRoute
.
url
);
});
}
return
this
.
_linkCache
;
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment