99// TypeRepository is the repository for all Pryv event types. It allows access
1010// to coercion and validation.
1111
12- import type { EventType , Content } from './types/interfaces' ;
12+ import type { EventType , Content } from './types/interfaces' ;
1313
1414const lodash = require ( 'lodash' ) ;
1515const superagent = require ( 'superagent' ) ;
@@ -35,26 +35,25 @@ function isSeriesType(name: string): boolean {
3535// A validator that can check values against a types JSON Schema.
3636//
3737class TypeValidator {
38-
38+
3939 // Validates the given event type against its schema.
4040 //
4141 validate ( type : EventType , content : Content ) : Promise < Content > {
4242 return type . callValidator ( this , content ) ;
4343 }
44-
44+
4545 validateWithSchema (
46- content : Content ,
46+ content : Content ,
4747 schema : any
48- ) : Promise < Content >
49- {
48+ ) : Promise < Content > {
5049 return bluebird . try ( ( ) => {
51- const validator = new ZSchemaValidator ( ) ;
52-
50+ const validator = new ZSchemaValidator ( ) ;
51+
5352 return bluebird
5453 . fromCallback (
5554 ( cb ) => validator . validate ( content , schema , cb ) )
5655 . then ( ( ) => content ) ;
57- } ) ;
56+ } ) ;
5857 }
5958}
6059
@@ -92,10 +91,10 @@ class TypeRepository {
9291 //
9392 isKnown ( name : string ) : boolean {
9493 if ( isSeriesType ( name ) ) {
95- const leafTypeName = name . slice ( SERIES_PREFIX . length ) ;
94+ const leafTypeName = name . slice ( SERIES_PREFIX . length ) ;
9695 return this . isKnown ( leafTypeName ) ;
9796 }
98-
97+
9998 return defaultTypes . types . hasOwnProperty ( name ) ;
10099 }
101100
@@ -104,18 +103,18 @@ class TypeRepository {
104103 // `event-types.default.json`.
105104 //
106105 lookupLeafType ( name : string ) : EventType {
107- if ( ! this . isKnown ( name ) ) throw new errors . TypeDoesNotExistError (
106+ if ( ! this . isKnown ( name ) ) throw new errors . TypeDoesNotExistError (
108107 `Type '${ name } ' does not exist in this Pryv instance.` ) ;
109108
110109 const typeSchema = defaultTypes . types [ name ] ;
111110
112111 if ( typeSchema . type === 'object' ) {
113112 return new ComplexType ( name , typeSchema ) ;
114113 }
115-
114+
116115 return new BasicType ( name , typeSchema ) ;
117116 }
118-
117+
119118 // Lookup a Pryv Event Type by name. To check if a type exists, use
120119 // `#isKnown`. Pryv types are either leaf types ('mass/kg', 'position/wgs84')
121120 // or series types ('series:LEAFTYPE').
@@ -124,48 +123,51 @@ class TypeRepository {
124123 //
125124 lookup ( name : string ) {
126125 if ( isSeriesType ( name ) ) {
127- const leafTypeName = name . slice ( SERIES_PREFIX . length ) ;
126+ const leafTypeName = name . slice ( SERIES_PREFIX . length ) ;
128127 const leafType = this . lookupLeafType ( leafTypeName ) ;
129-
128+
130129 return new InfluxRowType ( leafType ) ;
131130 }
132-
131+
133132 // assert: Not a series type, must be a leaf type.
134133 return this . lookupLeafType ( name ) ;
135134 }
136135
137136 // Produces a validator instance.
138137 //
139138 validator ( ) : TypeValidator {
140- return new TypeValidator ( ) ;
139+ return new TypeValidator ( ) ;
141140 }
142141
143142 // Tries to update the stored type definitions with a file found on the
144143 // internet.
145144 //
146- tryUpdate ( sourceURL : string ) : Promise < void > {
145+ tryUpdate ( sourceURL : string , apiVersion : string ) : Promise < void > {
147146 function unavailableError ( err ) {
148147 throw new Error (
149148 'Could not update event types from ' + sourceURL +
150149 '\nError: ' + err . message ) ;
151150 }
152151 function invalidError ( err ) {
153152 throw new Error (
154- 'Invalid event types schema returned from ' + sourceURL +
153+ 'Invalid event types schema returned from ' + sourceURL +
155154 '\nErrors: ' + err . errors ) ;
156155 }
157-
156+
157+ const USER_AGENT_PREFIX : string = 'Pryv.io/' ;
158+
158159 return superagent
159160 . get ( sourceURL )
161+ . set ( 'User-Agent' , USER_AGENT_PREFIX + apiVersion )
160162 . catch ( unavailableError )
161163 . then ( ( res ) => {
162- const validator = new ZSchemaValidator ( ) ;
163- const schema = res . body ;
164-
164+ const validator = new ZSchemaValidator ( ) ;
165+ const schema = res . body ;
166+
165167 return bluebird . try ( ( ) => {
166- if ( ! validator . validateSchema ( schema ) )
168+ if ( ! validator . validateSchema ( schema ) )
167169 return invalidError ( validator . lastReport ) ;
168-
170+
169171 // Overwrite defaultTypes with the merged list of type schemata.
170172 defaultTypes = lodash . merge ( defaultTypes , schema ) ;
171173 } ) ;
@@ -174,10 +176,10 @@ class TypeRepository {
174176}
175177
176178module . exports = {
177- TypeRepository : TypeRepository ,
179+ TypeRepository : TypeRepository ,
178180 InfluxRowType : InfluxRowType ,
179181 isSeriesType : isSeriesType ,
180- errors : errors ,
182+ errors : errors ,
181183} ;
182184
183185export type { InfluxRowType } ;
0 commit comments