UNPKG

@upstart.gg/sdk

Version:

You can test the CLI without recompiling by running:

64 lines (58 loc) 2.35 kB
import { type Static, Type, type TArray } from "@sinclair/typebox"; import type { Datasource } from "./datasources/types"; /** * For now, defineDatasources() force the usage of a custom (internal) datasource * @returns */ export function defineDatasource<D extends Datasource>(datasource: D) { return { ...datasource, provider: "internal", // make sure to use Upstart provider schema: mapDatasourceSchemaWithInternalProperties( // datasource.provider === "internal" ? datasource.schema : getSchemaByProvider(datasource.provider), datasource.schema, ), }; } export const datasourceSystemProperties = Type.Object({ $id: Type.Optional(Type.String({ title: "Id", format: "text" })), $publicationDate: Type.Optional(Type.String({ title: "Publication Date", format: "date-time" })), $slug: Type.Optional(Type.String({ title: "Slug", format: "slug" })), $lastModificationDate: Type.Optional(Type.String({ title: "Last Modification", format: "date-time" })), }); export type DatasourceSystemProperties = Static<typeof datasourceSystemProperties>; /** * Map a datasource schema to include $id, $createdAt, and $updatedAt properties. */ export function mapDatasourceSchemaWithInternalProperties(schema: TArray): Datasource["schema"] { const { items, ...rest } = schema; return { ...rest, items: { ...items, type: "object", properties: { ...items.properties, ...datasourceSystemProperties.properties, }, // We should likely reuse `datasourceSystemProperties.required` instead of hardcoding but I'm not sure // how the dashboard is handling it right now when creating new items required: Array.from(new Set(["$id", "$slug", "$lastModificationDate", ...items.required])), }, }; } /** * Extract the indexed fields of a datasource with their titles */ export function getDatasourceIndexedFieldsWithTitles(datasource: Datasource) { if ("indexes" in datasource === false) { console.error("no datasource or indexes found", datasource); return []; } const properties = datasource.schema?.items?.properties || {}; const uniqueFields = Array.from(new Set(datasource.indexes?.map((idx) => idx.fields[0]) ?? [])); return uniqueFields.map((field) => ({ value: field, title: properties[field]?.title || field, })); }