Skip to content

Commit

Permalink
Validate params before request (#42)
Browse files Browse the repository at this point in the history
* Update for flights

* Update changelog

* Validate parameters with corresponding class

* Cleanup test

* Simplify the test
  • Loading branch information
pcothenet authored Sep 7, 2021
1 parent 343d96a commit 1d8d5bb
Show file tree
Hide file tree
Showing 9 changed files with 102 additions and 61 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [1.11.1] - 2021-09-07

### Changed

- Body parameters are validated before being sent to the API. This gives developers faster feedback as they develop their applications.

## [1.11.0] - 2021-09-07

### Added
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@patch-technology/patch",
"version": "1.11.0",
"version": "1.11.1",
"description": "Node.js wrapper for the Patch API",
"license": "MIT",
"repository": {
Expand Down
2 changes: 1 addition & 1 deletion src/ApiClient.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class ApiClient {
};

this.defaultHeaders = {
'User-Agent': 'patch-node/1.11.0'
'User-Agent': 'patch-node/1.11.1'
};

/**
Expand Down
66 changes: 48 additions & 18 deletions src/api/EstimatesApi.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,17 @@ export default class EstimatesApi {
}

createBitcoinEstimateWithHttpInfo(createBitcoinEstimateRequest) {
let postBody = createBitcoinEstimateRequest;
const _createBitcoinEstimateRequest =
CreateBitcoinEstimateRequest.constructFromObject(
createBitcoinEstimateRequest,
new CreateBitcoinEstimateRequest()
);
let postBody = _createBitcoinEstimateRequest;

// verify the required parameter 'createBitcoinEstimateRequest' is set
if (
createBitcoinEstimateRequest === undefined ||
createBitcoinEstimateRequest === null
_createBitcoinEstimateRequest === undefined ||
_createBitcoinEstimateRequest === null
) {
throw new Error(
"Missing the required parameter 'createBitcoinEstimateRequest' when calling createBitcoinEstimate"
Expand Down Expand Up @@ -64,12 +69,17 @@ export default class EstimatesApi {
}

createEthereumEstimateWithHttpInfo(createEthereumEstimateRequest) {
let postBody = createEthereumEstimateRequest;
const _createEthereumEstimateRequest =
CreateEthereumEstimateRequest.constructFromObject(
createEthereumEstimateRequest,
new CreateEthereumEstimateRequest()
);
let postBody = _createEthereumEstimateRequest;

// verify the required parameter 'createEthereumEstimateRequest' is set
if (
createEthereumEstimateRequest === undefined ||
createEthereumEstimateRequest === null
_createEthereumEstimateRequest === undefined ||
_createEthereumEstimateRequest === null
) {
throw new Error(
"Missing the required parameter 'createEthereumEstimateRequest' when calling createEthereumEstimate"
Expand Down Expand Up @@ -108,12 +118,17 @@ export default class EstimatesApi {
}

createFlightEstimateWithHttpInfo(createFlightEstimateRequest) {
let postBody = createFlightEstimateRequest;
const _createFlightEstimateRequest =
CreateFlightEstimateRequest.constructFromObject(
createFlightEstimateRequest,
new CreateFlightEstimateRequest()
);
let postBody = _createFlightEstimateRequest;

// verify the required parameter 'createFlightEstimateRequest' is set
if (
createFlightEstimateRequest === undefined ||
createFlightEstimateRequest === null
_createFlightEstimateRequest === undefined ||
_createFlightEstimateRequest === null
) {
throw new Error(
"Missing the required parameter 'createFlightEstimateRequest' when calling createFlightEstimate"
Expand Down Expand Up @@ -150,12 +165,17 @@ export default class EstimatesApi {
}

createMassEstimateWithHttpInfo(createMassEstimateRequest) {
let postBody = createMassEstimateRequest;
const _createMassEstimateRequest =
CreateMassEstimateRequest.constructFromObject(
createMassEstimateRequest,
new CreateMassEstimateRequest()
);
let postBody = _createMassEstimateRequest;

// verify the required parameter 'createMassEstimateRequest' is set
if (
createMassEstimateRequest === undefined ||
createMassEstimateRequest === null
_createMassEstimateRequest === undefined ||
_createMassEstimateRequest === null
) {
throw new Error(
"Missing the required parameter 'createMassEstimateRequest' when calling createMassEstimate"
Expand Down Expand Up @@ -192,12 +212,17 @@ export default class EstimatesApi {
}

createShippingEstimateWithHttpInfo(createShippingEstimateRequest) {
let postBody = createShippingEstimateRequest;
const _createShippingEstimateRequest =
CreateShippingEstimateRequest.constructFromObject(
createShippingEstimateRequest,
new CreateShippingEstimateRequest()
);
let postBody = _createShippingEstimateRequest;

// verify the required parameter 'createShippingEstimateRequest' is set
if (
createShippingEstimateRequest === undefined ||
createShippingEstimateRequest === null
_createShippingEstimateRequest === undefined ||
_createShippingEstimateRequest === null
) {
throw new Error(
"Missing the required parameter 'createShippingEstimateRequest' when calling createShippingEstimate"
Expand Down Expand Up @@ -236,12 +261,17 @@ export default class EstimatesApi {
}

createVehicleEstimateWithHttpInfo(createVehicleEstimateRequest) {
let postBody = createVehicleEstimateRequest;
const _createVehicleEstimateRequest =
CreateVehicleEstimateRequest.constructFromObject(
createVehicleEstimateRequest,
new CreateVehicleEstimateRequest()
);
let postBody = _createVehicleEstimateRequest;

// verify the required parameter 'createVehicleEstimateRequest' is set
if (
createVehicleEstimateRequest === undefined ||
createVehicleEstimateRequest === null
_createVehicleEstimateRequest === undefined ||
_createVehicleEstimateRequest === null
) {
throw new Error(
"Missing the required parameter 'createVehicleEstimateRequest' when calling createVehicleEstimate"
Expand Down
8 changes: 6 additions & 2 deletions src/api/OrdersApi.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,14 @@ export default class OrdersApi {
}

createOrderWithHttpInfo(createOrderRequest) {
let postBody = createOrderRequest;
const _createOrderRequest = CreateOrderRequest.constructFromObject(
createOrderRequest,
new CreateOrderRequest()
);
let postBody = _createOrderRequest;

// verify the required parameter 'createOrderRequest' is set
if (createOrderRequest === undefined || createOrderRequest === null) {
if (_createOrderRequest === undefined || _createOrderRequest === null) {
throw new Error(
"Missing the required parameter 'createOrderRequest' when calling createOrder"
);
Expand Down
11 changes: 8 additions & 3 deletions src/api/PreferencesApi.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,17 @@ export default class PreferencesApi {
}

createPreferenceWithHttpInfo(createPreferenceRequest) {
let postBody = createPreferenceRequest;
const _createPreferenceRequest =
CreatePreferenceRequest.constructFromObject(
createPreferenceRequest,
new CreatePreferenceRequest()
);
let postBody = _createPreferenceRequest;

// verify the required parameter 'createPreferenceRequest' is set
if (
createPreferenceRequest === undefined ||
createPreferenceRequest === null
_createPreferenceRequest === undefined ||
_createPreferenceRequest === null
) {
throw new Error(
"Missing the required parameter 'createPreferenceRequest' when calling createPreference"
Expand Down
5 changes: 1 addition & 4 deletions test/integration/estimates.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,14 +80,11 @@ describe('Estimates Integration', function () {
});

it('supports creating bitcoin estimates without parameters', async function () {
const { data: estimate } = await patch.estimates.createBitcoinEstimate({
create_order: false // TODO: this should work without this
});
const { data: estimate } = await patch.estimates.createBitcoinEstimate();

expect(estimate.type).to.be.eq('bitcoin');
expect(estimate.mass_g).to.be.above(0);
expect(estimate.production).to.be.eq(false);
expect(estimate.order).to.be.eq(null);
});

it('supports creating bitcoin estimates with a timestamp', async function () {
Expand Down
59 changes: 29 additions & 30 deletions test/integration/preferences.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,45 +2,44 @@ import { expect } from 'chai';
import Patch from '../../dist/index';
const patch = Patch(process.env.SANDBOX_API_KEY);

async function getProjectId() {
const retrieveProjectResponse = await patch.projects.retrieveProjects();
return retrieveProjectResponse.data[0].id;
}

describe('Preferences Integration', async function () {
it('supports creating, deleting, and listing preferences', async function () {
const preferencesList = await patch.preferences.retrievePreferences();

// If there is an existing preference, delete it and create a new one with the same
// project id. If there is not any existing preferences, create and delete one with
// any project id.
if (preferencesList.data.length > 0) {
const preference = preferencesList.data[0];
const projectId = preference.project.id;

await patch.preferences.deletePreference(preference.id);
const createdPreference = await patch.preferences.createPreference({
project_id: projectId
});

expect(createdPreference.data.project.id).to.eq(projectId);
const projectId = await getProjectId();

const preferencesList2 = await patch.preferences.retrievePreferences();
expect(preferencesList2.data.length).to.be.above(0);
} else {
const projectResponse = await patch.projects.retrieveProjects();
expect(projectResponse.data.length).to.be.above(0);
const projectId = projectResponse.data[0].id;
let preferenceId;

try {
const createdPreference = await patch.preferences.createPreference({
project_id: projectId
});
preferenceId = createdPreference.data.id;
} catch (err) {
if (
!/Your organization already has a preferred project/.test(
err.error.message
)
) {
throw err;
}
const preferencesList = await patch.preferences.retrievePreferences();
preferenceId = preferencesList.data[0].id;
}

expect(createdPreference.data.projectId).to.eq(projectId);

const preferencesList2 = await patch.preferences.retrievePreferences();
const preferencesCount = preferencesList2.data.length;
expect(preferencesList2.data.length).to.be.above(0);
const preferencesList = await patch.preferences.retrievePreferences();
expect(preferencesList.data.length).to.be.above(0);

await patch.preferences.deletePreference(preference.id);
const preferencesResponse = await patch.preferences.retrievePreference(
preferenceId
);
expect(preferencesResponse.data.id).to.eq(preferenceId);

const preferencesList3 = await patch.preferences.retrievePreferences();
expect(preferencesList3.data.length).to.eq(preferencesCount - 1);
}
const deletePreferenceResponse =
await patch.preferences.retrievePreferences(preferenceId);
expect(deletePreferenceResponse.data[0].id).to.eq(preferenceId);
});
});

0 comments on commit 1d8d5bb

Please sign in to comment.