|
|
|
@ -1,52 +1,168 @@
|
|
|
|
|
```markdown
|
|
|
|
|
# @api.global/typedserver
|
|
|
|
|
Easy serving of static files
|
|
|
|
|
|
|
|
|
|
## Install
|
|
|
|
|
To install @api.global/typedserver, run the following command in your terminal:
|
|
|
|
|
A TypeScript-based framework for serving static files with advanced features including live reloading, compression, and type-safe API requests. Part of the @api.global ecosystem, it integrates seamlessly with @api.global/typedrequest for type-safe HTTP requests and @api.global/typedsocket for WebSocket communication.
|
|
|
|
|
|
|
|
|
|
## Features
|
|
|
|
|
|
|
|
|
|
- **Type-Safe API Ecosystem**:
|
|
|
|
|
- HTTP Requests via @api.global/typedrequest
|
|
|
|
|
- WebSocket Support via @api.global/typedsocket
|
|
|
|
|
- Full TypeScript support across all endpoints
|
|
|
|
|
- **Service Worker Integration**: Advanced caching and offline capabilities
|
|
|
|
|
- **Edge Worker Support**: Optimized edge computing capabilities
|
|
|
|
|
- **Live Reload**: Automatic browser refresh on file changes
|
|
|
|
|
- **Compression**: Built-in support for response compression
|
|
|
|
|
- **CORS Management**: Flexible cross-origin resource sharing
|
|
|
|
|
- **TypeScript First**: Built with and for TypeScript
|
|
|
|
|
|
|
|
|
|
## Components
|
|
|
|
|
|
|
|
|
|
### Core Server (`ts/`)
|
|
|
|
|
- Static file serving with Express
|
|
|
|
|
- Type-safe request handling
|
|
|
|
|
- Live reload functionality
|
|
|
|
|
- Compression middleware
|
|
|
|
|
|
|
|
|
|
### Service Worker (`ts_web_serviceworker/`)
|
|
|
|
|
- `CacheManager`: Advanced caching strategies
|
|
|
|
|
- `NetworkManager`: Request/response handling
|
|
|
|
|
- `UpdateManager`: Cache invalidation and updates
|
|
|
|
|
- `ServiceWorker`: Core service worker implementation
|
|
|
|
|
|
|
|
|
|
### Edge Worker (`ts_edgeworker/`)
|
|
|
|
|
- Edge computing capabilities
|
|
|
|
|
- Request/response transformation
|
|
|
|
|
- Edge caching strategies
|
|
|
|
|
|
|
|
|
|
### Web Inject (`ts_web_inject/`)
|
|
|
|
|
- Live reload script injection
|
|
|
|
|
- Runtime dependency management
|
|
|
|
|
- Dynamic module loading
|
|
|
|
|
|
|
|
|
|
## Installation
|
|
|
|
|
|
|
|
|
|
```bash
|
|
|
|
|
npm install @api.global/typedserver --save
|
|
|
|
|
npm install @api.global/typedserver
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
This will add `@api.global/typedserver` to your project's dependencies.
|
|
|
|
|
|
|
|
|
|
## Usage
|
|
|
|
|
|
|
|
|
|
`@api.global/typedserver` is designed to make serving static files and handling web requests in a TypeScript environment easy and efficient. It leverages Express under the hood, providing a powerful API for web server creation with additional utilities for live reloading, typed requests/responses, and more, embracing TypeScript's static typing advantages.
|
|
|
|
|
|
|
|
|
|
### Setting up a Basic Web Server
|
|
|
|
|
|
|
|
|
|
The following example demonstrates how to set up a basic web server serving files from a directory.
|
|
|
|
|
## Quick Start
|
|
|
|
|
|
|
|
|
|
```typescript
|
|
|
|
|
import { TypedServer } from '@api.global/typedserver';
|
|
|
|
|
|
|
|
|
|
const serverOptions = {
|
|
|
|
|
port: 8080, // Port to listen on
|
|
|
|
|
serveDir: 'public', // Directory to serve static files from
|
|
|
|
|
watch: true, // Enable live reloading of changes
|
|
|
|
|
injectReload: true, // Inject live reload script into served HTML files
|
|
|
|
|
cors: true // Enable CORS
|
|
|
|
|
};
|
|
|
|
|
const server = new TypedServer({
|
|
|
|
|
port: 3000,
|
|
|
|
|
serveDir: './public',
|
|
|
|
|
watch: true,
|
|
|
|
|
compression: true
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const typedServer = new TypedServer(serverOptions);
|
|
|
|
|
|
|
|
|
|
async function startServer() {
|
|
|
|
|
await typedServer.start();
|
|
|
|
|
console.log(`Server is running on http://localhost:${serverOptions.port}`);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
startServer().catch(console.error);
|
|
|
|
|
server.start();
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
In the example above, `TypedServer` is instantiated with an `IServerOptions` object specifying options like the port to listen on (`8080`), the directory containing static files to serve (`public`), and live reload features. Calling `start()` on the `typedServer` instance initiates the server.
|
|
|
|
|
## Type-Safe API Integration
|
|
|
|
|
|
|
|
|
|
### Using Typed Requests and Responses
|
|
|
|
|
### HTTP Requests with TypedRequest
|
|
|
|
|
```typescript
|
|
|
|
|
import { TypedRequest } from '@api.global/typedrequest';
|
|
|
|
|
|
|
|
|
|
`TypedServer` supports typed requests and responses, making API development more robust and maintainable. Define your request and response types, and use them to type-check incoming requests and their responses.
|
|
|
|
|
// Define your request/response interface
|
|
|
|
|
interface IUserRequest {
|
|
|
|
|
method: 'getUser';
|
|
|
|
|
request: { userId: string };
|
|
|
|
|
response: { username: string; email: string; };
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
First, define the types:
|
|
|
|
|
// Create and use a typed request
|
|
|
|
|
const getUserRequest = new TypedRequest<IUserRequest>('/api/users', 'getUser');
|
|
|
|
|
const user = await getUserRequest.fire({ userId: '123' });
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
### WebSocket Communication
|
|
|
|
|
```typescript
|
|
|
|
|
import { TypedSocket } from '@api.global/typedsocket';
|
|
|
|
|
|
|
|
|
|
// Server setup
|
|
|
|
|
const typedRouter = new TypedRouter();
|
|
|
|
|
const server = await TypedSocket.createServer(typedRouter);
|
|
|
|
|
|
|
|
|
|
// Client connection
|
|
|
|
|
const client = await TypedSocket.createClient(typedRouter, 'ws://localhost:3000');
|
|
|
|
|
|
|
|
|
|
// Type-safe real-time messaging
|
|
|
|
|
interface IChatMessage {
|
|
|
|
|
method: 'sendMessage';
|
|
|
|
|
request: { text: string };
|
|
|
|
|
response: { id: string; timestamp: number; };
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
## Advanced Usage
|
|
|
|
|
|
|
|
|
|
### Service Worker Setup
|
|
|
|
|
|
|
|
|
|
```typescript
|
|
|
|
|
import { ServiceWorker } from '@api.global/typedserver/web_serviceworker';
|
|
|
|
|
|
|
|
|
|
const sw = new ServiceWorker({
|
|
|
|
|
cacheStrategy: 'network-first',
|
|
|
|
|
offlineSupport: true
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
sw.register();
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
### Edge Worker Configuration
|
|
|
|
|
|
|
|
|
|
```typescript
|
|
|
|
|
import { EdgeWorker } from '@api.global/typedserver/edgeworker';
|
|
|
|
|
|
|
|
|
|
const edge = new EdgeWorker({
|
|
|
|
|
transforms: ['compress', 'minify'],
|
|
|
|
|
caching: true
|
|
|
|
|
});
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
## Configuration
|
|
|
|
|
|
|
|
|
|
### Server Options
|
|
|
|
|
```typescript
|
|
|
|
|
interface IServerOptions {
|
|
|
|
|
port?: number;
|
|
|
|
|
host?: string;
|
|
|
|
|
serveDir: string;
|
|
|
|
|
watch?: boolean;
|
|
|
|
|
compression?: boolean;
|
|
|
|
|
cors?: boolean | CorsOptions;
|
|
|
|
|
cache?: CacheOptions;
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
### Cache Strategies
|
|
|
|
|
```typescript
|
|
|
|
|
type CacheStrategy =
|
|
|
|
|
| 'network-first'
|
|
|
|
|
| 'cache-first'
|
|
|
|
|
| 'stale-while-revalidate';
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
## API Reference
|
|
|
|
|
|
|
|
|
|
See [API Documentation](https://api.global/docs/typedserver) for detailed API reference.
|
|
|
|
|
|
|
|
|
|
## Contributing
|
|
|
|
|
|
|
|
|
|
1. Fork the repository
|
|
|
|
|
2. Create your feature branch
|
|
|
|
|
3. Commit your changes
|
|
|
|
|
4. Push to the branch
|
|
|
|
|
5. Create a Pull Request
|
|
|
|
|
|
|
|
|
|
## License
|
|
|
|
|
|
|
|
|
|
MIT License - see LICENSE for details.
|
|
|
|
|
|
|
|
|
|
Task Venture Capital GmbH © 2024
|
|
|
|
|
|
|
|
|
|
```typescript
|
|
|
|
|
// Define a request type
|
|
|
|
|