Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Auto generate index.d.ts by typescript #238

Closed
wants to merge 9 commits into from
Closed

Conversation

m2rads
Copy link
Contributor

@m2rads m2rads commented Jan 2, 2025

This PR addresses issue #231

Changes

  • Update build:types script to generate types using tsc
  • Refactor index.ts to import expect
  • Update test files to reflect the new changes
  • Update readme file

Copy link

vercel bot commented Jan 2, 2025

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
shortest ✅ Ready (Inspect) Visit Preview 💬 Add feedback Jan 2, 2025 2:01am

Copy link
Contributor Author

@m2rads m2rads left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added self-review. I can further cleanup the types to improve DX further.

@@ -55,7 +55,7 @@ yarn shortest
headless: false,
baseUrl: 'http://localhost:3000',
testPattern: '**/*.test.ts',
anthropicKey: process.env.ANTHROPIC_API_KEY
anthropicKey: process.env.ANTHROPIC_API_KEY || '',
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is now necessary as typescript is considering env vars as string | undefined therefore we have to add an fallback empty string to silence typescript errors. As mentioned before we can have these managed internally without having users to declare these here.

@@ -72,7 +72,7 @@ You can also use callback functions to add additional assertions and other logic
execution in browser is completed.

```typescript
import { shortest } from '@antiwork/shortest';
import { shortest, expect } from '@antiwork/shortest';
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One of the main reasons why we created index.d.ts manually was to allow users to use jestExpect without having to import it. With the new changes this is necessary.

@@ -5,28 +5,27 @@
"type": "module",
"main": "./dist/index.js",
"module": "./dist/index.js",
"types": "./index.d.ts",
"types": "./dist/types/index.d.ts",
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

reading the autogenerated types instead of the file we created manually

],
"scripts": {
"build": "rm -rf dist && pnpm build:types && pnpm build:js && pnpm build:cli",
"prepare": "pnpm build",
"prepublishOnly": "pnpm build",
"postinstall": "node -e \"if (process.platform !== 'win32') { try { require('child_process').execSync('chmod +x dist/cli/bin.js') } catch (_) {} }\"",
"build:types": "tsc --emitDeclarationOnly --outDir dist/types && cp index.d.ts dist/",
"build:types": "tsc --emitDeclarationOnly --outDir dist/types",
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This script is in charge of generating the types.

@@ -227,6 +224,7 @@ export const test: TestAPI = Object.assign(
},
);

export const expect = jestExpect;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Making jestExpect available as an import in the test files

Comment on lines +16 to +25

declare global {
namespace NodeJS {
interface Global {
__shortest__: ShortestGlobals;
}
}
}

export {};
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Part of jest setup. we had it in index.d.ts before but now we declare it here. This will get autogenerated by typescript.

Comment on lines +7 to +10
anthropicKey: process.env.ANTHROPIC_API_KEY || "",
mailosaur: {
apiKey: process.env.MAILOSAUR_API_KEY,
serverId: process.env.MAILOSAUR_SERVER_ID,
apiKey: process.env.MAILOSAUR_API_KEY || "",
serverId: process.env.MAILOSAUR_SERVER_ID || "",
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Have to do this otherwise typescript throws an error.

@slavingia
Copy link
Contributor

If the expect changes are necessary, would be good to do in its own PR. cc @amk-dev

@slavingia
Copy link
Contributor

Would be good to resolve merge conflicts now that the other branch was merged. I personally don't think doing || "" is a great idea, it pollutes the README/config and looks quite ugly. I'm sure there's a more elegant solution. Importing expect isn't a big deal, especially since we don't really need expect since the shortest itself should know what to look for to pass. So it'll act like a smell if it's included at all.

@m2rads
Copy link
Contributor Author

m2rads commented Jan 2, 2025

I think for the env vars we can just have the user set them up in their .env file and we directly read them from there. I think Vercel AI SDK is like that.

For expect I had to update it in this PR otherwise it would have introduced a bug to the package. Since we already have expect implemented, if a user upgrade their shortest, their implementation would be broken.

If you don't think expect is necessary, I can delete the jestExpect implementation all together.

@slavingia
Copy link
Contributor

Makes sense to me to remove it.

@amk-dev
Copy link
Contributor

amk-dev commented Jan 2, 2025

I personally don't think doing || "" is a great idea, it pollutes the README/config and looks quite ugly. I'm sure there's a more elegant solution.

the merged PR should take care of this.

Importing expect isn't a big deal, especially since we don't really need expect since the shortest itself should know what to look for to pass. So it'll act like a smell if it's included at all.

the merged PR preserves the current behaviour. expect is globally available. didn't want to introduce a breaking change.

I don't belive there's any functional issues remaining after that PR was merged. now its any new changes you guys want to make. (eg: remove expect)


one major difference from this PR to the merged PR is the use of tsup. its a pretty standared tool used on many popular projects. has a lot of features, but we're not using any atm apart from that global expect. but if you guys want to remove it, that's also fine.

@amk-dev
Copy link
Contributor

amk-dev commented Jan 2, 2025

I think for the env vars we can just have the user set them up in their .env file and we directly read them from there. I think Vercel AI SDK is like that.

had a discussion in the last PR about this.

#231 (comment)

after my PR merge,
-> any field that can accept an env variable is optional. the code has a fallback to the env variable.

@m2rads
Copy link
Contributor Author

m2rads commented Jan 2, 2025

I think for the env vars we can just have the user set them up in their .env file and we directly read them from there. I think Vercel AI SDK is like that.

had a discussion in the last PR about this.

#231 (comment)

after my PR merge, -> any field that can accept an env variable is optional. the code has a fallback to the env variable.

Just saw your PR. This PR is not required anymore as your solves it in a better way.

@m2rads m2rads closed this Jan 2, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants