-
Notifications
You must be signed in to change notification settings - Fork 97
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #591 from YaSuenag/pr/generate-npm-client
Publish WebAPI client library for NPM
- Loading branch information
Showing
14 changed files
with
492 additions
and
51 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
name: 4.a.1-Generate WebAPI client library for NPM | ||
|
||
on: | ||
workflow_call: | ||
inputs: | ||
image: | ||
required: true | ||
type: string | ||
apiver: | ||
required: true | ||
type: string | ||
|
||
jobs: | ||
generate-npm-client: | ||
runs-on: ubuntu-latest | ||
services: | ||
webapi: | ||
image: ${{ inputs.image }} | ||
ports: | ||
- 8080:8080 | ||
options: >- | ||
--health-cmd "curl -sS http://localhost:8080/health" | ||
--health-interval 3s | ||
--health-timeout 5s | ||
--health-retries 5 | ||
permissions: | ||
packages: write | ||
env: | ||
API: http://localhost:8080/api/v1/swagger.yaml | ||
steps: | ||
- name: Setup Node.js 22 | ||
uses: actions/setup-node@v4 | ||
with: | ||
node-version: 22.x | ||
registry-url: https://npm.pkg.github.com | ||
- name: Prepare | ||
run: | | ||
mkdir work | ||
npm install -g @openapitools/[email protected] | ||
- name: Generate client library | ||
run: | | ||
echo -n '{"npmName": "@${{ github.repository_owner }}/casdk-client", "npmVersion": "${{ inputs.apiver }}", "licenseName": "MIT", "gitUserId": "${{ github.repository_owner }}", "gitRepoId": "' > config.json | ||
echo -n "${{ github.repository }}" | sed -e 's|^.\+/||' >> config.json | ||
echo -n '", "npmRepository": "https://npm.pkg.github.com"}' >> config.json | ||
openapi-generator-cli generate -i ${{ env.API }} -g typescript-axios -o work -c config.json | ||
shell: bash | ||
- name: Publish NPM library | ||
working-directory: work | ||
run: | | ||
npm install | ||
npm publish | ||
env: | ||
NODE_AUTH_TOKEN: ${{ github.token }} | ||
- name: Setup TypeDoc | ||
run: npm install -g typedoc | ||
- name: Generate documents | ||
working-directory: work | ||
run: | | ||
echo '{"out": "docs", "excludePrivate": true, "excludeProtected": true, "excludeExternals": true, "includeVersion": true}' > ../typedoc.json | ||
npx typedoc --options ../typedoc.json --entryPoints index.ts | ||
- name: Upload TypeDoc | ||
uses: actions/upload-artifact@v4 | ||
with: | ||
name: typedoc | ||
path: docs |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
//npm.pkg.github.com/:_authToken=${GH_TOKEN} | ||
@Green-Software-Foundation:registry=https://npm.pkg.github.com |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
# NPM Client Example | ||
|
||
This folder contains an example for WebAPI client in NPM. Client library would be pulled from [GitHub Packages](https://github.com/orgs/Green-Software-Foundation/packages?repo_name=carbon-aware-sdk). | ||
|
||
TypeDoc is [here](https://carbon-aware-sdk.greensoftware.foundation/client-apidocs/1.0.0/npm). | ||
|
||
## Requirements | ||
|
||
- WebAPI instance | ||
- See the [Overview](../../docs/overview.md#publish-webapi-with-container) if you'd like to start it on container. | ||
- Node.js 18 or later | ||
|
||
## Client code | ||
|
||
[index.js](index.js) is an example program to call WebAPI endpoint. It calls all of endpoints, and shows the result. | ||
|
||
Following methods are available: | ||
|
||
- `processBestEmissionsDataByLocations` | ||
- Call /emissions/bylocations/best | ||
- Gather the best emission data for west/central/east US yesterday. | ||
- `processEmissionsDataByLocations` | ||
- Call /emissions/bylocations | ||
- Gather emission data for west/central/east US yesterday. | ||
- `processEmissionsDataByLocation` | ||
- Call /emissions/bylocation | ||
- Gather emission data for westus yesterday. | ||
- `processCurrentForecastData` | ||
- Call /emissions/forecasts/current | ||
- Gather forecast data for westus. | ||
- `processForecastBatchData` | ||
- Call /emissions/forecasts/batch | ||
- Gather forecast data for westus. | ||
- `processAverageData` | ||
- Call /emissions/average-carbon-intensity . | ||
- Gather average data for westus yesterday. | ||
- `processAverageBatchData` | ||
- Call /emissions/average-carbon-intensity/batch | ||
- Gather average data for westus yesterday. | ||
|
||
## How it works | ||
|
||
### 1. Set WebAPI endpoint | ||
|
||
You need to set base URL of WebAPI endpoint in `index.ts`. `http://localhost:8080` is set by default. | ||
|
||
```typescript | ||
const conf = new Configuration({basePath: 'http://localhost:8080'}); | ||
``` | ||
|
||
### 2. Install required packages | ||
|
||
You have to run `npm install` at first. CASDK client module would be downloaded from GitHub Packages, so you need to set GitHub Parsonal Access Token. You can set it to `GH_TOKEN` environment variable used in [.npmrc](.npmrc). | ||
|
||
If you have not yet got GitHub PAT, see [this guide](https://docs.github.com/authentication/keeping-your-account-and-data-secure/managing-your-personal-access-tokens). | ||
|
||
|
||
```sh | ||
GH_TOKEN=<GitHub PAT> npm install | ||
``` | ||
|
||
### 3. Run | ||
|
||
```sh | ||
npx ts-node index.ts | ||
``` |
Oops, something went wrong.