ks-smart-api
v1.1.3
Published
A small, dependency-light API helper built on top of Axios.
Readme
ks-smart-api
A small, dependency-light API helper built on top of Axios.
- Create a shared Axios client once (
createClient) - Make requests with optional Bearer auth (
request) - Store/clear an in-memory token (
setToken,clearToken) - Optional console logging (
setLogger)
Install
npm i ks-smart-apiQuick start
const { createClient, request, setToken, setLogger } = require('ks-smart-api');
createClient({ baseURL: 'https://api.example.com', timeout: 10000 });
setLogger(true); // optional
setToken('YOUR_JWT_OR_TOKEN'); // optional
async function run() {
const me = await request({ method: 'GET', url: '/me' });
console.log(me);
}
run().catch(console.error);API
createClient({ baseURL, timeout? })
Creates and stores an Axios client used by request.
- baseURL: string (required)
- timeout: number in ms (optional, default:
10000)
Returns the created Axios instance.
request({ method, url, data?, params? })
Performs a request using the created client.
- method: HTTP method string (e.g.
"GET","POST","PUT","DELETE") - url: string path or URL (commonly a path like
"/users") - data: request body (optional)
- params: query params object (optional)
Auth behavior:
- If a token is set via
setToken(token),requestsendsAuthorization: Bearer <token>.
Return value:
- Resolves with
response.data.
Errors:
- If the server responded, throws an object like
{ status, message } - Otherwise throws
{ status: 500, message }
setToken(token)
Stores an in-memory token used by request.
clearToken()
Clears the stored token.
setLogger(boolean)
Enable/disable console logging. When enabled, logs with a [API] prefix.
Notes
- Token storage is in-memory (not persisted). If you need persistence, store the token in your app and call
setToken()on startup. - You must call
createClient()beforerequest()or you’ll getError: API client not initialized.
