Skip to content

Commit

Permalink
reuse existing types for fetching assets
Browse files Browse the repository at this point in the history
  • Loading branch information
buckhalt committed Jan 23, 2025
1 parent f536868 commit 28ede8c
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 34 deletions.
2 changes: 1 addition & 1 deletion hooks/useProtocolImport.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ export const useProtocolImport = () => {

const existingAssetIds: string[] = [];

let newAssetsWithCombinedMetadata: AssetInsertType = [];
let newAssetsWithCombinedMetadata: AssetInsertType[] = [];

// Check if the assets are already in the database.
// If yes, add them to existingAssetIds to be connected to the protocol.
Expand Down
22 changes: 10 additions & 12 deletions schemas/protocol.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,22 @@
import { z } from 'zod';

const assetInsertSchema = z.array(
z.object({
key: z.string(),
assetId: z.string(),
name: z.string(),
type: z.string(),
url: z.string(),
size: z.number(),
value: z.string().optional(),
}),
);
const assetInsertSchema = z.object({
key: z.string(),
assetId: z.string(),
name: z.string(),
type: z.string(),
url: z.string(),
size: z.number(),
value: z.string().optional(),
});

export type AssetInsertType = z.infer<typeof assetInsertSchema>;

export const protocolInsertSchema = z
.object({
protocol: z.unknown(), // TODO: replace this with zod schema version of Protocol type
protocolName: z.string(),
newAssets: assetInsertSchema,
newAssets: z.array(assetInsertSchema),
existingAssetIds: z.array(z.string()),
})
.passthrough();
28 changes: 7 additions & 21 deletions utils/protocolImport.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type Zip from 'jszip';
import { type Protocol } from '~/lib/protocol-validation/schemas/src/8.zod';
import { type AssetInsertType } from '~/schemas/protocol';

// Fetch protocol.json as a parsed object from the protocol zip.
export const getProtocolJson = async (protocolZip: Zip) => {
Expand All @@ -22,27 +23,12 @@ export const getProtocolJson = async (protocolZip: Zip) => {
* metadata about the asset.
*/

type ProtocolAsset = {
assetId: string;
name: string;
type: string;
}

type FileAsset = {
file: File;
} & ProtocolAsset

type ApiKeyAsset = {
value: string;
key: string;
size: number;
url: string;
} & ProtocolAsset
type FetchedFileAsset = Omit<AssetInsertType, 'value' | 'key' | 'size' | 'url'> & { file: File };

type ProtocolAssetsResult = {
fileAssets: FileAsset[];
apikeyAssets: ApiKeyAsset[];
}
fileAssets: FetchedFileAsset[];
apikeyAssets: AssetInsertType[];
};

export const getProtocolAssets = async (
protocolJson: Protocol,
Expand All @@ -66,8 +52,8 @@ export const getProtocolAssets = async (
* Assets with type 'apikey' are handled differently:
* - They are not actually files. The key itself is stored in the value field.
*/
const fileAssets: FileAsset[] = [];
const apikeyAssets: ApiKeyAsset[] = [];
const fileAssets: FetchedFileAsset[] = [];
const apikeyAssets: AssetInsertType[] = [];

await Promise.all(
Object.keys(assetManifest).map(async (key) => {
Expand Down

0 comments on commit 28ede8c

Please sign in to comment.