Skip to content

Commit 01b273a

Browse files
committed
improve schema
1 parent 92d1827 commit 01b273a

33 files changed

+164
-251
lines changed

functions/package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

functions/package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "functions",
3-
"version": "1.8.0",
3+
"version": "2.0.0",
44
"scripts": {
55
"lint": "eslint --ext .js,.ts .",
66
"lint:fix": "eslint --fix --ext .js,.ts .",
@@ -26,9 +26,9 @@
2626
"firebase-admin": "^12.1.0",
2727
"firebase-functions": "^4.9.0",
2828
"sharp": "^0.33.3",
29-
"stripe": "^15.2.0",
29+
"stripe": "^15.3.0",
3030
"uuid": "^9.0.1",
31-
"zod": "^3.22.4"
31+
"zod": "^3.22.5"
3232
},
3333
"devDependencies": {
3434
"@types/express": "^4.17.21",

functions/src/contents.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ const publish = onCall<PublishContentData>(async request => {
3131
if (spaceSnapshot.exists && contentSnapshot.exists) {
3232
const space: Space = spaceSnapshot.data() as Space;
3333
const content: ContentDocument = contentSnapshot.data() as ContentDocument;
34-
const schemas = schemasSnapshot.docs.filter(it => it.exists).map(it => it.data() as Schema);
34+
const schemas = new Map(schemasSnapshot.docs.map(it => [it.id, it.data() as Schema]));
3535
for (const locale of space.locales) {
3636
const documentStorage: ContentDocumentStorage = {
3737
id: contentSnapshot.id,
@@ -96,7 +96,7 @@ const onContentUpdate = onDocumentUpdated('spaces/{spaceId}/contents/{contentId}
9696
const schemasSnapshot = await findSchemas(spaceId).get();
9797
const space: Space = spaceSnapshot.data() as Space;
9898
const content: ContentDocument = contentAfter;
99-
const schemas = schemasSnapshot.docs.filter(it => it.exists).map(it => it.data() as Schema);
99+
const schemas = new Map(schemasSnapshot.docs.map(it => [it.id, it.data() as Schema]));
100100
for (const locale of space.locales) {
101101
const documentStorage: ContentDocumentStorage = {
102102
id: event.data.after.id,

functions/src/models/schema.model.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ export enum SchemaType {
99
export type Schema = SchemaComponent | SchemaEnum;
1010

1111
export interface SchemaBase {
12-
name: string;
1312
type: SchemaType;
1413
displayName?: string;
1514

functions/src/models/schema.zod.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
import { z } from 'zod';
22
import { AssetFileType, SchemaFieldKind, SchemaType } from './schema.model';
33

4+
const ID_PATTERN = /^[a-z]+[a-zA-Z0-9_]*[a-zA-Z0-9]+$/;
5+
46
export const schemaTypeSchema = z.nativeEnum(SchemaType);
57

68
export const schemaBaseSchema = z.object({
7-
id: z.string(),
8-
name: z.string(),
9+
id: z.string().regex(ID_PATTERN),
910
type: schemaTypeSchema,
1011
displayName: z.string().optional(),
1112
});
@@ -151,7 +152,7 @@ export const schemaComponentSchema = schemaBaseSchema.extend({
151152
type: z.union([z.literal(SchemaType.ROOT), z.literal(SchemaType.NODE)]),
152153
previewField: z.string().optional(),
153154
previewImage: z.string().optional(),
154-
fields: z.array(schemaFieldSchema).optional(),
155+
// fields: z.array(schemaFieldSchema).optional(),
155156
});
156157

157158
export const schemaSchema = z.union([schemaComponentSchema, schemaEnumSchema]);

functions/src/services/content.service.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -137,12 +137,12 @@ export function contentCachePath(spaceId: string, contentId: string, version: st
137137
* @param {string} locale locale
138138
* @return {ContentData} content
139139
*/
140-
export function extractContent(content: ContentData, schemas: Schema[], locale: string): ContentData {
140+
export function extractContent(content: ContentData, schemas: Map<string, Schema>, locale: string): ContentData {
141141
const extractedContentData: ContentData = {
142142
_id: content._id,
143143
schema: content.schema,
144144
};
145-
const schema = schemas.find(it => it.name == content.schema);
145+
const schema = schemas.get(content.schema);
146146
if (schema && (schema.type === SchemaType.ROOT || schema.type === SchemaType.NODE)) {
147147
for (const field of schema?.fields || []) {
148148
if (field.kind === SchemaFieldKind.SCHEMA) {

functions/src/services/open-api.service.ts

Lines changed: 15 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -185,10 +185,9 @@ export function generateOpenApi(schemasById: Map<string, Schema>): OpenApiSchema
185185
},
186186
};
187187
const rootSchemas: string[] = [];
188-
for (const item of schemasById.values()) {
188+
for (const [key, item] of schemasById.entries()) {
189189
if (item.type === SchemaType.ROOT) {
190-
const { name } = item;
191-
const pascalName = name[0].toUpperCase() + name.slice(1);
190+
const pascalName = key[0].toUpperCase() + key.slice(1);
192191
rootSchemas.push(pascalName);
193192
}
194193
}
@@ -203,8 +202,9 @@ export function generateOpenApi(schemasById: Map<string, Schema>): OpenApiSchema
203202
propertyName: 'schema',
204203
},
205204
};
206-
for (const item of schemasById.values()) {
207-
const [name, schema] = schemaToOpenApiSchemaDefinition(item, schemasById);
205+
206+
for (const [id, item] of schemasById.entries()) {
207+
const [name, schema] = schemaToOpenApiSchemaDefinition(id, item);
208208
schemasDefinition[name] = schema;
209209
}
210210

@@ -617,13 +617,12 @@ export function generateOpenApi(schemasById: Map<string, Schema>): OpenApiSchema
617617

618618
/**
619619
* Schema
620+
* @param {string} id - result
620621
* @param {Schema} schema - result
621-
* @param {Map<string, Schema>} schemasById - zzz
622622
* @return {OpenApiSchemaDefinition} asfdasf
623623
*/
624-
export function schemaToOpenApiSchemaDefinition(schema: Schema, schemasById: Map<string, Schema>): [string, OpenApiSchemaDefinition] {
625-
const { name } = schema;
626-
const pascalName = name[0].toUpperCase() + name.slice(1);
624+
export function schemaToOpenApiSchemaDefinition(id: string, schema: Schema): [string, OpenApiSchemaDefinition] {
625+
const pascalName = id[0].toUpperCase() + id.slice(1);
627626
if (schema.type === SchemaType.ENUM) {
628627
return [
629628
pascalName,
@@ -645,12 +644,12 @@ export function schemaToOpenApiSchemaDefinition(schema: Schema, schemasById: Map
645644
},
646645
schema: {
647646
type: 'string',
648-
enum: [name],
647+
enum: [id],
649648
description: 'Schema name',
650649
},
651650
};
652651
for (const item of schema.fields || []) {
653-
const [name, schema] = fieldToOpenApiSchemaDefinition(item, schemasById);
652+
const [name, schema] = fieldToOpenApiSchemaDefinition(item);
654653
schemasDefinition[name] = schema;
655654
}
656655
return [
@@ -673,10 +672,9 @@ export function schemaToOpenApiSchemaDefinition(schema: Schema, schemasById: Map
673672
/**
674673
* Convert from Internal to OpenAPI
675674
* @param {SchemaField} field
676-
* @param {Map<string, Schema>} schemasById
677675
* @return {OpenApiSchema} OpenApiSchema
678676
*/
679-
export function fieldToOpenApiSchemaDefinition(field: SchemaField, schemasById: Map<string, Schema>): [string, OpenApiSchemaDefinition] {
677+
export function fieldToOpenApiSchemaDefinition(field: SchemaField): [string, OpenApiSchemaDefinition] {
680678
if (field.kind === SchemaFieldKind.TEXT || field.kind === SchemaFieldKind.TEXTAREA || field.kind === SchemaFieldKind.MARKDOWN) {
681679
return [
682680
field.name,
@@ -749,8 +747,7 @@ export function fieldToOpenApiSchemaDefinition(field: SchemaField, schemasById:
749747
},
750748
];
751749
} else {
752-
const ref = schemasById.get(field.source);
753-
const name = ref?.name || 'unknown';
750+
const name = field.source || 'unknown';
754751
const pascalName = name[0].toUpperCase() + name.slice(1);
755752
return [
756753
field.name,
@@ -777,8 +774,7 @@ export function fieldToOpenApiSchemaDefinition(field: SchemaField, schemasById:
777774
},
778775
];
779776
} else {
780-
const ref = schemasById.get(field.source);
781-
const name = ref?.name || 'unknown';
777+
const name = field.source;
782778
const pascalName = name[0].toUpperCase() + name.slice(1);
783779
return [
784780
field.name,
@@ -852,8 +848,7 @@ export function fieldToOpenApiSchemaDefinition(field: SchemaField, schemasById:
852848
description: field.description,
853849
oneOf:
854850
field.schemas?.map(it => {
855-
const ref = schemasById.get(it);
856-
const name = ref?.name || 'unknown';
851+
const name = it;
857852
const pascalName = name[0].toUpperCase() + name.slice(1);
858853
return {
859854
$ref: `#/components/schemas/${pascalName}`,
@@ -874,8 +869,7 @@ export function fieldToOpenApiSchemaDefinition(field: SchemaField, schemasById:
874869
items: {
875870
oneOf:
876871
field.schemas?.map(it => {
877-
const ref = schemasById.get(it);
878-
const name = ref?.name || 'unknown';
872+
const name = it;
879873
const pascalName = name[0].toUpperCase() + name.slice(1);
880874
return {
881875
$ref: `#/components/schemas/${pascalName}`,

functions/src/tasks.ts

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -553,7 +553,6 @@ async function schemasExport(spaceId: string, taskId: string, task: Task): Promi
553553
if (schema.type === SchemaType.ROOT || schema.type === SchemaType.NODE) {
554554
exportSchemas.push({
555555
id: doc.id,
556-
name: schema.name,
557556
type: schema.type,
558557
displayName: schema.displayName,
559558
previewField: schema.previewField,
@@ -563,7 +562,6 @@ async function schemasExport(spaceId: string, taskId: string, task: Task): Promi
563562
} else if (schema.type === SchemaType.ENUM) {
564563
exportSchemas.push({
565564
id: doc.id,
566-
name: schema.name,
567565
type: schema.type,
568566
displayName: schema.displayName,
569567
values: schema.values,
@@ -621,7 +619,6 @@ async function schemasImport(spaceId: string, taskId: string): Promise<ZodError
621619
if (schemaSnapshot.exists) {
622620
if (schema.type === SchemaType.ROOT || schema.type === SchemaType.NODE) {
623621
const update: UpdateData<SchemaComponent> = {
624-
name: schema.name,
625622
type: schema.type,
626623
displayName: schema.displayName || FieldValue.delete(),
627624
previewField: schema.previewField || FieldValue.delete(),
@@ -632,7 +629,6 @@ async function schemasImport(spaceId: string, taskId: string): Promise<ZodError
632629
batch.update(schemaRef, update);
633630
} else if (schema.type === SchemaType.ENUM) {
634631
const update: UpdateData<SchemaEnum> = {
635-
name: schema.name,
636632
type: schema.type,
637633
displayName: schema.displayName || FieldValue.delete(),
638634
values: schema.values || FieldValue.delete(),
@@ -643,7 +639,6 @@ async function schemasImport(spaceId: string, taskId: string): Promise<ZodError
643639
} else {
644640
if (schema.type === SchemaType.ROOT || schema.type === SchemaType.NODE) {
645641
const add: WithFieldValue<SchemaComponent> = {
646-
name: schema.name,
647642
type: schema.type,
648643
createdAt: FieldValue.serverTimestamp(),
649644
updatedAt: FieldValue.serverTimestamp(),
@@ -655,7 +650,6 @@ async function schemasImport(spaceId: string, taskId: string): Promise<ZodError
655650
batch.set(schemaRef, add);
656651
} else if (schema.type === SchemaType.ENUM) {
657652
const add: WithFieldValue<SchemaEnum> = {
658-
name: schema.name,
659653
type: schema.type,
660654
createdAt: FieldValue.serverTimestamp(),
661655
updatedAt: FieldValue.serverTimestamp(),

functions/src/v1.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import {
1414
findSpaceById,
1515
findTokenById,
1616
identifySpaceLocale,
17-
validateToken
17+
validateToken,
1818
} from './services';
1919

2020
// API V1

0 commit comments

Comments
 (0)