|
| 1 | +/** |
| 2 | + * Copyright 2017-2025, Voxel51, Inc. |
| 3 | + */ |
| 4 | + |
| 5 | +import { Sample } from "@fiftyone/looker"; |
| 6 | +import { |
| 7 | + FetchFunctionConfig, |
| 8 | + FetchFunctionResult, |
| 9 | + getFetchFunctionExtended, |
| 10 | + MalformedRequestError, |
| 11 | + NotFoundError, |
| 12 | +} from "@fiftyone/utilities"; |
| 13 | +import * as jsonpatch from "fast-json-patch"; |
| 14 | +import { encodeURIPath, parseETag } from "./util"; |
| 15 | + |
| 16 | +/** |
| 17 | + * List of JSON-patch operation deltas between two versions of a json object. |
| 18 | + */ |
| 19 | +export type JSONDeltas = jsonpatch.Operation[]; |
| 20 | + |
| 21 | +export type PatchSampleRequest = { |
| 22 | + datasetId: string; |
| 23 | + sampleId: string; |
| 24 | + deltas: JSONDeltas; |
| 25 | + versionToken: string; |
| 26 | + path?: string; |
| 27 | + labelId?: string; |
| 28 | +}; |
| 29 | + |
| 30 | +export type ErrorResponse = { |
| 31 | + errors: string[]; |
| 32 | +}; |
| 33 | + |
| 34 | +export type PatchSampleResponse = { |
| 35 | + sample: Sample; |
| 36 | + versionToken: string; |
| 37 | +}; |
| 38 | + |
| 39 | +/** |
| 40 | + * Error resulting from a failed update operation. |
| 41 | + */ |
| 42 | +export class PatchApplicationError extends Error { |
| 43 | + constructor(message?: string) { |
| 44 | + super(message); |
| 45 | + this.name = "Patch Application Error"; |
| 46 | + } |
| 47 | +} |
| 48 | + |
| 49 | +const handleErrorResponse = async (response: Response) => { |
| 50 | + if (response.status === 400) { |
| 51 | + // either a malformed request, or a list of errors from applying the patch |
| 52 | + let errorResponse: ErrorResponse | undefined; |
| 53 | + try { |
| 54 | + // expected error response: '["error 1","error 2"]' |
| 55 | + const body = await response.text(); |
| 56 | + const errorList = JSON.parse(body); |
| 57 | + if (Array.isArray(errorList)) { |
| 58 | + errorResponse = { errors: errorList }; |
| 59 | + } |
| 60 | + } catch (err) { |
| 61 | + // doesn't look like a list of errors |
| 62 | + } |
| 63 | + if (errorResponse?.errors) { |
| 64 | + throw new PatchApplicationError(errorResponse.errors.join(", ")); |
| 65 | + } |
| 66 | + |
| 67 | + throw new MalformedRequestError(); |
| 68 | + } else if (response.status === 404) { |
| 69 | + throw new NotFoundError({ path: "sample" }); |
| 70 | + } |
| 71 | +}; |
| 72 | + |
| 73 | +/** |
| 74 | + * `fetch` with headers, error handling, etc. |
| 75 | + * |
| 76 | + * @param config fetch configuration |
| 77 | + */ |
| 78 | +const doFetch = <A, R>( |
| 79 | + config: FetchFunctionConfig<A> |
| 80 | +): Promise<FetchFunctionResult<R>> => { |
| 81 | + return getFetchFunctionExtended()({ |
| 82 | + errorHandler: handleErrorResponse, |
| 83 | + ...config, |
| 84 | + }); |
| 85 | +}; |
| 86 | + |
| 87 | +/** |
| 88 | + * Patch a sample, applying the specified updates to its fields. |
| 89 | + * |
| 90 | + * @param request Patch sample request |
| 91 | + */ |
| 92 | +export const patchSample = async ( |
| 93 | + request: PatchSampleRequest |
| 94 | +): Promise<PatchSampleResponse> => { |
| 95 | + const pathParts = ["dataset", request.datasetId, "sample", request.sampleId]; |
| 96 | + if (request.path && request.labelId) { |
| 97 | + pathParts.push(request.path, request.labelId); |
| 98 | + } |
| 99 | + |
| 100 | + const response = await doFetch<JSONDeltas, Sample>({ |
| 101 | + path: encodeURIPath(pathParts), |
| 102 | + method: "PATCH", |
| 103 | + body: request.deltas, |
| 104 | + headers: { |
| 105 | + "Content-Type": "application/json-patch+json", |
| 106 | + "If-Match": `"${request.versionToken}"`, |
| 107 | + }, |
| 108 | + }); |
| 109 | + |
| 110 | + return { |
| 111 | + sample: response.response, |
| 112 | + versionToken: parseETag(response.headers.get("ETag")), |
| 113 | + }; |
| 114 | +}; |
0 commit comments