Skip to content

Commit ae090e6

Browse files
committed
fix: resolve issues in ESM build process and improve Rollup configuration
1 parent c876ce0 commit ae090e6

File tree

6 files changed

+209
-0
lines changed

6 files changed

+209
-0
lines changed

.gitignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Environment variables
2+
.env
3+
.env.*
4+
!.env.example
5+
node_modules

examples/nodejs/.env.example

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
HACKMD_ACCESS_TOKEN=YOUR_ACCESS_TOKEN

examples/nodejs/README.md

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
# HackMD API Client Example
2+
3+
This is an example project demonstrating the usage of the HackMD API client.
4+
5+
## Setup
6+
7+
1. First, install the dependencies:
8+
```bash
9+
npm install
10+
```
11+
12+
2. Set up your HackMD access token using one of these methods:
13+
14+
a. Set it as an environment variable:
15+
```bash
16+
# For Unix/Linux/macOS
17+
export HACKMD_ACCESS_TOKEN=your_access_token_here
18+
19+
# For Windows PowerShell
20+
$env:HACKMD_ACCESS_TOKEN="your_access_token_here"
21+
```
22+
23+
b. Or create a `.env` file in the project root (not tracked by git):
24+
```
25+
HACKMD_ACCESS_TOKEN=your_access_token_here
26+
```
27+
28+
You can get your access token from [HackMD API documentation](https://hackmd.io/@hackmd-api/developer-portal).
29+
30+
## Running the Example
31+
32+
To run the example:
33+
34+
```bash
35+
npm start
36+
```
37+
38+
## What's Demonstrated
39+
40+
The example demonstrates several features of the HackMD API client:
41+
42+
1. Getting user information
43+
2. Creating a new note
44+
3. Using ETag support for caching
45+
4. Updating note content
46+
5. Getting raw response data
47+
6. Deleting notes
48+
49+
## Features Shown
50+
51+
- Retry configuration with exponential backoff
52+
- ETag support for caching
53+
- Response data unwrapping
54+
- Error handling
55+
- Environment variable configuration

examples/nodejs/index.js

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
import HackMDAPI from '@hackmd/api';
2+
import dotenv from 'dotenv';
3+
4+
// Load environment variables
5+
dotenv.config();
6+
7+
// Check for required environment variable
8+
if (!process.env.HACKMD_ACCESS_TOKEN) {
9+
console.error('Error: HACKMD_ACCESS_TOKEN environment variable is not set.');
10+
console.error('Please set your HackMD access token using one of these methods:');
11+
console.error('1. Create a .env file with HACKMD_ACCESS_TOKEN=your_token_here');
12+
console.error('2. Set the environment variable directly: export HACKMD_ACCESS_TOKEN=your_token_here');
13+
process.exit(1);
14+
}
15+
16+
// Create API client with retry configuration
17+
const client = new HackMDAPI(process.env.HACKMD_ACCESS_TOKEN, 'https://api.hackmd.io/v1', {
18+
retryConfig: {
19+
maxRetries: 3,
20+
baseDelay: 100
21+
}
22+
});
23+
24+
async function main() {
25+
try {
26+
// Example 1: Get user information
27+
console.log('Getting user information...');
28+
const me = await client.getMe();
29+
console.log('User email:', me.email);
30+
console.log('User name:', me.name);
31+
32+
// Example 2: Create a new note
33+
console.log('\nCreating a new note...');
34+
const newNote = await client.createNote({
35+
title: 'Test Note',
36+
content: '# Hello from HackMD API\n\nThis is a test note created using the API client.',
37+
readPermission: 'guest',
38+
writePermission: 'owner'
39+
});
40+
console.log('Created note ID:', newNote.id);
41+
console.log('Note URL:', newNote.publishLink);
42+
43+
// Example 3: Get note with ETag support
44+
console.log('\nGetting note with ETag support...');
45+
const note = await client.getNote(newNote.id);
46+
console.log('Note content:', note.content);
47+
48+
// Second request with ETag
49+
const updatedNote = await client.getNote(newNote.id, { etag: note.etag });
50+
console.log('Note status:', updatedNote.status);
51+
52+
// Example 4: Update note content
53+
console.log('\nUpdating note content...');
54+
const updatedContent = await client.updateNoteContent(newNote.id, '# Updated Content\n\nThis note has been updated!');
55+
console.log('Updated note content:', updatedContent.content);
56+
57+
// Example 5: Get raw response (unwrapData: false)
58+
console.log('\nGetting raw response...');
59+
const rawResponse = await client.getNote(newNote.id, { unwrapData: false });
60+
console.log('Response headers:', rawResponse.headers);
61+
console.log('Response status:', rawResponse.status);
62+
63+
// Example 6: Delete the test note
64+
console.log('\nCleaning up - deleting test note...');
65+
await client.deleteNote(newNote.id);
66+
console.log('Note deleted successfully');
67+
68+
} catch (error) {
69+
console.error('Error:', error.message);
70+
if (error.response) {
71+
console.error('Response status:', error.response.status);
72+
console.error('Response data:', error.response.data);
73+
}
74+
}
75+
}
76+
77+
main();

examples/nodejs/package-lock.json

Lines changed: 57 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/nodejs/package.json

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"name": "hackmd-api-example",
3+
"version": "1.0.0",
4+
"description": "Example usage of HackMD API client",
5+
"main": "index.js",
6+
"type": "module",
7+
"scripts": {
8+
"start": "node index.js"
9+
},
10+
"dependencies": {
11+
"@hackmd/api": "file:../../nodejs",
12+
"dotenv": "^16.4.5"
13+
}
14+
}

0 commit comments

Comments
 (0)