Skip to content

Commit 9a6cb59

Browse files
committed
fix: add an optional filename parameter for methods accepting a file
1 parent af9e42f commit 9a6cb59

File tree

3 files changed

+39
-1
lines changed

3 files changed

+39
-1
lines changed

discovery/v1-generated.ts

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -588,6 +588,7 @@ class DiscoveryV1 extends BaseService {
588588
* "Subject": "Apples"
589589
* } ```.
590590
* @param {string} [params.file_content_type] - The content type of file.
591+
* @param {string} [params.filename] - The filename for file.
591592
* @param {Object} [params.headers] - Custom request headers
592593
* @param {Function} [callback] - The callback that handles the response.
593594
* @returns {NodeJS.ReadableStream|void}
@@ -600,10 +601,18 @@ class DiscoveryV1 extends BaseService {
600601
if (missingParams) {
601602
return _callback(missingParams);
602603
}
604+
605+
if (_params.file && !_params.filename) {
606+
console.warn(
607+
'WARNING: `filename` should be provided if `file` is not null. This will be REQUIRED in the next major release.'
608+
);
609+
}
610+
603611
const formData = {
604612
'configuration': _params.configuration,
605613
'file': {
606614
data: _params.file,
615+
filename: _params.filename,
607616
contentType: _params.file_content_type
608617
},
609618
'metadata': _params.metadata
@@ -1079,6 +1088,7 @@ class DiscoveryV1 extends BaseService {
10791088
* "Subject": "Apples"
10801089
* } ```.
10811090
* @param {string} [params.file_content_type] - The content type of file.
1091+
* @param {string} [params.filename] - The filename for file.
10821092
* @param {Object} [params.headers] - Custom request headers
10831093
* @param {Function} [callback] - The callback that handles the response.
10841094
* @returns {NodeJS.ReadableStream|void}
@@ -1091,9 +1101,17 @@ class DiscoveryV1 extends BaseService {
10911101
if (missingParams) {
10921102
return _callback(missingParams);
10931103
}
1104+
1105+
if (_params.file && !_params.filename) {
1106+
console.warn(
1107+
'WARNING: `filename` should be provided if `file` is not null. This will be REQUIRED in the next major release.'
1108+
);
1109+
}
1110+
10941111
const formData = {
10951112
'file': {
10961113
data: _params.file,
1114+
filename: _params.filename,
10971115
contentType: _params.file_content_type
10981116
},
10991117
'metadata': _params.metadata
@@ -1225,6 +1243,7 @@ class DiscoveryV1 extends BaseService {
12251243
* "Subject": "Apples"
12261244
* } ```.
12271245
* @param {string} [params.file_content_type] - The content type of file.
1246+
* @param {string} [params.filename] - The filename for file.
12281247
* @param {Object} [params.headers] - Custom request headers
12291248
* @param {Function} [callback] - The callback that handles the response.
12301249
* @returns {NodeJS.ReadableStream|void}
@@ -1237,9 +1256,17 @@ class DiscoveryV1 extends BaseService {
12371256
if (missingParams) {
12381257
return _callback(missingParams);
12391258
}
1259+
1260+
if (_params.file && !_params.filename) {
1261+
console.warn(
1262+
'WARNING: `filename` should be provided if `file` is not null. This will be REQUIRED in the next major release.'
1263+
);
1264+
}
1265+
12401266
const formData = {
12411267
'file': {
12421268
data: _params.file,
1269+
filename: _params.filename,
12431270
contentType: _params.file_content_type
12441271
},
12451272
'metadata': _params.metadata
@@ -3017,6 +3044,8 @@ namespace DiscoveryV1 {
30173044
metadata?: string;
30183045
/** The content type of file. */
30193046
file_content_type?: TestConfigurationInEnvironmentConstants.FileContentType | string;
3047+
/** The filename for file. */
3048+
filename?: string;
30203049
headers?: Object;
30213050
}
30223051

@@ -3166,6 +3195,8 @@ namespace DiscoveryV1 {
31663195
metadata?: string;
31673196
/** The content type of file. */
31683197
file_content_type?: AddDocumentConstants.FileContentType | string;
3198+
/** The filename for file. */
3199+
filename?: string;
31693200
headers?: Object;
31703201
}
31713202

@@ -3218,6 +3249,8 @@ namespace DiscoveryV1 {
32183249
metadata?: string;
32193250
/** The content type of file. */
32203251
file_content_type?: UpdateDocumentConstants.FileContentType | string;
3252+
/** The filename for file. */
3253+
filename?: string;
32213254
headers?: Object;
32223255
}
32233256

lib/helper.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ export interface FileOptions {
3535
export interface FileParamAttributes {
3636
data: NodeJS.ReadableStream | Buffer | FileObject;
3737
contentType: string;
38+
filename: string;
3839
}
3940

4041
export interface FileStream extends NodeJS.ReadableStream {
@@ -166,7 +167,10 @@ export function buildRequestFileObject(
166167
): FileObject {
167168
// build filename
168169
let filename: string | Buffer;
169-
if (
170+
if (fileParams.filename) {
171+
// prioritize user-given filenames
172+
filename = fileParams.filename;
173+
} else if (
170174
isFileObject(fileParams.data) &&
171175
fileParams.data.options &&
172176
fileParams.data.value

test/integration/test.discovery.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,7 @@ describe('discovery_integration', function() {
153153
environment_id: environment_id,
154154
collection_id: collection_id,
155155
file: fs.createReadStream(path.join(__dirname, '../resources/sampleWord.docx')),
156+
filename: 'sampleWord.docx',
156157
};
157158

158159
discovery.addDocument(document_obj, function(err, response) {

0 commit comments

Comments
 (0)