@flatfile/listener 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,77 @@
1
+ import { objectMatches } from './glob.match'
2
+
3
+ describe('objectMatches', () => {
4
+ test('matches an object', () => {
5
+ expect(objectMatches({ foo: 'bar' }, { foo: 'bar' })).toBe(true)
6
+ expect(objectMatches({ foo: ['bar', 'baz'] }, { foo: 'bar' })).toBe(true)
7
+ })
8
+
9
+ test('denies an object with no exact match', () => {
10
+ expect(objectMatches({ foo: 'bar' }, { foo: 'bar', baz: 'bar' })).toBe(
11
+ false
12
+ )
13
+ })
14
+
15
+ test('accepts an object with one of a match', () => {
16
+ expect(objectMatches({ foo: 'bar' }, { foo: ['bar', 'baz'] })).toBe(true)
17
+ expect(
18
+ objectMatches({ foo: ['bar', 'crux'] }, { foo: ['bar', 'baz'] })
19
+ ).toBe(true)
20
+ })
21
+
22
+ test('accepts an object with wildcard match', () => {
23
+ expect(objectMatches({ foo: 'name:bar' }, { foo: 'name:*' })).toBe(true)
24
+ expect(objectMatches({ foo: 'name:bar' }, { foo: '*:*' })).toBe(true)
25
+ expect(objectMatches({ foo: 'name:bar' }, { foo: '*:bar' })).toBe(true)
26
+ expect(objectMatches({ foo: 'name:bar' }, { foo: '**' })).toBe(true)
27
+
28
+ expect(
29
+ objectMatches({ foo: ['name:bar', 'smth:bar'] }, { foo: 'name:*' })
30
+ ).toBe(true)
31
+ expect(
32
+ objectMatches({ foo: ['name:bar', 'smth:bar'] }, { foo: '*:*' })
33
+ ).toBe(true)
34
+ expect(
35
+ objectMatches({ foo: ['name:bar', 'smth:bar'] }, { foo: '*:bar' })
36
+ ).toBe(true)
37
+ expect(
38
+ objectMatches({ foo: ['name:bar', 'smth:bar'] }, { foo: '**' })
39
+ ).toBe(true)
40
+ })
41
+
42
+ test('denies an object with no wildcard match', () => {
43
+ expect(objectMatches({ foo: 'title:bar' }, { foo: 'name:*' })).toBe(false)
44
+ expect(objectMatches({ foo: 'name:bar' }, { foo: '*:baz' })).toBe(false)
45
+
46
+ expect(
47
+ objectMatches({ foo: ['title:bar', 'cool:cat'] }, { foo: 'name:*' })
48
+ ).toBe(false)
49
+ expect(
50
+ objectMatches({ foo: ['name:bar', 'cool:cat'] }, { foo: '*:baz' })
51
+ ).toBe(false)
52
+ })
53
+
54
+ test('accepts an object with one of a wildcard match', () => {
55
+ expect(
56
+ objectMatches({ foo: 'name:bar' }, { foo: ['name:*', 'title:*'] })
57
+ ).toBe(true)
58
+ expect(
59
+ objectMatches(
60
+ { foo: ['name:bar', 'pork:bar'] },
61
+ { foo: ['name:*', 'title:*'] }
62
+ )
63
+ ).toBe(true)
64
+ })
65
+
66
+ test('denies an object with none of a wildcard match', () => {
67
+ expect(
68
+ objectMatches({ foo: 'pork:bar' }, { foo: ['name:*', 'title:*'] })
69
+ ).toBe(false)
70
+ expect(
71
+ objectMatches(
72
+ { foo: ['pork:bar', 'crinkle:cut'] },
73
+ { foo: ['name:*', 'title:*'] }
74
+ )
75
+ ).toBe(false)
76
+ })
77
+ })
@@ -0,0 +1,49 @@
1
+ import { Arrayable } from './event.handler'
2
+ import wildMatch from 'wildcard-match'
3
+
4
+ /**
5
+ * Glob style matching of a value
6
+ *
7
+ * @param val
8
+ * @param filter
9
+ */
10
+ export function glob(val: any, filter: string | string[]): boolean {
11
+ if (!val || typeof val !== 'string') {
12
+ return false
13
+ }
14
+ return wildMatch(filter || '**', ':')(val)
15
+ }
16
+
17
+ /**
18
+ * Glob style matching of values in an object
19
+ *
20
+ * @param object
21
+ * @param filter
22
+ */
23
+ export function objectMatches(
24
+ object: Record<string, any>,
25
+ filter: Record<string, Arrayable<string>>
26
+ ): boolean {
27
+ let denied = false
28
+ for (const x in filter) {
29
+ const prop: Arrayable<string> = object[x]
30
+
31
+ if (!object?.[x]) {
32
+ return false
33
+ }
34
+
35
+ if (
36
+ typeof prop === 'string' ||
37
+ (Array.isArray(prop) && typeof prop[0] === 'string')
38
+ ) {
39
+ if (Array.isArray(prop)) {
40
+ denied ||= !prop.some((p) => {
41
+ return glob(p, filter[x])
42
+ })
43
+ } else {
44
+ denied ||= !glob(prop, filter[x])
45
+ }
46
+ }
47
+ }
48
+ return !denied
49
+ }
@@ -0,0 +1,3 @@
1
+ export * from './event.handler'
2
+ export * from './flatfile.event'
3
+ export * from './authenticated.client'
File without changes
@@ -0,0 +1,38 @@
1
+ import { Client, FlatfileVirtualMachine } from '../'
2
+
3
+ const example = Client.create((client) => {
4
+ /**
5
+ * This is a basic hook on events with no sugar on top
6
+ */
7
+ client.on('records:*', { target: 'sheet(TestSheet)' }, async (event) => {
8
+ const { workbookId, sheetId } = event.context
9
+ try {
10
+ const records = await event.data
11
+ const recordsUpdates = records?.records.map((record: any) => {
12
+ record.values.middleName.value = 'TestSheet'
13
+
14
+ return record
15
+ })
16
+ await client.api.updateRecords({
17
+ workbookId,
18
+ sheetId,
19
+ recordsUpdates,
20
+ })
21
+ } catch (e) {
22
+ console.log(`Error getting records: ${e}`)
23
+ }
24
+ })
25
+
26
+ /**
27
+ * This is a setup of the space with its workbooks
28
+ */
29
+ client.on('client:init', async (event) => {
30
+ console.log('client:init')
31
+ })
32
+ })
33
+
34
+ const FlatfileVM = new FlatfileVirtualMachine()
35
+
36
+ example.mount(FlatfileVM)
37
+
38
+ export default example
package/src/index.ts ADDED
@@ -0,0 +1,4 @@
1
+ export * from './Client'
2
+ export * from './event-drivers'
3
+ export * from './plugins'
4
+ export * from './events'
@@ -0,0 +1 @@
1
+ export * from './useConfigure'
@@ -0,0 +1,72 @@
1
+ import { Client } from '../Client'
2
+ import { Blueprint } from '@flatfile/api'
3
+ import { Agent } from '@flatfile/configure'
4
+
5
+ type IConfigureOptions = { environmentId?: string; createSpaces?: boolean }
6
+
7
+ // TODO: first pass at useConfigure Plugin. Does not register hooks, only deploys the spaceConfig
8
+ export const useConfigure = (agent: Agent, options?: IConfigureOptions) => {
9
+ const configure = async (client: Client) => {
10
+ const {
11
+ options: { spaceConfigs },
12
+ } = agent
13
+ for (const slug in spaceConfigs) {
14
+ const spaceConfig = spaceConfigs[slug]
15
+
16
+ try {
17
+ const spacePatternConfig = {
18
+ name: spaceConfig.options.name,
19
+ // TODO Do we need a unique slug for this in the Platform SDK or X? Should we generate them in X?
20
+ slug,
21
+ blueprints: mapObj(
22
+ spaceConfig.options.workbookConfigs,
23
+ (wb, wbSlug, i) => {
24
+ return {
25
+ name: wb.options.name,
26
+ slug: `${slug}/${wbSlug}`,
27
+ primary: i === 0,
28
+ sheets: mapObj(wb.options.sheets, (model, modelSlug) =>
29
+ model.toBlueprint(wbSlug, modelSlug)
30
+ ),
31
+ } as Blueprint
32
+ }
33
+ ),
34
+ }
35
+ const spaceConfigRes = await client.api.addSpaceConfig({
36
+ spacePatternConfig,
37
+ })
38
+
39
+ const spaceConfigId = spaceConfigRes?.data?.id || ''
40
+
41
+ if (options?.createSpaces && options.environmentId) {
42
+ try {
43
+ const space = await client.api.addSpace({
44
+ spaceConfig: {
45
+ spaceConfigId,
46
+ environmentId: options.environmentId,
47
+ name: spaceConfig.options.name,
48
+ },
49
+ })
50
+ } catch (e) {
51
+ console.log(`Error Creating Space error: ${e}`)
52
+ }
53
+ }
54
+ } catch (e) {
55
+ console.log(`Error Creating Space Config: ${e}`)
56
+ }
57
+ }
58
+ }
59
+ return configure
60
+ }
61
+
62
+ function mapObj<T, K>(
63
+ obj: Record<string, K>,
64
+ cb: (value: K, key: string, i: number) => T
65
+ ): T[] {
66
+ const slugs = Object.keys(obj)
67
+ let i = 0
68
+ return slugs.map((slug) => {
69
+ const model = obj[slug]
70
+ return cb(model, slug, i++)
71
+ })
72
+ }
package/tsconfig.json ADDED
@@ -0,0 +1,10 @@
1
+ {
2
+ "extends": "@flatfile/ts-config-platform-sdk/base.json",
3
+ "include": ["./src"],
4
+ "exclude": ["dist", "build", "node_modules"],
5
+ "compilerOptions": {
6
+ "lib": ["ESNext"],
7
+ "module": "ESNext",
8
+ "target": "ES6"
9
+ },
10
+ }