Skip to content

Commit

Permalink
Fix the examples (#205)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
remcohaszing authored Dec 9, 2024
1 parent c3d0de1 commit 9bf67d4
Show file tree
Hide file tree
Showing 12 changed files with 74 additions and 62 deletions.
2 changes: 2 additions & 0 deletions examples/.eslintrc.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
module.exports = {
rules: {
'no-console': 0,
'import/no-extraneous-dependencies': 0,
'import/no-unresolved': 0,
},
}
14 changes: 8 additions & 6 deletions examples/convert_to_webp.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
// Run this file as:
//
// env TRANSLOADIT_KEY=xxx TRANSLOADIT_SECRET=yyy node examples/convert_to_webp.js ./fixtures/berkley.jpg
// env TRANSLOADIT_KEY=xxx TRANSLOADIT_SECRET=yyy node examples/convert_to_webp.js ./examples/fixtures/berkley.jpg
//
// You'll likely just want to `require('transloadit')`, but we're requiring the local
// variant here for easier testing:
const Transloadit = require('../src/Transloadit')
// You may need to build the project first using:
//
// yarn prepack
//
const { Transloadit } = require('transloadit')

const transloadit = new Transloadit({
authKey: process.env.TRANSLOADIT_KEY,
authSecret: process.env.TRANSLOADIT_SECRET,
authKey: /** @type {string} */ (process.env.TRANSLOADIT_KEY),
authSecret: /** @type {string} */ (process.env.TRANSLOADIT_SECRET),
})

const filePath = process.argv[2]
Expand Down
23 changes: 12 additions & 11 deletions examples/credentials.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,18 @@
//
// env TRANSLOADIT_KEY=xxx TRANSLOADIT_SECRET=yyy node template_api.js
//
// You'll likely just want to `require('transloadit')`, but we're requiring the local
// variant here for easier testing:
const Transloadit = require('../src/Transloadit')
// You may need to build the project first using:
//
// yarn prepack
//
const { Transloadit } = require('transloadit')

const transloadit = new Transloadit({
authKey: process.env.TRANSLOADIT_KEY,
authSecret: process.env.TRANSLOADIT_SECRET,
// authKey : process.env.API2_SYSTEMTEST_AUTH_KEY,
// authSecret: process.env.API2_SYSTEMTEST_SECRET_KEY,
// endpoint : 'https://api2-vbox.transloadit.com',
authKey: /** @type {string} */ (process.env.TRANSLOADIT_KEY),
authSecret: /** @type {string} */ (process.env.TRANSLOADIT_SECRET),
// authKey: /** @type {string} */ (process.env.API2_SYSTEMTEST_AUTH_KEY),
// authSecret: /** @type {string} */ (process.env.API2_SYSTEMTEST_SECRET_KEY),
// endpoint: /** @type {string} */ ('https://api2-vbox.transloadit.com'),
})

const firstName = 'myProductionS3'
Expand Down Expand Up @@ -51,9 +53,8 @@ const credentialParams = {
}

console.log(`==> createTemplateCredential`)
const createTemplateCredentialResult = await transloadit.createTemplateCredential(
credentialParams
)
const createTemplateCredentialResult =
await transloadit.createTemplateCredential(credentialParams)
console.log('TemplateCredential created successfully:', createTemplateCredentialResult)
// ^-- with Templates, there is `ok`, `message`, `id`, `content`, `name`, `require_signature_auth`. Same is true for: created, updated, fetched
// 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
Expand Down
17 changes: 9 additions & 8 deletions examples/face_detect_download.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,22 @@
// Run this file as:
//
// env TRANSLOADIT_KEY=xxx TRANSLOADIT_SECRET=yyy node examples/face_detect_download.js ./fixtures/berkley.jpg
// env TRANSLOADIT_KEY=xxx TRANSLOADIT_SECRET=yyy node examples/face_detect_download.js ./examples/fixtures/berkley.jpg
//
// You may need to build the project first using:
//
// yarn prepack
//
// This example will take an image and find a face and crop out the face.
// Then it will download the result as a file in the current directory
// See https://transloadit.com/demos/artificial-intelligence/detect-faces-in-images/

const got = require('got')
const { createWriteStream } = require('fs')

// You'll likely just want to `require('transloadit')`, but we're requiring the local
// variant here for easier testing:
const Transloadit = require('../src/Transloadit')
const { Transloadit } = require('transloadit')

const transloadit = new Transloadit({
authKey: process.env.TRANSLOADIT_KEY,
authSecret: process.env.TRANSLOADIT_SECRET,
authKey: /** @type {string} */ (process.env.TRANSLOADIT_KEY),
authSecret: /** @type {string} */ (process.env.TRANSLOADIT_SECRET),
})

const filePath = process.argv[2]
Expand Down Expand Up @@ -46,7 +47,7 @@ const filePath = process.argv[2]
// Now save the file
const outPath = './output-face.jpg'
const stream = createWriteStream(outPath)
await got.stream(status.results.facesDetected[0].url).pipe(stream)
await got.default.stream(status.results.facesDetected[0].url).pipe(stream)
console.log('Your cropped face has been saved to', outPath)
} catch (err) {
console.error('createAssembly failed', err)
Expand Down
16 changes: 8 additions & 8 deletions examples/fetch_costs_of_all_assemblies_in_timeframe.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@
//
// env TRANSLOADIT_KEY=xxx TRANSLOADIT_SECRET=yyy node fetch_costs_of_all_assemblies_in_timeframe.js
//
// make sure to "npm install p-map" for this demo
// You may need to build the project first using:
//
// yarn prepack
//
const pMap = require('p-map')

// You'll likely just want to `require('transloadit')`, but we're requiring the local
// variant here for easier testing:
const Transloadit = require('../src/Transloadit')
const { Transloadit } = require('transloadit')

const fromdate = '2020-12-31 15:30:00'
const todate = '2020-12-31 15:30:01'
Expand All @@ -21,8 +21,8 @@ const todate = '2020-12-31 15:30:01'
}

const transloadit = new Transloadit({
authKey: process.env.TRANSLOADIT_KEY,
authSecret: process.env.TRANSLOADIT_SECRET,
authKey: /** @type {string} */ (process.env.TRANSLOADIT_KEY),
authSecret: /** @type {string} */ (process.env.TRANSLOADIT_SECRET),
})

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

const { bytes_usage: bytesUsage } = assemblyFull
Expand Down
14 changes: 8 additions & 6 deletions examples/rasterize_svg_to_png.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
// Run this file as:
//
// env TRANSLOADIT_KEY=xxx TRANSLOADIT_SECRET=yyy node rasterize_svg_to_png.js ./fixtures/circle.svg
// env TRANSLOADIT_KEY=xxx TRANSLOADIT_SECRET=yyy node rasterize_svg_to_png.js ./examples/fixtures/circle.svg
//
// You'll likely just want to `require('transloadit')`, but we're requiring the local
// variant here for easier testing:
const Transloadit = require('../src/Transloadit')
// You may need to build the project first using:
//
// yarn prepack
//
const { Transloadit } = require('transloadit')

const transloadit = new Transloadit({
authKey: process.env.TRANSLOADIT_KEY,
authSecret: process.env.TRANSLOADIT_SECRET,
authKey: /** @type {string} */ (process.env.TRANSLOADIT_KEY),
authSecret: /** @type {string} */ (process.env.TRANSLOADIT_SECRET),
})

const filePath = process.argv[2]
Expand Down
14 changes: 8 additions & 6 deletions examples/resize_an_image.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
// Run this file as:
//
// env TRANSLOADIT_KEY=xxx TRANSLOADIT_SECRET=yyy node resize_an_image.js ./fixtures/berkley.jpg
// env TRANSLOADIT_KEY=xxx TRANSLOADIT_SECRET=yyy node resize_an_image.js ./examples/fixtures/berkley.jpg
//
// You'll likely just want to `require('transloadit')`, but we're requiring the local
// variant here for easier testing:
const Transloadit = require('../src/Transloadit')
// You may need to build the project first using:
//
// yarn prepack
//
const { Transloadit } = require('transloadit')

const transloadit = new Transloadit({
authKey: process.env.TRANSLOADIT_KEY,
authSecret: process.env.TRANSLOADIT_SECRET,
authKey: /** @type {string} */ (process.env.TRANSLOADIT_KEY),
authSecret: /** @type {string} */ (process.env.TRANSLOADIT_SECRET),
})

const filePath = process.argv[2]
Expand Down
17 changes: 8 additions & 9 deletions examples/retry.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,16 @@
//
// env TRANSLOADIT_KEY=xxx TRANSLOADIT_SECRET=yyy node retry.js
//

// eslint-disable-next-line import/no-extraneous-dependencies
// You may need to build the project first using:
//
// yarn prepack
//
const pRetry = require('p-retry')

// You'll likely just want to `require('transloadit')`, but we're requiring the local
// variant here for easier testing:
const Transloadit = require('../src/Transloadit')
const { Transloadit, TransloaditError } = require('transloadit')

const transloadit = new Transloadit({
authKey: process.env.TRANSLOADIT_KEY,
authSecret: process.env.TRANSLOADIT_SECRET,
authKey: /** @type {string} */ (process.env.TRANSLOADIT_KEY),
authSecret: /** @type {string} */ (process.env.TRANSLOADIT_SECRET),
})

async function run() {
Expand All @@ -23,7 +22,7 @@ async function run() {
const { items } = await transloadit.listTemplates({ sort: 'created', order: 'asc' })
return items
} catch (err) {
if (err.transloaditErrorCode === 'INVALID_SIGNATURE') {
if (err instanceof TransloaditError && err.transloaditErrorCode === 'INVALID_SIGNATURE') {
// This is an unrecoverable error, abort retry
throw new pRetry.AbortError('INVALID_SIGNATURE')
}
Expand Down
12 changes: 7 additions & 5 deletions examples/template_api.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@
//
// env TRANSLOADIT_KEY=xxx TRANSLOADIT_SECRET=yyy node template_api.js
//
// You'll likely just want to `require('transloadit')`, but we're requiring the local
// variant here for easier testing:
const Transloadit = require('../src/Transloadit')
// You may need to build the project first using:
//
// yarn prepack
//
const { Transloadit } = require('transloadit')

const transloadit = new Transloadit({
authKey: process.env.TRANSLOADIT_KEY,
authSecret: process.env.TRANSLOADIT_SECRET,
authKey: /** @type {string} */ (process.env.TRANSLOADIT_KEY),
authSecret: /** @type {string} */ (process.env.TRANSLOADIT_SECRET),
})

const template = {
Expand Down
2 changes: 1 addition & 1 deletion src/Transloadit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export {
MaxRedirectsError,
TimeoutError,
} from 'got'
export { InconsistentResponseError }
export { InconsistentResponseError, TransloaditError }

const log = debug('transloadit')
const logWarn = debug('transloadit:warn')
Expand Down
2 changes: 1 addition & 1 deletion test/generate-coverage-badge.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const { makeBadge } = require('badge-maker')
// eslint-disable-next-line import/newline-after-import
;(async () => {
try {
const json = JSON.parse(await fs.readFile(process.argv[2]), 'utf-8')
const json = JSON.parse(await fs.readFile(process.argv[2], 'utf-8'))

// We only care about "statements"
const coveragePercent = `${json.total.statements.pct}%`
Expand Down
3 changes: 2 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
{
"exclude": ["src"],
"exclude": ["dist", "src"],
"references": [{ "path": "./tsconfig.build.json" }],
"compilerOptions": {
"checkJs": true,
"isolatedModules": true,
"module": "node16",
"noImplicitOverride": true,
Expand Down

0 comments on commit 9bf67d4

Please sign in to comment.