Skip to content

Commit

Permalink
[lib] Use response validators in multiple-holder calls
Browse files Browse the repository at this point in the history
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
  • Loading branch information
barthap committed Oct 10, 2024
1 parent 11b6ebf commit 4ca6710
Showing 1 changed file with 32 additions and 17 deletions.
49 changes: 32 additions & 17 deletions lib/utils/blob-service.js
Original file line number Diff line number Diff line change
Expand Up @@ -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://';

Expand Down Expand Up @@ -113,29 +120,27 @@ async function uploadBlob(
}

async function assignMultipleHolders(
holders: $ReadOnlyArray<{ +blobHash: string, +holder: string }>,
holders: $ReadOnlyArray<BlobInfo>,
headers: { [string]: string },
): Promise<
| { +result: 'success' }
| { +result: 'error', +status: number, +statusText: string }
| {
+failedRequests: $ReadOnlyArray<{
+blobHash: string,
+holder: string,
}>,
+failedRequests: $ReadOnlyArray<BlobInfo>,
+result: 'failed_requests',
},
> {
const requestBody: AssignHoldersRequest = {
requests: holders,
};
const assignMultipleHoldersResponse = await fetch(
makeBlobServiceEndpointURL(
blobServiceConfig.httpEndpoints.ASSIGN_MULTIPLE_HOLDERS,
),
{
method: blobServiceConfig.httpEndpoints.ASSIGN_MULTIPLE_HOLDERS.method,
headers: { ...headers, 'Content-Type': 'application/json' },
body: JSON.stringify({
requests: holders,
}),
body: JSON.stringify(requestBody),
},
);

Expand All @@ -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 }));
Expand All @@ -157,28 +166,29 @@ async function assignMultipleHolders(
}

async function removeMultipleHolders(
holders: $ReadOnlyArray<BlobHashAndHolder>,
holders: $ReadOnlyArray<BlobInfo>,
headers: { [string]: string },
instantDelete?: boolean,
): Promise<
| { +result: 'success' }
| { +result: 'error', +status: number, +statusText: string }
| {
+result: 'failed_requests',
+failedRequests: $ReadOnlyArray<BlobHashAndHolder>,
+failedRequests: $ReadOnlyArray<BlobInfo>,
},
> {
const requestBody: RemoveHoldersRequest = {
requests: holders,
instantDelete: !!instantDelete,
};
const response = await fetch(
makeBlobServiceEndpointURL(
blobServiceConfig.httpEndpoints.REMOVE_MULTIPLE_HOLDERS,
),
{
method: blobServiceConfig.httpEndpoints.REMOVE_MULTIPLE_HOLDERS.method,
headers: { ...headers, 'Content-Type': 'application/json' },
body: JSON.stringify({
requests: holders,
instantDelete: !!instantDelete,
}),
body: JSON.stringify(requestBody),
},
);

Expand All @@ -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 };
}
Expand Down

0 comments on commit 4ca6710

Please sign in to comment.