cointickerapi
v1.1.7
Published
A dynamic pricing && currency conversion package with React component and API client
Maintainers
Readme
cointickerapi
A small npm package that provides a currency conversion API client and a React component CoinTickerComponent for displaying conversion results.
This repository contains both the TypeScript source and a built distribution in dist/. The package is intended to be consumed by JavaScript and TypeScript projects (including Create React App, Next.js, Vite, etc.).
Table of contents
- Installation
- From npm (published)
- Locally (pack / link)
- Usage
- JavaScript
- TypeScript
- React component
- API Reference
configureCoinTicker(config)createTickerClient()/CoinTickerclassCoinTickerComponentprops
- Build & Packaging
- Troubleshooting
- "Could not find a declaration file for module 'cointickerapi'"
- Multiple React copies / Invalid hook call
- Local install file/tarball issues
- Development
- License
Installation
From npm (when published):
npm install cointickerapi
# or
yarn add cointickerapiLocally (during development) you can either npm pack and install the generated tarball or use npm link:
# In package root (this repo)
npm run build
npm pack
# creates cointickerapi-<version>.tgz
# In your app (test-app or nextjs app)
npm install ../cointickerapi-<version>.tgz
# OR use link
cd /path/to/cointickerapi
npm link
cd /path/to/your-app
npm link cointickerapiNote: On Windows, prefer using Command Prompt (cmd.exe) for npm pack/install operations if you face path or PowerShell execution policy issues.
Usage
JavaScript (simple)
import { createTickerClient, configureCoinTicker } from 'cointickerapi';
configureCoinTicker({ apiKey: 'YOUR_API_KEY' });
const client = createTickerClient();
client.getTicker('USD', 100).then(console.log).catch(console.error);TypeScript
import { createTickerClient, configureCoinTicker } from 'cointickerapi';
import type { TickerResponse } from 'cointickerapi';
configureCoinTicker({ apiKey: 'YOUR_API_KEY' });
const client = createTickerClient();
const resp: TickerResponse = await client.getTicker('USD', 100);React component
import React from 'react';
import { CoinTickerComponent, configureCoinTicker } from 'cointickerapi';
configureCoinTicker({ apiKey: 'YOUR_API_KEY' });
function App() {
return (
<CoinTickerComponent
currency="USD"
amount={100}
onSuccess={(data) => console.log('Success', data)}
onError={(e) => console.error('Error', e)}
refreshInterval={30000}
/>
);
}CoinTickerComponent props (TypeScript):
currency: string- source currency (e.g. "USD", "EUR")amount: number- amount to convertonSuccess?: (data: TickerResponse) => void- callback on successonError?: (error: Error) => void- callback on errorrefreshInterval?: number- milliseconds between automatic refreshes (default depends on component)style?: React.CSSProperties- optional inline style
API Reference
configureCoinTicker(config: { apiKey: string; baseUrl?: string })- configure global API key and optional base URLcreateTickerClient(): CoinTicker- create a client instance (hasgetTicker(from, amount))CoinTickerclass - exposesgetTicker(from: string, amount: number)which returns the API response typed asTickerResponse
TickerResponse (shape may evolve - check src/types/index.ts):
export interface TickerResponse {
from: string;
to: string;
amount: number;
converted_amount: number;
exchange_rates?: Record<string, number>;
}Build & Packaging
The project uses Rollup to build both CommonJS and ESM bundles. The configured outputs are:
dist/index.js(CommonJS)dist/index.esm.js(ESM)dist/index.d.ts(TypeScript declarations)
Scripts:
npm run build- run rollup buildnpm pack- packages the project into a tarball for local installnpm link- link for local dev
If you publish to npm, ensure types in package.json points to dist/index.d.ts.
Troubleshooting
"Could not find a declaration file for module 'cointickerapi'"
This happens in TypeScript consumers when TypeScript can't find the .d.ts file for the package. To resolve:
- Build the package so
dist/index.d.tsexists:
cd /path/to/cointickerapi
npm run build
npm pack- Install the packed tarball into your Next.js / TypeScript app:
cd /path/to/your-next-app
npm install ../cointickerapi-<version>.tgz- Ensure your
package.jsonincludes thetypesfield at the root of the package and thedist/index.d.tsis included in the tarball. Example in the packagepackage.json:
{
"main": "dist/index.js",
"module": "dist/index.esm.js",
"types": "dist/index.d.ts",
"files": ["dist"]
}If the package is already installed and you still get the warning in your editor, try restarting your editor/TypeScript server and run
npm iagain.As a fallback (not recommended long-term): declare the module in your Next.js project by adding a
declare module 'cointickerapi';type file (e.g.types/cointickerapi.d.ts):
// types/cointickerapi.d.ts
declare module 'cointickerapi';Then include "typeRoots": ["./types", "./node_modules/@types"] in your tsconfig.json if necessary.
Invalid hook call / Multiple React copies
Symptoms: "Invalid hook call. Hooks can only be called inside of the body of a function component." or errors about useState being undefined.
Root causes:
- More than one copy of React in the consumer app and the package. To fix:
- Mark
reactandreact-domaspeerDependenciesin the package'spackage.json(they should not be bundled). Example:
- Mark
"peerDependencies": {
"react": "^17.0.0 || ^18.0.0",
"react-dom": "^17.0.0 || ^18.0.0"
}- Ensure Rollup externalizes
reactandreact-domso they are not bundled into the output. Inrollup.config.js:
external: ['react', 'react-dom', ...Object.keys(packageJson.peerDependencies || {})]- Rebuild the package and reinstall it in the app.
Problems installing local tarball (corrupted file / ENOENT)
- Ensure
npm packcompleted successfully and the.tgzfile exists in the package root. Usedirorlsto confirm. - Use absolute or cmd-friendly paths on Windows when installing with
npm install. - If PowerShell causes issues, use
cmd.exeto run the install.
Development
- Source files are in
src/. - The component is in
src/components/CoinTickerComponent.tsx. - Types are in
src/types/index.ts.
Suggested workflow for making changes and testing in test-app:
# in package root
npm run build
npm pack
# in test-app
npm uninstall cointickerapi
npm install ../cointickerapi-<version>.tgz
npm startOr use npm link:
cd /path/to/cointickerapi
npm link
cd /path/to/test-app
npm link cointickerapi
npm startLicense
MIT
