1+ import { createClient } from './client.js' ;
2+ import EntityDefinition from './factory/entity-definition.factory.js' ;
3+ import EntityHydrator from './data/entity-hydrator.data.js' ;
4+ import ChangesetGenerator from './data/changeset-generator.data.js' ;
5+ import EntityFactory from './data/entity-factory.data.js' ;
6+ import Repository from './data/repository.data.js' ;
7+
8+ export default class Api {
9+ constructor ( url , token , version ) {
10+ this . url = url ;
11+ this . client = createClient ( url , token , version )
12+ this . version = version ;
13+ }
14+
15+ async _initialize ( ) {
16+ let schema = await this . client . get ( '_info/entity-schema.json' ) ;
17+
18+ this . EntityDefinition = EntityDefinition ;
19+
20+ Object . keys ( schema . data ) . forEach ( ( entityName ) => {
21+ this . EntityDefinition . add ( entityName , schema . data [ entityName ] ) ;
22+ } ) ;
23+
24+ const hydrator = new EntityHydrator ( this . EntityDefinition ) ;
25+ const changesetGenerator = new ChangesetGenerator ( this . EntityDefinition ) ;
26+ const entityFactory = new EntityFactory ( ) ;
27+
28+ this . create = ( entityName , route , options ) => {
29+ if ( ! route ) {
30+ route = `/${ entityName . replace ( / _ / g, '-' ) } ` ;
31+ }
32+ options = options || { } ;
33+
34+ if ( options . version === undefined ) {
35+ options . version = this . version ;
36+ }
37+
38+ const definition = this . EntityDefinition . get ( entityName ) ;
39+
40+ return new Repository (
41+ route ,
42+ definition . entity ,
43+ this . client ,
44+ hydrator ,
45+ changesetGenerator ,
46+ entityFactory ,
47+ options
48+ ) ;
49+ } ;
50+ }
51+
52+ defaultContext ( ) {
53+ return {
54+ apiPath : `${ this . url } /api` ,
55+ apiResourcePath : `${ this . url } /api/v${ this . version } ` ,
56+ apiVersion : this . version ,
57+ authToken : {
58+ access : this . client . token . access_token
59+ }
60+ }
61+ }
62+ }
0 commit comments