Skip to content

Commit

Permalink
Merge branch 'main' of github.com:dfpc-coe/etl-base
Browse files Browse the repository at this point in the history
  • Loading branch information
ingalls committed Jan 31, 2024
2 parents 9627a02 + c79aa88 commit e4d84d5
Show file tree
Hide file tree
Showing 4 changed files with 190 additions and 117 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,14 @@

## Version History

### v2.1.0

- :tada: Add generic fetch method

### v2.0.2

- :bug: Fix existance check not being thrown

### v2.0.1

- :bug: Misunderstood Object type -> `JSONSchema6`
Expand Down
24 changes: 22 additions & 2 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,13 +73,14 @@ export default class TaskBase {
}
};

if (!this.etl.api) throw new Error('No ETL API URL Provided');
if (!this.etl.layer) throw new Error('No ETL Layer Provided');

// This is just a helper function for local development, signing with the (unsecure) default secret
if (!this.etl.token && (new URL(this.etl.api)).hostname === 'localhost') {
this.etl.token = jwt.sign({ access: 'cot', layer: parseInt(this.etl.layer) }, 'coe-wildland-fire')
}

if (!this.etl.api) throw new Error('No ETL API URL Provided');
if (!this.etl.layer) throw new Error('No ETL Layer Provided');
if (!this.etl.token) throw new Error('No ETL Token Provided');
}

Expand Down Expand Up @@ -117,6 +118,25 @@ export default class TaskBase {
}
}

async fetch(url: string, method: string, body: object): Promise<object> {
console.log(`ok - ${method}: ${url}`);
const res = await fetch(new URL(url, this.etl.api), {
method,
headers: {
'Authorization': `Bearer ${this.etl.token}`,
'Content-Type': 'application/json'
},
body: JSON.stringify(body)
});

if (!res.ok) {
console.error(await res.text());
throw new Error('Failed to make request to API');
} else {
return await res.json();
}
}

/**
* Post an Alert to the Layer Alert API
*
Expand Down
Loading

0 comments on commit e4d84d5

Please sign in to comment.