Skip to content

Commit

Permalink
Method input validations (#1581)
Browse files Browse the repository at this point in the history
* add validation to getStateWasteCodes that state code must be two characters long, unit test included

* add validation for when siteID is an input, unit test included

* add validation for siteType which should be one of 4 enum strign values corresponding to handler type

* add DateType type declaration, and validation

* add validateInput config option, update docs

By Default, we do not validate user input. This keeps the library unopinionated, but keeps this faily simple option open to users interested

* add conditional to check is user input should be validated before API request made

* increment minor package number and publish.

also quick clean up error messages, add typescrip example to docs

* add test showing validateInput behavior is disabled by default

this commit also adds a new mock handler for a request to the site
  • Loading branch information
dpgraham4401 authored Aug 11, 2023
1 parent 2bc6566 commit a1cd32a
Show file tree
Hide file tree
Showing 8 changed files with 266 additions and 64 deletions.
81 changes: 56 additions & 25 deletions emanifest-js/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@
## Intro

The [emanifest npm package](https://www.npmjs.com/package/emanifest) is an API client library.
It simplifies the task of using the RCRAInfo/e-Manifest web services by abstracting the
It simplifies the task of using the RCRAInfo/e-Manifest web services by abstracting the
authentication process, providing developer friendly API, and exporting TypeScript types.
It's built on top of the [Axios](https://axios-http.com/) library, and can be used in both Node and browser
runtime environments (EPA has discussed making some public API available that do not need authentication in the near future).
It's built on top of the [Axios](https://axios-http.com/) library, and can be used in both Node and browser
runtime environments (EPA has discussed making some public API available that do not need authentication in the near
future).

For additional information about e-Manifest, check out the below links

Expand All @@ -22,71 +23,100 @@ For a python alternative see the [emanifest package on PyPI](https://pypi.org/pr

## Installation

```bash
$ npm install emanifest
```bash
$ npm install emanifest
or
$ yarn add emanifest
```

## Basic Usage

The primary export of the `emanifest` package is the `newClient` function.
A constructor that accepts a configuration object and returns a new `RcraClient`
instance.
A constructor that accepts a configuration object and returns a new `RcraClient`
instance.

```typescript
import { AxiosResponse } from 'axios';
import { newClient, RCRAINFO_PREPROD, AuthResponse } from 'emanifest'
import { newClient, RCRAINFO_PREPROD, AuthResponse } from 'emanifest';

// The newClient accepts an instance of the RcraClientConfig which follows this interface
// interface RcraClientConfig {
// apiBaseURL?: RcrainfoEnv; // default: RCRAINFO_PREPROD
// apiID?: string;
// apiKey?: string;
// authAuth?: Boolean; // default: false
// autoAuth?: Boolean; // default: false
// validateInput?: Boolean; // default: false
// }

const rcrainfo = newClient({ apiBaseURL: RCRAINFO_PREPROD, apiID: 'my_api_id', apiKey: 'my_api_key' })
const authResponse: AxiosResponse<AuthResponse> = await rcrainfo.authenticate()
console.log(authResponse.data)

const rcrainfo = newClient({ apiBaseURL: RCRAINFO_PREPROD, apiID: 'my_api_id', apiKey: 'my_api_key' });
const authResponse: AxiosResponse<AuthResponse> = await rcrainfo.authenticate();
console.log(authResponse.data);
```

### Other Exports
### Other Exports

The emanifest package also exports the `RCRAINFO_PREPROD` and `RCRAINFO_PROD` constants which can be used to set
The emanifest package also exports the `RCRAINFO_PREPROD` and `RCRAINFO_PROD` constants which can be used to set
the `apiBaseURL` property of the `RcraClientConfig` object.

### Types

The emanifest package exports a types/interfaces that can be used in statically typed projects.
The types follow the OpenAPI schema definitions that can be found in the [USEPA/e-manifest schema directory](https://github.com/USEPA/e-manifest/tree/master/Services-Information/Schema)
The emanifest package exports types/interfaces that can be used in statically typed projects.
The types follow the OpenAPI schema definitions that can be found in
the [USEPA/e-manifest schema directory](https://github.com/USEPA/e-manifest/tree/master/Services-Information/Schema)
however, some names have been modified for clarity (for example `RcraCode` instead of simply `Code`).


## Auto-Authentication

The `emanifest` package can be explicitly configured to automatically authenticate when needed.

```typescript
import { AxiosResponse } from 'axios';
import { newClient, RCRAINFO_PREPROD, AuthResponse, RcraClientClass, RcraCode } from 'emanifest'
import { newClient, RCRAINFO_PREPROD, AuthResponse, RcraClientClass, RcraCode } from 'emanifest';

const rcrainfo = newClient({
apiBaseURL: RCRAINFO_PREPROD,
apiID: 'my_api_id',
apiKey: 'my_api_key',
autoAuth: true // Set the RcraClient to automatically authenticate as needed
})
autoAuth: true, // Set the RcraClient to automatically authenticate as needed
});

// the authenticate method is NOT explicitly called
const resp: AxtiosResponse<RcraCode> = await rcrainfo.getStateWasteCodes('VA')
const resp: AxtiosResponse<RcraCode> = await rcrainfo.getStateWasteCodes('VA');

console.log(resp.data); // [ { code: 'BCRUSH', description: 'Bulb or Lamp Crusher' } ]

console.log(rcrainfo.isAuthenticated()); // true
```

## Input Validation

console.log(resp.data) // [ { code: 'BCRUSH', description: 'Bulb or Lamp Crusher' } ]
The `emanifest` package can be explicitly configured to provide some simple input validation. This behavior is disabled
by default. It must be explicitly enabled by setting the `validateInput` property of the `RcraClientConfig` on
initiation.

console.log(rcrainfo.isAuthenticated()) // true
1. `siteID` must be a string of length 12
2. `stateCode` must be a string of length 2
3. `siteType` must be one of the following: ['Generator', 'Tsdf', 'Transporter', 'Rejection_AlternateTsdf']
4. `dateType` (a search parameter) must be one of the
following: ['CertifiedDate', 'ReceivedDate', 'ShippedDate', 'UpdatedDate']
5. `manifestTrackingNumber` must be a string of length 12

Upon validation failure, the `RcraClient` will throw an error which can be caught and handled the same as an error
received from the axios library.

```typescript
import { newClient } from 'emanifest';

const rcrainfo = newClient({ validateInput: true });

try {
const resp = await rcrainfo.getSite('VA12345');
} catch (err) {
console.log(err.message); // "siteID must be a string of length 12"
}
```

```
## Disclaimer
Expand All @@ -99,4 +129,5 @@ processes, or services by service mark, trademark, manufacturer, or
otherwise, does not constitute or imply their endorsement, recommendation
or favoring by EPA. The EPA seal and logo shall not be used in any manner
to imply endorsement of any commercial product or activity by EPA or
the United States Government.
the United States Government.
```
2 changes: 1 addition & 1 deletion emanifest-js/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "emanifest",
"version": "0.1.0",
"version": "0.2.0",
"description": "API client library, written in TypeScript, for using the EPA e-Manifest/RCRAInfo web services.",
"type": "module",
"main": "./dist/emanifest-lib.umd.cjs",
Expand Down
Loading

0 comments on commit a1cd32a

Please sign in to comment.