From 4ca6710fe160d1c13707bfc95dcb2b3066b46326 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bart=C5=82omiej=20Klocek?= Date: Sat, 5 Oct 2024 13:50:17 +0200 Subject: [PATCH] [lib] Use response validators in multiple-holder calls Summary: Used types and validators from D13655 to make Blob `fetch` calls more type safe. Depends on D13655 Test Plan: Flow. Verified that blob service calls using these functions succeed. Reviewers: kamil, tomek Reviewed By: kamil Subscribers: ashoat Differential Revision: https://phab.comm.dev/D13656 --- lib/utils/blob-service.js | 49 +++++++++++++++++++++++++-------------- 1 file changed, 32 insertions(+), 17 deletions(-) diff --git a/lib/utils/blob-service.js b/lib/utils/blob-service.js index 5527613a47..31d659b5b9 100644 --- a/lib/utils/blob-service.js +++ b/lib/utils/blob-service.js @@ -5,9 +5,16 @@ import uuid from 'uuid'; import { toBase64URL } from './base64.js'; import { replacePathParams, type URLPathParams } from './url-utils.js'; +import { assertWithValidator } from './validation-utils.js'; import type { BlobServiceHTTPEndpoint } from '../facts/blob-service.js'; import blobServiceConfig from '../facts/blob-service.js'; -import type { BlobHashAndHolder } from '../types/holder-types.js'; +import { + type BlobInfo, + type AssignHoldersRequest, + type RemoveHoldersRequest, + assignHoldersResponseValidator, + removeHoldersResponseValidator, +} from '../types/blob-service-types.js'; const BLOB_SERVICE_URI_PREFIX = 'comm-blob-service://'; @@ -113,19 +120,19 @@ async function uploadBlob( } async function assignMultipleHolders( - holders: $ReadOnlyArray<{ +blobHash: string, +holder: string }>, + holders: $ReadOnlyArray, headers: { [string]: string }, ): Promise< | { +result: 'success' } | { +result: 'error', +status: number, +statusText: string } | { - +failedRequests: $ReadOnlyArray<{ - +blobHash: string, - +holder: string, - }>, + +failedRequests: $ReadOnlyArray, +result: 'failed_requests', }, > { + const requestBody: AssignHoldersRequest = { + requests: holders, + }; const assignMultipleHoldersResponse = await fetch( makeBlobServiceEndpointURL( blobServiceConfig.httpEndpoints.ASSIGN_MULTIPLE_HOLDERS, @@ -133,9 +140,7 @@ async function assignMultipleHolders( { method: blobServiceConfig.httpEndpoints.ASSIGN_MULTIPLE_HOLDERS.method, headers: { ...headers, 'Content-Type': 'application/json' }, - body: JSON.stringify({ - requests: holders, - }), + body: JSON.stringify(requestBody), }, ); @@ -144,7 +149,11 @@ async function assignMultipleHolders( return { result: 'error', status, statusText }; } - const { results } = await assignMultipleHoldersResponse.json(); + const responseJson = await assignMultipleHoldersResponse.json(); + const { results } = assertWithValidator( + responseJson, + assignHoldersResponseValidator, + ); const failedRequests = results .filter(result => !result.success) .map(({ blobHash, holder }) => ({ blobHash, holder })); @@ -157,7 +166,7 @@ async function assignMultipleHolders( } async function removeMultipleHolders( - holders: $ReadOnlyArray, + holders: $ReadOnlyArray, headers: { [string]: string }, instantDelete?: boolean, ): Promise< @@ -165,9 +174,13 @@ async function removeMultipleHolders( | { +result: 'error', +status: number, +statusText: string } | { +result: 'failed_requests', - +failedRequests: $ReadOnlyArray, + +failedRequests: $ReadOnlyArray, }, > { + const requestBody: RemoveHoldersRequest = { + requests: holders, + instantDelete: !!instantDelete, + }; const response = await fetch( makeBlobServiceEndpointURL( blobServiceConfig.httpEndpoints.REMOVE_MULTIPLE_HOLDERS, @@ -175,10 +188,7 @@ async function removeMultipleHolders( { method: blobServiceConfig.httpEndpoints.REMOVE_MULTIPLE_HOLDERS.method, headers: { ...headers, 'Content-Type': 'application/json' }, - body: JSON.stringify({ - requests: holders, - instantDelete: !!instantDelete, - }), + body: JSON.stringify(requestBody), }, ); @@ -187,7 +197,12 @@ async function removeMultipleHolders( return { result: 'error', status, statusText }; } - const { failedRequests } = await response.json(); + const responseJson = await response.json(); + const { failedRequests } = assertWithValidator( + responseJson, + removeHoldersResponseValidator, + ); + if (failedRequests.length !== 0) { return { result: 'failed_requests', failedRequests }; }