-
Notifications
You must be signed in to change notification settings - Fork 315
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(airtable): add create record action
- Loading branch information
1 parent
c413ab0
commit 3916a40
Showing
11 changed files
with
274 additions
and
1 deletion.
There are no files selected for viewing
92 changes: 92 additions & 0 deletions
92
packages/backend/src/apps/airtable/actions/create-record/index.js
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,92 @@ | ||
import defineAction from '../../../../helpers/define-action.js'; | ||
|
||
export default defineAction({ | ||
name: 'Create record', | ||
key: 'createRecord', | ||
description: 'Creates a new record with fields that automatically populate.', | ||
arguments: [ | ||
{ | ||
label: 'Base', | ||
key: 'baseId', | ||
type: 'dropdown', | ||
required: true, | ||
description: 'Base in which to create the record.', | ||
variables: true, | ||
source: { | ||
type: 'query', | ||
name: 'getDynamicData', | ||
arguments: [ | ||
{ | ||
name: 'key', | ||
value: 'listBases', | ||
}, | ||
], | ||
}, | ||
}, | ||
{ | ||
label: 'Table', | ||
key: 'tableId', | ||
type: 'dropdown', | ||
required: true, | ||
dependsOn: ['parameters.baseId'], | ||
description: '', | ||
variables: true, | ||
source: { | ||
type: 'query', | ||
name: 'getDynamicData', | ||
arguments: [ | ||
{ | ||
name: 'key', | ||
value: 'listTables', | ||
}, | ||
{ | ||
name: 'parameters.baseId', | ||
value: '{parameters.baseId}', | ||
}, | ||
], | ||
}, | ||
additionalFields: { | ||
type: 'query', | ||
name: 'getDynamicFields', | ||
arguments: [ | ||
{ | ||
name: 'key', | ||
value: 'listFields', | ||
}, | ||
{ | ||
name: 'parameters.baseId', | ||
value: '{parameters.baseId}', | ||
}, | ||
{ | ||
name: 'parameters.tableId', | ||
value: '{parameters.tableId}', | ||
}, | ||
], | ||
}, | ||
}, | ||
], | ||
|
||
async run($) { | ||
const { baseId, tableId, ...rest } = $.step.parameters; | ||
|
||
const fields = Object.entries(rest).reduce((result, [key, value]) => { | ||
if (Array.isArray(value)) { | ||
result[key] = value.map((item) => item.value); | ||
} else { | ||
result[key] = value; | ||
} | ||
return result; | ||
}, {}); | ||
|
||
const body = { | ||
typecast: true, | ||
fields, | ||
}; | ||
|
||
const { data } = await $.http.post(`/v0/${baseId}/${tableId}`, body); | ||
|
||
$.setActionItem({ | ||
raw: data, | ||
}); | ||
}, | ||
}); |
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,3 @@ | ||
import createRecord from './create-record/index.js'; | ||
|
||
export default [createRecord]; |
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,4 @@ | ||
import listBases from './list-bases/index.js'; | ||
import listTables from './list-tables/index.js'; | ||
|
||
export default [listBases, listTables]; |
28 changes: 28 additions & 0 deletions
28
packages/backend/src/apps/airtable/dynamic-data/list-bases/index.js
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,28 @@ | ||
export default { | ||
name: 'List bases', | ||
key: 'listBases', | ||
|
||
async run($) { | ||
const bases = { | ||
data: [], | ||
}; | ||
|
||
const params = {}; | ||
|
||
do { | ||
const { data } = await $.http.get('/v0/meta/bases', { params }); | ||
params.offset = data.offset; | ||
|
||
if (data?.bases) { | ||
for (const base of data.bases) { | ||
bases.data.push({ | ||
value: base.id, | ||
name: base.name, | ||
}); | ||
} | ||
} | ||
} while (params.offset); | ||
|
||
return bases; | ||
}, | ||
}; |
35 changes: 35 additions & 0 deletions
35
packages/backend/src/apps/airtable/dynamic-data/list-tables/index.js
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,35 @@ | ||
export default { | ||
name: 'List tables', | ||
key: 'listTables', | ||
|
||
async run($) { | ||
const tables = { | ||
data: [], | ||
}; | ||
const baseId = $.step.parameters.baseId; | ||
|
||
if (!baseId) { | ||
return tables; | ||
} | ||
|
||
const params = {}; | ||
|
||
do { | ||
const { data } = await $.http.get(`/v0/meta/bases/${baseId}/tables`, { | ||
params, | ||
}); | ||
params.offset = data.offset; | ||
|
||
if (data?.tables) { | ||
for (const table of data.tables) { | ||
tables.data.push({ | ||
value: table.id, | ||
name: table.name, | ||
}); | ||
} | ||
} | ||
} while (params.offset); | ||
|
||
return tables; | ||
}, | ||
}; |
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,3 @@ | ||
import listFields from './list-fields/index.js'; | ||
|
||
export default [listFields]; |
86 changes: 86 additions & 0 deletions
86
packages/backend/src/apps/airtable/dynamic-fields/list-fields/index.js
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,86 @@ | ||
const hasValue = (value) => value !== null && value !== undefined; | ||
|
||
export default { | ||
name: 'List fields', | ||
key: 'listFields', | ||
|
||
async run($) { | ||
const options = []; | ||
const { baseId, tableId } = $.step.parameters; | ||
|
||
if (!hasValue(baseId) || !hasValue(tableId)) { | ||
return; | ||
} | ||
|
||
const { data } = await $.http.get(`/v0/meta/bases/${baseId}/tables`); | ||
|
||
const selectedTable = data.tables.find((table) => table.id === tableId); | ||
|
||
if (!selectedTable) return; | ||
|
||
selectedTable.fields.forEach((field) => { | ||
if (field.type === 'singleSelect') { | ||
options.push({ | ||
label: field.name, | ||
key: field.name, | ||
type: 'dropdown', | ||
required: false, | ||
variables: true, | ||
options: field.options.choices.map((choice) => ({ | ||
label: choice.name, | ||
value: choice.id, | ||
})), | ||
}); | ||
} else if (field.type === 'multipleSelects') { | ||
options.push({ | ||
label: field.name, | ||
key: field.name, | ||
type: 'dynamic', | ||
required: false, | ||
variables: true, | ||
fields: [ | ||
{ | ||
label: 'Value', | ||
key: 'value', | ||
type: 'dropdown', | ||
required: false, | ||
variables: true, | ||
options: field.options.choices.map((choice) => ({ | ||
label: choice.name, | ||
value: choice.id, | ||
})), | ||
}, | ||
], | ||
}); | ||
} else if (field.type === 'checkbox') { | ||
options.push({ | ||
label: field.name, | ||
key: field.name, | ||
type: 'dropdown', | ||
required: false, | ||
variables: true, | ||
options: [ | ||
{ | ||
label: 'Yes', | ||
value: 'true', | ||
}, | ||
{ | ||
label: 'No', | ||
value: 'false', | ||
}, | ||
], | ||
}); | ||
} else { | ||
options.push({ | ||
label: field.name, | ||
key: field.name, | ||
type: 'string', | ||
required: false, | ||
variables: true, | ||
}); | ||
} | ||
}); | ||
|
||
return options; | ||
}, | ||
}; |
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
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,12 @@ | ||
--- | ||
favicon: /favicons/airtable.svg | ||
items: | ||
- name: Create record | ||
desc: Creates a new record with fields that automatically populate. | ||
--- | ||
|
||
<script setup> | ||
import CustomListing from '../../components/CustomListing.vue' | ||
</script> | ||
|
||
<CustomListing /> |
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