Skip to content

Commit fd2449c

Browse files
tomasr78claude
andcommitted
Run the examples against staging and fail on their errors; fix the thumbnail example
The examples step sent the staging token to the production API and passed on 401s, because every example catches and prints its own error. A CI-only preload now points the clients at CONVERT_API_BASE_URI and any printed error fails the step. pdf -> extract is now the data-extraction converter (returns JSON), so the thumbnail example converts page 1 straight to JPG. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 603ec6f commit fd2449c

4 files changed

Lines changed: 25 additions & 13 deletions

File tree

‎.github/examples-base-uri.js‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// Preloaded by CI before the examples (node --require): the examples call require('../lib')(token)
2+
// against the production API, so this points every client they create at CONVERT_API_BASE_URI instead.
3+
const lib = require.resolve('../lib');
4+
const ConvertAPI = require(lib);
5+
6+
if (process.env.CONVERT_API_BASE_URI) {
7+
require.cache[lib].exports = (credentials, options = {}) => ConvertAPI(
8+
credentials,
9+
Object.assign({ baseUri: process.env.CONVERT_API_BASE_URI }, options),
10+
);
11+
}

‎.github/workflows/build.yml‎

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,12 +64,18 @@ jobs:
6464
- name: Build
6565
run: npm run build
6666

67-
# The examples catch and print their own errors, so this step shows they run against the built lib
68-
# rather than gating the merge.
67+
# The examples call the production API and catch and print their own errors, so the preload points
68+
# them at CONVERT_API_BASE_URI and any printed error fails the step.
6969
- name: Run the examples
7070
env:
7171
API_TOKEN: ${{ secrets.CONVERTAPI_STAGING_TOKEN }}
72-
run: npm run test:examples
72+
run: |
73+
set -euo pipefail
74+
node --require ./.github/examples-base-uri.js examples/ 2>&1 | tee examples.log
75+
if grep -q '^Error' examples.log; then
76+
echo "::error::An example failed, see the errors above"
77+
exit 1
78+
fi
7379
7480
build:
7581
name: build

‎CHANGELOG.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ each one is a GitHub Release `v{version}` (see [RELEASING.md](RELEASING.md)).
99
closes the high and critical advisories in what `npm install convertapi` resolves, among them
1010
GHSA-jqh4-m9w3-8hp9, GHSA-fjxv-7rqg-78g4 and GHSA-r4q5-vmmm-2653.
1111
- The examples and README use an API token (`API_TOKEN`) instead of the retired secret and API key.
12+
- The PDF thumbnail example converts the first page straight to JPG (`PageRange: 1`), since `pdf → extract`
13+
is now the data-extraction converter and returns JSON.
1214
- `package.json` points at the renamed repository, https://github.com/ConvertAPI/convertapi-library-node.
1315
- Published from GitHub Actions with npm provenance. Tested on Node.js 20, 22 and 24.
1416

‎examples/create_pdf_thumbnail.js‎

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,14 @@
33
// set your api token
44
var convertapi = require('../lib')(process.env.API_TOKEN);
55

6-
// Example of extracting first page from PDF and then chaining conversion PDF page to JPG.
7-
// https://www.convertapi.com/pdf-to-extract
6+
// Example of converting the first page of a PDF to a JPG thumbnail.
87
// https://www.convertapi.com/pdf-to-jpg
98

109
var dir = require('os').tmpdir();
1110

12-
var pdfResultPromise = convertapi.convert('extract', {
13-
File: './examples/files/test.pdf',
14-
PageRange: 1
15-
});
16-
1711
convertapi.convert('jpg', {
18-
File: pdfResultPromise,
19-
ScaleImage: true,
20-
ScaleProportions: true,
12+
File: './examples/files/test.pdf',
13+
PageRange: 1,
2114
ImageHeight: 300,
2215
ImageWidth: 300
2316
})

0 commit comments

Comments
 (0)