@owlmeans/image-resource 0.1.2 → 0.1.4
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/README.md +22 -235
- package/package.json +4 -3
- package/tsconfig.json +4 -4
package/README.md
CHANGED
|
@@ -1,257 +1,44 @@
|
|
|
1
1
|
# @owlmeans/image-resource
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Image-specific type extensions for OwlMeans object storage — AJV schemas for image metadata.
|
|
4
4
|
|
|
5
|
-
##
|
|
5
|
+
## Overview
|
|
6
6
|
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
-
|
|
10
|
-
- **Image Metadata**: Enhanced metadata management specific to image files
|
|
11
|
-
- **Validation Schemas**: AJV schemas for image data validation
|
|
12
|
-
- **Type Safety**: TypeScript interfaces for image-related data structures
|
|
13
|
-
- **Storage Integration**: Seamless integration with OwlMeans storage infrastructure
|
|
14
|
-
|
|
15
|
-
## Key Concepts
|
|
16
|
-
|
|
17
|
-
### Image-specific Storage
|
|
18
|
-
Extends the basic file storage functionality with image-specific operations and metadata handling.
|
|
19
|
-
|
|
20
|
-
### Type Extensions
|
|
21
|
-
Provides specialized TypeScript interfaces that extend the base storage types for image-specific use cases.
|
|
22
|
-
|
|
23
|
-
### Schema Validation
|
|
24
|
-
Includes AJV schemas for validating image metadata and data structures.
|
|
25
|
-
|
|
26
|
-
### Storage Abstraction
|
|
27
|
-
Builds upon the storage-common package to provide higher-level image management capabilities.
|
|
7
|
+
- `ImageMeta` / `StoredImage` / `ImageData` — typed image record interfaces
|
|
8
|
+
- `ImageMetaSchema` / `StoredImageSchema` / `ImageDataSchema` — AJV JSON schemas for validation
|
|
9
|
+
- Extends `@owlmeans/storage-common` types with image semantics
|
|
28
10
|
|
|
29
11
|
## Installation
|
|
30
12
|
|
|
31
13
|
```bash
|
|
32
|
-
|
|
33
|
-
```
|
|
34
|
-
|
|
35
|
-
## API Reference
|
|
36
|
-
|
|
37
|
-
### Types
|
|
38
|
-
|
|
39
|
-
#### `ImageMeta`
|
|
40
|
-
Metadata interface for stored images.
|
|
41
|
-
|
|
42
|
-
```typescript
|
|
43
|
-
interface ImageMeta extends StoredFileMeta {
|
|
44
|
-
// Extends base file metadata with image-specific properties
|
|
45
|
-
}
|
|
46
|
-
```
|
|
47
|
-
|
|
48
|
-
#### `StoredImage`
|
|
49
|
-
Interface for stored image files.
|
|
50
|
-
|
|
51
|
-
```typescript
|
|
52
|
-
interface StoredImage extends StoredFile {
|
|
53
|
-
// Extends base stored file interface for images
|
|
54
|
-
}
|
|
55
|
-
```
|
|
56
|
-
|
|
57
|
-
#### `ImageData`
|
|
58
|
-
Interface for image data with content.
|
|
59
|
-
|
|
60
|
-
```typescript
|
|
61
|
-
interface ImageData extends StoredFileWithData {
|
|
62
|
-
// Extends base file data interface for images
|
|
63
|
-
}
|
|
64
|
-
```
|
|
65
|
-
|
|
66
|
-
### Validation Schemas
|
|
67
|
-
|
|
68
|
-
#### `ImageMetaSchema`
|
|
69
|
-
AJV schema for validating image metadata.
|
|
70
|
-
|
|
71
|
-
```typescript
|
|
72
|
-
const ImageMetaSchema: JSONSchemaType<ImageMeta>
|
|
73
|
-
```
|
|
74
|
-
|
|
75
|
-
#### `StoredImageSchema`
|
|
76
|
-
AJV schema for validating stored image objects.
|
|
77
|
-
|
|
78
|
-
```typescript
|
|
79
|
-
const StoredImageSchema: JSONSchemaType<StoredImage>
|
|
14
|
+
bun add @owlmeans/image-resource
|
|
80
15
|
```
|
|
81
16
|
|
|
82
|
-
|
|
83
|
-
AJV schema for validating image data with content.
|
|
17
|
+
## Usage
|
|
84
18
|
|
|
85
19
|
```typescript
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
## Usage Examples
|
|
90
|
-
|
|
91
|
-
### Basic Image Validation
|
|
92
|
-
|
|
93
|
-
```typescript
|
|
94
|
-
import { ImageMetaSchema, StoredImageSchema, ImageDataSchema } from '@owlmeans/image-resource'
|
|
95
|
-
import Ajv from 'ajv'
|
|
96
|
-
|
|
97
|
-
const ajv = new Ajv()
|
|
98
|
-
|
|
99
|
-
// Validate image metadata
|
|
100
|
-
const validateImageMeta = ajv.compile(ImageMetaSchema)
|
|
101
|
-
const isValidMeta = validateImageMeta(imageMetadata)
|
|
102
|
-
|
|
103
|
-
// Validate stored image
|
|
104
|
-
const validateStoredImage = ajv.compile(StoredImageSchema)
|
|
105
|
-
const isValidImage = validateStoredImage(imageObject)
|
|
106
|
-
|
|
107
|
-
// Validate image data
|
|
108
|
-
const validateImageData = ajv.compile(ImageDataSchema)
|
|
109
|
-
const isValidData = validateImageData(imageDataObject)
|
|
110
|
-
```
|
|
111
|
-
|
|
112
|
-
### Integration with Storage Systems
|
|
113
|
-
|
|
114
|
-
```typescript
|
|
115
|
-
import type { ImageMeta, StoredImage, ImageData } from '@owlmeans/image-resource'
|
|
116
|
-
import { ImageMetaSchema } from '@owlmeans/image-resource'
|
|
117
|
-
|
|
118
|
-
class ImageStorageService {
|
|
119
|
-
async storeImage(file: File, metadata: Partial<ImageMeta>): Promise<StoredImage> {
|
|
120
|
-
// Validate metadata
|
|
121
|
-
const validateMeta = ajv.compile(ImageMetaSchema)
|
|
122
|
-
if (!validateMeta(metadata)) {
|
|
123
|
-
throw new Error('Invalid image metadata')
|
|
124
|
-
}
|
|
125
|
-
|
|
126
|
-
// Process and store image
|
|
127
|
-
const storedImage: StoredImage = {
|
|
128
|
-
id: generateId(),
|
|
129
|
-
filename: file.name,
|
|
130
|
-
contentType: file.type,
|
|
131
|
-
size: file.size,
|
|
132
|
-
...metadata
|
|
133
|
-
}
|
|
134
|
-
|
|
135
|
-
return storedImage
|
|
136
|
-
}
|
|
137
|
-
|
|
138
|
-
async getImageData(imageId: string): Promise<ImageData> {
|
|
139
|
-
// Retrieve image with data
|
|
140
|
-
const imageData = await this.storage.getWithData(imageId)
|
|
141
|
-
|
|
142
|
-
// Validate returned data
|
|
143
|
-
const validateData = ajv.compile(ImageDataSchema)
|
|
144
|
-
if (!validateData(imageData)) {
|
|
145
|
-
throw new Error('Invalid image data format')
|
|
146
|
-
}
|
|
147
|
-
|
|
148
|
-
return imageData as ImageData
|
|
149
|
-
}
|
|
150
|
-
}
|
|
151
|
-
```
|
|
20
|
+
import { StoredImageSchema, ImageDataSchema } from '@owlmeans/image-resource'
|
|
21
|
+
import type { StoredImage, ImageData } from '@owlmeans/image-resource'
|
|
152
22
|
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
```typescript
|
|
156
|
-
import type { ImageMeta, StoredImage } from '@owlmeans/image-resource'
|
|
157
|
-
|
|
158
|
-
interface ImageProcessor {
|
|
159
|
-
processImage(image: StoredImage): Promise<StoredImage>
|
|
160
|
-
getImageMetadata(imageId: string): Promise<ImageMeta>
|
|
161
|
-
updateImageMetadata(imageId: string, metadata: Partial<ImageMeta>): Promise<void>
|
|
162
|
-
}
|
|
163
|
-
|
|
164
|
-
class BasicImageProcessor implements ImageProcessor {
|
|
165
|
-
async processImage(image: StoredImage): Promise<StoredImage> {
|
|
166
|
-
// Process the image (resize, compress, etc.)
|
|
167
|
-
return {
|
|
168
|
-
...image,
|
|
169
|
-
// Updated properties after processing
|
|
170
|
-
}
|
|
171
|
-
}
|
|
172
|
-
|
|
173
|
-
async getImageMetadata(imageId: string): Promise<ImageMeta> {
|
|
174
|
-
const metadata = await this.storage.getMetadata(imageId)
|
|
175
|
-
return metadata as ImageMeta
|
|
176
|
-
}
|
|
177
|
-
|
|
178
|
-
async updateImageMetadata(imageId: string, metadata: Partial<ImageMeta>): Promise<void> {
|
|
179
|
-
await this.storage.updateMetadata(imageId, metadata)
|
|
180
|
-
}
|
|
181
|
-
}
|
|
23
|
+
// Use AJV schema for validation
|
|
24
|
+
const validate = ajv.compile<StoredImage>(StoredImageSchema)
|
|
182
25
|
```
|
|
183
26
|
|
|
184
|
-
##
|
|
27
|
+
## API
|
|
185
28
|
|
|
186
|
-
###
|
|
187
|
-
|
|
188
|
-
```typescript
|
|
189
|
-
import type { ImageData } from '@owlmeans/image-resource'
|
|
190
|
-
import { ImageDataSchema } from '@owlmeans/image-resource'
|
|
191
|
-
|
|
192
|
-
async function handleImageUpload(file: File): Promise<ImageData> {
|
|
193
|
-
// Create image data object
|
|
194
|
-
const imageData: ImageData = {
|
|
195
|
-
id: generateId(),
|
|
196
|
-
filename: file.name,
|
|
197
|
-
contentType: file.type,
|
|
198
|
-
size: file.size,
|
|
199
|
-
data: await file.arrayBuffer(),
|
|
200
|
-
uploadedAt: new Date(),
|
|
201
|
-
// Additional image-specific metadata
|
|
202
|
-
}
|
|
203
|
-
|
|
204
|
-
// Validate before processing
|
|
205
|
-
const validate = ajv.compile(ImageDataSchema)
|
|
206
|
-
if (!validate(imageData)) {
|
|
207
|
-
throw new Error('Invalid image data')
|
|
208
|
-
}
|
|
209
|
-
|
|
210
|
-
return imageData
|
|
211
|
-
}
|
|
212
|
-
```
|
|
213
|
-
|
|
214
|
-
### Storage Resource Integration
|
|
215
|
-
|
|
216
|
-
```typescript
|
|
217
|
-
import { StorageResource } from '@owlmeans/storage-resource'
|
|
218
|
-
import type { StoredImage } from '@owlmeans/image-resource'
|
|
219
|
-
|
|
220
|
-
class ImageResource extends StorageResource<StoredImage> {
|
|
221
|
-
constructor() {
|
|
222
|
-
super({
|
|
223
|
-
schema: StoredImageSchema,
|
|
224
|
-
bucket: 'images',
|
|
225
|
-
// Image-specific configuration
|
|
226
|
-
})
|
|
227
|
-
}
|
|
228
|
-
|
|
229
|
-
async uploadImage(file: File, metadata?: Partial<ImageMeta>): Promise<StoredImage> {
|
|
230
|
-
const imageData = await this.processFile(file)
|
|
231
|
-
return await this.store({
|
|
232
|
-
...imageData,
|
|
233
|
-
...metadata
|
|
234
|
-
})
|
|
235
|
-
}
|
|
236
|
-
}
|
|
237
|
-
```
|
|
238
|
-
|
|
239
|
-
## Best Practices
|
|
29
|
+
### Types
|
|
240
30
|
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
4. **Integration**: Leverage with storage-common and storage-resource packages
|
|
245
|
-
5. **Error Handling**: Implement proper error handling for validation failures
|
|
31
|
+
- `ImageMeta` — extends `StoredFileMeta` with image-specific fields
|
|
32
|
+
- `StoredImage` — extends `StoredFile` for stored image records
|
|
33
|
+
- `ImageData` — extends `StoredFileWithData` for image upload payloads
|
|
246
34
|
|
|
247
|
-
|
|
35
|
+
### Schemas
|
|
248
36
|
|
|
249
|
-
|
|
250
|
-
-
|
|
251
|
-
- `
|
|
37
|
+
- `ImageMetaSchema: JSONSchemaType<ImageMeta>`
|
|
38
|
+
- `StoredImageSchema: JSONSchemaType<StoredImage>`
|
|
39
|
+
- `ImageDataSchema: JSONSchemaType<ImageData>`
|
|
252
40
|
|
|
253
41
|
## Related Packages
|
|
254
42
|
|
|
255
|
-
- [`@owlmeans/storage-common`](../storage-common)
|
|
256
|
-
- [`@owlmeans/storage-resource`](../storage-resource)
|
|
257
|
-
- [`@owlmeans/resource`](../resource) - Base resource management
|
|
43
|
+
- [`@owlmeans/storage-common`](../storage-common) — `StoredFileMeta`, `StoredFile`, `StoredFileWithData` base types
|
|
44
|
+
- [`@owlmeans/storage-resource`](../storage-resource) — S3 resource used to persist images
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@owlmeans/image-resource",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "0.1.
|
|
4
|
+
"version": "0.1.4",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"scripts": {
|
|
7
7
|
"build": "tsc -b",
|
|
@@ -24,12 +24,13 @@
|
|
|
24
24
|
"ajv": "*"
|
|
25
25
|
},
|
|
26
26
|
"dependencies": {
|
|
27
|
-
"@owlmeans/storage-resource": "^0.1.
|
|
27
|
+
"@owlmeans/storage-resource": "^0.1.4"
|
|
28
28
|
},
|
|
29
29
|
"devDependencies": {
|
|
30
|
+
"@owlmeans/dep-config": "workspace:*",
|
|
30
31
|
"@types/node": "^24.10.1",
|
|
31
32
|
"nodemon": "^3.1.11",
|
|
32
|
-
"typescript": "^
|
|
33
|
+
"typescript": "^6.0.2"
|
|
33
34
|
},
|
|
34
35
|
"publishConfig": {
|
|
35
36
|
"access": "public"
|
package/tsconfig.json
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
{
|
|
2
2
|
"extends": [
|
|
3
|
-
"
|
|
3
|
+
"@owlmeans/dep-config/tsconfig.base.json",
|
|
4
|
+
"@owlmeans/dep-config/tsconfig.node.json"
|
|
4
5
|
],
|
|
5
6
|
"compilerOptions": {
|
|
6
|
-
"rootDir": "./src/",
|
|
7
|
-
"outDir": "./build/"
|
|
8
|
-
"moduleResolution": "Bundler",
|
|
7
|
+
"rootDir": "./src/",
|
|
8
|
+
"outDir": "./build/"
|
|
9
9
|
},
|
|
10
10
|
"exclude": [
|
|
11
11
|
"./dist/**/*",
|