-
-
Notifications
You must be signed in to change notification settings - Fork 14
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Custom authority connector for Airtable #97
Open
mMoliere
wants to merge
1
commit into
eeditiones:master
Choose a base branch
from
mMoliere:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,169 @@ | ||
/* Modifies Airtable connector to enable custom URI in TEI Publisher */ | ||
|
||
import '@lrnwebcomponents/es-global-bridge'; | ||
import { Registry } from './registry.js'; | ||
import { resolveURL } from '../utils.js'; | ||
|
||
function expandTemplate(template = '', options) { | ||
return template.replace(/\${([^}]+)}/g, (match, p) => { | ||
let replacement; | ||
if (options[p]) { | ||
replacement = options[p]; | ||
} | ||
return replacement || ''; | ||
}); | ||
} | ||
|
||
function getTemplate(configElem, selector) { | ||
const template = configElem.querySelector(selector); | ||
if (template instanceof HTMLTemplateElement) { | ||
const wrapper = document.createElement('div'); | ||
wrapper.appendChild(template.content.cloneNode(true)); | ||
return wrapper.innerHTML; | ||
} | ||
return ''; | ||
} | ||
|
||
export class Airtable extends Registry { | ||
|
||
constructor(configElem) { | ||
super(configElem); | ||
this.apiKey = configElem.getAttribute('api-key'); | ||
this.baseKey = configElem.getAttribute('base'); | ||
this.table = configElem.getAttribute('table'); | ||
this.view = configElem.getAttribute('view'); | ||
this.filterExpr = configElem.getAttribute('filter'); | ||
this.labelExpr = configElem.getAttribute('label'); | ||
const fieldsDef = configElem.getAttribute('fields'); | ||
if (fieldsDef) { | ||
this.fields = fieldsDef.split(/\s*,\s*/); | ||
} else { | ||
this.fields = ['ID']; | ||
} | ||
const tokenizeDef = configElem.getAttribute('tokenize'); | ||
if (tokenizeDef) { | ||
this.tokenize = tokenizeDef.split(/\s*,\s*/); | ||
} else { | ||
this.tokenize = [this.fields[0]]; | ||
} | ||
|
||
this.tokenizeChars = configElem.getAttribute('tokenize-regex') || "\\W"; | ||
|
||
this.infoExpr = getTemplate(configElem, '.info'); | ||
this.detailExpr = getTemplate(configElem, '.detail'); | ||
|
||
this._init(); | ||
} | ||
|
||
_init() { | ||
window.ESGlobalBridge.requestAvailability(); | ||
const path = resolveURL('https://unpkg.com/[email protected]/build/airtable.browser.js'); | ||
window.ESGlobalBridge.instance.load('airtable', path); | ||
window.addEventListener( | ||
'es-bridge-airtable-loaded', | ||
this._initAirtable.bind(this), | ||
{ once: true }, | ||
); | ||
} | ||
|
||
_initAirtable() { | ||
const Airtable = require('airtable'); | ||
this.base = new Airtable({ apiKey: this.apiKey }).base(this.baseKey); | ||
} | ||
|
||
async query(key) { | ||
key = key.toLowerCase(); | ||
const results = []; | ||
const filter = this.filterExpr ? expandTemplate(this.filterExpr, { key }) : null; | ||
const options = { | ||
fields: this.fields, | ||
// Selecting the first 3 records in Grid view: | ||
maxRecords: 100 | ||
}; | ||
if (this.view) { | ||
options.view = this.view; | ||
} | ||
if (filter) { | ||
options.filterByFormula = filter; | ||
} | ||
return new Promise((resolve, reject) => { | ||
this.base(this.table) | ||
.select(options) | ||
.firstPage((err, records) => { | ||
if (err) { | ||
console.error(err); | ||
reject(err); | ||
return; | ||
} | ||
records.forEach(record => { | ||
const data = {}; | ||
this.fields.forEach((field) => { data[field] = record.get(field); }); | ||
const result = { | ||
register: this._register, | ||
// Adding field name for custom URI in Airtable | ||
id: record.myURI, | ||
label: expandTemplate(this.labelExpr, data), | ||
details: expandTemplate(this.detailExpr, data), | ||
provider: 'airtable' | ||
}; | ||
results.push(result); | ||
}); | ||
resolve({ | ||
totalItems: 3, | ||
items: results, | ||
}); | ||
}); | ||
}); | ||
} | ||
|
||
info(key, container) { | ||
return new Promise((resolve, reject) => { | ||
const options = { | ||
fields: this.fields, | ||
filterByFormula: `RECORD_ID()='${key}'` | ||
}; | ||
this.base(this.table) | ||
.select(options) | ||
.firstPage((err, records) => { | ||
if (err) { | ||
switch (err.statusCode) { | ||
case 404: | ||
reject(`No record found for ${key} in table ${this.table}`); | ||
break; | ||
default: | ||
reject(`${err.statusCode}: ${err.message}`); | ||
break; | ||
} | ||
return; | ||
} | ||
const record = records[0]; | ||
if (Object.keys(record.fields).length === 0) { | ||
console.warn(`Retrieved an empty record for %s from table %s`, key, this.table); | ||
return; | ||
} | ||
let strings = []; | ||
const data = {}; | ||
this.fields.forEach((field) => { | ||
const value = record.get(field); | ||
if (value) { | ||
data[field] = value; | ||
strings.push(value); | ||
} | ||
}); | ||
const regex = new RegExp(this.tokenizeChars); | ||
this.tokenize.forEach((key) => { | ||
strings = strings.concat(data[key].split(regex)); | ||
}); | ||
strings = strings.filter(tok => !/^\d+$/.test(tok)); | ||
strings.sort((s1, s2) => s2.length - s1.length); | ||
console.log(strings); | ||
container.innerHTML = expandTemplate(this.infoExpr, data); | ||
|
||
resolve({ | ||
id: record.id, | ||
strings | ||
}); | ||
}); | ||
}) | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shouldn't this class be named differently to avoid conflicts with the existing
Airtable
class? The tests fail due to this.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we could streamline this by making the ID to use configurable. I'll make a proposal tomorrow...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So I would propose the following: instead of creating a new connector, change the existing airtable connector to accept an additional attribute
use-as-key
in the config element. Right now I can't test this myself, but I'd say the required changes would be:this.idField = configElem.getAttribute('use-as-key');
id
property toid: this.idField ? record.get(this.idField) : record.id,
filterByFormula
expression needs to be different, so replace withfilterByFormula: this.idField ?
${this.idField}='${key}':
RECORD_ID()='${key}'`id
property:id: this.idField ? record.get(this.idField) : record.id,
Could you try this and report if it works?