@forge/kvs
1.2.7-next.0-experimental-d9973071.2.7-next.0-experimental-44a932f
out/storage-api.jsout/storage-api.js+24−52
Index: package/out/storage-api.js
===================================================================
--- package/out/storage-api.js
+++ package/out/storage-api.js
@@ -3,87 +3,78 @@
exports.StorageApi = void 0;
const types_1 = require("./interfaces/types");
const error_handling_1 = require("./utils/error-handling");
const errors_1 = require("./errors");
-var ResponseType;
-(function (ResponseType) {
- ResponseType[ResponseType["NONE"] = 0] = "NONE";
- ResponseType[ResponseType["EXPECTED"] = 1] = "EXPECTED";
- ResponseType[ResponseType["OPTIONAL"] = 2] = "OPTIONAL";
-})(ResponseType || (ResponseType = {}));
class StorageApi {
apiClient;
constructor(apiClient) {
this.apiClient = apiClient;
}
async get(body) {
const rs = await this.handleKeyNotFound(async () => {
- return this.request('/api/v1/get', body, ResponseType.EXPECTED);
+ return this.request('/api/v1/get', body, true);
});
- return this.processGetResponse(rs, body.options);
+ return this.processGetResponse(rs, body.options?.metadataFields);
}
async getSecret(body) {
const rs = await this.handleKeyNotFound(async () => {
- return this.request('/api/v1/secret/get', body, ResponseType.EXPECTED);
+ return this.request('/api/v1/secret/get', body, true);
});
- return this.processGetResponse(rs, body.options);
+ return this.processGetResponse(rs, body.options?.metadataFields);
}
async getEntity(body) {
const rs = await this.handleKeyNotFound(async () => {
- return this.request('/api/v1/entity/get', body, ResponseType.EXPECTED);
+ return this.request('/api/v1/entity/get', body, true);
});
- return this.processGetResponse(rs, body.options);
+ return this.processGetResponse(rs, body.options?.metadataFields);
}
async set(body) {
- const rs = await this.request('/api/v1/set', body, ResponseType.OPTIONAL);
- return rs && (0, types_1.isOverrideAndReturnOptions)(body.options) ? this.processSetResponse(rs, body.options) : undefined;
+ await this.request('/api/v1/set', body, false);
}
async setSecret(body) {
- const rs = await this.request('/api/v1/secret/set', body, ResponseType.OPTIONAL);
- return rs && (0, types_1.isOverrideAndReturnOptions)(body.options) ? this.processSetResponse(rs, body.options) : undefined;
+ await this.request('/api/v1/secret/set', body, false);
}
async setEntity(body) {
- const rs = await this.request('/api/v1/entity/set', body, ResponseType.OPTIONAL);
- return rs && (0, types_1.isOverrideAndReturnOptions)(body.options) ? this.processSetResponse(rs, body.options) : undefined;
+ await this.request('/api/v1/entity/set', body, false);
}
async delete(body) {
await this.handleKeyNotFound(async () => {
- return this.request('/api/v1/delete', body, ResponseType.NONE);
+ return this.request('/api/v1/delete', body, false);
});
}
async deleteSecret(body) {
await this.handleKeyNotFound(async () => {
- return this.request('/api/v1/secret/delete', body, ResponseType.NONE);
+ return this.request('/api/v1/secret/delete', body, false);
});
}
async deleteEntity(body) {
await this.handleKeyNotFound(async () => {
- return this.request('/api/v1/entity/delete', body, ResponseType.NONE);
+ return this.request('/api/v1/entity/delete', body, false);
});
}
async query(body) {
- const rs = await this.request('/api/v1/query', body, ResponseType.EXPECTED);
+ const rs = await this.request('/api/v1/query', body, true);
return {
results: rs.data,
nextCursor: rs.cursor
};
}
async queryEntity(body) {
- const rs = await this.request('/api/v1/entity/query', body, ResponseType.EXPECTED);
+ const rs = await this.request('/api/v1/entity/query', body, true);
return {
results: rs.data,
nextCursor: rs.cursor
};
}
async batchSet(body) {
- const rs = await this.request('/api/v1/batch/set', body, ResponseType.EXPECTED);
+ const rs = await this.request('/api/v1/batch/set', body, true);
return {
successfulKeys: rs.successfulKeys,
failedKeys: rs.failedKeys
};
}
async transact(transactionRequest) {
- await this.request('/api/v1/transaction', transactionRequest, ResponseType.NONE);
+ await this.request('/api/v1/transaction', transactionRequest, false);
}
async handleKeyNotFound(fn) {
try {
return await fn();
@@ -94,9 +85,9 @@
}
throw e;
}
}
- async request(path, body, responseType) {
+ async request(path, body, isResponseExpected) {
const requestBody = {
method: 'POST',
body: JSON.stringify(body),
headers: {
@@ -104,53 +95,34 @@
}
};
const response = await this.apiClient(path, requestBody);
await (0, error_handling_1.checkResponseError)(response);
- if (responseType === ResponseType.NONE) {
- return;
+ if (!isResponseExpected) {
+ return {};
}
const responseText = await response.text();
- if (responseType === ResponseType.OPTIONAL && !responseText) {
- return undefined;
- }
try {
return JSON.parse(responseText);
}
catch (error) {
throw new errors_1.ForgeKvsError(`Unexpected error. Response was not valid JSON: ${responseText}`);
}
}
- processGetResponse(response, options) {
- if (response && options) {
- const maybeCreatedAt = options.metadataFields?.includes(types_1.MetadataField.CREATED_AT)
+ processGetResponse(response, requestedMetadataFields) {
+ if (response && requestedMetadataFields?.length) {
+ const maybeCreatedAt = requestedMetadataFields.includes(types_1.MetadataField.CREATED_AT)
? { createdAt: response.createdAt }
: {};
- const maybeUpdatedAt = options.metadataFields?.includes(types_1.MetadataField.UPDATED_AT)
+ const maybeUpdatedAt = requestedMetadataFields.includes(types_1.MetadataField.UPDATED_AT)
? { updatedAt: response.updatedAt }
: {};
- const maybeExpireTime = options.metadataFields?.includes(types_1.MetadataField.EXPIRE_TIME)
- ? { expireTime: response.expireTime }
- : {};
return {
key: response.key,
value: response.value,
...maybeCreatedAt,
- ...maybeUpdatedAt,
- ...maybeExpireTime
+ ...maybeUpdatedAt
};
}
return response?.value;
}
- processSetResponse(response, options) {
- if (!response) {
- return undefined;
- }
- return {
- key: response.key,
- value: response.value,
- ...(options.returnMetadataFields?.includes(types_1.MetadataField.CREATED_AT) && { createdAt: response.createdAt }),
- ...(options.returnMetadataFields?.includes(types_1.MetadataField.UPDATED_AT) && { updatedAt: response.updatedAt }),
- ...(options.returnMetadataFields?.includes(types_1.MetadataField.EXPIRE_TIME) && { expireTime: response.expireTime })
- };
- }
}
exports.StorageApi = StorageApi;