@tileflow/static 0.1.0-alpha.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 +16 -0
- package/dist/index.d.ts +247 -0
- package/dist/index.js +410 -0
- package/package.json +45 -0
package/README.md
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
# @tileflow/static
|
|
2
|
+
|
|
3
|
+
Static map schemas, overlay helpers, and request utilities for Tileflow static map rendering.
|
|
4
|
+
|
|
5
|
+
```ts
|
|
6
|
+
import { marker, validateStaticScene } from "@tileflow/static";
|
|
7
|
+
|
|
8
|
+
const scene = validateStaticScene({
|
|
9
|
+
map: "madrid",
|
|
10
|
+
camera: { type: "center", center: [-3.7038, 40.4168], zoom: 12 },
|
|
11
|
+
size: { width: 1200, height: 800 },
|
|
12
|
+
overlays: [marker({ coordinate: [-3.7038, 40.4168] })],
|
|
13
|
+
});
|
|
14
|
+
```
|
|
15
|
+
|
|
16
|
+
Docs: https://tileflow.dev/docs
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,247 @@
|
|
|
1
|
+
import { z } from 'zod';
|
|
2
|
+
|
|
3
|
+
declare const staticSceneSchemaVersion = 1;
|
|
4
|
+
declare const staticRendererSchemaVersion = 1;
|
|
5
|
+
declare const staticSceneLimits: {
|
|
6
|
+
readonly maxDimension: 1280;
|
|
7
|
+
readonly maxGeoJsonBytes: 96000;
|
|
8
|
+
readonly maxOverlays: 24;
|
|
9
|
+
readonly maxPathCoordinates: 2000;
|
|
10
|
+
readonly maxPhysicalPixels: number;
|
|
11
|
+
readonly minDimension: 64;
|
|
12
|
+
};
|
|
13
|
+
declare const coordinateSchema: z.ZodTuple<[z.ZodNumber, z.ZodNumber], null>;
|
|
14
|
+
declare const lineOverlaySchema: z.ZodObject<{
|
|
15
|
+
color: z.ZodDefault<z.ZodString>;
|
|
16
|
+
coordinates: z.ZodArray<z.ZodTuple<[z.ZodNumber, z.ZodNumber], null>>;
|
|
17
|
+
id: z.ZodOptional<z.ZodString>;
|
|
18
|
+
opacity: z.ZodDefault<z.ZodNumber>;
|
|
19
|
+
type: z.ZodLiteral<"line">;
|
|
20
|
+
width: z.ZodDefault<z.ZodNumber>;
|
|
21
|
+
}, z.core.$strip>;
|
|
22
|
+
declare const circleOverlaySchema: z.ZodObject<{
|
|
23
|
+
color: z.ZodDefault<z.ZodString>;
|
|
24
|
+
coordinate: z.ZodTuple<[z.ZodNumber, z.ZodNumber], null>;
|
|
25
|
+
id: z.ZodOptional<z.ZodString>;
|
|
26
|
+
opacity: z.ZodDefault<z.ZodNumber>;
|
|
27
|
+
radius: z.ZodDefault<z.ZodNumber>;
|
|
28
|
+
strokeColor: z.ZodOptional<z.ZodString>;
|
|
29
|
+
strokeWidth: z.ZodDefault<z.ZodNumber>;
|
|
30
|
+
type: z.ZodLiteral<"circle">;
|
|
31
|
+
}, z.core.$strip>;
|
|
32
|
+
declare const markerOverlaySchema: z.ZodObject<{
|
|
33
|
+
color: z.ZodDefault<z.ZodString>;
|
|
34
|
+
coordinate: z.ZodTuple<[z.ZodNumber, z.ZodNumber], null>;
|
|
35
|
+
id: z.ZodOptional<z.ZodString>;
|
|
36
|
+
radius: z.ZodDefault<z.ZodNumber>;
|
|
37
|
+
strokeColor: z.ZodDefault<z.ZodString>;
|
|
38
|
+
strokeWidth: z.ZodDefault<z.ZodNumber>;
|
|
39
|
+
type: z.ZodLiteral<"marker">;
|
|
40
|
+
}, z.core.$strip>;
|
|
41
|
+
declare const polygonOverlaySchema: z.ZodObject<{
|
|
42
|
+
coordinates: z.ZodArray<z.ZodArray<z.ZodTuple<[z.ZodNumber, z.ZodNumber], null>>>;
|
|
43
|
+
fill: z.ZodDefault<z.ZodString>;
|
|
44
|
+
id: z.ZodOptional<z.ZodString>;
|
|
45
|
+
opacity: z.ZodDefault<z.ZodNumber>;
|
|
46
|
+
stroke: z.ZodOptional<z.ZodString>;
|
|
47
|
+
strokeWidth: z.ZodDefault<z.ZodNumber>;
|
|
48
|
+
type: z.ZodLiteral<"polygon">;
|
|
49
|
+
}, z.core.$strip>;
|
|
50
|
+
declare const staticOverlaySchema: z.ZodDiscriminatedUnion<[z.ZodObject<{
|
|
51
|
+
color: z.ZodDefault<z.ZodString>;
|
|
52
|
+
coordinates: z.ZodArray<z.ZodTuple<[z.ZodNumber, z.ZodNumber], null>>;
|
|
53
|
+
id: z.ZodOptional<z.ZodString>;
|
|
54
|
+
opacity: z.ZodDefault<z.ZodNumber>;
|
|
55
|
+
type: z.ZodLiteral<"line">;
|
|
56
|
+
width: z.ZodDefault<z.ZodNumber>;
|
|
57
|
+
}, z.core.$strip>, z.ZodObject<{
|
|
58
|
+
color: z.ZodDefault<z.ZodString>;
|
|
59
|
+
coordinate: z.ZodTuple<[z.ZodNumber, z.ZodNumber], null>;
|
|
60
|
+
id: z.ZodOptional<z.ZodString>;
|
|
61
|
+
opacity: z.ZodDefault<z.ZodNumber>;
|
|
62
|
+
radius: z.ZodDefault<z.ZodNumber>;
|
|
63
|
+
strokeColor: z.ZodOptional<z.ZodString>;
|
|
64
|
+
strokeWidth: z.ZodDefault<z.ZodNumber>;
|
|
65
|
+
type: z.ZodLiteral<"circle">;
|
|
66
|
+
}, z.core.$strip>, z.ZodObject<{
|
|
67
|
+
color: z.ZodDefault<z.ZodString>;
|
|
68
|
+
coordinate: z.ZodTuple<[z.ZodNumber, z.ZodNumber], null>;
|
|
69
|
+
id: z.ZodOptional<z.ZodString>;
|
|
70
|
+
radius: z.ZodDefault<z.ZodNumber>;
|
|
71
|
+
strokeColor: z.ZodDefault<z.ZodString>;
|
|
72
|
+
strokeWidth: z.ZodDefault<z.ZodNumber>;
|
|
73
|
+
type: z.ZodLiteral<"marker">;
|
|
74
|
+
}, z.core.$strip>, z.ZodObject<{
|
|
75
|
+
coordinates: z.ZodArray<z.ZodArray<z.ZodTuple<[z.ZodNumber, z.ZodNumber], null>>>;
|
|
76
|
+
fill: z.ZodDefault<z.ZodString>;
|
|
77
|
+
id: z.ZodOptional<z.ZodString>;
|
|
78
|
+
opacity: z.ZodDefault<z.ZodNumber>;
|
|
79
|
+
stroke: z.ZodOptional<z.ZodString>;
|
|
80
|
+
strokeWidth: z.ZodDefault<z.ZodNumber>;
|
|
81
|
+
type: z.ZodLiteral<"polygon">;
|
|
82
|
+
}, z.core.$strip>], "type">;
|
|
83
|
+
declare const staticSceneSchema: z.ZodObject<{
|
|
84
|
+
camera: z.ZodDiscriminatedUnion<[z.ZodObject<{
|
|
85
|
+
bearing: z.ZodOptional<z.ZodNumber>;
|
|
86
|
+
center: z.ZodTuple<[z.ZodNumber, z.ZodNumber], null>;
|
|
87
|
+
type: z.ZodLiteral<"center">;
|
|
88
|
+
zoom: z.ZodNumber;
|
|
89
|
+
}, z.core.$strip>, z.ZodObject<{
|
|
90
|
+
bearing: z.ZodOptional<z.ZodNumber>;
|
|
91
|
+
bounds: z.ZodTuple<[z.ZodNumber, z.ZodNumber, z.ZodNumber, z.ZodNumber], null>;
|
|
92
|
+
padding: z.ZodOptional<z.ZodNumber>;
|
|
93
|
+
type: z.ZodLiteral<"bounds">;
|
|
94
|
+
}, z.core.$strip>], "type">;
|
|
95
|
+
map: z.ZodString;
|
|
96
|
+
overlays: z.ZodDefault<z.ZodArray<z.ZodDiscriminatedUnion<[z.ZodObject<{
|
|
97
|
+
color: z.ZodDefault<z.ZodString>;
|
|
98
|
+
coordinates: z.ZodArray<z.ZodTuple<[z.ZodNumber, z.ZodNumber], null>>;
|
|
99
|
+
id: z.ZodOptional<z.ZodString>;
|
|
100
|
+
opacity: z.ZodDefault<z.ZodNumber>;
|
|
101
|
+
type: z.ZodLiteral<"line">;
|
|
102
|
+
width: z.ZodDefault<z.ZodNumber>;
|
|
103
|
+
}, z.core.$strip>, z.ZodObject<{
|
|
104
|
+
color: z.ZodDefault<z.ZodString>;
|
|
105
|
+
coordinate: z.ZodTuple<[z.ZodNumber, z.ZodNumber], null>;
|
|
106
|
+
id: z.ZodOptional<z.ZodString>;
|
|
107
|
+
opacity: z.ZodDefault<z.ZodNumber>;
|
|
108
|
+
radius: z.ZodDefault<z.ZodNumber>;
|
|
109
|
+
strokeColor: z.ZodOptional<z.ZodString>;
|
|
110
|
+
strokeWidth: z.ZodDefault<z.ZodNumber>;
|
|
111
|
+
type: z.ZodLiteral<"circle">;
|
|
112
|
+
}, z.core.$strip>, z.ZodObject<{
|
|
113
|
+
color: z.ZodDefault<z.ZodString>;
|
|
114
|
+
coordinate: z.ZodTuple<[z.ZodNumber, z.ZodNumber], null>;
|
|
115
|
+
id: z.ZodOptional<z.ZodString>;
|
|
116
|
+
radius: z.ZodDefault<z.ZodNumber>;
|
|
117
|
+
strokeColor: z.ZodDefault<z.ZodString>;
|
|
118
|
+
strokeWidth: z.ZodDefault<z.ZodNumber>;
|
|
119
|
+
type: z.ZodLiteral<"marker">;
|
|
120
|
+
}, z.core.$strip>, z.ZodObject<{
|
|
121
|
+
coordinates: z.ZodArray<z.ZodArray<z.ZodTuple<[z.ZodNumber, z.ZodNumber], null>>>;
|
|
122
|
+
fill: z.ZodDefault<z.ZodString>;
|
|
123
|
+
id: z.ZodOptional<z.ZodString>;
|
|
124
|
+
opacity: z.ZodDefault<z.ZodNumber>;
|
|
125
|
+
stroke: z.ZodOptional<z.ZodString>;
|
|
126
|
+
strokeWidth: z.ZodDefault<z.ZodNumber>;
|
|
127
|
+
type: z.ZodLiteral<"polygon">;
|
|
128
|
+
}, z.core.$strip>], "type">>>;
|
|
129
|
+
size: z.ZodObject<{
|
|
130
|
+
dpr: z.ZodOptional<z.ZodLiteral<1>>;
|
|
131
|
+
height: z.ZodNumber;
|
|
132
|
+
width: z.ZodNumber;
|
|
133
|
+
}, z.core.$strip>;
|
|
134
|
+
}, z.core.$strip>;
|
|
135
|
+
declare const staticRenderManifestSchema: z.ZodObject<{
|
|
136
|
+
mapId: z.ZodString;
|
|
137
|
+
rendererVersion: z.ZodString;
|
|
138
|
+
schemaVersion: z.ZodLiteral<1>;
|
|
139
|
+
scene: z.ZodObject<{
|
|
140
|
+
camera: z.ZodDiscriminatedUnion<[z.ZodObject<{
|
|
141
|
+
bearing: z.ZodOptional<z.ZodNumber>;
|
|
142
|
+
center: z.ZodTuple<[z.ZodNumber, z.ZodNumber], null>;
|
|
143
|
+
type: z.ZodLiteral<"center">;
|
|
144
|
+
zoom: z.ZodNumber;
|
|
145
|
+
}, z.core.$strip>, z.ZodObject<{
|
|
146
|
+
bearing: z.ZodOptional<z.ZodNumber>;
|
|
147
|
+
bounds: z.ZodTuple<[z.ZodNumber, z.ZodNumber, z.ZodNumber, z.ZodNumber], null>;
|
|
148
|
+
padding: z.ZodOptional<z.ZodNumber>;
|
|
149
|
+
type: z.ZodLiteral<"bounds">;
|
|
150
|
+
}, z.core.$strip>], "type">;
|
|
151
|
+
map: z.ZodString;
|
|
152
|
+
overlays: z.ZodDefault<z.ZodArray<z.ZodDiscriminatedUnion<[z.ZodObject<{
|
|
153
|
+
color: z.ZodDefault<z.ZodString>;
|
|
154
|
+
coordinates: z.ZodArray<z.ZodTuple<[z.ZodNumber, z.ZodNumber], null>>;
|
|
155
|
+
id: z.ZodOptional<z.ZodString>;
|
|
156
|
+
opacity: z.ZodDefault<z.ZodNumber>;
|
|
157
|
+
type: z.ZodLiteral<"line">;
|
|
158
|
+
width: z.ZodDefault<z.ZodNumber>;
|
|
159
|
+
}, z.core.$strip>, z.ZodObject<{
|
|
160
|
+
color: z.ZodDefault<z.ZodString>;
|
|
161
|
+
coordinate: z.ZodTuple<[z.ZodNumber, z.ZodNumber], null>;
|
|
162
|
+
id: z.ZodOptional<z.ZodString>;
|
|
163
|
+
opacity: z.ZodDefault<z.ZodNumber>;
|
|
164
|
+
radius: z.ZodDefault<z.ZodNumber>;
|
|
165
|
+
strokeColor: z.ZodOptional<z.ZodString>;
|
|
166
|
+
strokeWidth: z.ZodDefault<z.ZodNumber>;
|
|
167
|
+
type: z.ZodLiteral<"circle">;
|
|
168
|
+
}, z.core.$strip>, z.ZodObject<{
|
|
169
|
+
color: z.ZodDefault<z.ZodString>;
|
|
170
|
+
coordinate: z.ZodTuple<[z.ZodNumber, z.ZodNumber], null>;
|
|
171
|
+
id: z.ZodOptional<z.ZodString>;
|
|
172
|
+
radius: z.ZodDefault<z.ZodNumber>;
|
|
173
|
+
strokeColor: z.ZodDefault<z.ZodString>;
|
|
174
|
+
strokeWidth: z.ZodDefault<z.ZodNumber>;
|
|
175
|
+
type: z.ZodLiteral<"marker">;
|
|
176
|
+
}, z.core.$strip>, z.ZodObject<{
|
|
177
|
+
coordinates: z.ZodArray<z.ZodArray<z.ZodTuple<[z.ZodNumber, z.ZodNumber], null>>>;
|
|
178
|
+
fill: z.ZodDefault<z.ZodString>;
|
|
179
|
+
id: z.ZodOptional<z.ZodString>;
|
|
180
|
+
opacity: z.ZodDefault<z.ZodNumber>;
|
|
181
|
+
stroke: z.ZodOptional<z.ZodString>;
|
|
182
|
+
strokeWidth: z.ZodDefault<z.ZodNumber>;
|
|
183
|
+
type: z.ZodLiteral<"polygon">;
|
|
184
|
+
}, z.core.$strip>], "type">>>;
|
|
185
|
+
size: z.ZodObject<{
|
|
186
|
+
dpr: z.ZodOptional<z.ZodLiteral<1>>;
|
|
187
|
+
height: z.ZodNumber;
|
|
188
|
+
width: z.ZodNumber;
|
|
189
|
+
}, z.core.$strip>;
|
|
190
|
+
}, z.core.$strip>;
|
|
191
|
+
styleId: z.ZodOptional<z.ZodString>;
|
|
192
|
+
styleRevision: z.ZodString;
|
|
193
|
+
styleUrl: z.ZodString;
|
|
194
|
+
}, z.core.$strip>;
|
|
195
|
+
type StaticCoordinate = z.infer<typeof coordinateSchema>;
|
|
196
|
+
type StaticSceneInput = z.input<typeof staticSceneSchema>;
|
|
197
|
+
type StaticScene = z.infer<typeof staticSceneSchema>;
|
|
198
|
+
type StaticOverlayInput = z.input<typeof staticOverlaySchema>;
|
|
199
|
+
type StaticOverlay = z.infer<typeof staticOverlaySchema>;
|
|
200
|
+
type StaticRenderManifest = z.infer<typeof staticRenderManifestSchema>;
|
|
201
|
+
type StaticMapResult = {
|
|
202
|
+
cached: boolean;
|
|
203
|
+
hash: string;
|
|
204
|
+
imageUrl: string;
|
|
205
|
+
status: "ready";
|
|
206
|
+
};
|
|
207
|
+
type StaticMapCreateOptions = {
|
|
208
|
+
apiKey?: string;
|
|
209
|
+
apiUrl?: string;
|
|
210
|
+
fetch?: typeof fetch;
|
|
211
|
+
};
|
|
212
|
+
declare function line(input: Omit<z.input<typeof lineOverlaySchema>, "type">): StaticOverlay;
|
|
213
|
+
declare function circle(input: Omit<z.input<typeof circleOverlaySchema>, "type">): StaticOverlay;
|
|
214
|
+
declare function marker(input: Omit<z.input<typeof markerOverlaySchema>, "type">): StaticOverlay;
|
|
215
|
+
declare function polygon(input: Omit<z.input<typeof polygonOverlaySchema>, "type">): StaticOverlay;
|
|
216
|
+
declare function validateStaticScene(input: unknown): {
|
|
217
|
+
ok: true;
|
|
218
|
+
scene: StaticScene;
|
|
219
|
+
} | {
|
|
220
|
+
error: string;
|
|
221
|
+
ok: false;
|
|
222
|
+
};
|
|
223
|
+
declare function validateStaticRenderManifest(input: unknown): {
|
|
224
|
+
manifest: StaticRenderManifest;
|
|
225
|
+
ok: true;
|
|
226
|
+
} | {
|
|
227
|
+
error: string;
|
|
228
|
+
ok: false;
|
|
229
|
+
};
|
|
230
|
+
declare function normalizeStaticScene(scene: StaticSceneInput): StaticScene;
|
|
231
|
+
declare function createRenderManifest(input: {
|
|
232
|
+
mapId: string;
|
|
233
|
+
rendererVersion: string;
|
|
234
|
+
scene: StaticSceneInput;
|
|
235
|
+
styleId?: string;
|
|
236
|
+
styleRevision: string;
|
|
237
|
+
styleUrl: string;
|
|
238
|
+
}): StaticRenderManifest;
|
|
239
|
+
declare function hashRenderManifest(manifest: StaticRenderManifest): Promise<string>;
|
|
240
|
+
declare function compileStaticOverlays(overlays: StaticOverlay[]): {
|
|
241
|
+
layers: Record<string, unknown>[];
|
|
242
|
+
sources: Record<string, Record<string, unknown>>;
|
|
243
|
+
};
|
|
244
|
+
declare function createStaticMap(scene: StaticSceneInput, options?: StaticMapCreateOptions): Promise<StaticMapResult>;
|
|
245
|
+
declare function stableStringify(value: unknown): string;
|
|
246
|
+
|
|
247
|
+
export { type StaticCoordinate, type StaticMapCreateOptions, type StaticMapResult, type StaticOverlay, type StaticOverlayInput, type StaticRenderManifest, type StaticScene, type StaticSceneInput, circle, compileStaticOverlays, createRenderManifest, createStaticMap, hashRenderManifest, line, marker, normalizeStaticScene, polygon, stableStringify, staticOverlaySchema, staticRenderManifestSchema, staticRendererSchemaVersion, staticSceneLimits, staticSceneSchema, staticSceneSchemaVersion, validateStaticRenderManifest, validateStaticScene };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,410 @@
|
|
|
1
|
+
// src/index.ts
|
|
2
|
+
import { z } from "zod";
|
|
3
|
+
var staticSceneSchemaVersion = 1;
|
|
4
|
+
var staticRendererSchemaVersion = 1;
|
|
5
|
+
var staticSceneLimits = {
|
|
6
|
+
maxDimension: 1280,
|
|
7
|
+
maxGeoJsonBytes: 96e3,
|
|
8
|
+
maxOverlays: 24,
|
|
9
|
+
maxPathCoordinates: 2e3,
|
|
10
|
+
maxPhysicalPixels: 1280 * 1280,
|
|
11
|
+
minDimension: 64
|
|
12
|
+
};
|
|
13
|
+
var coordinateSchema = z.tuple([z.number().finite(), z.number().finite()]).refine(
|
|
14
|
+
([lng, lat]) => lng >= -180 && lng <= 180 && lat >= -90 && lat <= 90,
|
|
15
|
+
{
|
|
16
|
+
message: "Expected [lng, lat] within world bounds"
|
|
17
|
+
}
|
|
18
|
+
);
|
|
19
|
+
var colorSchema = z.string().regex(/^#(?:[0-9a-fA-F]{3}|[0-9a-fA-F]{6})$/, {
|
|
20
|
+
message: "Expected a hex color like #C6A15B"
|
|
21
|
+
});
|
|
22
|
+
var sizeSchema = z.object({
|
|
23
|
+
dpr: z.literal(1).optional(),
|
|
24
|
+
height: z.number().int().min(staticSceneLimits.minDimension).max(staticSceneLimits.maxDimension),
|
|
25
|
+
width: z.number().int().min(staticSceneLimits.minDimension).max(staticSceneLimits.maxDimension)
|
|
26
|
+
}).refine(
|
|
27
|
+
(size) => size.width * size.height * (size.dpr ?? 1) <= staticSceneLimits.maxPhysicalPixels,
|
|
28
|
+
{
|
|
29
|
+
message: "Static map exceeds the maximum render pixel budget"
|
|
30
|
+
}
|
|
31
|
+
);
|
|
32
|
+
var centerCameraSchema = z.object({
|
|
33
|
+
bearing: z.number().finite().min(-180).max(180).optional(),
|
|
34
|
+
center: coordinateSchema,
|
|
35
|
+
type: z.literal("center"),
|
|
36
|
+
zoom: z.number().finite().min(0).max(22)
|
|
37
|
+
});
|
|
38
|
+
var boundsCameraSchema = z.object({
|
|
39
|
+
bearing: z.number().finite().min(-180).max(180).optional(),
|
|
40
|
+
bounds: z.tuple([
|
|
41
|
+
z.number().finite(),
|
|
42
|
+
z.number().finite(),
|
|
43
|
+
z.number().finite(),
|
|
44
|
+
z.number().finite()
|
|
45
|
+
]).refine(
|
|
46
|
+
([west, south, east, north]) => west >= -180 && west <= 180 && east >= -180 && east <= 180 && south >= -90 && south <= 90 && north >= -90 && north <= 90 && west !== east && south !== north,
|
|
47
|
+
{ message: "Expected bounds as [west, south, east, north]" }
|
|
48
|
+
),
|
|
49
|
+
padding: z.number().int().min(0).max(256).optional(),
|
|
50
|
+
type: z.literal("bounds")
|
|
51
|
+
}).refine((camera) => camera.bounds[3] > camera.bounds[1], {
|
|
52
|
+
message: "Bounds north must be greater than south"
|
|
53
|
+
});
|
|
54
|
+
var lineOverlaySchema = z.object({
|
|
55
|
+
color: colorSchema.default("#C6A15B"),
|
|
56
|
+
coordinates: z.array(coordinateSchema).min(2).max(staticSceneLimits.maxPathCoordinates),
|
|
57
|
+
id: z.string().trim().min(1).max(64).optional(),
|
|
58
|
+
opacity: z.number().finite().min(0).max(1).default(1),
|
|
59
|
+
type: z.literal("line"),
|
|
60
|
+
width: z.number().finite().min(0.5).max(32).default(4)
|
|
61
|
+
});
|
|
62
|
+
var circleOverlaySchema = z.object({
|
|
63
|
+
color: colorSchema.default("#C6A15B"),
|
|
64
|
+
coordinate: coordinateSchema,
|
|
65
|
+
id: z.string().trim().min(1).max(64).optional(),
|
|
66
|
+
opacity: z.number().finite().min(0).max(1).default(1),
|
|
67
|
+
radius: z.number().finite().min(1).max(64).default(6),
|
|
68
|
+
strokeColor: colorSchema.optional(),
|
|
69
|
+
strokeWidth: z.number().finite().min(0).max(16).default(0),
|
|
70
|
+
type: z.literal("circle")
|
|
71
|
+
});
|
|
72
|
+
var markerOverlaySchema = z.object({
|
|
73
|
+
color: colorSchema.default("#C6A15B"),
|
|
74
|
+
coordinate: coordinateSchema,
|
|
75
|
+
id: z.string().trim().min(1).max(64).optional(),
|
|
76
|
+
radius: z.number().finite().min(2).max(64).default(8),
|
|
77
|
+
strokeColor: colorSchema.default("#ffffff"),
|
|
78
|
+
strokeWidth: z.number().finite().min(0).max(16).default(2),
|
|
79
|
+
type: z.literal("marker")
|
|
80
|
+
});
|
|
81
|
+
var polygonOverlaySchema = z.object({
|
|
82
|
+
coordinates: z.array(
|
|
83
|
+
z.array(coordinateSchema).min(4).max(staticSceneLimits.maxPathCoordinates)
|
|
84
|
+
).min(1).max(16),
|
|
85
|
+
fill: colorSchema.default("#C6A15B"),
|
|
86
|
+
id: z.string().trim().min(1).max(64).optional(),
|
|
87
|
+
opacity: z.number().finite().min(0).max(1).default(0.28),
|
|
88
|
+
stroke: colorSchema.optional(),
|
|
89
|
+
strokeWidth: z.number().finite().min(0).max(16).default(0),
|
|
90
|
+
type: z.literal("polygon")
|
|
91
|
+
});
|
|
92
|
+
var staticOverlaySchema = z.discriminatedUnion("type", [
|
|
93
|
+
lineOverlaySchema,
|
|
94
|
+
circleOverlaySchema,
|
|
95
|
+
markerOverlaySchema,
|
|
96
|
+
polygonOverlaySchema
|
|
97
|
+
]);
|
|
98
|
+
var staticSceneSchema = z.object({
|
|
99
|
+
camera: z.discriminatedUnion("type", [
|
|
100
|
+
centerCameraSchema,
|
|
101
|
+
boundsCameraSchema
|
|
102
|
+
]),
|
|
103
|
+
map: z.string().trim().min(1).max(128),
|
|
104
|
+
overlays: z.array(staticOverlaySchema).max(staticSceneLimits.maxOverlays).default([]),
|
|
105
|
+
size: sizeSchema
|
|
106
|
+
});
|
|
107
|
+
var staticRenderManifestSchema = z.object({
|
|
108
|
+
mapId: z.string().trim().min(1).max(128),
|
|
109
|
+
rendererVersion: z.string().trim().min(1).max(64),
|
|
110
|
+
schemaVersion: z.literal(staticRendererSchemaVersion),
|
|
111
|
+
scene: staticSceneSchema,
|
|
112
|
+
styleId: z.string().trim().min(1).max(128).optional(),
|
|
113
|
+
styleRevision: z.string().trim().min(1).max(128),
|
|
114
|
+
styleUrl: z.string().trim().url().max(512).refine((value) => /^https?:\/\//.test(value), {
|
|
115
|
+
message: "Expected an http(s) style URL"
|
|
116
|
+
})
|
|
117
|
+
});
|
|
118
|
+
function line(input) {
|
|
119
|
+
return normalizeOverlay({ ...input, type: "line" });
|
|
120
|
+
}
|
|
121
|
+
function circle(input) {
|
|
122
|
+
return normalizeOverlay({ ...input, type: "circle" });
|
|
123
|
+
}
|
|
124
|
+
function marker(input) {
|
|
125
|
+
return normalizeOverlay({ ...input, type: "marker" });
|
|
126
|
+
}
|
|
127
|
+
function polygon(input) {
|
|
128
|
+
return normalizeOverlay({ ...input, type: "polygon" });
|
|
129
|
+
}
|
|
130
|
+
function validateStaticScene(input) {
|
|
131
|
+
const parsed = staticSceneSchema.safeParse(input);
|
|
132
|
+
if (!parsed.success) {
|
|
133
|
+
return {
|
|
134
|
+
error: parsed.error.issues.map((issue) => `${issue.path.join(".") || "scene"}: ${issue.message}`).join("; "),
|
|
135
|
+
ok: false
|
|
136
|
+
};
|
|
137
|
+
}
|
|
138
|
+
if (jsonByteLength(parsed.data.overlays) > staticSceneLimits.maxGeoJsonBytes) {
|
|
139
|
+
return {
|
|
140
|
+
error: `overlays exceed ${staticSceneLimits.maxGeoJsonBytes} bytes`,
|
|
141
|
+
ok: false
|
|
142
|
+
};
|
|
143
|
+
}
|
|
144
|
+
return { ok: true, scene: normalizeStaticScene(parsed.data) };
|
|
145
|
+
}
|
|
146
|
+
function validateStaticRenderManifest(input) {
|
|
147
|
+
const parsed = staticRenderManifestSchema.safeParse(input);
|
|
148
|
+
if (!parsed.success) {
|
|
149
|
+
return {
|
|
150
|
+
error: parsed.error.issues.map(
|
|
151
|
+
(issue) => `${issue.path.join(".") || "manifest"}: ${issue.message}`
|
|
152
|
+
).join("; "),
|
|
153
|
+
ok: false
|
|
154
|
+
};
|
|
155
|
+
}
|
|
156
|
+
const sceneValidation = validateStaticScene(parsed.data.scene);
|
|
157
|
+
if (!sceneValidation.ok) {
|
|
158
|
+
return sceneValidation;
|
|
159
|
+
}
|
|
160
|
+
return {
|
|
161
|
+
manifest: {
|
|
162
|
+
...parsed.data,
|
|
163
|
+
scene: sceneValidation.scene
|
|
164
|
+
},
|
|
165
|
+
ok: true
|
|
166
|
+
};
|
|
167
|
+
}
|
|
168
|
+
function normalizeStaticScene(scene) {
|
|
169
|
+
const parsed = staticSceneSchema.parse(scene);
|
|
170
|
+
return stripUndefined({
|
|
171
|
+
camera: parsed.camera.type === "center" ? {
|
|
172
|
+
bearing: roundNumber(parsed.camera.bearing ?? 0),
|
|
173
|
+
center: normalizeCoordinate(parsed.camera.center),
|
|
174
|
+
type: "center",
|
|
175
|
+
zoom: roundNumber(parsed.camera.zoom)
|
|
176
|
+
} : {
|
|
177
|
+
bearing: roundNumber(parsed.camera.bearing ?? 0),
|
|
178
|
+
bounds: parsed.camera.bounds.map(roundNumber),
|
|
179
|
+
padding: parsed.camera.padding ?? 32,
|
|
180
|
+
type: "bounds"
|
|
181
|
+
},
|
|
182
|
+
map: parsed.map,
|
|
183
|
+
overlays: parsed.overlays.map(
|
|
184
|
+
(overlay, index) => normalizeOverlay({
|
|
185
|
+
...overlay,
|
|
186
|
+
id: overlay.id ?? `overlay-${index + 1}`
|
|
187
|
+
})
|
|
188
|
+
),
|
|
189
|
+
size: {
|
|
190
|
+
dpr: parsed.size.dpr ?? 1,
|
|
191
|
+
height: parsed.size.height,
|
|
192
|
+
width: parsed.size.width
|
|
193
|
+
}
|
|
194
|
+
});
|
|
195
|
+
}
|
|
196
|
+
function createRenderManifest(input) {
|
|
197
|
+
return staticRenderManifestSchema.parse(
|
|
198
|
+
stripUndefined({
|
|
199
|
+
mapId: input.mapId,
|
|
200
|
+
rendererVersion: input.rendererVersion,
|
|
201
|
+
schemaVersion: staticRendererSchemaVersion,
|
|
202
|
+
scene: normalizeStaticScene({
|
|
203
|
+
...input.scene,
|
|
204
|
+
map: input.mapId
|
|
205
|
+
}),
|
|
206
|
+
styleId: input.styleId,
|
|
207
|
+
styleRevision: input.styleRevision,
|
|
208
|
+
styleUrl: input.styleUrl
|
|
209
|
+
})
|
|
210
|
+
);
|
|
211
|
+
}
|
|
212
|
+
async function hashRenderManifest(manifest) {
|
|
213
|
+
const normalized = staticRenderManifestSchema.parse({
|
|
214
|
+
...manifest,
|
|
215
|
+
scene: normalizeStaticScene(manifest.scene)
|
|
216
|
+
});
|
|
217
|
+
const bytes = new TextEncoder().encode(stableStringify(normalized));
|
|
218
|
+
const digest = await crypto.subtle.digest("SHA-256", bytes);
|
|
219
|
+
return base64Url(new Uint8Array(digest));
|
|
220
|
+
}
|
|
221
|
+
function compileStaticOverlays(overlays) {
|
|
222
|
+
const sources = {};
|
|
223
|
+
const layers = [];
|
|
224
|
+
for (const [index, overlay] of overlays.entries()) {
|
|
225
|
+
const id = safeLayerId(
|
|
226
|
+
`overlay-${index + 1}-${overlay.id ?? overlay.type}`
|
|
227
|
+
);
|
|
228
|
+
const sourceId = `${id}-source`;
|
|
229
|
+
if (overlay.type === "line") {
|
|
230
|
+
sources[sourceId] = {
|
|
231
|
+
data: feature("LineString", overlay.coordinates),
|
|
232
|
+
type: "geojson"
|
|
233
|
+
};
|
|
234
|
+
layers.push({
|
|
235
|
+
id,
|
|
236
|
+
layout: { "line-cap": "round", "line-join": "round" },
|
|
237
|
+
paint: {
|
|
238
|
+
"line-color": overlay.color,
|
|
239
|
+
"line-opacity": overlay.opacity,
|
|
240
|
+
"line-width": overlay.width
|
|
241
|
+
},
|
|
242
|
+
source: sourceId,
|
|
243
|
+
type: "line"
|
|
244
|
+
});
|
|
245
|
+
continue;
|
|
246
|
+
}
|
|
247
|
+
if (overlay.type === "polygon") {
|
|
248
|
+
sources[sourceId] = {
|
|
249
|
+
data: feature("Polygon", overlay.coordinates),
|
|
250
|
+
type: "geojson"
|
|
251
|
+
};
|
|
252
|
+
layers.push({
|
|
253
|
+
id,
|
|
254
|
+
paint: {
|
|
255
|
+
"fill-color": overlay.fill,
|
|
256
|
+
"fill-opacity": overlay.opacity
|
|
257
|
+
},
|
|
258
|
+
source: sourceId,
|
|
259
|
+
type: "fill"
|
|
260
|
+
});
|
|
261
|
+
if (overlay.stroke && overlay.strokeWidth > 0) {
|
|
262
|
+
layers.push({
|
|
263
|
+
id: `${id}-stroke`,
|
|
264
|
+
layout: { "line-cap": "round", "line-join": "round" },
|
|
265
|
+
paint: {
|
|
266
|
+
"line-color": overlay.stroke,
|
|
267
|
+
"line-width": overlay.strokeWidth
|
|
268
|
+
},
|
|
269
|
+
source: sourceId,
|
|
270
|
+
type: "line"
|
|
271
|
+
});
|
|
272
|
+
}
|
|
273
|
+
continue;
|
|
274
|
+
}
|
|
275
|
+
sources[sourceId] = {
|
|
276
|
+
data: feature("Point", overlay.coordinate),
|
|
277
|
+
type: "geojson"
|
|
278
|
+
};
|
|
279
|
+
layers.push({
|
|
280
|
+
id,
|
|
281
|
+
paint: {
|
|
282
|
+
"circle-color": overlay.color,
|
|
283
|
+
"circle-opacity": overlay.type === "circle" ? overlay.opacity : 1,
|
|
284
|
+
"circle-radius": overlay.radius,
|
|
285
|
+
"circle-stroke-color": overlay.strokeColor ?? overlay.color,
|
|
286
|
+
"circle-stroke-width": overlay.strokeWidth
|
|
287
|
+
},
|
|
288
|
+
source: sourceId,
|
|
289
|
+
type: "circle"
|
|
290
|
+
});
|
|
291
|
+
}
|
|
292
|
+
return { layers, sources };
|
|
293
|
+
}
|
|
294
|
+
async function createStaticMap(scene, options = {}) {
|
|
295
|
+
const validation = validateStaticScene(scene);
|
|
296
|
+
if (!validation.ok) {
|
|
297
|
+
throw new Error(`Invalid Tileflow static scene: ${validation.error}`);
|
|
298
|
+
}
|
|
299
|
+
const fetcher = options.fetch ?? fetch;
|
|
300
|
+
const apiUrl = normalizeUrl(options.apiUrl ?? "https://api.tileflow.dev");
|
|
301
|
+
const headers = {
|
|
302
|
+
"Content-Type": "application/json"
|
|
303
|
+
};
|
|
304
|
+
if (options.apiKey) {
|
|
305
|
+
headers.Authorization = `Bearer ${options.apiKey}`;
|
|
306
|
+
}
|
|
307
|
+
const response = await fetcher(`${apiUrl}/v1/static/maps`, {
|
|
308
|
+
body: JSON.stringify(validation.scene),
|
|
309
|
+
headers,
|
|
310
|
+
method: "POST"
|
|
311
|
+
});
|
|
312
|
+
if (!response.ok) {
|
|
313
|
+
const error = await response.text();
|
|
314
|
+
throw new Error(`Tileflow static map failed: ${response.status} ${error}`);
|
|
315
|
+
}
|
|
316
|
+
return await response.json();
|
|
317
|
+
}
|
|
318
|
+
function stableStringify(value) {
|
|
319
|
+
return JSON.stringify(sortJson(value));
|
|
320
|
+
}
|
|
321
|
+
function normalizeOverlay(overlay) {
|
|
322
|
+
const parsed = staticOverlaySchema.parse(overlay);
|
|
323
|
+
const normalized = stripUndefined({ ...parsed });
|
|
324
|
+
if ("coordinate" in normalized) {
|
|
325
|
+
normalized.coordinate = normalizeCoordinate(
|
|
326
|
+
normalized.coordinate
|
|
327
|
+
);
|
|
328
|
+
}
|
|
329
|
+
if ("coordinates" in normalized) {
|
|
330
|
+
normalized.coordinates = normalizeCoordinates(normalized.coordinates);
|
|
331
|
+
}
|
|
332
|
+
return normalized;
|
|
333
|
+
}
|
|
334
|
+
function normalizeCoordinates(value) {
|
|
335
|
+
if (!Array.isArray(value)) {
|
|
336
|
+
return value;
|
|
337
|
+
}
|
|
338
|
+
if (value.length === 2 && typeof value[0] === "number" && typeof value[1] === "number") {
|
|
339
|
+
return normalizeCoordinate(value);
|
|
340
|
+
}
|
|
341
|
+
return value.map(normalizeCoordinates);
|
|
342
|
+
}
|
|
343
|
+
function normalizeCoordinate(coordinate) {
|
|
344
|
+
return [roundNumber(coordinate[0]), roundNumber(coordinate[1])];
|
|
345
|
+
}
|
|
346
|
+
function roundNumber(value) {
|
|
347
|
+
return Number(value.toFixed(6));
|
|
348
|
+
}
|
|
349
|
+
function stripUndefined(value) {
|
|
350
|
+
if (!value || typeof value !== "object" || Array.isArray(value)) {
|
|
351
|
+
return value;
|
|
352
|
+
}
|
|
353
|
+
return Object.fromEntries(
|
|
354
|
+
Object.entries(value).filter(([, entry]) => entry !== void 0)
|
|
355
|
+
);
|
|
356
|
+
}
|
|
357
|
+
function sortJson(value) {
|
|
358
|
+
if (Array.isArray(value)) {
|
|
359
|
+
return value.map(sortJson);
|
|
360
|
+
}
|
|
361
|
+
if (value && typeof value === "object") {
|
|
362
|
+
return Object.fromEntries(
|
|
363
|
+
Object.entries(value).sort(([left], [right]) => left.localeCompare(right)).map(([key, entry]) => [key, sortJson(entry)])
|
|
364
|
+
);
|
|
365
|
+
}
|
|
366
|
+
return value;
|
|
367
|
+
}
|
|
368
|
+
function feature(type, coordinates) {
|
|
369
|
+
return {
|
|
370
|
+
geometry: { coordinates, type },
|
|
371
|
+
properties: {},
|
|
372
|
+
type: "Feature"
|
|
373
|
+
};
|
|
374
|
+
}
|
|
375
|
+
function safeLayerId(value) {
|
|
376
|
+
return value.replace(/[^a-zA-Z0-9_-]/g, "-").slice(0, 80);
|
|
377
|
+
}
|
|
378
|
+
function jsonByteLength(value) {
|
|
379
|
+
return new TextEncoder().encode(JSON.stringify(value)).length;
|
|
380
|
+
}
|
|
381
|
+
function base64Url(bytes) {
|
|
382
|
+
let binary = "";
|
|
383
|
+
for (const byte of bytes) {
|
|
384
|
+
binary += String.fromCharCode(byte);
|
|
385
|
+
}
|
|
386
|
+
return btoa(binary).replace(/\+/g, "-").replace(/\//g, "_").replace(/=+$/g, "");
|
|
387
|
+
}
|
|
388
|
+
function normalizeUrl(value) {
|
|
389
|
+
return value.replace(/\/+$/, "");
|
|
390
|
+
}
|
|
391
|
+
export {
|
|
392
|
+
circle,
|
|
393
|
+
compileStaticOverlays,
|
|
394
|
+
createRenderManifest,
|
|
395
|
+
createStaticMap,
|
|
396
|
+
hashRenderManifest,
|
|
397
|
+
line,
|
|
398
|
+
marker,
|
|
399
|
+
normalizeStaticScene,
|
|
400
|
+
polygon,
|
|
401
|
+
stableStringify,
|
|
402
|
+
staticOverlaySchema,
|
|
403
|
+
staticRenderManifestSchema,
|
|
404
|
+
staticRendererSchemaVersion,
|
|
405
|
+
staticSceneLimits,
|
|
406
|
+
staticSceneSchema,
|
|
407
|
+
staticSceneSchemaVersion,
|
|
408
|
+
validateStaticRenderManifest,
|
|
409
|
+
validateStaticScene
|
|
410
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@tileflow/static",
|
|
3
|
+
"version": "0.1.0-alpha.0",
|
|
4
|
+
"description": "Static map request schemas and helpers for Tileflow.",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"homepage": "https://tileflow.dev",
|
|
7
|
+
"repository": {
|
|
8
|
+
"type": "git",
|
|
9
|
+
"url": "git+https://github.com/tileflow/tileflow.git",
|
|
10
|
+
"directory": "packages/static"
|
|
11
|
+
},
|
|
12
|
+
"bugs": {
|
|
13
|
+
"url": "https://github.com/tileflow/tileflow/issues"
|
|
14
|
+
},
|
|
15
|
+
"keywords": [
|
|
16
|
+
"tileflow",
|
|
17
|
+
"maps",
|
|
18
|
+
"static-maps",
|
|
19
|
+
"maplibre",
|
|
20
|
+
"tiles"
|
|
21
|
+
],
|
|
22
|
+
"main": "./dist/index.js",
|
|
23
|
+
"types": "./dist/index.d.ts",
|
|
24
|
+
"files": [
|
|
25
|
+
"dist"
|
|
26
|
+
],
|
|
27
|
+
"exports": {
|
|
28
|
+
".": {
|
|
29
|
+
"types": "./dist/index.d.ts",
|
|
30
|
+
"import": "./dist/index.js",
|
|
31
|
+
"default": "./dist/index.js"
|
|
32
|
+
}
|
|
33
|
+
},
|
|
34
|
+
"publishConfig": {
|
|
35
|
+
"access": "public"
|
|
36
|
+
},
|
|
37
|
+
"dependencies": {
|
|
38
|
+
"zod": "^4.4.3"
|
|
39
|
+
},
|
|
40
|
+
"scripts": {
|
|
41
|
+
"build": "tsup src/index.ts --format esm --dts --clean",
|
|
42
|
+
"typecheck": "tsc -p tsconfig.json --noEmit",
|
|
43
|
+
"lint": "tsc -p tsconfig.json --noEmit"
|
|
44
|
+
}
|
|
45
|
+
}
|