From 9bf67d462a2c1355adb3930391ae406965f146fd Mon Sep 17 00:00:00 2001 From: Remco Haszing Date: Mon, 9 Dec 2024 15:48:56 +0100 Subject: [PATCH] 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 --- examples/.eslintrc.js | 2 ++ examples/convert_to_webp.js | 14 ++++++----- examples/credentials.js | 23 ++++++++++--------- examples/face_detect_download.js | 17 +++++++------- ...ch_costs_of_all_assemblies_in_timeframe.js | 16 ++++++------- examples/rasterize_svg_to_png.js | 14 ++++++----- examples/resize_an_image.js | 14 ++++++----- examples/retry.js | 17 +++++++------- examples/template_api.js | 12 ++++++---- src/Transloadit.ts | 2 +- test/generate-coverage-badge.js | 2 +- tsconfig.json | 3 ++- 12 files changed, 74 insertions(+), 62 deletions(-) diff --git a/examples/.eslintrc.js b/examples/.eslintrc.js index dd3a4b50..c359ca1d 100644 --- a/examples/.eslintrc.js +++ b/examples/.eslintrc.js @@ -1,5 +1,7 @@ module.exports = { rules: { 'no-console': 0, + 'import/no-extraneous-dependencies': 0, + 'import/no-unresolved': 0, }, } diff --git a/examples/convert_to_webp.js b/examples/convert_to_webp.js index 2843e868..e092273b 100644 --- a/examples/convert_to_webp.js +++ b/examples/convert_to_webp.js @@ -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] diff --git a/examples/credentials.js b/examples/credentials.js index 37030c66..20195f6e 100644 --- a/examples/credentials.js +++ b/examples/credentials.js @@ -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' @@ -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 diff --git a/examples/face_detect_download.js b/examples/face_detect_download.js index 97d6d57c..530f11a2 100644 --- a/examples/face_detect_download.js +++ b/examples/face_detect_download.js @@ -1,6 +1,10 @@ // 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 @@ -8,14 +12,11 @@ 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] @@ -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) diff --git a/examples/fetch_costs_of_all_assemblies_in_timeframe.js b/examples/fetch_costs_of_all_assemblies_in_timeframe.js index f7726a24..b425791d 100644 --- a/examples/fetch_costs_of_all_assemblies_in_timeframe.js +++ b/examples/fetch_costs_of_all_assemblies_in_timeframe.js @@ -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' @@ -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 @@ -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 diff --git a/examples/rasterize_svg_to_png.js b/examples/rasterize_svg_to_png.js index e21bfcd5..20386668 100644 --- a/examples/rasterize_svg_to_png.js +++ b/examples/rasterize_svg_to_png.js @@ -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] diff --git a/examples/resize_an_image.js b/examples/resize_an_image.js index 3ad14b29..6da45070 100644 --- a/examples/resize_an_image.js +++ b/examples/resize_an_image.js @@ -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] diff --git a/examples/retry.js b/examples/retry.js index d15905ea..d967c82e 100644 --- a/examples/retry.js +++ b/examples/retry.js @@ -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() { @@ -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') } diff --git a/examples/template_api.js b/examples/template_api.js index f00d7f07..0ff856bc 100644 --- a/examples/template_api.js +++ b/examples/template_api.js @@ -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 = { diff --git a/src/Transloadit.ts b/src/Transloadit.ts index 5fa82cb7..efd6a91a 100644 --- a/src/Transloadit.ts +++ b/src/Transloadit.ts @@ -28,7 +28,7 @@ export { MaxRedirectsError, TimeoutError, } from 'got' -export { InconsistentResponseError } +export { InconsistentResponseError, TransloaditError } const log = debug('transloadit') const logWarn = debug('transloadit:warn') diff --git a/test/generate-coverage-badge.js b/test/generate-coverage-badge.js index 592079d3..a981fa6c 100644 --- a/test/generate-coverage-badge.js +++ b/test/generate-coverage-badge.js @@ -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}%` diff --git a/tsconfig.json b/tsconfig.json index 7f03e031..de0ce382 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -1,7 +1,8 @@ { - "exclude": ["src"], + "exclude": ["dist", "src"], "references": [{ "path": "./tsconfig.build.json" }], "compilerOptions": { + "checkJs": true, "isolatedModules": true, "module": "node16", "noImplicitOverride": true,