Skip to content

Commit

Permalink
feat: throw error for legacy dynamic require users (#794)
Browse files Browse the repository at this point in the history
* feat: throw error for legacy dynamic `require` users

* chore: whitespace

* fix: add to files array

* refactor: only use input if it's a string

* chore: consistent script naming

* refactor: gitignore declaration file, add to postbuild

* chore: add @ts-check directive
  • Loading branch information
kanadgupta authored Oct 30, 2023
1 parent 7a8b0e2 commit 1c66a59
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 3 deletions.
1 change: 1 addition & 0 deletions packages/api/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
coverage/
dist/
node_modules/
legacy-require-handler.d.cts
29 changes: 29 additions & 0 deletions packages/api/legacy-require-handler.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// @ts-check
/* eslint-disable import/no-commonjs */

/**
* In `api` v6, this package was a JS library used JavaScript Proxies to dynamically generate SDKs at runtime.
* This functionality has been sunset in `api` v7 to focus on its CLI, which builds strongly-typed SDKs.
* This file is here to throw errors for users that attempt to use the legacy library.
*
*
* @param {unknown} input
*/
module.exports = function legacyHandler(input) {
let identifier = 'https://raw.githubusercontent.com/OAI/OpenAPI-Specification/main/examples/v3.0/petstore.json';

if (typeof input === 'string') {
identifier = input;
}

throw new Error(`Oops! You're attempting to use a legacy usage pattern that no longer exists.
You can read more about this in our docs: TKTK
\`api\` can now generate strongly-typed SDKs that work in pretty much every JS runtime!
Try running this command to get started:
npx api@latest install ${identifier}
`);
};
10 changes: 7 additions & 3 deletions packages/api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,20 @@
"bin": {
"api": "bin/api.js"
},
"main": "legacy-require-handler.cjs",
"scripts": {
"attw": "attw --pack --format table-flipped",
"build": "tsc",
"build:versionedfiles": "NODE_OPTIONS=--no-warnings node --loader ts-node/esm bin/buildVersionedFiles.ts",
"build:legacy-handler": "tsc legacy-require-handler.cjs --checkJs --declaration --emitDeclarationOnly",
"build:versioned-files": "NODE_OPTIONS=--no-warnings node --loader ts-node/esm bin/buildVersionedFiles.ts",
"debug:bin": "NODE_OPTIONS=--no-warnings node --loader ts-node/esm src/bin.ts",
"lint:types": "tsc --noEmit",
"prebuild": "rm -rf dist/ && npm run build:versionedfiles",
"prebuild": "rm -rf dist/ && npm run build:versioned-files",
"postbuild": "npm run build:legacy-handler",
"prepack": "npm run build",
"test": "vitest run --coverage",
"test:smoke": "vitest --config=vitest-smoketest.config.ts",
"version": "npm run build:versionedfiles && git add schema.json src/packageInfo.ts"
"version": "npm run build:versioned-files && git add schema.json src/packageInfo.ts"
},
"repository": {
"type": "git",
Expand All @@ -34,6 +37,7 @@
},
"files": [
"dist",
"legacy-require-handler.**",
"schema.json"
],
"keywords": [
Expand Down

0 comments on commit 1c66a59

Please sign in to comment.