Derive TS type from JSON Schema
So we don't define (almost) the same schema twice
This commit is contained in:
@@ -1,53 +1,43 @@
|
||||
import BaseClient from 'remotestoragejs/release/types/baseclient';
|
||||
import Geohash from 'latlon-geohash';
|
||||
import { ulid } from 'ulid';
|
||||
import { FromSchema } from 'json-schema-to-ts';
|
||||
|
||||
interface Place {
|
||||
id: string;
|
||||
title: string;
|
||||
lat: number;
|
||||
lon: number;
|
||||
geohash: string;
|
||||
zoom?: number;
|
||||
url?: string;
|
||||
osmId?: string;
|
||||
osmType?: string;
|
||||
osmTags?: Record<string, string>;
|
||||
description?: string;
|
||||
tags?: string[];
|
||||
createdAt: string;
|
||||
updatedAt?: string;
|
||||
[key: string]: any;
|
||||
}
|
||||
const placeSchema = {
|
||||
type: 'object',
|
||||
properties: {
|
||||
id: { type: 'string' },
|
||||
title: { type: 'string' },
|
||||
lat: { type: 'number' },
|
||||
lon: { type: 'number' },
|
||||
geohash: { type: 'string' },
|
||||
zoom: { type: 'number' },
|
||||
url: { type: 'string' },
|
||||
osmId: { type: 'string' },
|
||||
osmType: { type: 'string' },
|
||||
osmTags: {
|
||||
type: 'object',
|
||||
additionalProperties: { type: 'string' },
|
||||
},
|
||||
description: { type: 'string' },
|
||||
tags: {
|
||||
type: 'array',
|
||||
items: { type: 'string' },
|
||||
default: [],
|
||||
},
|
||||
createdAt: { type: 'string', format: 'date-time' },
|
||||
updatedAt: { type: 'string', format: 'date-time' },
|
||||
},
|
||||
required: ['id', 'title', 'lat', 'lon', 'geohash', 'createdAt'],
|
||||
} as const;
|
||||
|
||||
type Place = FromSchema<typeof placeSchema> & { [key: string]: any };
|
||||
|
||||
const Places = function (
|
||||
privateClient: BaseClient /*, publicClient: BaseClient */
|
||||
) {
|
||||
// Define Schema
|
||||
privateClient.declareType('place', {
|
||||
type: 'object',
|
||||
properties: {
|
||||
id: { type: 'string', required: true },
|
||||
title: { type: 'string', required: true },
|
||||
lat: { type: 'number', required: true },
|
||||
lon: { type: 'number', required: true },
|
||||
geohash: { type: 'string', required: true },
|
||||
zoom: { type: 'number' },
|
||||
url: { type: 'string' },
|
||||
osmId: { type: 'string' },
|
||||
osmType: { type: 'string' },
|
||||
osmTags: { type: 'object' },
|
||||
description: { type: 'string' },
|
||||
tags: {
|
||||
type: 'array',
|
||||
items: { type: 'string' },
|
||||
default: [],
|
||||
},
|
||||
createdAt: { type: 'string', format: 'date-time', required: true },
|
||||
updatedAt: { type: 'string', format: 'date-time' },
|
||||
},
|
||||
required: ['id', 'title', 'lat', 'lon', 'geohash', 'createdAt'],
|
||||
});
|
||||
privateClient.declareType('place', placeSchema as any);
|
||||
|
||||
// Helper to normalize place object
|
||||
function preparePlace(data: Partial<Place>): Place {
|
||||
@@ -70,6 +60,7 @@ const Places = function (
|
||||
lon,
|
||||
geohash,
|
||||
title,
|
||||
tags: data.tags || [],
|
||||
createdAt: data.createdAt || now,
|
||||
updatedAt: data.id ? now : undefined,
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user