Skip to content

Commit 17b8a39

Browse files
authored
Merge pull request #37 from hackmdio/release/2.5.0
Release 2.5.0
2 parents a9d95a6 + 2b304e0 commit 17b8a39

21 files changed

+3651
-2628
lines changed

.github/workflows/ci.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ name: Node.js CI
22

33
on:
44
push:
5-
branches: [ master ]
5+
branches: [ master, develop ]
66
pull_request:
7-
branches: [ master ]
7+
branches: [ master, develop ]
88

99
jobs:
1010
test:

.github/workflows/publish.yml

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
name: Publish to NPM
2+
3+
on:
4+
push:
5+
tags:
6+
- 'v*'
7+
8+
jobs:
9+
publish:
10+
runs-on: ubuntu-latest
11+
steps:
12+
- uses: actions/checkout@v4
13+
14+
- name: Set up Node.js
15+
uses: actions/setup-node@v4
16+
with:
17+
node-version: '20'
18+
registry-url: 'https://registry.npmjs.org'
19+
cache: 'npm'
20+
cache-dependency-path: nodejs/package-lock.json
21+
22+
- name: Install dependencies
23+
working-directory: nodejs
24+
run: npm ci
25+
26+
- name: Build
27+
working-directory: nodejs
28+
run: npm run build
29+
30+
- name: Publish to NPM
31+
working-directory: nodejs
32+
run: npm publish --access public
33+
env:
34+
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}

.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

README.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,30 @@ This repository contains a set of packages for interacting with the [HackMD API]
66

77
See [README](./nodejs)
88

9+
## Examples
10+
11+
To help you get started quickly, we provide comprehensive usage examples in the `examples/` directory:
12+
13+
### Node.js Example
14+
15+
The `examples/nodejs/` directory contains a complete example project demonstrating:
16+
17+
- User information retrieval
18+
- Note creation and management
19+
- ETag support for caching
20+
- Content updates
21+
- Error handling with retry logic
22+
- Environment variable configuration
23+
24+
To run the Node.js example:
25+
26+
1. Navigate to the example directory: `cd examples/nodejs`
27+
2. Follow the setup instructions in [examples/nodejs/README.md](./examples/nodejs/README.md)
28+
3. Set your HackMD access token
29+
4. Run `npm start`
30+
31+
The example includes detailed comments and demonstrates best practices for using the HackMD API client.
32+
933
## LICENSE
1034

1135
MIT

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

nodejs/.eslintrc.js renamed to nodejs/.eslintrc.cjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ const config = {
2222
]
2323
},
2424
"ignorePatterns": [
25-
".eslintrc.js"
25+
".eslintrc.cjs"
2626
],
2727
}
2828

0 commit comments

Comments
 (0)