Open source website builder and Webflow alternative. Webstudio is an advanced visual builder that connects to any headless CMS, supports all CSS properties, and can be hosted anywhere, including with us.
TypeScript accessibilityalternativecloudflarecloudflare-workersdesignfreefrontendopen-sourceradix-uireactremixweb-developmentwebflowwebsite-builderwebsite-developmentwebsiteswebstudio
fixing types
Oleg Isonenc69a74cc9101
diff --git a/apps/designer/app/designer/shared/assets/asset-upload.tsx b/apps/designer/app/designer/shared/assets/asset-upload.tsxindex 5e5fc55923..3e27ca3bc9 100644--- a/apps/designer/app/designer/shared/assets/asset-upload.tsx+++ b/apps/designer/app/designer/shared/assets/asset-upload.tsx@@ -3,6 +3,7 @@ import ObjectID from "bson-objectid";import { ChangeEvent, useRef } from "react";import { Button, Flex, Text } from "@webstudio-is/design-system";import { UploadIcon } from "@webstudio-is/icons";+import type { AssetType } from "@webstudio-is/asset-uploader";import type { BaseAsset } from "./types";const readAssets = (fileList: FileList): Promise<BaseAsset[]> => {@@ -37,7 +38,7 @@ const acceptMap = {type AssetUploadProps = {onSubmit: (assets: Array<BaseAsset>) => void;- type: "image" | "font";+ type: AssetType;};export const AssetUpload = ({ onSubmit, type }: AssetUploadProps) => {diff --git a/packages/asset-uploader/src/db/create.ts b/packages/asset-uploader/src/db/create.tsindex 51e1780ce2..a54c481be9 100644--- a/packages/asset-uploader/src/db/create.ts+++ b/packages/asset-uploader/src/db/create.ts@@ -3,49 +3,42 @@ import {type Location,type Project,} from "@webstudio-is/prisma-client";-import { type Metadata } from "sharp";-import { formatAsset } from "../utils/format-asset";+import {+ formatAsset,+ type FontMeta,+ type ImageMeta,+} from "../utils/format-asset";type BaseOptions = {name: string;size: number;- metadata: Metadata;location: Location;+ format?: string;};type Options =| ({type: "image";+ meta: ImageMeta;} & BaseOptions)- | ({ type: "font" } & BaseOptions);+ | ({ type: "font"; meta: FontMeta } & BaseOptions);const create = (projectId: Project["id"], options: Options) => {- const size = options.size || options.metadata.size || 0;- const { metadata, name, location } = options;+ const size = options.size || 0;+ const { meta, format, name, location } = options;- if (options.type === "image") {- return prisma.asset.create({- data: {- location,- name,- size,- format: metadata.format,- meta: JSON.stringify({- width: metadata.width,- height: metadata.height,- }),- projectId,- },- });- }+ const baseData = {+ location,+ name,+ size,+ format,+ projectId,+ };return prisma.asset.create({data: {- location,- name,- size,- format: metadata.format,- projectId,+ ...baseData,+ meta: JSON.stringify(meta),},});};diff --git a/packages/asset-uploader/src/targets/s3/upload.ts b/packages/asset-uploader/src/targets/s3/upload.tsindex 0c084df44a..476f784170 100644--- a/packages/asset-uploader/src/targets/s3/upload.ts+++ b/packages/asset-uploader/src/targets/s3/upload.ts@@ -10,7 +10,7 @@ import { S3EnvVariables } from "../../schema";import { toBuffer } from "../../utils/to-buffer";import { getAssetData } from "../../utils/get-asset-data";import { createMany } from "../../db";-import { Asset } from "../../types";+import type { Asset } from "../../types";import { getUniqueFilename } from "../../utils/get-unique-filename";import { getS3Client } from "./client";@@ -36,6 +36,7 @@ export const uploadToS3 = async ({}));+ // @todo fontsconst imagesFormData = formData.getAll("image") as Array<string>;const assetsData = imagesFormData.map((dataString) => {// @todo validate with zod@@ -88,14 +89,33 @@ const uploadHandler = async ({AssetsUploadedSuccess.parse(await upload.done());- // @todo fonts- const assetOptions = {- type: "image" as const,+ const type = file.contentType.startsWith("image")+ ? ("image" as const)+ : ("font" as const);++ const baseAssetOptions = {name: uniqueFilename,size: buffer.byteLength,buffer,location: Location.REMOTE,};+ let assetOptions;++ if (type === "image") {+ assetOptions = {+ type,+ ...baseAssetOptions,+ };+ } else if (type === "font") {+ assetOptions = {+ type,+ ...baseAssetOptions,+ };+ }++ if (assetOptions === undefined) {+ throw new Error("Asset type not supported");+ }const assetData = await getAssetData(assetOptions);diff --git a/packages/asset-uploader/src/types.ts b/packages/asset-uploader/src/types.tsindex 92b1146fb3..0fceb9d21e 100644--- a/packages/asset-uploader/src/types.ts+++ b/packages/asset-uploader/src/types.ts@@ -6,3 +6,5 @@ export type Asset = Omit<DbAsset, "meta"> & {status?: "uploading" | "uploaded";meta: ImageMeta | FontMeta;};++export type AssetType = "image" | "font";diff --git a/packages/asset-uploader/src/utils/get-asset-data.ts b/packages/asset-uploader/src/utils/get-asset-data.tsindex 1d206fc611..944b94a5bd 100644--- a/packages/asset-uploader/src/utils/get-asset-data.ts+++ b/packages/asset-uploader/src/utils/get-asset-data.ts@@ -1,21 +1,23 @@import { Location } from "@webstudio-is/prisma-client";-import sharp, { type Metadata } from "sharp";+import sharp from "sharp";+import type { FontMeta, ImageMeta } from "./format-asset";type BaseData = {name: string;size: number;location: Location;+ format?: string;};type ImageData = BaseData & {type: "image";- metadata: Metadata;+ meta: ImageMeta;};type FontData = BaseData & {type: "font";// @todo fonts meta- metadata: {};+ meta: FontMeta;};export type AssetData = ImageData | FontData;@@ -43,8 +45,13 @@ export const getAssetData = async (};if (options.type === "image") {const sharpImage = sharp(options.buffer);- const metadata = await sharpImage.metadata();- return { ...baseData, type: "image", metadata };+ const { width, height, format } = await sharpImage.metadata();+ return {+ ...baseData,+ type: options.type,+ format,+ meta: { width, height },+ };}- return { ...baseData, type: "font", metadata: {} };+ return { type: options.type, ...baseData, meta: {} };};