Hooks are made wrapping CWMSjs using code similar to the React example below.
They also provide variables for you to track the state, progress, and data of the request.
The header on the Groundwork-Water webpage provides a list of currently available hooks.
npm install cwmsjs --save
import { TimeSeriesApi, Configuration } from "cwmsjs";
// If you want to change the server or any other base level configuration default items you can use
// Configuration
const v2_config = new Configuration({
basePath: 'https://water.usace.army.mil/cwms-data',
headers: {
accept: 'application/json;version=2',
},
});
const ts_api = new TimeSeriesApi(v2_config);
ts_api
.getDataTimeseries({
office: 'SWT',
name: 'KEYS.Elev.Inst.1Hour.0.Ccp-Rev',
})
.then((data) => {
console.log(data?.values);
})
.catch(async (e) => {
if (e.response) {
const error_msg = e.response.json();
e.message = `${e.response.url}\n${e.message}\n${JSON.stringify(error_msg, null, 2)}`;
console.error(e);
throw e;
} else {
throw e;
}
});
curl -O "https://raw.githubusercontent.com/HydrologicEngineeringCenter/cwms-data-api-client-javascript/main/src/dist/bundle.js"
<!-- Include the bundle.js file -->
<script src="./bundle.js"></script>
<!-- Call the cwmsjs after the bundle has loaded -->
<script type="module">
// If you want to change the server or any other base level configuration default items you can use
// Configuration
const v2_config = new cwmsjs.Configuration({
basePath: 'https://water.usace.army.mil/cwms-data',
headers: {
accept: 'application/json;version=2',
},
});
const ts_api = new cwmsjs.TimeSeriesApi(v2_config);
ts_api
.getDataTimeseries({
office: 'SWT',
name: 'KEYS.Elev.Inst.1Hour.0.Ccp-Rev',
})
.then((data) => {
console.log(data?.values);
})
.catch(async (e) => {
if (e.response) {
const error_msg = e.response.json();
e.message = `${e.response.url}\n${e.message}\n${JSON.stringify(error_msg, null, 2)}`;
console.error(e);
throw e;
} else {
throw e;
}
});
</script>
An example for calling the Configuration endpoint via CDA in JavaScript
Swagger UI for Endpoints