Skip to content

Commit

Permalink
chore
Browse files Browse the repository at this point in the history
  • Loading branch information
bxb100 committed Nov 6, 2024
1 parent 67a2700 commit 82d8ab0
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 46 deletions.
43 changes: 43 additions & 0 deletions __tests__/connect.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { Connect } from '../src/api/connect'

describe('connect references regex test', () => {
it('test suit 1', () => {
const ref =
'op://d4472vsddcneis7b4onz4v75nu/Secure Note/weuaijejjhpp2o3em5dtokca2a'
expect(ref).toMatch(Connect.REGEX)

const exec = Connect.REGEX.exec(ref)
expect(exec).not.toBeNull()
expect(exec?.groups).not.toBeNull()
expect(exec?.groups?.vault_name).toBe('d4472vsddcneis7b4onz4v75nu')
expect(exec?.groups?.item_name).toBe('Secure Note')
expect(exec?.groups?.section_name).toBeUndefined()
expect(exec?.groups?.field_name).toBe('weuaijejjhpp2o3em5dtokca2a')
})

it('test suit 2', () => {
const ref = 'op://dev/5vtojw237m2cxymfkfhtxsrazm/text'
expect(ref).toMatch(Connect.REGEX)

const exec = Connect.REGEX.exec(ref)
expect(exec).not.toBeNull()
expect(exec?.groups).not.toBeNull()
expect(exec?.groups?.vault_name).toBe('dev')
expect(exec?.groups?.item_name).toBe('5vtojw237m2cxymfkfhtxsrazm')
expect(exec?.groups?.section_name).toBeUndefined()
expect(exec?.groups?.field_name).toBe('text')
})

it('test suit 3', () => {
const ref = 'op://dev/5vtojw237m2cxymfkfhtxsrazm/cs/text'
expect(ref).toMatch(Connect.REGEX)

const exec = Connect.REGEX.exec(ref)
expect(exec).not.toBeNull()
expect(exec?.groups).not.toBeNull()
expect(exec?.groups?.vault_name).toBe('dev')
expect(exec?.groups?.item_name).toBe('5vtojw237m2cxymfkfhtxsrazm')
expect(exec?.groups?.section_name).toBe('cs')
expect(exec?.groups?.field_name).toBe('text')
})
})
2 changes: 1 addition & 1 deletion badges/coverage.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
35 changes: 17 additions & 18 deletions dist/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/index.js.map

Large diffs are not rendered by default.

58 changes: 32 additions & 26 deletions src/api/connect.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Item, ResolveSecretReference, Vault } from './types'
import { Item, ItemField, ResolveSecretReference, Vault } from './types'
import { $Fetch, ofetch } from 'ofetch'
import process from 'node:process'
import { envConnectHost, envConnectToken } from '../constants'
Expand Down Expand Up @@ -36,44 +36,50 @@ export class Connect implements ResolveSecretReference {
field_name: string
}

const first_item = async <T = Vault | Item>(
res: Promise<T[]>,
predicate: (v: T) => boolean
): Promise<T> => {
const items = (await res).filter(predicate)
if (items.length === 0) {
throw new Error('No items found')
const find_item = <T = Vault | Item | ItemField>(
items: Awaited<T[]>,
id_predicate: (v: T) => boolean,
name_predicate: (v: T) => boolean
): T => {
const id_res = items.find(id_predicate)
if (id_res) {
return id_res
}
return items[0]
const name_res = items.filter(name_predicate)
if (name_res.length > 1) {
throw new Error('Multiple items found')
} else if (name_res.length === 1) {
return name_res[0]
}
throw new Error('No items found')
}

// cache response may cause other potential security issues
const vault = await first_item(
this.api<Vault[]>('/v1/vaults'),
v => v.name === vault_name || v.id === vault_name
const vault = find_item(
await this.api<Vault[]>('/v1/vaults'),
v => v.id === vault_name,
v => v.name === vault_name
)
const item = await first_item(
this.api<Item[]>(`/v1/vaults/${vault.id}/items`),
i => i.title === item_name || i.id === item_name
const item = find_item(
await this.api<Item[]>(`/v1/vaults/${vault.id}/items`),
i => i.id === item_name,
i => i.title === item_name
)
const item_detail = await this.api<Item>(
`/v1/vaults/${vault.id}/items/${item.id}`
)
let section_id: string | undefined
let item_fields = item_detail.fields
if (section_name) {
const section = item_detail.sections.filter(
s => s.label === section_name || s.id === section_name
)[0]
section_id = section.id
item_fields = item_fields.filter(f => f.section?.id === section.id)
}
const field = item_detail.fields
.filter(f => {
if (section_id) {
return f.section?.id === section_id
}
return true
})
.filter(f => f.label === field_name || f.id === field_name)[0]
return field.value

return find_item(
item_fields,
f => f.id === field_name,
f => f.label === field_name
).value
}
}

0 comments on commit 82d8ab0

Please sign in to comment.