Skip to content

Commit

Permalink
Merge pull request #524 from ripe-tech/jc/shipment-attachment-api
Browse files Browse the repository at this point in the history
feat: shipment attachment API
  • Loading branch information
3rdvision authored Oct 17, 2023
2 parents 1a99b5c + 4d408c1 commit eae9240
Show file tree
Hide file tree
Showing 2 changed files with 120 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Added

* Added waybill and invoice creation methods to the `ShipmentAPI` - [#4853](https://github.com/ripe-tech/ripe-core/issues/4853)
* Added shipment attachment methods to the `ShipmentAPI` - [#4853](https://github.com/ripe-tech/ripe-core/issues/4853)

### Changed

Expand Down
119 changes: 119 additions & 0 deletions src/js/api/shipment.js
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,125 @@ ripe.Ripe.prototype.deleteShipmentP = function(number, options) {
});
};

/**
* Returns all the attachments of a shipment.
*
* @param {Number} number The number of the shipment to find by.
* @param {Object} options An object of options to configure the request.
* @param {Function} callback Function with the result of the request.
* @returns {XMLHttpRequest} The XMLHttpRequest instance of the API request.
*/
ripe.Ripe.prototype.attachmentsShipment = function(number, options, callback) {
callback = typeof options === "function" ? options : callback;
options = typeof options === "function" || options === undefined ? {} : options;
const url = `${this.url}shipments/${number}/attachments`;
options = Object.assign(options, {
url: url,
method: "GET",
auth: true
});
options = this._build(options);
return this._cacheURL(options.url, options, callback);
};

/**
* Returns all the attachments of a shipment.
*
* @param {Number} number The number of the shipment to find by.
* @param {Object} options An object of options to configure the request.
* @returns {Promise} An object containing all the attachments of a shipment.
*/
ripe.Ripe.prototype.attachmentsShipmentP = function(number, options) {
return new Promise((resolve, reject) => {
this.attachmentsShipment(number, options, (result, isValid, request) => {
isValid ? resolve(result) : reject(new ripe.RemoteError(request, null, result));
});
});
};

/**
* Adds an attachment to a shipment.
*
* @param {Number} number The number of the shipment where the attachment will be added.
* @param {Object} file The attachment file to be added to the shipment.
* @param {Object} options An object of options to configure the request.
* @param {Function} callback Function with the result of the request.
* @returns {XMLHttpRequest} The XMLHttpRequest instance of the API request.
*/
ripe.Ripe.prototype.createAttachmentShipment = function(number, file, options, callback) {
callback = typeof options === "function" ? options : callback;
options = typeof options === "function" || options === undefined ? {} : options;
const url = `${this.url}shipments/${number}/attachments`;
const dataM = { file: file };
if (options.name !== undefined) dataM.name = options.name;
if (options.meta !== undefined) dataM.meta = JSON.stringify(options.meta);
options = Object.assign(options, {
url: url,
method: "POST",
dataM: dataM,
auth: true
});
options = this._build(options);
return this._cacheURL(options.url, options, callback);
};

/**
* Adds an attachment to a shipment.
*
* @param {Number} number The number of the shipment where the attachment will be added.
* @param {Object} file The attachment file to be added to the shipment.
* @param {Object} options An object of options to configure the request.
* @param {Function} callback Function with the result of the request.
* @returns {Promise} The attachment added to the shipment.
*/
ripe.Ripe.prototype.createAttachmentShipmentP = function(number, file, options) {
return new Promise((resolve, reject) => {
this.createAttachmentShipment(number, file, options, (result, isValid, request) => {
isValid ? resolve(result) : reject(new ripe.RemoteError(request, null, result));
});
});
};

/**
* Returns the attachment of a shipment requested by ID.
*
* @param {Number} number The number of the shipment to find by.
* @param {Number} attachmentId The ID of the attachment of the shipment to find by.
* @param {Object} options An object of options to configure the request.
* @param {Function} callback Function with the result of the request.
* @returns {XMLHttpRequest} The XMLHttpRequest instance of the API request.
*/
ripe.Ripe.prototype.attachmentShipment = function(number, attachmentId, options, callback) {
callback = typeof options === "function" ? options : callback;
options = typeof options === "function" || options === undefined ? {} : options;
const url = `${this.url}shipments/${number}/attachments/${attachmentId}`;
options = Object.assign(options, {
url: url,
method: "GET",
auth: true,
cached: false
});
options = this._build(options);
return this._cacheURL(options.url, options, callback);
};

/**
* Returns the attachment of a shipment requested by ID.
*
* @param {Number} number The number of the shipment to find by.
* @param {Number} attachmentId The ID of the attachment of the shipment to find by.
* @param {Object} options An object of options to configure the request.
* @param {Function} callback Function with the result of the request.
* @returns {Promise} The attachment file of an shipment requested by ID.
*/
ripe.Ripe.prototype.attachmentShipmentP = function(number, attachmentId, options) {
return new Promise((resolve, reject) => {
this.attachmentShipment(number, attachmentId, options, (result, isValid, request) => {
isValid ? resolve(result) : reject(new ripe.RemoteError(request, null, result));
});
});
};

/**
* Creates a shipping waybill for the shipment with the provided number.
*
Expand Down

1 comment on commit eae9240

@vercel
Copy link

@vercel vercel bot commented on eae9240 Oct 17, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

ripe-sdk – ./

ripe-sdk-platforme.vercel.app
ripe-sdk-git-master-platforme.vercel.app
ripe-sdk-docs-csr.vercel.app

Please sign in to comment.