From bcdfa714a535d0e5382f7b6186ca22be09651ec2 Mon Sep 17 00:00:00 2001 From: Jim Toth Date: Wed, 6 Sep 2023 13:38:40 -0400 Subject: [PATCH 1/3] Adds fetching of publication by slug and slug-or-id --- src/legacy/legacy.ts | 24 +++++++++++++ test/e2e/artbycity.e2e.ts | 73 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 97 insertions(+) diff --git a/src/legacy/legacy.ts b/src/legacy/legacy.ts index ae50fba..2ca7119 100644 --- a/src/legacy/legacy.ts +++ b/src/legacy/legacy.ts @@ -87,6 +87,30 @@ export default class ArtByCityLegacy { } } + async fetchPublicationBySlugOrId( + slugOrId: string + ): Promise { + try { + return await this.fetchPublicationBySlug(slugOrId) + } catch (error) {} + + return this.fetchPublication(slugOrId) + } + + async fetchPublicationBySlug( + slug: string + ): Promise { + const { transactions } = await this + .transactions + .query('artwork', { tags: [ { name: 'slug', value: slug } ] }) + + if (transactions.length < 1) { + throw new Error(`404 Publication Not Found: slug://${slug}`) + } + + return this.fetchPublication(transactions[0].id) + } + async fetchPublication( manifestId: string ): Promise { diff --git a/test/e2e/artbycity.e2e.ts b/test/e2e/artbycity.e2e.ts index f914849..380b7e8 100644 --- a/test/e2e/artbycity.e2e.ts +++ b/test/e2e/artbycity.e2e.ts @@ -22,6 +22,7 @@ const AUDIO_MANIFEST_ID = 'q0GSg9bSntJQIj-FiHYApMat1e20EGM2JkdgrkhtZjI' const MODEL_MANIFEST_ID = 'N_nbvz1vWrNI1kl0Nxefgi3Zo0p9F-onoF1mafWaKUU' // const LICENSE_BUNDLE_ID = 'BgRQtrLdwexvWcbP5RKheYRGnQUsAZtZ6uKD_5A-CzE' const LICENSE_MANIFEST_ID = 'q0GSg9bSntJQIj-FiHYApMat1e20EGM2JkdgrkhtZjI' +const SLUG = 'modified-icosahedron-3d' const PROFILE_ID = '2aLkIcBH52s2LtZoyRQC_YaFGGSB2r2yGUtgYM5MjZc' @@ -180,6 +181,55 @@ describe(`ArtByCity (web)`, () => { .with.length(43) }) + it('Fetches a publication by slug', async () => { + const abc = new ArtByCity(arweave) + + const publication = await abc.legacy.fetchPublicationBySlug(SLUG) + + expect(publication.id).to.be.a('string').with.length(43) + expect(publication.category).to.equal('artwork') + expect(publication.subCategory).to.be.oneOf( + [ 'image', 'audio', 'model' ] + ) + expect(publication.published).to.be.a('Date') + expect(publication.year).to.be.a('string') + expect(publication.slug).to.equal(SLUG) + expect(publication.title).to.be.a('string') + expect(publication.images).to.not.be.empty + expect(publication.images[0].image).to.be.a('string').with.length(43) + expect(publication.images[0].preview).to.be.a('string').with.length(43) + expect(publication.images[0].preview4k) + .to.be.a('string') + .with.length(43) + }) + + context('Fetches publication by slug or id', () => { + it('by slug', async () => { + const abc = new ArtByCity(arweave) + + const publication = await abc.legacy.fetchPublicationBySlugOrId(SLUG) + expect(publication.id).to.be.a('string').with.length(43) + expect(publication.category).to.equal('artwork') + expect(publication.subCategory).to.be.oneOf( + [ 'image', 'audio', 'model' ] + ) + expect(publication.slug).to.equal(SLUG) + }) + + it('by id', async () => { + const abc = new ArtByCity(arweave) + + const publication = await abc.legacy.fetchPublicationBySlugOrId( + MODEL_MANIFEST_ID + ) + expect(publication.id).to.be.a('string').with.length(43) + expect(publication.category).to.equal('artwork') + expect(publication.subCategory).to.be.oneOf( + [ 'image', 'audio', 'model' ] + ) + }) + }) + it('Fetches a publication by manifest id from bundle', async () => { const abc = new ArtByCity(arweave) @@ -295,6 +345,29 @@ describe(`ArtByCity (web)`, () => { `404 Publication Not Found: ar://${badManifestId}` ) }) + + it('Throws 404 on fetches by slug', () => { + const abc = new ArtByCity(arweave) + const badSlug = '404thisslugnotexistpleaselol' + + // eslint-disable-next-line @typescript-eslint/no-floating-promises + expect(abc.legacy.fetchPublicationBySlug(badSlug)).to.be.rejectedWith( + Error, + `404 Publication Not Found: slug://${badSlug}` + ) + }) + + it('Throws 404 on fetches by slug or id', () => { + const abc = new ArtByCity(arweave) + const badSlug = '404thisslugnotexistpleaselol' + + // eslint-disable-next-line @typescript-eslint/no-floating-promises + expect(abc.legacy.fetchPublicationBySlugOrId(badSlug)) + .to.be.rejectedWith( + Error, + `404 Publication Not Found: ar://${badSlug}` + ) + }) }) context('Fetching Profiles', () => { From 1039b3262b2978ecff8cbcaf8aea04e677fa9a84 Mon Sep 17 00:00:00 2001 From: Jim Toth Date: Wed, 6 Sep 2023 13:41:22 -0400 Subject: [PATCH 2/3] linting --- src/legacy/legacy.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/legacy/legacy.ts b/src/legacy/legacy.ts index 2ca7119..d872c3f 100644 --- a/src/legacy/legacy.ts +++ b/src/legacy/legacy.ts @@ -92,7 +92,7 @@ export default class ArtByCityLegacy { ): Promise { try { return await this.fetchPublicationBySlug(slugOrId) - } catch (error) {} + } catch (error) { /* eslint-disable-line no-empty */} return this.fetchPublication(slugOrId) } From c4d9df0481cfaac5908156e9f120b3f3ca73d3f9 Mon Sep 17 00:00:00 2001 From: Jim Toth Date: Wed, 6 Sep 2023 13:41:56 -0400 Subject: [PATCH 3/3] Cleanup --- src/legacy/legacy.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/legacy/legacy.ts b/src/legacy/legacy.ts index d872c3f..6c8d533 100644 --- a/src/legacy/legacy.ts +++ b/src/legacy/legacy.ts @@ -92,7 +92,7 @@ export default class ArtByCityLegacy { ): Promise { try { return await this.fetchPublicationBySlug(slugOrId) - } catch (error) { /* eslint-disable-line no-empty */} + } catch (error) { /* eslint-disable-line no-empty */ } return this.fetchPublication(slugOrId) }