A comprehensive collection of TypeScript utilities for modern web development, focusing on API interactions, authentication, form handling, validation, and more. Designed to streamline development workflows with well-documented, type-safe functions.
npm install svarog
or
pnpm add svarog
import { handleApiResponse, isSuccessResponse } from 'svarog/api';
// Get user profile
async function fetchUserProfile(userId: string) {
const response = await fetch(`/api/users/${userId}`);
const result = await handleApiResponse<UserProfile>(response);
if (isSuccessResponse(result)) {
return result.data;
} else {
console.error(`Error ${result.status}: ${result.message}`);
return null;
}
}
import {
validateEmail,
validatePassword,
validateMinLength,
composeValidators
} from 'svarog/validation';
// Create a composite validator
const validateUsername = composeValidators(validateMinLength(3), (value) =>
!/^[a-zA-Z0-9_]+$/.test(value)
? 'Username can only contain letters, numbers, and underscores'
: null
);
// Use in form submit handler
function handleSubmit(event) {
const email = emailInput.value;
const password = passwordInput.value;
const emailError = validateEmail(email);
const passwordError = validatePassword(password);
if (emailError || passwordError) {
// Handle validation errors
return;
}
// Proceed with form submission
}
import { enhanceForm } from 'svarog/form';
// In your Svelte component
const enhance = enhanceForm({
messages: {
success: 'Your profile was updated successfully',
defaultError: 'Could not update profile'
},
isLoadingStore: loadingStore,
notificationStore: toastStore
});
// Use in your form
<form method="POST" use:enhance>
<!-- form fields -->
</form>
import { formatDate, formatDateTime, formatRelativeTime } from 'svarog/time';
// Format a date with custom options
formatDate(new Date(), {
year: true,
month: 'long',
day: true
}); // "March 6, 2025"
// Format relative time
formatRelativeTime(new Date(Date.now() - 3600000)); // "1 hour ago"
import { parseMarkdown, extractFrontmatter } from 'svarog/markdown';
// Parse markdown with custom options
const html = await parseMarkdown(markdownContent, {
gfm: true,
highlight: true,
toc: true,
headingAnchors: true
});
// Extract and use frontmatter
const [metadata, content] = extractFrontmatter(markdownWithFrontmatter);
import { getUserFromToken, requireAuth } from 'svarog/token';
// In a SvelteKit load function
export async function load({ event }) {
// Get authenticated user or null
const user = await getUserFromToken(event);
return {
user
};
}
// For protected routes
export async function load({ event }) {
// This will redirect to login if not authenticated
const user = await requireAuth(event);
return {
user
};
}
<script>
import SEO from 'svarog/seo';
</script>
<SEO
title="Dashboard | Svarog"
description="View your application data and analytics."
keywords="dashboard, analytics, data visualization"
/>
With advanced configuration:
<SEO
title="Project Management | Svarog"
description="Manage your projects and track progress efficiently."
canonical="https://example.com/projects"
twitterSite="svarogapp"
og={{
type: 'article',
locale: 'en_US'
}}
schema={{
type: 'WebPage',
publishDate: '2025-01-15',
authorName: 'Your Organization',
authorUrl: 'https://example.com'
}}
/>
Create reusable SEO configurations:
import { createSEOConfig } from 'svarog/seo';
// In a shared config file
export const seoConfig = createSEOConfig({
siteName: 'Svarog',
baseUrl: 'https://example.com',
defaultImage: 'https://example.com/images/og.png'
});
// In a page component
import { seoConfig } from '$lib/config';
const pageSEO = seoConfig({
title: 'Dashboard',
description: 'View your application data'
});
The package is organized into several modules:
Each module is thoroughly documented with JSDoc comments. For detailed API reference, please refer to the source code or the generated documentation.
Prop | Type | Default | Description |
---|---|---|---|
title |
string |
"Svarog" |
Page title (displayed in browser tab) |
description |
string |
"A customizable web application framework." |
Page description for search engines |
keywords |
string |
"web, application, framework, ..." |
Keywords for search engines |
robots |
string |
"index, follow" |
Robots directive for search engines |
canonical |
string |
undefined |
Canonical URL for the page |
ogTitle |
string |
title |
Open Graph title (for social media sharing) |
ogUrl |
string |
"https://example.com" |
URL for the Open Graph object |
ogImage |
string |
"https://example.com/images/og.png" |
Image URL for social media shares |
twitterCard |
string |
"summary_large_image" |
Twitter card type |
twitterSite |
string |
undefined |
Twitter username (without @) |
og |
object |
{} |
Open Graph configuration object |
schema |
object |
{} |
Schema.org structured data configuration |
This package is written in TypeScript and includes comprehensive type definitions for all exported functions and interfaces.
MIT
Contributions are welcome! Please feel free to submit a Pull Request.
git checkout -b feature/amazing-feature
)git commit -m 'Add some amazing feature'
)git push origin feature/amazing-feature
)