{"id":42028050,"url":"https://github.com/counterapi/counter.js","last_synced_at":"2026-01-26T04:12:32.445Z","repository":{"id":45220127,"uuid":"352041034","full_name":"counterapi/counter.js","owner":"counterapi","description":"Counter API Javascript Library","archived":false,"fork":false,"pushed_at":"2026-01-19T16:05:47.000Z","size":1056,"stargazers_count":10,"open_issues_count":10,"forks_count":2,"subscribers_count":0,"default_branch":"main","last_synced_at":"2026-01-19T22:37:58.182Z","etag":null,"topics":["api","counter","counterapi","typescript"],"latest_commit_sha":null,"homepage":"https://counterapi.dev","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/counterapi.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null,"notice":null,"maintainers":null,"copyright":null,"agents":null,"dco":null,"cla":null}},"created_at":"2021-03-27T10:15:07.000Z","updated_at":"2026-01-19T06:47:07.000Z","dependencies_parsed_at":"2025-12-29T08:00:48.729Z","dependency_job_id":null,"html_url":"https://github.com/counterapi/counter.js","commit_stats":null,"previous_names":["counterapi/counter.js","counterapi/counter-js"],"tags_count":500,"template":false,"template_full_name":null,"purl":"pkg:github/counterapi/counter.js","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/counterapi%2Fcounter.js","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/counterapi%2Fcounter.js/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/counterapi%2Fcounter.js/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/counterapi%2Fcounter.js/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/counterapi","download_url":"https://codeload.github.com/counterapi/counter.js/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/counterapi%2Fcounter.js/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":28766595,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-01-26T03:54:34.369Z","status":"ssl_error","status_checked_at":"2026-01-26T03:54:33.031Z","response_time":59,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.5:443 state=error: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["api","counter","counterapi","typescript"],"created_at":"2026-01-26T04:12:31.869Z","updated_at":"2026-01-26T04:12:32.434Z","avatar_url":"https://github.com/counterapi.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# CounterAPI JavaScript Client\n\nA lightweight, universal JavaScript client for [CounterAPI](https://counterapi.dev) - a simple API for tracking and managing counters.\n\n## Features\n\n- Universal JavaScript library (Node.js, browser, ESM)\n- Support for both v1 and v2 CounterAPI endpoints\n- Promise-based API\n- TypeScript support\n- Custom error handling\n- Debugging mode\n\n## Installation\n\n```bash\nnpm install counterapi\n```\n\n## Usage\n\n### Import\n\n#### ES Modules (recommended)\n\n```js\nimport { Counter } from 'counterapi';\n```\n\n#### CommonJS\n\n```js\nconst { Counter } = require('counterapi');\n```\n\n#### Browser via CDN\n\n```html\n\u003cscript src=\"https://cdn.jsdelivr.net/npm/counterapi/dist/counter.browser.min.js\"\u003e\u003c/script\u003e\n\u003cscript\u003e\n  // Counter is available as a global variable\n  const counter = new Counter({ workspace: 'my-workspace' });\n\u003c/script\u003e\n```\n\n### Creating a Client\n\n```js\n// For v1 API\nconst counterV1 = new Counter({\n  version: 'v1',       // Use v1 API\n  namespace: 'my-app', // Your namespace\n  debug: false,        // Optional: Enable debug logging\n  timeout: 5000,       // Optional: Request timeout in ms (default: 10000)\n});\n\n// For v2 API (default)\nconst counterV2 = new Counter({\n  workspace: 'my-workspace', // Your workspace name\n  debug: false,              // Optional: Enable debug logging\n  timeout: 5000,             // Optional: Request timeout in ms (default: 10000)\n  accessToken: 'your-token'  // Optional: Authentication token for API requests\n});\n```\n\n### API Methods\n\n#### Get Counter\n\n```js\n// Get the current value of a counter\nconst counter = await counterClient.get('page-views');\nconsole.log(`Current count: ${counter.value}`);\n```\n\n#### Increment Counter\n\n```js\n// Increment a counter by 1\nconst counter = await counterClient.up('page-views');\nconsole.log(`New count after increment: ${counter.value}`);\nconsole.log(`Up count: ${counter.data.up_count}, Down count: ${counter.data.down_count}`);\n```\n\n#### Decrement Counter\n\n```js\n// Decrement a counter by 1\nconst counter = await counterClient.down('page-views');\nconsole.log(`New count after decrement: ${counter.value}`);\nconsole.log(`Up count: ${counter.data.up_count}, Down count: ${counter.data.down_count}`);\n```\n\n#### Set Counter Value (V1 API only)\n\n```js\n// Set a counter to a specific value\nconst counter = await counterV1.set('page-views', 100);\nconsole.log(`Counter set to: ${counter.value}`);\n```\n\n#### Reset Counter (V2 API only)\n\n```js\n// Reset a counter to 0\nconst counter = await counterV2.reset('page-views');\nconsole.log(`Counter reset to: ${counter.value}`);\n```\n\n#### Get Counter Stats (V2 API only)\n\n```js\n// Get statistics for a counter\nconst result = await counterV2.stats('page-views');\nconsole.log(`Up count: ${result.data.up_count}`);\nconsole.log(`Down count: ${result.data.down_count}`);\nconsole.log(`Today's up count: ${result.data.stats.today.up}`);\nconsole.log(`This week's down count: ${result.data.stats.this_week.down}`);\nconsole.log(`Most active hour: ${getMostActiveHour(result.data.stats.temporal.hours)}`);\n```\n\n### Response Types\n\n#### Standard Counter Response\n\nBasic counter operations return a response with this structure:\n\n```js\n{\n  code: \"200\",\n  data: {\n    created_at: \"2025-06-17T11:33:23Z\",\n    description: \"\",\n    down_count: 4,\n    id: 1,\n    name: \"test\",\n    slug: \"test\",\n    team_id: 4,\n    up_count: 4,\n    updated_at: \"2025-06-17T11:33:23Z\",\n    user_id: 7,\n    workspace_id: 1,\n    workspace_slug: \"test\"\n  }\n}\n```\n\n#### Stats Response (V2 API)\n\nThe `stats()` method returns a comprehensive statistics object:\n\n```js\n{\n  code: \"200\",\n  data: {\n    id: 1,\n    counter_id: 1,\n    up_count: 6,\n    down_count: 4,\n    stats: {\n      today: {\n        up: 6,\n        down: 4\n      },\n      this_week: {\n        up: 6,\n        down: 4\n      },\n      temporal: {\n        hours: {\n          // Hourly breakdown (00-23)\n          \"07\": { up: 6, down: 4 },\n          // ... other hours\n        },\n        weekdays: {\n          // Day of week breakdown\n          \"monday\": { up: 0, down: 0 },\n          \"tuesday\": { up: 0, down: 0 },\n          \"wednesday\": { up: 6, down: 4 },\n          // ... other days\n        },\n        quarters: {\n          // Quarterly breakdown\n          \"q1\": { up: 0, down: 0 },\n          \"q2\": { up: 6, down: 4 },\n          \"q3\": { up: 0, down: 0 },\n          \"q4\": { up: 0, down: 0 }\n        }\n      }\n    },\n    created_at: \"2025-06-17T11:33:23Z\",\n    updated_at: \"2025-06-18T07:44:11Z\"\n  },\n  message: \"Counter stats retrieved successfully\"\n}\n```\n\n## Error Handling\n\nUse standard Promise error handling:\n\n```js\ntry {\n  const counter = await counterClient.up('page-views');\n} catch (error) {\n  console.error('Error:', error.message);\n  console.error('Status:', error.status);\n  console.error('Code:', error.code);\n}\n```\n\n## Examples\n\nWe provide ready-to-use examples to help you get started with CounterAPI:\n\n### Browser Example\n\nThe [browser example](./examples/browser) demonstrates how to use CounterAPI in a web application:\n\n- Interactive UI for counter operations\n- Display of up and down counts\n- Visualization of counter statistics\n- Real-time API responses\n\nTo run the browser example:\n\n```bash\n# Build the library first\nnpm run build\n\n# Serve the example using a web server\ncd examples/browser\npython -m http.server 8000\n# Then open http://localhost:8000 in your browser\n```\n\n### Node.js Example\n\nThe [Node.js example](./examples/node) shows how to use CounterAPI in a server-side application:\n\n- Simple counter retrieval\n- Using ESM imports\n- Error handling\n\nTo run the Node.js example:\n\n```bash\n# Build the library first\nnpm run build\n\n# Run the example\ncd examples/node\nnode index.js\n```\n\n### Visualizing Stats\n\nYou can use the stats data to create visualizations:\n\n```js\n// Example: Creating a simple chart from hourly stats\nfunction createHourlyChart(stats) {\n  const hours = stats.data.stats.temporal.hours;\n  const labels = Object.keys(hours).sort();\n  const upData = labels.map(hour =\u003e hours[hour].up);\n  const downData = labels.map(hour =\u003e hours[hour].down);\n  \n  // Use your preferred charting library\n  renderChart({\n    labels,\n    datasets: [\n      { label: 'Up', data: upData },\n      { label: 'Down', data: downData }\n    ]\n  });\n}\n\n// Example usage\nconst stats = await counter.stats('my-counter');\ncreateHourlyChart(stats);\n```\n\n## License\n\nMIT\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcounterapi%2Fcounter.js","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcounterapi%2Fcounter.js","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcounterapi%2Fcounter.js/lists"}