From 8b5cfcec82ca75c98c2910f3409fa4403006182e Mon Sep 17 00:00:00 2001 From: Anton Golub Date: Fri, 22 Sep 2023 10:03:35 +0300 Subject: [PATCH] feat: introduce entry boxing to hold id --- README.md | 12 ++++++------ src/main/ts/index.ts | 32 +++++++++++++++++++++----------- src/main/ts/interface.ts | 11 +++++++---- src/test/ts/index.test.ts | 22 +++++++++++----------- 4 files changed, 45 insertions(+), 32 deletions(-) diff --git a/README.md b/README.md index 3d29396..f262bb0 100644 --- a/README.md +++ b/README.md @@ -13,18 +13,18 @@ const tower = createTower({ branch: 'tagtower' }) -const tag: string = 'some@tag' -const entry: Record = { +const id: string = 'some@tag' +const data: Record = { hash: '3f9f0a88b411a8932bce289a3dd498d70a4dc96c', author: 'Anton Golub ', message: `feat: initial feat` } -await tower.create(tag, entry) // stores entry to the specified remote -await tower.read(tag) // returns found TEntry | null +await tower.create(id, data) // stores entry to the specified remote +await tower.read(id) // returns found TEntry | null await tower.read() // if tag is empty, returns TEntry[] -await tower.update(tag, entry) // just a shortcut for delete & create -await tower.delete(tag) // void +await tower.update(id, data) // just a shortcut for delete & create +await tower.delete(id) // void ``` ## License diff --git a/src/main/ts/index.ts b/src/main/ts/index.ts index 17d9269..ea2ec38 100644 --- a/src/main/ts/index.ts +++ b/src/main/ts/index.ts @@ -1,25 +1,35 @@ import {readTags, deleteTag, pushTags} from './git' -import {TTower, TTowerFactory, TTowerOpts} from './interface' +import {TTagEntry, TTower, TTowerFactory, TTowerOpts} from './interface' export const createTower: TTowerFactory = (opts: TTowerOpts): TTower => ({ - async create(tag, entry){ - await pushTags({...opts, tags: [{tag, body: JSON.stringify(entry)}]}) + async create(tag, data){ + await pushTags({...opts, tags: [{tag, body: JSON.stringify(data)}]}) }, - async read(tag?: string) { - const tags = (await readTags(opts)).map(({tag, body}) => ({tag, entry: JSON.parse(body.trim())})) - if (tag === undefined) { - return tags + // eslint-disable-next-line @typescript-eslint/ban-ts-comment + // @ts-ignore + async read(id?: string) { + const entries: TTagEntry[] = (await readTags(opts)).map(({tag: id, body}) => ({id, data: jsonParse(body)})) + if (id === undefined) { + return entries } - return tags.find(({tag: _tag}) => _tag === tag)?.entry || null + return entries.find(({id: _id}) => _id === id) || null }, async delete(tag) { await deleteTag({...opts, tag}) }, - async update(tag, entry) { - await this.delete(tag) - await this.create(tag, entry) + async update(id, data) { + await this.delete(id) + await this.create(id, data) } }) +const jsonParse = (value: any): TTagEntry['data'] => { + try { + return JSON.parse(value.trim()) + } catch { + return {value} + } +} + export * from './interface' diff --git a/src/main/ts/interface.ts b/src/main/ts/interface.ts index a4762b3..3a600aa 100644 --- a/src/main/ts/interface.ts +++ b/src/main/ts/interface.ts @@ -1,11 +1,14 @@ -export type TTagEntry = Record +export type TTagEntry = { + id: string, + data: Record +} export type TTower = { - create(id: string, entry: TTagEntry): Promise - read(id: string): Promise + create(id: string, data: TTagEntry['data']): Promise + read(id: string): Promise read(): Promise - update(id: string, entry: TTagEntry): Promise + update(id: string, data: TTagEntry['data']): Promise delete(id: string): Promise } diff --git a/src/test/ts/index.test.ts b/src/test/ts/index.test.ts index 7b09df9..b3cbf19 100644 --- a/src/test/ts/index.test.ts +++ b/src/test/ts/index.test.ts @@ -9,7 +9,7 @@ import process from "node:process"; const temp = path.resolve(process.cwd(), 'temp') describe('tagTower', () => { - it('provides CRUD', async () => { + it('provides CRUD-like API', async () => { const cwd = path.resolve(temp, 'local-repo') await fs.mkdir(cwd, { recursive: true }) await exec('git', ['init', '--bare'], {cwd}) @@ -20,24 +20,24 @@ describe('tagTower', () => { temp }) - const tag = 'some@tag' - const entry: Record = { + const id = 'some@tag' + const data: Record = { hash: '3f9f0a88b411a8932bce289a3dd498d70a4dc96c', author: 'Anton Golub ', message: `feat: initial feat` } - await tower.create(tag, entry) - assert.deepEqual(await tower.read(tag), entry) + await tower.create(id, data) + assert.deepEqual((await tower.read(id))?.data, data) assert.equal(await tower.read('not-found'), null) - const tags = await tower.read() - assert.ok(tags.some(entry => entry.tag === tag)) + const entries = await tower.read() + assert.ok(entries.some(entry => entry.id === id)) - await tower.update(tag, {...entry, foo: 'bar'}) - assert.equal((await tower.read(tag))?.foo, 'bar') + await tower.update(id, {...data, foo: 'bar'}) + assert.equal((await tower.read(id))?.data.foo, 'bar') - await tower.delete(tag) - assert.equal(await tower.read(tag), null) + await tower.delete(id) + assert.equal(await tower.read(id), null) }) })