From 31000da7aa2b728273456fb4cd35d088d87fd367 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Nieto?= Date: Mon, 20 May 2024 13:55:06 -0600 Subject: [PATCH 1/2] add SequenceIndexerOpts --- packages/indexer/package.json | 4 +- packages/indexer/src/index.ts | 32 ++++++++- packages/indexer/tests/constructor.ts | 94 +++++++++++++++++++++++++++ 3 files changed, 127 insertions(+), 3 deletions(-) create mode 100644 packages/indexer/tests/constructor.ts diff --git a/packages/indexer/package.json b/packages/indexer/package.json index 42aa98f0b..7a290cbd8 100644 --- a/packages/indexer/package.json +++ b/packages/indexer/package.json @@ -9,7 +9,9 @@ "author": "Horizon Blockchain Games", "license": "Apache-2.0", "scripts": { - "test": "echo", + "test": "pnpm test:run", + "test:run": "pnpm test:file tests/**.ts", + "test:file": "NODE_OPTIONS='--import tsx' DEBUG=server mocha -R spec --timeout 30000", "typecheck": "tsc --noEmit" }, "dependencies": {}, diff --git a/packages/indexer/src/index.ts b/packages/indexer/src/index.ts index 544c2ce4b..a85c1234b 100644 --- a/packages/indexer/src/index.ts +++ b/packages/indexer/src/index.ts @@ -4,13 +4,40 @@ import { Indexer as IndexerRpc } from './indexer.gen' const fetch = typeof global === 'object' ? global.fetch : window.fetch +export interface SequenceIndexerOpts { + JWTAuth?: string + ProjectAccessKey?: string +} + export class SequenceIndexer extends IndexerRpc { + public jwtAuth?: string + public projectAccessKey?: string + constructor( hostname: string, - public projectAccessKey?: string, - public jwtAuth?: string + opts: SequenceIndexerOpts + ); + constructor( + hostname: string, + projectAccessKey?: string, + jwtAuth?: string + ); + constructor( + hostname: string, + ...args: any[] ) { super(hostname.endsWith('/') ? hostname.slice(0, -1) : hostname, fetch) + + if (args.length === 1 && typeof args[0] === 'object') { + const opts = args[0] as SequenceIndexerOpts + this.projectAccessKey = opts.ProjectAccessKey + this.jwtAuth = opts.JWTAuth + } else { + const [projectAccessKey, jwtAuth] = args + this.projectAccessKey = projectAccessKey || undefined + this.jwtAuth = jwtAuth || undefined + } + this.fetch = this._fetch } @@ -19,6 +46,7 @@ export class SequenceIndexer extends IndexerRpc { // if its been set on the api client const headers: { [key: string]: any } = {} + const jwtAuth = this.jwtAuth const projectAccessKey = this.projectAccessKey diff --git a/packages/indexer/tests/constructor.ts b/packages/indexer/tests/constructor.ts new file mode 100644 index 000000000..a202f218f --- /dev/null +++ b/packages/indexer/tests/constructor.ts @@ -0,0 +1,94 @@ +import * as chai from 'chai' +import chaiAsPromised from 'chai-as-promised' + +import { SequenceIndexer, SequenceIndexerOpts } from '@0xsequence/indexer' + +const { expect } = chai.use(chaiAsPromised) + +describe('SequenceIndexer initialization', () => { + it('should initialize without arguments', async () => { + const x = new SequenceIndexer('http://localhost:3000') + expect(x.jwtAuth).to.be.undefined + expect(x.projectAccessKey).to.be.undefined + }); + + it('should initialize with PAK', async () => { + const x = new SequenceIndexer( + 'http://localhost:3000', + 'project-access-key' + ) + expect(x.jwtAuth).to.be.undefined + expect(x.projectAccessKey).to.equal('project-access-key') + }) + + it('should initialize with JWT', async () => { + const x = new SequenceIndexer( + 'http://localhost:3000', + null, + 'jwt-auth' + ) + + expect(x.jwtAuth).to.equal('jwt-auth') + expect(x.projectAccessKey).to.be.undefined + }) + + it('should initialize with JWT and PAK', async () => { + const x = new SequenceIndexer( + 'http://localhost:3000', + 'project-access-key', + 'jwt-auth' + ) + + expect(x.jwtAuth).to.equal('jwt-auth') + expect(x.projectAccessKey).to.equal('project-access-key') + }) + + it('should initialize with opts', async () => { + const x = new SequenceIndexer( + 'http://localhost:3000', + { + JWTAuth: 'jwt-auth', + ProjectAccessKey: 'project-access-key' + } + ) + + expect(x.jwtAuth).to.equal('jwt-auth') + expect(x.projectAccessKey).to.equal('project-access-key') + }) + + it('should initialize with only PAK in opts', async () => { + const x = new SequenceIndexer( + 'http://localhost:3000', + { + ProjectAccessKey: 'project-access-key' + } + ) + + expect(x.jwtAuth).to.be.undefined + expect(x.projectAccessKey).to.equal('project-access-key') + }) + + it('should initialize with empty opts', async () => { + const x = new SequenceIndexer( + 'http://localhost:3000', + { + } + ) + + expect(x.jwtAuth).to.be.undefined + expect(x.projectAccessKey).to.be.undefined + }) + + it('should ignore invalid arguments', async () => { + const x = new SequenceIndexer( + 'http://localhost:3000', + { + A: 'a', + B: 'b' + }, + ) + + expect(x.jwtAuth).to.be.undefined + expect(x.projectAccessKey).to.be.undefined + }) +}); From 2ec8f8a1b9bd84bb06414d8fff7aa68ffcd545ee Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Nieto?= Date: Mon, 20 May 2024 13:57:55 -0600 Subject: [PATCH 2/2] remove DEBUG --- packages/indexer/package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/indexer/package.json b/packages/indexer/package.json index 7a290cbd8..59b42d3ef 100644 --- a/packages/indexer/package.json +++ b/packages/indexer/package.json @@ -11,7 +11,7 @@ "scripts": { "test": "pnpm test:run", "test:run": "pnpm test:file tests/**.ts", - "test:file": "NODE_OPTIONS='--import tsx' DEBUG=server mocha -R spec --timeout 30000", + "test:file": "NODE_OPTIONS='--import tsx' mocha -R spec --timeout 30000", "typecheck": "tsc --noEmit" }, "dependencies": {},