From 69d5537f3ebd8cda5fdd654a31508105f0771d1b Mon Sep 17 00:00:00 2001 From: vitorflg Date: Thu, 24 Oct 2024 14:41:35 -0300 Subject: [PATCH] Extend/vbase client v2 (#1236) * Extend VBase clients with conflict handler endpoints * Extend checkForConflicts to verify any conflict inside the pages-graphql bucket * Fix checkForConflicts condition --- CHANGELOG.md | 2 ++ src/api/clients/IOClients/infra/VBase.ts | 41 +++++++++++++++++------- 2 files changed, 31 insertions(+), 12 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index bf5f7cb37..77133906d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,8 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). ## [Unreleased] +### Updated +- Extend VBase client, integrating the `getConflicts` and the `resolveConflict` endpoints. ## [4.1.0] - 2024-08-01 diff --git a/src/api/clients/IOClients/infra/VBase.ts b/src/api/clients/IOClients/infra/VBase.ts index f4ac28944..0d61a07ef 100644 --- a/src/api/clients/IOClients/infra/VBase.ts +++ b/src/api/clients/IOClients/infra/VBase.ts @@ -2,8 +2,9 @@ import { InfraClient, InstanceOptions, IOContext } from '@vtex/api' import { IOClientFactory } from '../IOClientFactory' const routes = { - Bucket: (bucket: string) => `/buckets/${bucket}`, - File: (bucket: string, path: string) => `${routes.Bucket(bucket)}/userData/files/${path}`, + Bucket: (bucket: string) => `${bucket}`, + File: (bucket: string, path: string) => `buckets/${routes.Bucket(bucket)}/files/${path}`, + Conflicts: (bucket: string) => `buckets/${routes.Bucket(bucket)}/conflicts`, } export class VBase extends InfraClient { @@ -21,16 +22,32 @@ export class VBase extends InfraClient { }) } + // Resolve a specific pages-graphql conflict + public resolveConflict = (bucketKey: string, path: string, content: any) => { + const data = [ + { + op: 'replace', + path, + value: content, + }, + ] + + return this.http.patch(routes.Conflicts(`vtex.pages-graphql/${bucketKey}`), data, { + metric: 'vbase-resolve-conflicts', + }) + } + + // List all conflicts in the pages-graphql bucket + public getConflicts = async () => { + return this.http.get(routes.Conflicts('vtex.pages-graphql/userData'), { + metric: 'vbase-get-conflicts', + }) + } + + // Verify if there is at least one conlfict in the pages-graphql bucket public checkForConflicts = async () => { - let status: number - try { - const response = await this.http.get(routes.File('vtex.pages-graphql', 'store/content.json'), { - metric: 'vbase-conflict', - }) - status = response.status - } catch (error) { - status = error.response && error.response.status - } - return status === 409 + const response = await this.getConflicts().then(res => res.data) + + return response?.length > 0 } }