Skip to content

Commit

Permalink
Added CI and extended token store functionality (#2)
Browse files Browse the repository at this point in the history
* initial typescript setup

* gotten tests to work

* created token store

* added alby wallet

* tested axios interceptor wrapper

* chore: update copyright year in LICENSE file

* refactor: remove sample.spec.js test file

* chore: Update launch.json and package.json
refactor: added method + hostname functionality to token store.

* added github actions.

* initial typescript setup

* gotten tests to work

* added alby wallet

* tested axios interceptor wrapper

* refactor: remove sample.spec.js test file

* chore: Update launch.json and package.json
refactor: added method + hostname functionality to token store.

* added github actions.

---------

Co-authored-by: Samuel Alarco <[email protected]>
  • Loading branch information
SamuelAl and Samuel Alarco authored May 8, 2024
1 parent f21c010 commit c58030d
Show file tree
Hide file tree
Showing 11 changed files with 2,178 additions and 160 deletions.
37 changes: 37 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
name: CI Workflow

on:
# Run on pull requests to any branch
pull_request:
branches:
- '*'

# Run on any push to the main branch
push:
branches:
- main

jobs:
build-and-test:
name: Build and Test
runs-on: ubuntu-latest

steps:
- name: Checkout code
uses: actions/checkout@v2

- name: Set up Node.js
uses: actions/setup-node@v3
with:
node-version: '18'

- name: Install dependencies
run: npm install

- name: Run tests
run: npm test

- name: Generate coverage report
run: |
npm install -g nyc
nyc report --reporter=text --reporter=html
9 changes: 9 additions & 0 deletions .nycrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"all": true,
"include": ["dist/**/*.js"],
"exclude": ["dist/**/*.test.js", "**/test/**"],
"extension": [".js"],
"reporter": ["text", "html"],
"sourceMap": false,
"instrument": true
}
4 changes: 2 additions & 2 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@
{
"type": "node",
"request": "launch",
"name": "Launch Program",
"name": "Launch Example",
"skipFiles": [
"<node_internals>/**"
],
"program": "${workspaceFolder}/src/yourNodeIndex.js"
"program": "${workspaceFolder}/examples/randomNumber.js"
}
]
}
48 changes: 48 additions & 0 deletions examples/randomNumber.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// Import required dependencies
const axios = require('axios');
const { setupL402Interceptor, AlbyWallet, MemoryTokenStore } = require('../dist'); // Adjust path to your interceptor file
const { Client } = require('@getalby/sdk');
require('dotenv').config();

// Load the Alby token from an environment variable
const albyToken = process.env.ALBY_BEARER_TOKEN;
if (!albyToken) {
console.error('Missing ALBY_BEARER_TOKEN environment variable.');
process.exit(1);
}

const albyClient = new Client(albyToken)

// Initialize the AlbyWallet using the bearer token
const albyWallet = new AlbyWallet(albyClient);

// Initialize the MemoryTokenStore
const store = new MemoryTokenStore();

// Create an Axios instance and configure it with the L402 interceptor
const axiosInstance = axios.create({
baseURL: 'https://rnd.ln.sulu.sh',
headers: { 'Content-Type': 'application/json' },
});

// Set up the L402 interceptor with the real Alby wallet and token store
setupL402Interceptor(axiosInstance, albyWallet, store);

// Function to fetch a random number using the configured Axios instance
async function fetchRandomNumber() {
try {
// Make a GET request to the random number API
const response = await axiosInstance.get('/randomnumber');
console.log('Random Number:', response.data);
} catch (error) {
// Handle errors gracefully
if (axios.isAxiosError(error)) {
console.error('Axios Error:', error.message, error.response?.status);
} else {
console.error('Unknown Error:', error);
}
}
}

// Execute the function to fetch the random number
fetchRandomNumber();
Loading

0 comments on commit c58030d

Please sign in to comment.