@verisure-italy/zipcode-types 1.3.0 → 1.5.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/README.md +171 -0
- package/package.json +2 -2
package/README.md
ADDED
|
@@ -0,0 +1,171 @@
|
|
|
1
|
+
# ZipCode Types
|
|
2
|
+
|
|
3
|
+
TypeScript types and Zod schemas for ZipCode service entities. This package provides type-safe definitions for areas and enabled zip codes.
|
|
4
|
+
|
|
5
|
+
## Features
|
|
6
|
+
|
|
7
|
+
- ✅ **Type-Safe** - Full TypeScript support with Zod runtime validation
|
|
8
|
+
- ✅ **Shared Types** - Built on top of `@verisure-italy/shared-types`
|
|
9
|
+
- ✅ **Runtime Validation** - Zod schemas for request/response validation
|
|
10
|
+
- ✅ **Tree-Shakeable** - ESM and CJS support for optimal bundle size
|
|
11
|
+
|
|
12
|
+
## Installation
|
|
13
|
+
|
|
14
|
+
```bash
|
|
15
|
+
pnpm add @verisure-italy/zipcode-types
|
|
16
|
+
```
|
|
17
|
+
|
|
18
|
+
## Types
|
|
19
|
+
|
|
20
|
+
### Area
|
|
21
|
+
|
|
22
|
+
Represents a geographic area with zip code information.
|
|
23
|
+
|
|
24
|
+
```typescript
|
|
25
|
+
import { Area, areaSchema } from '@verisure-italy/zipcode-types'
|
|
26
|
+
|
|
27
|
+
// TypeScript type
|
|
28
|
+
const area: Area = {
|
|
29
|
+
id: '123e4567-e89b-12d3-a456-426614174000',
|
|
30
|
+
city: 'Milano',
|
|
31
|
+
province: 'MI',
|
|
32
|
+
region: 'Lombardia',
|
|
33
|
+
town: 'Milano',
|
|
34
|
+
zipCode: '20100',
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
// Runtime validation with Zod
|
|
38
|
+
const result = areaSchema.safeParse(data)
|
|
39
|
+
if (result.success) {
|
|
40
|
+
const validArea: Area = result.data
|
|
41
|
+
}
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
**Schema:**
|
|
45
|
+
|
|
46
|
+
```typescript
|
|
47
|
+
const areaSchema = z.object({
|
|
48
|
+
id: z.uuid(), // UUID v4
|
|
49
|
+
city: z.string().min(2, 'city.required'), // Min 2 chars
|
|
50
|
+
province: z.string().min(2).max(3), // 2-3 chars (e.g., MI, RM)
|
|
51
|
+
region: z.string().min(2, 'region.required'), // Min 2 chars
|
|
52
|
+
town: z.string().min(2, 'town.required'), // Min 2 chars
|
|
53
|
+
zipCode: zipCodeSchema, // From shared-types
|
|
54
|
+
})
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
### Enabled
|
|
58
|
+
|
|
59
|
+
Represents an enabled zip code.
|
|
60
|
+
|
|
61
|
+
```typescript
|
|
62
|
+
import { Enabled, enabledSchema } from '@verisure-italy/zipcode-types'
|
|
63
|
+
|
|
64
|
+
// TypeScript type
|
|
65
|
+
const enabled: Enabled = {
|
|
66
|
+
zipCode: '20100',
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
// Runtime validation with Zod
|
|
70
|
+
const result = enabledSchema.safeParse(data)
|
|
71
|
+
if (result.success) {
|
|
72
|
+
const validEnabled: Enabled = result.data
|
|
73
|
+
}
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
**Schema:**
|
|
77
|
+
|
|
78
|
+
```typescript
|
|
79
|
+
const enabledSchema = z.object({
|
|
80
|
+
zipCode: zipCodeSchema, // From shared-types
|
|
81
|
+
})
|
|
82
|
+
```
|
|
83
|
+
|
|
84
|
+
## Usage Examples
|
|
85
|
+
|
|
86
|
+
### Express Route with Validation
|
|
87
|
+
|
|
88
|
+
```typescript
|
|
89
|
+
import express from 'express'
|
|
90
|
+
import { areaSchema, type Area } from '@verisure-italy/zipcode-types'
|
|
91
|
+
|
|
92
|
+
const router = express.Router()
|
|
93
|
+
|
|
94
|
+
router.post('/areas', async (req, res) => {
|
|
95
|
+
// Validate request body
|
|
96
|
+
const result = areaSchema.safeParse(req.body)
|
|
97
|
+
|
|
98
|
+
if (!result.success) {
|
|
99
|
+
return res.status(400).json({
|
|
100
|
+
error: 'Invalid area data',
|
|
101
|
+
details: result.error.issues
|
|
102
|
+
})
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
const area: Area = result.data
|
|
106
|
+
// ... save to database ...
|
|
107
|
+
|
|
108
|
+
res.json(area)
|
|
109
|
+
})
|
|
110
|
+
```
|
|
111
|
+
|
|
112
|
+
### With Router Middleware
|
|
113
|
+
|
|
114
|
+
```typescript
|
|
115
|
+
import { createRestResource } from '@verisure-italy/router-middleware'
|
|
116
|
+
import { areaSchema, type Area } from '@verisure-italy/zipcode-types'
|
|
117
|
+
|
|
118
|
+
const areaResource = createRestResource<Area>({
|
|
119
|
+
name: 'area',
|
|
120
|
+
schema: areaSchema,
|
|
121
|
+
// ... repository methods ...
|
|
122
|
+
})
|
|
123
|
+
|
|
124
|
+
// Validation is handled automatically by router-middleware
|
|
125
|
+
```
|
|
126
|
+
|
|
127
|
+
### Type Guards
|
|
128
|
+
|
|
129
|
+
```typescript
|
|
130
|
+
import { areaSchema, type Area } from '@verisure-italy/zipcode-types'
|
|
131
|
+
|
|
132
|
+
function isValidArea(data: unknown): data is Area {
|
|
133
|
+
return areaSchema.safeParse(data).success
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
// Usage
|
|
137
|
+
if (isValidArea(unknownData)) {
|
|
138
|
+
// TypeScript knows this is an Area
|
|
139
|
+
console.log(unknownData.city)
|
|
140
|
+
}
|
|
141
|
+
```
|
|
142
|
+
|
|
143
|
+
## Dependencies
|
|
144
|
+
|
|
145
|
+
- `@verisure-italy/shared-types` - Provides `zipCodeSchema` and other shared types
|
|
146
|
+
- `zod` ^4.1.12 - Runtime validation library
|
|
147
|
+
|
|
148
|
+
## Related Packages
|
|
149
|
+
|
|
150
|
+
- [`@verisure-italy/shared-types`](../shared-types) - Shared types across services
|
|
151
|
+
- [`@verisure-italy/router-middleware`](../router-middleware) - REST resource creation with validation
|
|
152
|
+
- [`@verisure-italy/error-handler-middleware`](../error-handler-middleware) - Handles validation errors
|
|
153
|
+
|
|
154
|
+
## TypeScript Configuration
|
|
155
|
+
|
|
156
|
+
This package is built with TypeScript 5.9+ and exports both type definitions and runtime schemas.
|
|
157
|
+
|
|
158
|
+
```json
|
|
159
|
+
{
|
|
160
|
+
"compilerOptions": {
|
|
161
|
+
"moduleResolution": "bundler",
|
|
162
|
+
"module": "ESNext",
|
|
163
|
+
"target": "ES2022"
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
```
|
|
167
|
+
|
|
168
|
+
## License
|
|
169
|
+
|
|
170
|
+
Internal Verisure Italy package
|
|
171
|
+
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@verisure-italy/zipcode-types",
|
|
3
3
|
"description": "Types for ZipCode service",
|
|
4
|
-
"version": "1.
|
|
4
|
+
"version": "1.5.0",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "dist/index.js",
|
|
7
7
|
"types": "dist/index.d.ts",
|
|
@@ -19,7 +19,7 @@
|
|
|
19
19
|
],
|
|
20
20
|
"dependencies": {
|
|
21
21
|
"zod": "4.1.12",
|
|
22
|
-
"@verisure-italy/shared-types": "1.
|
|
22
|
+
"@verisure-italy/shared-types": "1.5.0"
|
|
23
23
|
},
|
|
24
24
|
"devDependencies": {
|
|
25
25
|
"@types/node": "24.6.0",
|