11const nets = require ( 'nets' ) ;
22
3+ const log = require ( './log' ) ;
4+
35const Asset = require ( './Asset' ) ;
46const Helper = require ( './Helper' ) ;
57
68/**
79 * @typedef {function } UrlFunction - A function which computes a URL from asset information.
810 * @param {Asset } - The asset for which the URL should be computed.
9- * @returns {string } - The URL for the asset.
11+ * @returns {(string|object) } - A string representing the URL for the asset request OR an object with configuration for
12+ * the underlying `nets` call (necessary for configuring e.g. authentication)
1013 */
1114
1215class WebHelper extends Helper {
1316 constructor ( parent ) {
1417 super ( parent ) ;
1518
1619 /**
17- * @type {Array.<SourceRecord> }
18- * @typedef {object } SourceRecord
19- * @property {Array.<string> } types - The types of asset provided by this source, from AssetType's name field.
20- * @property {UrlFunction } urlFunction - A function which computes a URL from an Asset.
20+ * @type {Array.<StoreRecord> }
21+ * @typedef {object } StoreRecord
22+ * @property {Array.<string> } types - The types of asset provided by this store, from AssetType's name field.
23+ * @property {UrlFunction } getFunction - A function which computes a URL from an Asset.
24+ * @property {UrlFunction } createFunction - A function which computes a URL from an Asset.
25+ * @property {UrlFunction } updateFunction - A function which computes a URL from an Asset.
2126 */
22- this . sources = [ ] ;
27+ this . stores = [ ] ;
2328 }
2429
2530 /**
2631 * Register a web-based source for assets. Sources will be checked in order of registration.
32+ * @deprecated Please use addStore
2733 * @param {Array.<AssetType> } types - The types of asset provided by this source.
2834 * @param {UrlFunction } urlFunction - A function which computes a URL from an Asset.
2935 */
3036 addSource ( types , urlFunction ) {
31- this . sources . push ( {
37+ log . warn ( 'Deprecation: WebHelper.addSource has been replaced with WebHelper.addStore.' ) ;
38+ this . addStore ( types , urlFunction ) ;
39+ }
40+
41+ /**
42+ * Register a web-based source for assets. Sources will be checked in order of registration.
43+ * @param {Array.<AssetType> } types - The types of asset provided by this source.
44+ * @param {UrlFunction } getFunction - A function which computes a GET URL for an Asset
45+ * @param {UrlFunction } createFunction - A function which computes a POST URL for an Asset
46+ * @param {UrlFunction } updateFunction - A function which computes a PUT URL for an Asset
47+ */
48+ addStore ( types , getFunction , createFunction , updateFunction ) {
49+ this . stores . push ( {
3250 types : types . map ( assetType => assetType . name ) ,
33- urlFunction : urlFunction
51+ get : getFunction ,
52+ create : createFunction ,
53+ update : updateFunction
3454 } ) ;
3555 }
3656
@@ -45,34 +65,42 @@ class WebHelper extends Helper {
4565
4666 /** @type {Array.<{url:string, result:*}> } List of URLs attempted & errors encountered. */
4767 const errors = [ ] ;
48- const sources = this . sources . slice ( ) ;
68+ const stores = this . stores . slice ( ) ;
4969 const asset = new Asset ( assetType , assetId , dataFormat ) ;
50- let sourceIndex = 0 ;
70+ let storeIndex = 0 ;
5171
5272 return new Promise ( ( fulfill , reject ) => {
5373
5474 const tryNextSource = ( ) => {
5575
5676 /** @type {UrlFunction } */
57- let urlFunction ;
77+ let reqConfigFunction ;
5878
59- while ( sourceIndex < sources . length ) {
60- const source = sources [ sourceIndex ] ;
61- ++ sourceIndex ;
62- if ( source . types . indexOf ( assetType . name ) >= 0 ) {
63- urlFunction = source . urlFunction ;
79+ while ( storeIndex < stores . length ) {
80+ const store = stores [ storeIndex ] ;
81+ ++ storeIndex ;
82+ if ( store . types . indexOf ( assetType . name ) >= 0 ) {
83+ reqConfigFunction = store . get ;
6484 break ;
6585 }
6686 }
6787
68- if ( urlFunction ) {
69- const url = urlFunction ( asset ) ;
70- if ( url === false ) {
88+ if ( reqConfigFunction ) {
89+ let reqConfig = reqConfigFunction ( asset ) ;
90+ if ( reqConfig === false ) {
7191 tryNextSource ( ) ;
7292 return ;
7393 }
94+ if ( typeof reqConfig === 'string' ) {
95+ reqConfig = {
96+ url : reqConfig
97+ } ;
98+ }
7499
75- nets ( { url : url } , ( err , resp , body ) => {
100+ nets ( {
101+ method : 'get' ,
102+ ...reqConfig
103+ } , ( err , resp , body ) => {
76104 // body is a Buffer
77105 if ( err || Math . floor ( resp . statusCode / 100 ) !== 2 ) {
78106 tryNextSource ( ) ;
0 commit comments