From 17c0ab04252193be5fd05730e110be7076585aca Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marcel=20Rebou=C3=A7as?= Date: Tue, 30 Jul 2024 09:32:26 -0300 Subject: [PATCH] refact(Singleton): rename the singleton methods --- README.md | 4 ++-- src/incogniaApi.ts | 18 +++++++++--------- test/incogniaApi.test.ts | 12 ++++++------ 3 files changed, 17 insertions(+), 17 deletions(-) diff --git a/README.md b/README.md index 951f200..4302b11 100644 --- a/README.md +++ b/README.md @@ -47,12 +47,12 @@ Additionally, the Incognia API instance can be stored in a global instance as fo sure that a single Incognia API client is created and used. ```js -IncogniaApi.setSingletonInstance( +IncogniaApi.setInstance( new IncogniaApi({ clientId: 'clientId', clientSecret: 'clientSecret' }) ) // Retrieves the stored instance -const incogniaApi = IncogniaApi.getSingletonInstance() +const incogniaApi = IncogniaApi.instance() ``` ## API methods diff --git a/src/incogniaApi.ts b/src/incogniaApi.ts index 039c90d..d5bb69a 100644 --- a/src/incogniaApi.ts +++ b/src/incogniaApi.ts @@ -60,28 +60,28 @@ export class IncogniaApi { ** Singleton support */ - private static instance?: IncogniaApi + private static apiInstance?: IncogniaApi - public static setSingletonInstance(instance: IncogniaApi): void { - if (!IncogniaApi.instance) { + public static setInstance(instance: IncogniaApi): void { + if (!IncogniaApi.apiInstance) { if (!instance) { throw new IncogniaError( 'No instance provided to set as singleton instance' ) } - IncogniaApi.instance = instance + IncogniaApi.apiInstance = instance } } - public static getSingletonInstance(): IncogniaApi { - if (!IncogniaApi.instance) { + public static instance(): IncogniaApi { + if (!IncogniaApi.apiInstance) { throw new IncogniaError('No singleton instance set') } - return IncogniaApi.instance + return IncogniaApi.apiInstance } - public static resetSingletonInstance(): void { - IncogniaApi.instance = undefined + public static resetInstance(): void { + IncogniaApi.apiInstance = undefined } /* diff --git a/test/incogniaApi.test.ts b/test/incogniaApi.test.ts index 0f6a48c..0749872 100644 --- a/test/incogniaApi.test.ts +++ b/test/incogniaApi.test.ts @@ -404,20 +404,20 @@ describe('IncogniaApi', () => { }) describe('Singleton support', () => { - beforeEach(() => IncogniaApi.resetSingletonInstance()) + beforeEach(() => IncogniaApi.resetInstance()) it('sets and gets the singleton instance', () => { - IncogniaApi.setSingletonInstance(incogniaApi) + IncogniaApi.setInstance(incogniaApi) const newClient = new IncogniaApi(credentials) - IncogniaApi.setSingletonInstance(newClient) + IncogniaApi.setInstance(newClient) - expect(IncogniaApi.getSingletonInstance()).toEqual(incogniaApi) + expect(IncogniaApi.instance()).toEqual(incogniaApi) }) it('throws an error when no singleton instance is set', () => { const getSingletonInstance = () => { - IncogniaApi.getSingletonInstance() + IncogniaApi.instance() } expect(getSingletonInstance).toThrow('No singleton instance set') expect(getSingletonInstance).toThrowError(IncogniaError) @@ -425,7 +425,7 @@ describe('IncogniaApi', () => { it('throws an error when no instance is provided', () => { const setSingletonInstance = () => { - IncogniaApi.setSingletonInstance(null as any) + IncogniaApi.setInstance(null as any) } expect(setSingletonInstance).toThrow( 'No instance provided to set as singleton instance'