@vtex/api
Version:
VTEX I/O API client
198 lines (197 loc) • 9.91 kB
JavaScript
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.VBase = void 0;
const mime_types_1 = __importDefault(require("mime-types"));
const path_1 = require("path");
const zlib_1 = require("zlib");
const HttpClient_1 = require("../../HttpClient");
const InfraClient_1 = require("./InfraClient");
const appId = process.env.VTEX_APP_ID;
const [runningAppName] = appId ? appId.split('@') : [''];
const routes = {
Bucket: (bucket) => `/buckets/${runningAppName}/${bucket}`,
Conflicts: (bucket) => `/buckets/${runningAppName}/${bucket}/conflicts`,
File: (bucket, path) => `${routes.Bucket(bucket)}/files/${path}`,
Files: (bucket) => `${routes.Bucket(bucket)}/files`,
};
const isVBaseOptions = (opts) => {
return typeof opts !== 'string' && !(opts instanceof String);
};
class VBase extends InfraClient_1.InfraClient {
constructor(context, options) {
super('vbase@2.x', context, options);
this.getBucket = (bucket, tracingConfig) => {
const inflightKey = HttpClient_1.inflightURL;
const metric = 'vbase-get-bucket';
return this.http.get(routes.Bucket(bucket), { inflightKey, metric, tracing: {
requestSpanNameSuffix: metric,
...tracingConfig === null || tracingConfig === void 0 ? void 0 : tracingConfig.tracing,
} });
};
this.resetBucket = (bucket, tracingConfig) => {
const metric = 'vbase-reset-bucket';
return this.http.delete(routes.Files(bucket), { metric, tracing: {
requestSpanNameSuffix: metric,
...tracingConfig === null || tracingConfig === void 0 ? void 0 : tracingConfig.tracing,
} });
};
this.listFiles = (bucket, opts, tracingConfig) => {
let params = {};
if (isVBaseOptions(opts)) {
params = opts;
}
else if (opts) {
params = { prefix: opts };
}
const metric = 'vbase-list';
const inflightKey = HttpClient_1.inflightUrlWithQuery;
return this.http.get(routes.Files(bucket), { inflightKey, metric, params, tracing: {
requestSpanNameSuffix: metric,
...tracingConfig === null || tracingConfig === void 0 ? void 0 : tracingConfig.tracing,
} });
};
this.getFile = (bucket, path, tracingConfig) => {
const inflightKey = HttpClient_1.inflightURL;
const metric = 'vbase-get-file';
return this.http.getBuffer(routes.File(bucket, path), { inflightKey, metric, tracing: {
requestSpanNameSuffix: metric,
...tracingConfig === null || tracingConfig === void 0 ? void 0 : tracingConfig.tracing,
} });
};
this.getJSON = (bucket, path, nullIfNotFound, conflictsResolver, tracingConfig) => {
return this.getRawJSON(bucket, path, nullIfNotFound, conflictsResolver, tracingConfig)
.then(response => response.data);
};
this.getRawJSON = (bucket, path, nullIfNotFound, conflictsResolver, tracingConfig) => {
const headers = conflictsResolver ? { 'X-Vtex-Detect-Conflicts': true } : {};
const inflightKey = HttpClient_1.inflightURL;
const metric = 'vbase-get-json';
return this.http
.getRaw(routes.File(bucket, path), {
headers,
inflightKey,
metric,
nullIfNotFound,
tracing: {
requestSpanNameSuffix: metric,
...tracingConfig === null || tracingConfig === void 0 ? void 0 : tracingConfig.tracing,
},
})
.catch(async (error) => {
const { response } = error;
if (response && response.status === 409 && conflictsResolver) {
try {
const conflictsMergedData = await conflictsResolver.resolve(this.context.logger);
return { ...response, data: conflictsMergedData };
}
catch (resolverError) {
const typedResolverError = resolverError;
if ((typedResolverError === null || typedResolverError === void 0 ? void 0 : typedResolverError.status) === 404) {
return this.http.getRaw(routes.File(bucket, path), {
'X-Vtex-Detect-Conflicts': false,
inflightKey,
metric,
nullIfNotFound,
tracing: {
requestSpanNameSuffix: metric,
...tracingConfig === null || tracingConfig === void 0 ? void 0 : tracingConfig.tracing,
},
});
}
throw resolverError;
}
}
throw error;
});
};
this.getFileStream = (bucket, path, tracingConfig) => {
const metric = 'vbase-get-file-s';
return this.http.getStream(routes.File(bucket, path), { metric, tracing: {
requestSpanNameSuffix: metric,
...tracingConfig === null || tracingConfig === void 0 ? void 0 : tracingConfig.tracing,
} });
};
this.saveFile = (bucket, path, stream, gzip = true, ttl, tracingConfig, ifMatch) => {
return this.saveContent(bucket, path, stream, { gzip, ttl }, tracingConfig, ifMatch);
};
this.getFileMetadata = (bucket, path, tracingConfig) => {
const metric = 'vbase-get-file-metadata';
return this.http.head(routes.File(bucket, path), { metric, tracing: {
requestSpanNameSuffix: metric,
...tracingConfig === null || tracingConfig === void 0 ? void 0 : tracingConfig.tracing,
} });
};
this.saveJSON = (bucket, path, data, tracingConfig, ifMatch) => {
const headers = { 'Content-Type': 'application/json' };
if (ifMatch) {
headers['If-Match'] = ifMatch;
}
const metric = 'vbase-save-json';
return this.http.put(routes.File(bucket, path), data, { headers, metric, tracing: {
requestSpanNameSuffix: metric,
...tracingConfig === null || tracingConfig === void 0 ? void 0 : tracingConfig.tracing,
} });
};
this.saveZippedContent = (bucket, path, stream, tracingConfig, ifMatch) => {
return this.saveContent(bucket, path, stream, { unzip: true }, tracingConfig, ifMatch);
};
this.deleteFile = (bucket, path, tracingConfig, ifMatch) => {
const headers = ifMatch ? { 'If-Match': ifMatch } : {};
const metric = 'vbase-delete-file';
return this.http.delete(routes.File(bucket, path), { headers, metric, tracing: {
requestSpanNameSuffix: metric,
...tracingConfig === null || tracingConfig === void 0 ? void 0 : tracingConfig.tracing,
} });
};
this.getConflicts = (bucket, tracingConfig) => {
const metric = 'vbase-get-conflicts';
return this.http.get(routes.Conflicts(bucket), { metric, tracing: {
requestSpanNameSuffix: metric,
...tracingConfig === null || tracingConfig === void 0 ? void 0 : tracingConfig.tracing,
} });
};
this.resolveConflict = (bucket, path, content, tracingConfig) => {
const data = [{
op: 'replace',
path,
value: content,
}];
const metric = 'vbase-resolve-conflicts';
return this.http.patch(routes.Conflicts(bucket), data, { metric, tracing: {
requestSpanNameSuffix: metric,
...tracingConfig === null || tracingConfig === void 0 ? void 0 : tracingConfig.tracing,
} });
};
this.saveContent = (bucket, path, stream, opts = {}, tracingConfig, ifMatch) => {
if (!stream.pipe || !stream.on) {
throw new Error(`Argument stream must be a readable stream`);
}
const params = opts.unzip ? { unzip: opts.unzip } : {};
const headers = {};
let finalStream = stream;
headers['Content-Type'] = mime_types_1.default.contentType((0, path_1.basename)(path)) || 'application/octet-stream';
if (opts.gzip) {
headers['Content-Encoding'] = 'gzip';
finalStream = stream.pipe((0, zlib_1.createGzip)());
}
if (opts.ttl && Number.isInteger(opts.ttl)) {
headers['X-VTEX-TTL'] = opts.ttl;
}
if (ifMatch) {
headers['If-Match'] = ifMatch;
}
const metric = 'vbase-save-blob';
return this.http.put(routes.File(bucket, path), finalStream, { headers, metric, params, tracing: {
requestSpanNameSuffix: metric,
...tracingConfig === null || tracingConfig === void 0 ? void 0 : tracingConfig.tracing,
} });
};
if (runningAppName === '') {
throw new Error(`Invalid path to access VBase. Variable VTEX_APP_ID is not available.`);
}
}
}
exports.VBase = VBase;