Skip to content

Commit

Permalink
fix: add an optional filename parameter for methods accepting a file
Browse files Browse the repository at this point in the history
  • Loading branch information
dpopp07 committed Aug 29, 2018
1 parent af9e42f commit 9a6cb59
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 1 deletion.
33 changes: 33 additions & 0 deletions discovery/v1-generated.ts
Original file line number Diff line number Diff line change
Expand Up @@ -588,6 +588,7 @@ class DiscoveryV1 extends BaseService {
* "Subject": "Apples"
* } ```.
* @param {string} [params.file_content_type] - The content type of file.
* @param {string} [params.filename] - The filename for file.
* @param {Object} [params.headers] - Custom request headers
* @param {Function} [callback] - The callback that handles the response.
* @returns {NodeJS.ReadableStream|void}
Expand All @@ -600,10 +601,18 @@ class DiscoveryV1 extends BaseService {
if (missingParams) {
return _callback(missingParams);
}

if (_params.file && !_params.filename) {
console.warn(
'WARNING: `filename` should be provided if `file` is not null. This will be REQUIRED in the next major release.'
);
}

const formData = {
'configuration': _params.configuration,
'file': {
data: _params.file,
filename: _params.filename,
contentType: _params.file_content_type
},
'metadata': _params.metadata
Expand Down Expand Up @@ -1079,6 +1088,7 @@ class DiscoveryV1 extends BaseService {
* "Subject": "Apples"
* } ```.
* @param {string} [params.file_content_type] - The content type of file.
* @param {string} [params.filename] - The filename for file.
* @param {Object} [params.headers] - Custom request headers
* @param {Function} [callback] - The callback that handles the response.
* @returns {NodeJS.ReadableStream|void}
Expand All @@ -1091,9 +1101,17 @@ class DiscoveryV1 extends BaseService {
if (missingParams) {
return _callback(missingParams);
}

if (_params.file && !_params.filename) {
console.warn(
'WARNING: `filename` should be provided if `file` is not null. This will be REQUIRED in the next major release.'
);
}

const formData = {
'file': {
data: _params.file,
filename: _params.filename,
contentType: _params.file_content_type
},
'metadata': _params.metadata
Expand Down Expand Up @@ -1225,6 +1243,7 @@ class DiscoveryV1 extends BaseService {
* "Subject": "Apples"
* } ```.
* @param {string} [params.file_content_type] - The content type of file.
* @param {string} [params.filename] - The filename for file.
* @param {Object} [params.headers] - Custom request headers
* @param {Function} [callback] - The callback that handles the response.
* @returns {NodeJS.ReadableStream|void}
Expand All @@ -1237,9 +1256,17 @@ class DiscoveryV1 extends BaseService {
if (missingParams) {
return _callback(missingParams);
}

if (_params.file && !_params.filename) {
console.warn(
'WARNING: `filename` should be provided if `file` is not null. This will be REQUIRED in the next major release.'
);
}

const formData = {
'file': {
data: _params.file,
filename: _params.filename,
contentType: _params.file_content_type
},
'metadata': _params.metadata
Expand Down Expand Up @@ -3017,6 +3044,8 @@ namespace DiscoveryV1 {
metadata?: string;
/** The content type of file. */
file_content_type?: TestConfigurationInEnvironmentConstants.FileContentType | string;
/** The filename for file. */
filename?: string;
headers?: Object;
}

Expand Down Expand Up @@ -3166,6 +3195,8 @@ namespace DiscoveryV1 {
metadata?: string;
/** The content type of file. */
file_content_type?: AddDocumentConstants.FileContentType | string;
/** The filename for file. */
filename?: string;
headers?: Object;
}

Expand Down Expand Up @@ -3218,6 +3249,8 @@ namespace DiscoveryV1 {
metadata?: string;
/** The content type of file. */
file_content_type?: UpdateDocumentConstants.FileContentType | string;
/** The filename for file. */
filename?: string;
headers?: Object;
}

Expand Down
6 changes: 5 additions & 1 deletion lib/helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ export interface FileOptions {
export interface FileParamAttributes {
data: NodeJS.ReadableStream | Buffer | FileObject;
contentType: string;
filename: string;
}

export interface FileStream extends NodeJS.ReadableStream {
Expand Down Expand Up @@ -166,7 +167,10 @@ export function buildRequestFileObject(
): FileObject {
// build filename
let filename: string | Buffer;
if (
if (fileParams.filename) {
// prioritize user-given filenames
filename = fileParams.filename;
} else if (
isFileObject(fileParams.data) &&
fileParams.data.options &&
fileParams.data.value
Expand Down
1 change: 1 addition & 0 deletions test/integration/test.discovery.js
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@ describe('discovery_integration', function() {
environment_id: environment_id,
collection_id: collection_id,
file: fs.createReadStream(path.join(__dirname, '../resources/sampleWord.docx')),
filename: 'sampleWord.docx',
};

discovery.addDocument(document_obj, function(err, response) {
Expand Down

0 comments on commit 9a6cb59

Please sign in to comment.