diff --git a/scripts/generate-types.ts b/scripts/generate-types.ts index fa76eec..6b8628d 100644 --- a/scripts/generate-types.ts +++ b/scripts/generate-types.ts @@ -2,18 +2,61 @@ import fs from 'fs'; const getJSONSchema = async () => { try { - const data = await fetch('https://dev.api.gateway.tech/swagger/doc.json'); + const data = await fetch('https://dev.api.gateway.tech/docs/swagger.json'); const body = await data.json(); const types = generateTypes(body.definitions); + const routes = generateRouteConstants(body.paths); + fs.writeFileSync('./src/common/types.ts', types); + fs.writeFileSync('./src/common/routes.ts', routes); } catch (error) { console.log(error); } }; +type PathItem = Record; + +interface Operation { + summary: string; +} + +function generateRouteConstants(paths: Record): string { + const constants: string[] = ['export const routes = {']; + + for (const path in paths) { + const pathItem = paths[path]; + + for (const method in pathItem) { + const operation = pathItem[method]; + const constantName = generateRouteConstantName( + method, + path, + operation.summary, + ); + const parameterizedPath = path; + constants.push(` ${constantName}: "${parameterizedPath}",`); + } + } + + constants.push('};'); + return constants.join('\n'); +} + +function generateRouteConstantName( + method: string, + path: string, + summary: string, +): string { + let name = summary.trim(); + if (name === '') { + name = path; + } + return toPascalCase(name); +} + function toPascalCase(str: string): string { const cleanedStr = str.replace(/^model\./, ''); return cleanedStr @@ -108,7 +151,6 @@ export interface OpenAPIClient { `export type ${toPascalCase(typeName)} = ${processSchema(schema)};\n`, ); }); - return types.join('\n'); }; diff --git a/src/common/routes.ts b/src/common/routes.ts new file mode 100644 index 0000000..09b2117 --- /dev/null +++ b/src/common/routes.ts @@ -0,0 +1,22 @@ +export const routes = { + CreateAccount: '/accounts', + GetMyAccount: '/accounts/me', + AuthenticateAccount: '/auth', + GenerateSignMessage: '/auth/message', + RefreshToken: '/auth/refresh-token', + CreateANewDataAsset: '/data-assets', + GetMyDataAssets: '/data-assets/me', + GetDataAssetByID: '/data-assets/{id}', + UpdateDataAssetByID: '/data-assets/{id}', + DeleteDataAssetByID: '/data-assets/{id}', + UpdateACLItemsToDataAsset: '/data-assets/{id}/acl', + AssignACLItemsToDataAsset: '/data-assets/{id}/acl', + DeleteAssignedRoleByACL: '/data-assets/{id}/acl', + DownloadDataAssetByID: '/data-assets/{id}/download', + ShareDataAssetByID: '/data-assets/{id}/share', + GetDataModels: '/data-models', + CreateDataModel: '/data-models', + GetDataModelsByUser: '/data-models/me', + GetDataModelByID: '/data-models/{id}', + UpdateDataModel: '/data-models/{id}', +}; diff --git a/test/auth.test.ts b/test/auth.test.ts index d7f4b70..59d88d8 100644 --- a/test/auth.test.ts +++ b/test/auth.test.ts @@ -11,6 +11,7 @@ import { mockPost, successMessage, } from './stubs/common.stub'; +import { routes } from '../src/common/routes'; jest.mock('openapi-fetch'); let auth: Auth; @@ -30,7 +31,7 @@ describe('Auth Unit Test', () => { const message = await auth.generateSignMessage(); expect(message).toBeDefined(); - expect(mockGet).toHaveBeenCalled(); + expect(mockGet).toHaveBeenCalledWith(routes.GenerateSignMessage); }); it('should throw GTWError for generate sign message', async () => { @@ -38,7 +39,7 @@ describe('Auth Unit Test', () => { await expect(auth.generateSignMessage()).rejects.toThrow(GTWError); expect(mockGet).toHaveBeenCalledWith('/auth/message'); - expect(mockGet).toHaveBeenCalled(); + expect(mockGet).toHaveBeenCalledWith(routes.GenerateSignMessage); }); test('should generate refresh token', async () => { @@ -47,15 +48,14 @@ describe('Auth Unit Test', () => { const message = await auth.generateRefreshToken(); expect(message).toBeDefined(); - expect(mockGet).toHaveBeenCalled(); + expect(mockGet).toHaveBeenCalledWith(routes.RefreshToken); }); it('should throw GTWError for generate refresh token', async () => { mockGet.mockResolvedValue(errorMessage()); await expect(auth.generateRefreshToken()).rejects.toThrow(GTWError); - expect(mockGet).toHaveBeenCalledWith('/auth/refresh-token'); - expect(mockGet).toHaveBeenCalled(); + expect(mockGet).toHaveBeenCalledWith(routes.RefreshToken); }); test('should login using etherum wallet', async () => { @@ -64,7 +64,9 @@ describe('Auth Unit Test', () => { const message = await auth.login(authDetails()); expect(message).toBeDefined(); - expect(mockPost).toHaveBeenCalled(); + expect(mockPost).toHaveBeenCalledWith(routes.AuthenticateAccount, { + body: authDetails(), + }); }); test('should login using solana wallet', async () => { @@ -73,16 +75,17 @@ describe('Auth Unit Test', () => { const message = await auth.login(authSolanaDetails()); expect(message).toBeDefined(); - expect(mockPost).toHaveBeenCalled(); + expect(mockPost).toHaveBeenCalledWith(routes.AuthenticateAccount, { + body: authDetails(), + }); }); it('should throw GTWError for login', async () => { mockPost.mockResolvedValue(errorMessage()); await expect(auth.login(authDetails())).rejects.toThrow(GTWError); - expect(mockPost).toHaveBeenCalledWith('/auth', { + expect(mockPost).toHaveBeenCalledWith(routes.AuthenticateAccount, { body: authDetails(), }); - expect(mockPost).toHaveBeenCalled(); }); }); diff --git a/test/data-asset.test.ts b/test/data-asset.test.ts index 1a35adb..ec07a39 100644 --- a/test/data-asset.test.ts +++ b/test/data-asset.test.ts @@ -1,5 +1,6 @@ import createClient from 'openapi-fetch'; import { + bodyStub, errorMessage, ID, linksStub, @@ -9,6 +10,7 @@ import { mockGet, mockPost, mockPut, + paramsStub, successMessage, } from './stubs/common.stub'; import { DataAsset } from '../src/modules/data-asset/data-asset'; @@ -20,6 +22,7 @@ import { blobStub, dataAssetStub, } from './stubs/data-asset.stub'; +import { routes } from '../src/common/routes'; jest.mock('openapi-fetch'); @@ -43,7 +46,10 @@ describe('Data Assets Test', () => { const pdaId = await dataAsset.createClaimBasedDataAsset(); expect(pdaId).toBeDefined(); - expect(mockPost).toHaveBeenCalled(); + expect(mockPost).toHaveBeenCalledWith( + routes.CreateANewDataAsset, + bodyStub(), + ); }); it('should throw GTWError for create claim based data asset', async () => { @@ -52,7 +58,10 @@ describe('Data Assets Test', () => { await expect(dataAsset.createClaimBasedDataAsset()).rejects.toThrow( GTWError, ); - expect(mockPost).toHaveBeenCalled(); + expect(mockPost).toHaveBeenCalledWith( + routes.CreateANewDataAsset, + bodyStub(), + ); }); it('should get my data assets', async () => { @@ -66,7 +75,7 @@ describe('Data Assets Test', () => { expect(data).toBeDefined(); expect(data.data.length).toBeGreaterThan(0); - expect(mockGet).toHaveBeenCalled(); + expect(mockGet).toHaveBeenCalledWith(routes.GetMyDataAssets, paramsStub()); }); it('should throw GTWError for get my data assets', async () => { @@ -74,7 +83,7 @@ describe('Data Assets Test', () => { await expect(dataAsset.getMyDataAssets()).rejects.toThrow(GTWError); - expect(mockGet).toHaveBeenCalled(); + expect(mockGet).toHaveBeenCalledWith(routes.GetMyDataAssets, paramsStub()); }); it('should get data asset by id', async () => { @@ -87,7 +96,10 @@ describe('Data Assets Test', () => { const data = await dataAsset.getDataAssetById(ID); expect(data).toBeDefined(); - expect(mockGet).toHaveBeenCalled(); + expect(mockGet).toHaveBeenCalledWith( + routes.GetDataAssetByID, + paramsStub({ params: { path: { id: 1 } } }), + ); }); it('should throw GTWError for get data asset by id', async () => { @@ -95,7 +107,10 @@ describe('Data Assets Test', () => { await expect(dataAsset.getDataAssetById(ID)).rejects.toThrow(GTWError); - expect(mockGet).toHaveBeenCalled(); + expect(mockGet).toHaveBeenCalledWith( + routes.GetDataAssetByID, + paramsStub({ params: { path: { id: 1 } } }), + ); }); it('should update data asset', async () => { @@ -104,14 +119,20 @@ describe('Data Assets Test', () => { const updatedDataAsset = await dataAsset.updateDataAsset(ID); expect(updatedDataAsset).toBeDefined(); - expect(mockPut).toHaveBeenCalled(); + expect(mockPut).toHaveBeenCalledWith(routes.UpdateDataAssetByID, { + body: {}, + params: paramsStub({ params: { path: { id: 1 } } }).params, + }); }); it('should throw GTWError for update data asset', async () => { mockPut.mockResolvedValue(errorMessage()); await expect(dataAsset.updateDataAsset(ID)).rejects.toThrow(GTWError); - expect(mockPut).toHaveBeenCalled(); + expect(mockPut).toHaveBeenCalledWith(routes.UpdateDataAssetByID, { + body: {}, + params: paramsStub({ params: { path: { id: 1 } } }).params, + }); }); it('should delete data asset by id', async () => { @@ -120,14 +141,20 @@ describe('Data Assets Test', () => { const deleteDataAsset = await dataAsset.deleteDataAsset(ID); expect(deleteDataAsset).toBeDefined(); - expect(mockDelete).toHaveBeenCalled(); + expect(mockDelete).toHaveBeenCalledWith( + routes.DeleteDataAssetByID, + paramsStub({ params: { path: { id: 1 } } }), + ); }); it('should throw GTWError for delete data asset by id', async () => { mockDelete.mockResolvedValue(errorMessage()); await expect(dataAsset.deleteDataAsset(ID)).rejects.toThrow(GTWError); - expect(mockDelete).toHaveBeenCalled(); + expect(mockDelete).toHaveBeenCalledWith( + routes.DeleteDataAssetByID, + paramsStub({ params: { path: { id: 1 } } }), + ); }); it('should update acl', async () => { @@ -136,7 +163,10 @@ describe('Data Assets Test', () => { const aclList = await dataAsset.updateACL(ID, [aclListStub()]); expect(aclList).toBeDefined(); - expect(mockPut).toHaveBeenCalled(); + expect(mockPut).toHaveBeenCalledWith(routes.UpdateACLItemsToDataAsset, { + body: bodyStub({ body: [aclListStub()] }).body, + params: paramsStub({ params: { path: { id: 1 } } }).params, + }); }); it('should throw GTWError for updating acl', async () => { @@ -145,7 +175,10 @@ describe('Data Assets Test', () => { await expect(dataAsset.updateACL(ID, [aclListStub()])).rejects.toThrow( GTWError, ); - expect(mockPut).toHaveBeenCalled(); + expect(mockPut).toHaveBeenCalledWith(routes.UpdateACLItemsToDataAsset, { + body: bodyStub({ body: [aclListStub()] }).body, + params: paramsStub({ params: { path: { id: 1 } } }).params, + }); }); it('should override acl', async () => { @@ -154,7 +187,10 @@ describe('Data Assets Test', () => { const aclList = await dataAsset.overrideACL(ID, [aclListStub()]); expect(aclList).toBeDefined(); - expect(mockPost).toHaveBeenCalled(); + expect(mockPost).toHaveBeenCalledWith(routes.AssignACLItemsToDataAsset, { + body: bodyStub({ body: [aclListStub()] }).body, + params: paramsStub({ params: { path: { id: 1 } } }).params, + }); }); it('should throw GTWError for override acl', async () => { @@ -163,7 +199,10 @@ describe('Data Assets Test', () => { await expect(dataAsset.overrideACL(ID, [aclListStub()])).rejects.toThrow( GTWError, ); - expect(mockPost).toHaveBeenCalled(); + expect(mockPost).toHaveBeenCalledWith(routes.AssignACLItemsToDataAsset, { + body: bodyStub({ body: [aclListStub()] }).body, + params: paramsStub({ params: { path: { id: 1 } } }).params, + }); }); it('should delete acl', async () => { @@ -172,7 +211,10 @@ describe('Data Assets Test', () => { const message = await dataAsset.deleteACL(ID, [aclListStub()]); expect(message).toBeDefined(); - expect(mockDelete).toHaveBeenCalled(); + expect(mockDelete).toHaveBeenCalledWith(routes.DeleteAssignedRoleByACL, { + body: bodyStub({ body: [aclListStub()] }).body, + params: paramsStub({ params: { path: { id: 1 } } }).params, + }); }); it('should throw GTWError for updating acl', async () => { @@ -181,7 +223,10 @@ describe('Data Assets Test', () => { await expect(dataAsset.deleteACL(ID, [aclListStub()])).rejects.toThrow( GTWError, ); - expect(mockDelete).toHaveBeenCalled(); + expect(mockDelete).toHaveBeenCalledWith(routes.DeleteAssignedRoleByACL, { + body: bodyStub({ body: [aclListStub()] }).body, + params: paramsStub({ params: { path: { id: 1 } } }).params, + }); }); it('should share data asset', async () => { diff --git a/test/stubs/common.stub.ts b/test/stubs/common.stub.ts index f48d1ac..48b8244 100644 --- a/test/stubs/common.stub.ts +++ b/test/stubs/common.stub.ts @@ -16,7 +16,7 @@ export const errorMessage = (overrideError?: any) => ({ ...overrideError, }); -export const ID = 8708467049103462; +export const ID = 1; export const authDetails = (overrideAuth?: any) => ({ message: 'test', @@ -34,6 +34,16 @@ export const authSolanaDetails = (overrideAuth?: any) => ({ ...overrideAuth, }); +export const paramsStub = (overrideParams?: any) => ({ + params: { query: { page: 1, page_size: 10 } }, + ...overrideParams, +}); + +export const bodyStub = (overrideBody?: any) => ({ + body: { data: '' }, + ...overrideBody, +}); + export const metaStub = (overrideMetaStub?: HelperMeta) => ({ current_page: 1, items_per_page: 10,