This repository has been archived by the owner on Sep 20, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.ts
79 lines (64 loc) · 2.37 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import { HydrofoilShell } from '@hydrofoil/hydrofoil-shell/hydrofoil-shell'
import { HydraResource } from 'alcaeus'
import { HydraClient } from 'alcaeus/alcaeus'
import { property } from 'lit-element'
import notify from './lib/notify'
type ShellConstructor = new (...args: any[]) => HydrofoilShell
interface AlcaeusLoader {
/**
* The Hydra entrypoint linked from the current API Documentation
*/
entrypoints: HydraResource[]
/**
* The Hydra client has to be set on the shell instance
*/
Hydra: HydraClient
}
type ReturnConstructor = new (...args: any[]) => HydrofoilShell & AlcaeusLoader
/**
* A base shell mixin class which uses `Alcaeus` Hydra client to load the resources
*
* @mixinFunction
*/
export default function<B extends ShellConstructor> (Base: B): B & ReturnConstructor {
class Mixin extends Base implements AlcaeusLoader {
/**
* Dispatched when the entrypoint has been loaded
*
* @event entrypoint-changed
*/
@property({ type: Object })
public entrypoints!: HydraResource[]
public Hydra!: HydraClient
protected async loadResourceInternal(url: string) {
if (!this.Hydra) {
return null
}
return this.Hydra.loadResource(url)
}
protected updated(props: Map<string, unknown>) {
super.updated(props)
notify(this, props, 'entrypoint')
}
protected onResourceLoaded() {
const entrypoints = this.Hydra.apiDocumentations.reduce((promises, docs) => {
const { root } = docs
if (!root || !('loadEntrypoint' in root)) {
this._log.warn(`Resource does not appear to be a hydra:ApiDocumentation`)
return promises
}
promises.push(root.loadEntrypoint()
.then(({ representation }) => representation?.root)
.catch((e) => {
this._log.warn(`Failed to load entrypoint`)
this._log.error(e)
}))
return promises
}, [] as Promise<HydraResource | void | null>[])
Promise.all(entrypoints).then((resolved) => {
this.entrypoints = resolved.filter(Boolean) as HydraResource[]
})
}
}
return Mixin
}