|
1 | | -import GeneratedDiscoveryV1 = require('./v1-generated'); |
2 | | -import extend = require('extend'); |
3 | | -import isStream = require('isstream'); |
4 | | - |
5 | | -class DiscoveryV1 extends GeneratedDiscoveryV1 { |
6 | | - static VERSION_DATE_2017_09_01: string = '2017-09-01'; |
7 | | - static VERSION_DATE_2017_08_01: string = '2017-08-01'; |
8 | | - static VERSION_DATE_2017_07_19: string = '2017-07-19'; |
9 | | - static VERSION_DATE_2017_06_25: string = '2017-06-25'; |
10 | | - static VERSION_DATE_2016_12_01: string = '2016-12-01'; |
11 | | - static VERSION_DATE_2017_04_27: string = '2017-04-27'; |
12 | | - static VERSION_DATE_2016_12_15: string = '2016-12-15'; |
13 | | - |
14 | | - static _ensureFilename(file) { |
15 | | - // no changes needed for streams created by fs.ReadStream (or similar looking streams) |
16 | | - if (isStream.isReadable(file) && file.path) { |
17 | | - return file; |
18 | | - } |
19 | | - |
20 | | - // next handle request-style value/options objects |
21 | | - if ( |
22 | | - file && |
23 | | - file.hasOwnProperty('value') && |
24 | | - file.hasOwnProperty('options') |
25 | | - ) { |
26 | | - if (file.options.filename) { |
27 | | - return file; |
28 | | - } |
29 | | - return { |
30 | | - value: file.value, |
31 | | - options: extend({ filename: '_' }, file.options) |
32 | | - }; |
33 | | - } |
34 | | - |
35 | | - // finally, handle all other cases by wrapping them in a request-style value/options object |
36 | | - return { |
37 | | - value: file, |
38 | | - options: { |
39 | | - filename: '_' |
40 | | - } |
41 | | - }; |
42 | | - } |
43 | | - |
44 | | - getEnvironments(params, callback) { |
45 | | - return super.listEnvironments(params, callback); |
46 | | - } |
47 | | - |
48 | | - createEnvironment(params, callback) { |
49 | | - // make sure environments with size 0 can be created |
50 | | - if (params.size !== 0 && !params.size) { |
51 | | - params.size = 1; |
52 | | - } |
53 | | - return super.createEnvironment(params, callback); |
54 | | - } |
55 | | - |
56 | | - updateEnvironment(params, callback) { |
57 | | - return super.updateEnvironment(params, callback); |
58 | | - } |
59 | | - |
60 | | - updateConfiguration(params, callback) { |
61 | | - // name is now a required parameter |
62 | | - // file is now split into conversions, enrichments and normalizations |
63 | | - const _params = params || {}; |
64 | | - if (_params.file) { |
65 | | - const { conversions, enrichments, normalizations } = _params.file; |
66 | | - _params.conversions = conversions; |
67 | | - _params.enrichments = enrichments; |
68 | | - _params.normalizations = normalizations; |
69 | | - } |
70 | | - _params.name = _params.name || '_'; |
71 | | - return super.updateConfiguration(_params, callback); |
72 | | - } |
73 | | - |
74 | | - getCollections(params, callback) { |
75 | | - return super.listCollections(params, callback); |
76 | | - } |
77 | | - |
78 | | - createCollection(params, callback) { |
79 | | - // language_code is now called language |
80 | | - if (params) { |
81 | | - params.language = params.language || params.language_code || 'en_us'; |
82 | | - } |
83 | | - return super.createCollection(params, callback); |
84 | | - } |
85 | | - |
86 | | - updateCollection(params, callback) { |
87 | | - // collection_name is now called name |
88 | | - if (params) { |
89 | | - params.name = params.name || params.collection_name; |
90 | | - } |
91 | | - return super.updateCollection(params, callback); |
92 | | - } |
93 | | - |
94 | | - getCollectionFields(params, callback) { |
95 | | - // listFields expects an array of collection ids |
96 | | - if (params && !Array.isArray(params.collection_id)) { |
97 | | - params.collection_ids = [params.collection_id]; |
98 | | - } |
99 | | - return super.listFields(params, callback); |
100 | | - } |
101 | | - |
102 | | - getConfigurations(params, callback) { |
103 | | - return super.listConfigurations(params, callback); |
104 | | - } |
105 | | - |
106 | | - createConfiguration(params, callback) { |
107 | | - // name is now a required parameter |
108 | | - // file is now split into conversions, enrichments and normalizations |
109 | | - const _params = params || {}; |
110 | | - if (_params.file) { |
111 | | - const { conversions, enrichments, normalizations } = _params.file; |
112 | | - _params.conversions = conversions; |
113 | | - _params.enrichments = enrichments; |
114 | | - _params.normalizations = normalizations; |
115 | | - } |
116 | | - _params.name = _params.name || '_'; |
117 | | - return super.createConfiguration(_params, callback); |
118 | | - } |
119 | | - |
120 | | - addJsonDocument(params, callback) { |
121 | | - const fileParamType: string = typeof params.file; |
122 | | - if (fileParamType !== 'object') { |
123 | | - throw new Error( |
124 | | - `Argument error: params.file must be an object, but got ${fileParamType}.` |
125 | | - ); |
126 | | - } |
127 | | - const _params = extend(params, { |
128 | | - file: { |
129 | | - value: JSON.stringify(params.file), |
130 | | - options: { |
131 | | - filename: '_.json' |
132 | | - } |
133 | | - } |
134 | | - }); |
135 | | - return this.addDocument(_params, callback); |
136 | | - } |
137 | | - |
138 | | - updateJsonDocument(params, callback) { |
139 | | - const fileParamType = typeof params.file; |
140 | | - if (fileParamType !== 'object') { |
141 | | - throw new Error( |
142 | | - `Argument error: params.file must be an object, but got ${fileParamType}.` |
143 | | - ); |
144 | | - } |
145 | | - const _params = extend(params, { |
146 | | - file: { |
147 | | - value: JSON.stringify(params.file), |
148 | | - options: { |
149 | | - filename: '_.json' |
150 | | - } |
151 | | - } |
152 | | - }); |
153 | | - return this.updateDocument(_params, callback); |
154 | | - } |
155 | | - |
156 | | - query(params, callback) { |
157 | | - let _params = params || {}; |
158 | | - // query and natural_language_query can't both be populated |
159 | | - if (_params.query && _params.natural_language_query) { |
160 | | - delete _params.natural_language_query; |
161 | | - } |
162 | | - if (_params.return) { |
163 | | - _params.return_fields = _params.return; |
164 | | - } |
165 | | - // passages parameters are now snake case |
166 | | - Object.keys(_params).forEach( |
167 | | - key => |
168 | | - key.match(/passages\..*/i) && |
169 | | - (_params[key.replace('.', '_')] = _params[key]) |
170 | | - ); |
171 | | - return super.query(_params, callback); |
172 | | - } |
173 | | -} |
174 | | - |
175 | | -export = DiscoveryV1; |
| 1 | +/** |
| 2 | + * Copyright 2017 IBM All Rights Reserved. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +import GeneratedDiscoveryV1 = require('./v1-generated'); |
| 18 | +import extend = require('extend'); |
| 19 | +import isStream = require('isstream'); |
| 20 | + |
| 21 | +class DiscoveryV1 extends GeneratedDiscoveryV1 { |
| 22 | + static VERSION_DATE_2017_09_01: string = '2017-09-01'; |
| 23 | + static VERSION_DATE_2017_08_01: string = '2017-08-01'; |
| 24 | + static VERSION_DATE_2017_07_19: string = '2017-07-19'; |
| 25 | + static VERSION_DATE_2017_06_25: string = '2017-06-25'; |
| 26 | + static VERSION_DATE_2016_12_01: string = '2016-12-01'; |
| 27 | + static VERSION_DATE_2017_04_27: string = '2017-04-27'; |
| 28 | + static VERSION_DATE_2016_12_15: string = '2016-12-15'; |
| 29 | + |
| 30 | + static _ensureFilename(file) { |
| 31 | + // no changes needed for streams created by fs.ReadStream (or similar looking streams) |
| 32 | + if (isStream.isReadable(file) && file.path) { |
| 33 | + return file; |
| 34 | + } |
| 35 | + |
| 36 | + // next handle request-style value/options objects |
| 37 | + if ( |
| 38 | + file && |
| 39 | + file.hasOwnProperty('value') && |
| 40 | + file.hasOwnProperty('options') |
| 41 | + ) { |
| 42 | + if (file.options.filename) { |
| 43 | + return file; |
| 44 | + } |
| 45 | + return { |
| 46 | + value: file.value, |
| 47 | + options: extend({ filename: '_' }, file.options) |
| 48 | + }; |
| 49 | + } |
| 50 | + |
| 51 | + // finally, handle all other cases by wrapping them in a request-style value/options object |
| 52 | + return { |
| 53 | + value: file, |
| 54 | + options: { |
| 55 | + filename: '_' |
| 56 | + } |
| 57 | + }; |
| 58 | + } |
| 59 | + |
| 60 | + getEnvironments(params, callback) { |
| 61 | + return super.listEnvironments(params, callback); |
| 62 | + } |
| 63 | + |
| 64 | + createEnvironment(params, callback) { |
| 65 | + // make sure environments with size 0 can be created |
| 66 | + if (params.size !== 0 && !params.size) { |
| 67 | + params.size = 1; |
| 68 | + } |
| 69 | + return super.createEnvironment(params, callback); |
| 70 | + } |
| 71 | + |
| 72 | + updateEnvironment(params, callback) { |
| 73 | + return super.updateEnvironment(params, callback); |
| 74 | + } |
| 75 | + |
| 76 | + updateConfiguration(params, callback) { |
| 77 | + // name is now a required parameter |
| 78 | + // file is now split into conversions, enrichments and normalizations |
| 79 | + const _params = params || {}; |
| 80 | + if (_params.file) { |
| 81 | + const { conversions, enrichments, normalizations } = _params.file; |
| 82 | + _params.conversions = conversions; |
| 83 | + _params.enrichments = enrichments; |
| 84 | + _params.normalizations = normalizations; |
| 85 | + } |
| 86 | + _params.name = _params.name || '_'; |
| 87 | + return super.updateConfiguration(_params, callback); |
| 88 | + } |
| 89 | + |
| 90 | + getCollections(params, callback) { |
| 91 | + return super.listCollections(params, callback); |
| 92 | + } |
| 93 | + |
| 94 | + createCollection(params, callback) { |
| 95 | + // language_code is now called language |
| 96 | + if (params) { |
| 97 | + params.language = params.language || params.language_code || 'en_us'; |
| 98 | + } |
| 99 | + return super.createCollection(params, callback); |
| 100 | + } |
| 101 | + |
| 102 | + updateCollection(params, callback) { |
| 103 | + // collection_name is now called name |
| 104 | + if (params) { |
| 105 | + params.name = params.name || params.collection_name; |
| 106 | + } |
| 107 | + return super.updateCollection(params, callback); |
| 108 | + } |
| 109 | + |
| 110 | + getCollectionFields(params, callback) { |
| 111 | + // listFields expects an array of collection ids |
| 112 | + if (params && !Array.isArray(params.collection_id)) { |
| 113 | + params.collection_ids = [params.collection_id]; |
| 114 | + } |
| 115 | + return super.listFields(params, callback); |
| 116 | + } |
| 117 | + |
| 118 | + getConfigurations(params, callback) { |
| 119 | + return super.listConfigurations(params, callback); |
| 120 | + } |
| 121 | + |
| 122 | + createConfiguration(params, callback) { |
| 123 | + // name is now a required parameter |
| 124 | + // file is now split into conversions, enrichments and normalizations |
| 125 | + const _params = params || {}; |
| 126 | + if (_params.file) { |
| 127 | + const { conversions, enrichments, normalizations } = _params.file; |
| 128 | + _params.conversions = conversions; |
| 129 | + _params.enrichments = enrichments; |
| 130 | + _params.normalizations = normalizations; |
| 131 | + } |
| 132 | + _params.name = _params.name || '_'; |
| 133 | + return super.createConfiguration(_params, callback); |
| 134 | + } |
| 135 | + |
| 136 | + addJsonDocument(params, callback) { |
| 137 | + const fileParamType: string = typeof params.file; |
| 138 | + if (fileParamType !== 'object') { |
| 139 | + throw new Error( |
| 140 | + `Argument error: params.file must be an object, but got ${fileParamType}.` |
| 141 | + ); |
| 142 | + } |
| 143 | + const _params = extend(params, { |
| 144 | + file: { |
| 145 | + value: JSON.stringify(params.file), |
| 146 | + options: { |
| 147 | + filename: '_.json' |
| 148 | + } |
| 149 | + } |
| 150 | + }); |
| 151 | + return this.addDocument(_params, callback); |
| 152 | + } |
| 153 | + |
| 154 | + updateJsonDocument(params, callback) { |
| 155 | + const fileParamType = typeof params.file; |
| 156 | + if (fileParamType !== 'object') { |
| 157 | + throw new Error( |
| 158 | + `Argument error: params.file must be an object, but got ${fileParamType}.` |
| 159 | + ); |
| 160 | + } |
| 161 | + const _params = extend(params, { |
| 162 | + file: { |
| 163 | + value: JSON.stringify(params.file), |
| 164 | + options: { |
| 165 | + filename: '_.json' |
| 166 | + } |
| 167 | + } |
| 168 | + }); |
| 169 | + return this.updateDocument(_params, callback); |
| 170 | + } |
| 171 | + |
| 172 | + query(params, callback) { |
| 173 | + const _params = params || {}; |
| 174 | + // query and natural_language_query can't both be populated |
| 175 | + if (_params.query && _params.natural_language_query) { |
| 176 | + delete _params.natural_language_query; |
| 177 | + } |
| 178 | + if (_params.return) { |
| 179 | + _params.return_fields = _params.return; |
| 180 | + } |
| 181 | + // passages parameters are now snake case |
| 182 | + Object.keys(_params).forEach( |
| 183 | + key => |
| 184 | + key.match(/passages\..*/i) && |
| 185 | + (_params[key.replace('.', '_')] = _params[key]) |
| 186 | + ); |
| 187 | + return super.query(_params, callback); |
| 188 | + } |
| 189 | +} |
| 190 | + |
| 191 | +export = DiscoveryV1; |
0 commit comments