Skip to content

Commit

Permalink
chore(sheetsTool): 4.x auth api
Browse files Browse the repository at this point in the history
  • Loading branch information
dckc committed Aug 27, 2023
1 parent aadad06 commit a8ee98c
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 7 deletions.
15 changes: 15 additions & 0 deletions packages/fincaps/jsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// This file can contain .js-specific Typescript compiler config.
{
"compilerOptions": {
"target": "es2020",

"noEmit": false,
"declaration": true,
"emitDeclarationOnly": true,
"outDir": "types",

"strictNullChecks": true,
"moduleResolution": "node"
},
"include": ["exports.js", "index.js", "src/**.js"]
}
50 changes: 43 additions & 7 deletions packages/fincaps/src/sheetsTool.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,48 @@
// @ts-check
import { E, Far } from '@endo/far';
import * as sheetsAmbient from 'google-spreadsheet';
import { JWT, auth } from 'google-auth-library';

const zip = (xs, ys) => xs.map((x, i) => [x, ys[i]]);

const { fromEntries } = Object;

/** @param {sheetsAmbient.GoogleSpreadsheetWorksheet} sheet */
/** @param {import('google-spreadsheet').GoogleSpreadsheetWorksheet} sheet */
const makeWorksheet = sheet => {
// be sure to loadHeaderRow() first
const toObj = row => fromEntries(zip(sheet.headerValues, row._rawData));

/**
* @param {string | number} key
* @throws on not found
*/
const find = async key => {
// load primary key column
await sheet.loadCells({
startColumnIndex: 0,
endColumnIndex: 1,
});

let rowIndex = 1;
for (; rowIndex < sheet.rowCount; rowIndex += 1) {
const { value } = sheet.getCell(rowIndex, 0);
if (value === null) throw RangeError(`${key}`); // empty row: end of data
if (key === value) {
break;
}
}
if (rowIndex === sheet.rowCount) throw RangeError(`${key}`);
const [row] = await sheet.getRows({ offset: rowIndex - 1, limit: 1 });
if (!row) throw TypeError('should not happen');
return toObj(row);
};

const rd = Far('WorksheetRd', {
readOnly: () => rd,
find,
getRows: async (offset = 0, limit = 100) => {
/** @type {sheetsAmbient.WorksheetGridRange} */
await sheet.loadHeaderRow();

const cellRange = {
startRowIndex: (offset || 0) + 1, // skip header
endRowIndex: (offset || 0) + (limit || 0) + 1, // + 1 for header
Expand All @@ -23,15 +54,15 @@ const makeWorksheet = sheet => {
sheet.getRows({ offset, limit }),
]);

const { headerValues } = sheet;
return rows.map(row => fromEntries(zip(headerValues, row._rawData)));
return rows.map(toObj);
},
});

const wr = Far('WorksheetWr', { ...rd });

return wr;
};
/** @typedef {ReturnType<typeof makeWorksheet>} Worksheet */

/** @param {sheetsAmbient.GoogleSpreadsheet} doc */
const makeSpreadsheet = doc => {
Expand All @@ -46,6 +77,7 @@ const makeSpreadsheet = doc => {

return wr;
};
/** @typedef {ReturnType<typeof makeSpreadsheet>} Spreadsheet */

export const make = () => {
const { GoogleSpreadsheet } = sheetsAmbient;
Expand All @@ -59,9 +91,13 @@ export const make = () => {
]);
assert.typeof(id, 'string');
assert.typeof(credTxt, 'string');
const creds = JSON.parse(credTxt);
const doc = new GoogleSpreadsheet(id);
await doc.useServiceAccountAuth(creds);
const keys = JSON.parse(credTxt);
const client = new JWT({
email: keys.client_email,
key: keys.private_key,
scopes: ['https://www.googleapis.com/auth/spreadsheets'],
});
const doc = new GoogleSpreadsheet(id, client);
await doc.loadInfo();
return makeSpreadsheet(doc);
},
Expand Down

0 comments on commit a8ee98c

Please sign in to comment.