Skip to content

Add TypeScript definitions #797

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

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open

Conversation

namoscato
Copy link

@namoscato namoscato commented May 7, 2025

Resolves #789

Summary

  • Add count.d.ts TypeScript definitions for public JavaScript API
  • Add README.npm.md, used as npm package's README during pack
  • Add reference to npm package in "Host count.js somewhere else" documentation

Testing

In an application codebase, run npm install with the full path to this repository, i.e.

[path/to/my-app] npm install path/to/goatcounter

Additionally, you can check to see what will by published by running npm pack:

[path/to/goatcounter] npm pack

Publishing

See Creating and publishing unscoped public packages for more information on how to publish this package to npm, i.e.

  1. create an npm account
  2. login to CLI
  3. publish package via npm publish

@@ -0,0 +1,30 @@
{
"name": "goatcounter",
"version": "4.0.0",
Copy link
Author

Choose a reason for hiding this comment

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

This intends to match these versions and could be bumped accordingly in the future.

| (GoatCounter.Settings &
GoatCounter.DataParameters &
GoatCounter.Methods)
| undefined;
Copy link
Author

Choose a reason for hiding this comment

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

undefined accounts for scenarios in which the async script might still be loading.

package.json Outdated
Comment on lines 13 to 14
"prepack": "cp README.md git.README.md && mv README.npm.md README.md",
"postpack": "mv README.md README.npm.md && mv git.README.md README.md"
Copy link
Author

Choose a reason for hiding this comment

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

This temporarily swaps the repository's README.md with README.npm.md during the npm packing process such that the npm package can use a different, slimmed down README.

@namoscato namoscato marked this pull request as ready for review May 7, 2025 19:07
@@ -7,6 +7,8 @@ inside `<script>` tags. You won’t get any new features or other updates, but t
To host `count.js` somewhere else just copy it from https://gc.zgo.at/count.js
and adjust the URL in `src`.

For convenience, the script is also published as an [npm package](https://www.npmjs.com/package/goatcounter) which can be copied into a public directory during your application's build process.
Copy link
Owner

Choose a reason for hiding this comment

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

I'm not sure if I understand what "can be copied into a public directory" means here?

Or to phrase it different: how would one use the npm package?

Copy link
Author

Choose a reason for hiding this comment

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

The count.js source code is included in the package in addition to the type definitions, so instead of manually copying the file (per the existing sentence above), an application's build process could copy it from ./node_modules/goatcounter/public/count.js into its public / webroot directory to serve locally.

Feel free to expand the suggested documentation update, or I could take another pass.

Copy link
Owner

Choose a reason for hiding this comment

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

How would you use the types? That also needs to be copied manually then I assume?

Copy link
Author

Choose a reason for hiding this comment

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

No, the package just has to be referenced in TSConfig types.

9b86465 expands documentation, but let me know if I can clarify anything else!

Copy link
Owner

Choose a reason for hiding this comment

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

Okay, I have it working locally by putting the count.d.ts in the public/ directory and adding /// <reference path="./count.d.ts" /> to count.js

I haven't looked at the whole npm story yet, but that seems the best way to get it working in a simple npm-less setups? At the very least it's useful for me testing/changing the count.d.ts file going forward.

Copy link
Author

Choose a reason for hiding this comment

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

Yeah, such triple-slash directives can be used for testing – they're just less common these days (as compared to TSConfig and package managers).

Copy link
Owner

Choose a reason for hiding this comment

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

Okay, thanks for the confirmation; appreciate it. There's tons of documentation on everything regarding JS/TS, with different ways of doing things, and not always so easy to see what the "right" or "wrong" way is.

I think all my questions are answered, but it will probably be a few weeks until I look at it in more detail because it's relatively time-consuming and I have some other things I want to do first.

* `null`.
*
* @see https://www.goatcounter.com/help/modify
*/
Copy link
Owner

Choose a reason for hiding this comment

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

Is using /* .. */ style comments required? Or will // comments also work?

Copy link
Author

Choose a reason for hiding this comment

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

Yeah, // is fine for single line comments, but this is conventional JSDoc (which is supported well in most IDEs).

namoscato added 3 commits May 23, 2025 21:31
* 'main' of github.com:namoscato/goatcounter:
  Update GeoDB
  Change default location of SQLite DB and ACME certs to goatcounter-data/
  Clarify "append not found" error in count.js
  Strip trailing slash from visitor counter
  Don't error out if email activation token is used more than once
  Update pt-BR translation
  Update dependencies and refspam
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.

TypeScript definitions
2 participants