@flatfile/safe-api
Version:
Flatfile Safe API client with streaming capabilities
95 lines (94 loc) • 4.11 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.getWorkbooksExtension = getWorkbooksExtension;
const workbooks_1 = require("../streaming/workbooks");
const find_or_fail_1 = require("../utils/find.or.fail");
const util_common_1 = require("@flatfile/util-common");
function getWorkbooksExtension(client) {
// Get the prototype that contains the base workbook methods
const basePrototype = Object.getPrototypeOf(client).constructor.prototype.workbooks;
// Create a new object with the base workbooks prototype
const originalWorkbooks = Object.create(basePrototype);
async function updateWorkbookLockStatus(workbookId, lock, options = {}) {
if (options.sheetId) {
const action = lock ? 'lockSheet' : 'unlockSheet';
return client.sheets[action](options.sheetId);
}
const sheetIds = await client.sheets
.list({ workbookId })
.then((response) => response.data.map((sheet) => sheet.id));
const action = lock ? 'lockSheet' : 'unlockSheet';
return Promise.all(sheetIds.map((id) => client.sheets[action](id)));
}
// Add our enhanced methods
return Object.assign(originalWorkbooks, {
get: async (workbookId, options) => {
const request = new workbooks_1.GetWorkbookRequest(workbookId, options);
return request.execute();
},
list: async (opts, options) => {
const request = new workbooks_1.ListWorkbooksRequest(opts, options);
return request.execute();
},
update: async (workbookId, data, options) => {
const request = new workbooks_1.UpdateWorkbookRequest(workbookId, data, options);
return request.execute();
},
create: async (config, options) => {
const request = new workbooks_1.CreateWorkbookRequest(config, options);
return request.execute();
},
bulk: {
create: async (config, options) => {
const request = new workbooks_1.BulkCreateWorkbookRequest(config, options);
return request.execute();
},
lock: async (workbookIds, options = {}) => {
if (options.sheetId) {
return client.sheets.lockSheet(options.sheetId);
}
if (!options.spaceId) {
throw new Error("spaceId is required for bulk lock operation");
}
const request = new workbooks_1.BulkSetLockStatusRequest({
workbookIds,
status: true,
spaceId: options.spaceId
});
return request.execute();
},
unlock: async (workbookIds, options = {}) => {
if (options.sheetId) {
return client.sheets.unlockSheet(options.sheetId);
}
if (!options.spaceId) {
throw new Error("spaceId is required for bulk unlock operation");
}
const request = new workbooks_1.BulkSetLockStatusRequest({
workbookIds,
status: false,
spaceId: options.spaceId
});
return request.execute();
}
},
sheets: (workbook) => {
if (!workbook.sheets) {
throw new Error("no sheets in workbook");
}
return workbook.sheets;
},
sheet: (workbook, nameOrSlug) => {
return (0, find_or_fail_1.findOrFail)(workbook.sheets, (s) => s.name === nameOrSlug || s.slug === nameOrSlug);
},
records: (sheetId) => {
return util_common_1.Simplified.getAllRecordsSeries(sheetId);
},
lock: (workbookId, options) => {
return updateWorkbookLockStatus(workbookId, true, options);
},
unlock: (workbookId, options) => {
return updateWorkbookLockStatus(workbookId, false, options);
}
});
}