@gofreego/tsutils 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +253 -0
- package/dist/index.d.mts +555 -0
- package/dist/index.d.ts +555 -0
- package/dist/index.js +1178 -0
- package/dist/index.js.map +1 -0
- package/dist/index.mjs +1153 -0
- package/dist/index.mjs.map +1 -0
- package/package.json +80 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 gofreego
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,253 @@
|
|
|
1
|
+
# @gofreego/tsutils
|
|
2
|
+
|
|
3
|
+
A comprehensive React + TypeScript library providing common utilities, components, theme system, and HTTP client for your projects.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install @gofreego/tsutils
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
or
|
|
12
|
+
|
|
13
|
+
```bash
|
|
14
|
+
yarn add @gofreego/tsutils
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
### Optional: Material UI (for ThemeToggle component)
|
|
18
|
+
|
|
19
|
+
If you want to use the `ThemeToggle` component, install Material UI:
|
|
20
|
+
|
|
21
|
+
```bash
|
|
22
|
+
npm install @mui/material @mui/icons-material @emotion/react @emotion/styled
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
## Features
|
|
26
|
+
|
|
27
|
+
- 🎨 **Theme System** - Customizable theme provider with light/dark mode and localStorage persistence
|
|
28
|
+
- 🔘 **Theme Toggle** - Material UI round button for theme switching
|
|
29
|
+
- 💾 **LocalStorage Utility** - Type-safe localStorage wrapper with error handling
|
|
30
|
+
- 🔧 **Utilities** - Common utility functions (debounce, throttle, formatDate, etc.)
|
|
31
|
+
- 🌐 **HTTP Client** - Type-safe HTTP client with timeout and error handling
|
|
32
|
+
- 🧩 **Components** - Pre-built React components
|
|
33
|
+
- 📦 **Tree-shakeable** - Only import what you need
|
|
34
|
+
- 🎯 **TypeScript** - Full type safety out of the box
|
|
35
|
+
|
|
36
|
+
## Usage
|
|
37
|
+
|
|
38
|
+
### Theme System
|
|
39
|
+
|
|
40
|
+
```tsx
|
|
41
|
+
import { ThemeProvider, useTheme, ThemeToggle, lightTheme, darkTheme } from '@gofreego/tsutils'
|
|
42
|
+
|
|
43
|
+
function App() {
|
|
44
|
+
return (
|
|
45
|
+
<ThemeProvider initialMode="light">
|
|
46
|
+
<YourApp />
|
|
47
|
+
</ThemeProvider>
|
|
48
|
+
)
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
function YourComponent() {
|
|
52
|
+
const { theme, themeMode, setThemeMode, toggleTheme } = useTheme()
|
|
53
|
+
|
|
54
|
+
return (
|
|
55
|
+
<div style={{
|
|
56
|
+
backgroundColor: theme.colors.background,
|
|
57
|
+
color: theme.colors.text
|
|
58
|
+
}}>
|
|
59
|
+
<p>Current theme: {themeMode}</p>
|
|
60
|
+
|
|
61
|
+
{/* Material UI Theme Toggle Button */}
|
|
62
|
+
<ThemeToggle />
|
|
63
|
+
|
|
64
|
+
{/* Toggle between light and dark */}
|
|
65
|
+
<button onClick={toggleTheme}>
|
|
66
|
+
Toggle Theme
|
|
67
|
+
</button>
|
|
68
|
+
|
|
69
|
+
{/* Set specific theme */}
|
|
70
|
+
<button onClick={() => setThemeMode('dark')}>
|
|
71
|
+
Dark Mode
|
|
72
|
+
</button>
|
|
73
|
+
<button onClick={() => setThemeMode('light')}>
|
|
74
|
+
Light Mode
|
|
75
|
+
</button>
|
|
76
|
+
</div>
|
|
77
|
+
)
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
// Theme persists automatically in localStorage
|
|
81
|
+
// Works across different apps on the same domain
|
|
82
|
+
function CustomThemedApp() {
|
|
83
|
+
return (
|
|
84
|
+
<ThemeProvider initialMode="dark" storageKey="my-app-theme">
|
|
85
|
+
<YourApp />
|
|
86
|
+
</ThemeProvider>
|
|
87
|
+
)
|
|
88
|
+
}
|
|
89
|
+
```
|
|
90
|
+
|
|
91
|
+
### HTTP Client
|
|
92
|
+
|
|
93
|
+
```tsx
|
|
94
|
+
import { HttpClient } from '@gofreego/tsutils'
|
|
95
|
+
|
|
96
|
+
const client = new HttpClient({
|
|
97
|
+
baseURL: 'https://api.example.com',
|
|
98
|
+
timeout: 5000,
|
|
99
|
+
headers: {
|
|
100
|
+
'Authorization': 'Bearer token'
|
|
101
|
+
}
|
|
102
|
+
})
|
|
103
|
+
|
|
104
|
+
// GET request
|
|
105
|
+
const response = await client.get('/users')
|
|
106
|
+
|
|
107
|
+
// POST request
|
|
108
|
+
const newUser = await client.post('/users', {
|
|
109
|
+
name: 'John Doe',
|
|
110
|
+
email: 'john@example.com'
|
|
111
|
+
})
|
|
112
|
+
```
|
|
113
|
+
|
|
114
|
+
### Utilities
|
|
115
|
+
|
|
116
|
+
```tsx
|
|
117
|
+
import { debounce, throttle, formatDate, cn, LocalStorage } from '@gofreego/tsutils'
|
|
118
|
+
|
|
119
|
+
// Debounce function
|
|
120
|
+
const debouncedSearch = debounce((query: string) => {
|
|
121
|
+
console.log('Searching for:', query)
|
|
122
|
+
}, 300)
|
|
123
|
+
|
|
124
|
+
// Throttle function
|
|
125
|
+
const throttledScroll = throttle(() => {
|
|
126
|
+
console.log('Scrolling...')
|
|
127
|
+
}, 100)
|
|
128
|
+
|
|
129
|
+
// Format date
|
|
130
|
+
const formatted = formatDate(new Date(), {
|
|
131
|
+
year: 'numeric',
|
|
132
|
+
month: 'short',
|
|
133
|
+
day: 'numeric'
|
|
134
|
+
})
|
|
135
|
+
|
|
136
|
+
// Combine class names
|
|
137
|
+
const className = cn('base-class', condition && 'conditional-class', 'another-class')
|
|
138
|
+
|
|
139
|
+
// LocalStorage utility
|
|
140
|
+
// Save data
|
|
141
|
+
LocalStorage.setItem('user', { name: 'John', id: 123 })
|
|
142
|
+
LocalStorage.setItem('theme', 'dark')
|
|
143
|
+
|
|
144
|
+
// Get data
|
|
145
|
+
const user = LocalStorage.getItem<{ name: string; id: number }>('user')
|
|
146
|
+
const theme = LocalStorage.getItem<string>('theme')
|
|
147
|
+
|
|
148
|
+
// Check if key exists
|
|
149
|
+
if (LocalStorage.hasItem('user')) {
|
|
150
|
+
console.log('User data exists')
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
// Remove item
|
|
154
|
+
LocalStorage.removeItem('theme')
|
|
155
|
+
|
|
156
|
+
// Get all keys
|
|
157
|
+
const keys = LocalStorage.keys()
|
|
158
|
+
|
|
159
|
+
// Clear all
|
|
160
|
+
LocalStorage.clear()
|
|
161
|
+
```
|
|
162
|
+
|
|
163
|
+
### Components
|
|
164
|
+
|
|
165
|
+
```tsx
|
|
166
|
+
import { Button } from '@gofreego/tsutils'
|
|
167
|
+
|
|
168
|
+
function Example() {
|
|
169
|
+
return (
|
|
170
|
+
<>
|
|
171
|
+
<Button variant="primary" size="md" onClick={() => alert('Clicked!')}>
|
|
172
|
+
Primary Button
|
|
173
|
+
</Button>
|
|
174
|
+
|
|
175
|
+
<Button variant="outline" size="lg">
|
|
176
|
+
Outline Button
|
|
177
|
+
</Button>
|
|
178
|
+
</>
|
|
179
|
+
)
|
|
180
|
+
}
|
|
181
|
+
```
|
|
182
|
+
|
|
183
|
+
## API Reference
|
|
184
|
+
|
|
185
|
+
### Theme
|
|
186
|
+
|
|
187
|
+
- `ThemeProvider` - Context provider for theme
|
|
188
|
+
- Props: `initialMode`, `initialTheme`, `storageKey`
|
|
189
|
+
- Automatically persists theme to localStorage
|
|
190
|
+
- `useTheme()` - Hook to access and update theme
|
|
191
|
+
- `theme` - Current theme object
|
|
192
|
+
- `themeMode` - Current theme mode ('light' | 'dark')
|
|
193
|
+
- `setTheme(theme)` - Set custom theme
|
|
194
|
+
- `setThemeMode(mode)` - Set theme mode
|
|
195
|
+
- `toggleTheme()` - Toggle between light and dark
|
|
196
|
+
- `ThemeToggle` - Material UI round button component for theme switching
|
|
197
|
+
- Props: `lightModeTooltip`, `darkModeTooltip`, `showTooltip`
|
|
198
|
+
- Automatically updates localStorage
|
|
199
|
+
- `lightTheme` - Predefined light theme
|
|
200
|
+
- `darkTheme` - Predefined dark theme
|
|
201
|
+
- `defaultTheme` - Default theme (alias for lightTheme)
|
|
202
|
+
|
|
203
|
+
### HTTP Client
|
|
204
|
+
|
|
205
|
+
- `HttpClient` - HTTP client class with methods: `get()`, `post()`, `put()`, `patch()`, `delete()`
|
|
206
|
+
|
|
207
|
+
### Utilities
|
|
208
|
+
|
|
209
|
+
- `debounce(func, wait)` - Debounce function calls
|
|
210
|
+
- `throttle(func, wait)` - Throttle function calls
|
|
211
|
+
- `formatDate(date, options)` - Format dates
|
|
212
|
+
- `cn(...classes)` - Combine class names
|
|
213
|
+
- `LocalStorage` - Safe localStorage wrapper with TypeScript support
|
|
214
|
+
- `getItem<T>(key)` - Get item from localStorage
|
|
215
|
+
- `setItem<T>(key, value)` - Set item in localStorage
|
|
216
|
+
- `removeItem(key)` - Remove item from localStorage
|
|
217
|
+
- `hasItem(key)` - Check if key exists
|
|
218
|
+
- `keys()` - Get all keys
|
|
219
|
+
- `clear()` - Clear all items
|
|
220
|
+
|
|
221
|
+
## Development
|
|
222
|
+
|
|
223
|
+
```bash
|
|
224
|
+
# Install dependencies
|
|
225
|
+
npm install
|
|
226
|
+
|
|
227
|
+
# Build the library
|
|
228
|
+
npm run build
|
|
229
|
+
|
|
230
|
+
# Watch mode for development
|
|
231
|
+
npm run dev
|
|
232
|
+
|
|
233
|
+
# Type check
|
|
234
|
+
npm run typecheck
|
|
235
|
+
```
|
|
236
|
+
|
|
237
|
+
## Publishing
|
|
238
|
+
|
|
239
|
+
```bash
|
|
240
|
+
# Login to npm
|
|
241
|
+
npm login
|
|
242
|
+
|
|
243
|
+
# Publish to npm
|
|
244
|
+
npm publish --access public
|
|
245
|
+
```
|
|
246
|
+
|
|
247
|
+
## License
|
|
248
|
+
|
|
249
|
+
MIT
|
|
250
|
+
|
|
251
|
+
## Author
|
|
252
|
+
|
|
253
|
+
gofreego
|