Skip to content

Commit 9bf67d4

Browse files
authored
Fix the examples (#205)
Since the migration to TypeScript, examples had to be adjusted. The SDK now uses named exports. Also the SDK needs to be built before examples can be run. This is now displayed on top of the example instructions. Since the SDK now used package exports, the examples can now import the package using its name instead of relative imports. Some ESLint warnings have been disabled for the examples. Incorrect paths to example fixtures have been fixed. The examples are now type-checked using JSDoc based type annotations. Closes #204
1 parent c3d0de1 commit 9bf67d4

12 files changed

+74
-62
lines changed

examples/.eslintrc.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
module.exports = {
22
rules: {
33
'no-console': 0,
4+
'import/no-extraneous-dependencies': 0,
5+
'import/no-unresolved': 0,
46
},
57
}

examples/convert_to_webp.js

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
11
// Run this file as:
22
//
3-
// env TRANSLOADIT_KEY=xxx TRANSLOADIT_SECRET=yyy node examples/convert_to_webp.js ./fixtures/berkley.jpg
3+
// env TRANSLOADIT_KEY=xxx TRANSLOADIT_SECRET=yyy node examples/convert_to_webp.js ./examples/fixtures/berkley.jpg
44
//
5-
// You'll likely just want to `require('transloadit')`, but we're requiring the local
6-
// variant here for easier testing:
7-
const Transloadit = require('../src/Transloadit')
5+
// You may need to build the project first using:
6+
//
7+
// yarn prepack
8+
//
9+
const { Transloadit } = require('transloadit')
810

911
const transloadit = new Transloadit({
10-
authKey: process.env.TRANSLOADIT_KEY,
11-
authSecret: process.env.TRANSLOADIT_SECRET,
12+
authKey: /** @type {string} */ (process.env.TRANSLOADIT_KEY),
13+
authSecret: /** @type {string} */ (process.env.TRANSLOADIT_SECRET),
1214
})
1315

1416
const filePath = process.argv[2]

examples/credentials.js

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,18 @@
33
//
44
// env TRANSLOADIT_KEY=xxx TRANSLOADIT_SECRET=yyy node template_api.js
55
//
6-
// You'll likely just want to `require('transloadit')`, but we're requiring the local
7-
// variant here for easier testing:
8-
const Transloadit = require('../src/Transloadit')
6+
// You may need to build the project first using:
7+
//
8+
// yarn prepack
9+
//
10+
const { Transloadit } = require('transloadit')
911

1012
const transloadit = new Transloadit({
11-
authKey: process.env.TRANSLOADIT_KEY,
12-
authSecret: process.env.TRANSLOADIT_SECRET,
13-
// authKey : process.env.API2_SYSTEMTEST_AUTH_KEY,
14-
// authSecret: process.env.API2_SYSTEMTEST_SECRET_KEY,
15-
// endpoint : 'https://api2-vbox.transloadit.com',
13+
authKey: /** @type {string} */ (process.env.TRANSLOADIT_KEY),
14+
authSecret: /** @type {string} */ (process.env.TRANSLOADIT_SECRET),
15+
// authKey: /** @type {string} */ (process.env.API2_SYSTEMTEST_AUTH_KEY),
16+
// authSecret: /** @type {string} */ (process.env.API2_SYSTEMTEST_SECRET_KEY),
17+
// endpoint: /** @type {string} */ ('https://api2-vbox.transloadit.com'),
1618
})
1719

1820
const firstName = 'myProductionS3'
@@ -51,9 +53,8 @@ const credentialParams = {
5153
}
5254

5355
console.log(`==> createTemplateCredential`)
54-
const createTemplateCredentialResult = await transloadit.createTemplateCredential(
55-
credentialParams
56-
)
56+
const createTemplateCredentialResult =
57+
await transloadit.createTemplateCredential(credentialParams)
5758
console.log('TemplateCredential created successfully:', createTemplateCredentialResult)
5859
// ^-- with Templates, there is `ok`, `message`, `id`, `content`, `name`, `require_signature_auth`. Same is true for: created, updated, fetched
5960
// with Credentials, there is `ok`, `message`, `credentials` <-- and a single object nested directly under it, which is unexpected with that plural imho. Same is true for created, updated, fetched

examples/face_detect_download.js

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,22 @@
11
// Run this file as:
22
//
3-
// env TRANSLOADIT_KEY=xxx TRANSLOADIT_SECRET=yyy node examples/face_detect_download.js ./fixtures/berkley.jpg
3+
// env TRANSLOADIT_KEY=xxx TRANSLOADIT_SECRET=yyy node examples/face_detect_download.js ./examples/fixtures/berkley.jpg
4+
//
5+
// You may need to build the project first using:
6+
//
7+
// yarn prepack
48
//
59
// This example will take an image and find a face and crop out the face.
610
// Then it will download the result as a file in the current directory
711
// See https://transloadit.com/demos/artificial-intelligence/detect-faces-in-images/
812

913
const got = require('got')
1014
const { createWriteStream } = require('fs')
11-
12-
// You'll likely just want to `require('transloadit')`, but we're requiring the local
13-
// variant here for easier testing:
14-
const Transloadit = require('../src/Transloadit')
15+
const { Transloadit } = require('transloadit')
1516

1617
const transloadit = new Transloadit({
17-
authKey: process.env.TRANSLOADIT_KEY,
18-
authSecret: process.env.TRANSLOADIT_SECRET,
18+
authKey: /** @type {string} */ (process.env.TRANSLOADIT_KEY),
19+
authSecret: /** @type {string} */ (process.env.TRANSLOADIT_SECRET),
1920
})
2021

2122
const filePath = process.argv[2]
@@ -46,7 +47,7 @@ const filePath = process.argv[2]
4647
// Now save the file
4748
const outPath = './output-face.jpg'
4849
const stream = createWriteStream(outPath)
49-
await got.stream(status.results.facesDetected[0].url).pipe(stream)
50+
await got.default.stream(status.results.facesDetected[0].url).pipe(stream)
5051
console.log('Your cropped face has been saved to', outPath)
5152
} catch (err) {
5253
console.error('createAssembly failed', err)

examples/fetch_costs_of_all_assemblies_in_timeframe.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@
22
//
33
// env TRANSLOADIT_KEY=xxx TRANSLOADIT_SECRET=yyy node fetch_costs_of_all_assemblies_in_timeframe.js
44
//
5-
// make sure to "npm install p-map" for this demo
5+
// You may need to build the project first using:
6+
//
7+
// yarn prepack
8+
//
69
const pMap = require('p-map')
7-
8-
// You'll likely just want to `require('transloadit')`, but we're requiring the local
9-
// variant here for easier testing:
10-
const Transloadit = require('../src/Transloadit')
10+
const { Transloadit } = require('transloadit')
1111

1212
const fromdate = '2020-12-31 15:30:00'
1313
const todate = '2020-12-31 15:30:01'
@@ -21,8 +21,8 @@ const todate = '2020-12-31 15:30:01'
2121
}
2222

2323
const transloadit = new Transloadit({
24-
authKey: process.env.TRANSLOADIT_KEY,
25-
authSecret: process.env.TRANSLOADIT_SECRET,
24+
authKey: /** @type {string} */ (process.env.TRANSLOADIT_KEY),
25+
authSecret: /** @type {string} */ (process.env.TRANSLOADIT_SECRET),
2626
})
2727

2828
let totalBytes = 0
@@ -38,7 +38,7 @@ const todate = '2020-12-31 15:30:01'
3838
items,
3939
// eslint-disable-next-line no-loop-func
4040
async (assembly) => {
41-
const assemblyFull = await transloadit.getAssembly(assembly.id)
41+
const assemblyFull = await transloadit.getAssembly(/** @type {string} */ (assembly.id))
4242
// console.log(assemblyFull.assembly_id)
4343

4444
const { bytes_usage: bytesUsage } = assemblyFull

examples/rasterize_svg_to_png.js

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
11
// Run this file as:
22
//
3-
// env TRANSLOADIT_KEY=xxx TRANSLOADIT_SECRET=yyy node rasterize_svg_to_png.js ./fixtures/circle.svg
3+
// env TRANSLOADIT_KEY=xxx TRANSLOADIT_SECRET=yyy node rasterize_svg_to_png.js ./examples/fixtures/circle.svg
44
//
5-
// You'll likely just want to `require('transloadit')`, but we're requiring the local
6-
// variant here for easier testing:
7-
const Transloadit = require('../src/Transloadit')
5+
// You may need to build the project first using:
6+
//
7+
// yarn prepack
8+
//
9+
const { Transloadit } = require('transloadit')
810

911
const transloadit = new Transloadit({
10-
authKey: process.env.TRANSLOADIT_KEY,
11-
authSecret: process.env.TRANSLOADIT_SECRET,
12+
authKey: /** @type {string} */ (process.env.TRANSLOADIT_KEY),
13+
authSecret: /** @type {string} */ (process.env.TRANSLOADIT_SECRET),
1214
})
1315

1416
const filePath = process.argv[2]

examples/resize_an_image.js

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
11
// Run this file as:
22
//
3-
// env TRANSLOADIT_KEY=xxx TRANSLOADIT_SECRET=yyy node resize_an_image.js ./fixtures/berkley.jpg
3+
// env TRANSLOADIT_KEY=xxx TRANSLOADIT_SECRET=yyy node resize_an_image.js ./examples/fixtures/berkley.jpg
44
//
5-
// You'll likely just want to `require('transloadit')`, but we're requiring the local
6-
// variant here for easier testing:
7-
const Transloadit = require('../src/Transloadit')
5+
// You may need to build the project first using:
6+
//
7+
// yarn prepack
8+
//
9+
const { Transloadit } = require('transloadit')
810

911
const transloadit = new Transloadit({
10-
authKey: process.env.TRANSLOADIT_KEY,
11-
authSecret: process.env.TRANSLOADIT_SECRET,
12+
authKey: /** @type {string} */ (process.env.TRANSLOADIT_KEY),
13+
authSecret: /** @type {string} */ (process.env.TRANSLOADIT_SECRET),
1214
})
1315

1416
const filePath = process.argv[2]

examples/retry.js

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,16 @@
44
//
55
// env TRANSLOADIT_KEY=xxx TRANSLOADIT_SECRET=yyy node retry.js
66
//
7-
8-
// eslint-disable-next-line import/no-extraneous-dependencies
7+
// You may need to build the project first using:
8+
//
9+
// yarn prepack
10+
//
911
const pRetry = require('p-retry')
10-
11-
// You'll likely just want to `require('transloadit')`, but we're requiring the local
12-
// variant here for easier testing:
13-
const Transloadit = require('../src/Transloadit')
12+
const { Transloadit, TransloaditError } = require('transloadit')
1413

1514
const transloadit = new Transloadit({
16-
authKey: process.env.TRANSLOADIT_KEY,
17-
authSecret: process.env.TRANSLOADIT_SECRET,
15+
authKey: /** @type {string} */ (process.env.TRANSLOADIT_KEY),
16+
authSecret: /** @type {string} */ (process.env.TRANSLOADIT_SECRET),
1817
})
1918

2019
async function run() {
@@ -23,7 +22,7 @@ async function run() {
2322
const { items } = await transloadit.listTemplates({ sort: 'created', order: 'asc' })
2423
return items
2524
} catch (err) {
26-
if (err.transloaditErrorCode === 'INVALID_SIGNATURE') {
25+
if (err instanceof TransloaditError && err.transloaditErrorCode === 'INVALID_SIGNATURE') {
2726
// This is an unrecoverable error, abort retry
2827
throw new pRetry.AbortError('INVALID_SIGNATURE')
2928
}

examples/template_api.js

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,15 @@
22
//
33
// env TRANSLOADIT_KEY=xxx TRANSLOADIT_SECRET=yyy node template_api.js
44
//
5-
// You'll likely just want to `require('transloadit')`, but we're requiring the local
6-
// variant here for easier testing:
7-
const Transloadit = require('../src/Transloadit')
5+
// You may need to build the project first using:
6+
//
7+
// yarn prepack
8+
//
9+
const { Transloadit } = require('transloadit')
810

911
const transloadit = new Transloadit({
10-
authKey: process.env.TRANSLOADIT_KEY,
11-
authSecret: process.env.TRANSLOADIT_SECRET,
12+
authKey: /** @type {string} */ (process.env.TRANSLOADIT_KEY),
13+
authSecret: /** @type {string} */ (process.env.TRANSLOADIT_SECRET),
1214
})
1315

1416
const template = {

src/Transloadit.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ export {
2828
MaxRedirectsError,
2929
TimeoutError,
3030
} from 'got'
31-
export { InconsistentResponseError }
31+
export { InconsistentResponseError, TransloaditError }
3232

3333
const log = debug('transloadit')
3434
const logWarn = debug('transloadit:warn')

0 commit comments

Comments
 (0)