From 03461bcf1f13dbb22161839b38da52775353aae6 Mon Sep 17 00:00:00 2001 From: Pablo Rios Date: Mon, 30 Oct 2023 12:21:32 +0000 Subject: [PATCH 1/5] adding getIndex --- src/metal.ts | 18 ++++++++++++++++++ tests/metal.test.ts | 25 +++++++++++++++++++++++++ 2 files changed, 43 insertions(+) diff --git a/src/metal.ts b/src/metal.ts index 1605691..587980b 100644 --- a/src/metal.ts +++ b/src/metal.ts @@ -579,6 +579,24 @@ export class Metal implements Client { return data } + async getIndex(indexId: string): Promise { + if (!indexId) { + throw new Error('indexId required'); + } + + const data = await request(`${API_URL}/v1/indexes/${indexId}`, { + method: 'GET', + headers: { + 'Content-Type': 'application/json', + 'x-metal-api-key': this.apiKey, + 'x-metal-client-id': this.clientId, + }, + }); + + return data; +} + + async updateIndex(indexId: string, payload: UpdateIndexPayload): Promise { if (!indexId) { throw new Error('Index id is required') diff --git a/tests/metal.test.ts b/tests/metal.test.ts index bcbee32..c0bf6ca 100644 --- a/tests/metal.test.ts +++ b/tests/metal.test.ts @@ -1241,6 +1241,31 @@ describe('Metal', () => { }) }) + + describe('getIndex()', () => { + + it('should error without `indexId`', async () => { + const metal = new Metal(API_KEY, CLIENT_ID); + // @ts-expect-error testing + const result = metal.getIndex(); + await expect(result).rejects.toThrowError('indexId required'); + }); + + it('should get an index by id', async () => { + const mockIndexId = 'test-index'; + + fetchMock.mockImplementationOnce(getMockRes({ id: mockIndexId, name: 'Some Index' })); + + const metal = new Metal(API_KEY, CLIENT_ID); + await metal.getIndex(mockIndexId); + + expect(fetchMock).toHaveBeenCalledWith(`https://api.getmetal.io/v1/indexes/${mockIndexId}`, { + method: 'GET', + headers: HEADERS, + }); + }); + }); + describe('updateIndex()', () => { it('should update an index with payload', async () => { const mockIndexId = 'test_index_id' From 770c5b1a4118f5ee057673839f88768f4cf86a5a Mon Sep 17 00:00:00 2001 From: Pablo Rios Date: Mon, 30 Oct 2023 14:30:05 +0000 Subject: [PATCH 2/5] adding updateApp --- src/metal.ts | 39 +++++++++++++++++++++++++++++++++++++ src/types.ts | 5 +++++ tests/metal.test.ts | 47 +++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 91 insertions(+) diff --git a/src/metal.ts b/src/metal.ts index 587980b..734747c 100644 --- a/src/metal.ts +++ b/src/metal.ts @@ -23,6 +23,7 @@ import { type AddDataEntityPayload, type UpdateIndexPayload, type CreateAppPayload, + type UpdateAppPayload, } from './types' export class Metal implements Client { @@ -689,6 +690,44 @@ export class Metal implements Client { return data } + + + async updateApp(appId: string, payload: UpdateAppPayload): Promise { + if (!appId) { + throw new Error('App id is required'); + } + + if (payload.indexes && payload.indexes.length !== 1) { + throw new Error('at this time, only one index can be added to an app'); + } + + const body: UpdateAppPayload = { + ...payload, + } + + const data = await fetch(`${API_URL}/v1/apps/${appId}`, { + method: 'PUT', + body: JSON.stringify(body), + headers: { + 'Content-Type': 'application/json', + 'x-metal-api-key': this.apiKey, + 'x-metal-client-id': this.clientId, + }, + }); + + if (!data.ok) { + const errorData = await data.json(); + throw new Error(errorData.message || 'Failed to update app'); + } + + return data + } + + + } + + + export default Metal diff --git a/src/types.ts b/src/types.ts index b7150dc..1848586 100644 --- a/src/types.ts +++ b/src/types.ts @@ -182,3 +182,8 @@ export interface UpdateIndexPayload { export interface CreateAppPayload { name: string } + +export interface UpdateAppPayload { + name?: string + indexes?: string[] +} diff --git a/tests/metal.test.ts b/tests/metal.test.ts index c0bf6ca..1e256d7 100644 --- a/tests/metal.test.ts +++ b/tests/metal.test.ts @@ -1431,5 +1431,52 @@ describe('Metal', () => { ) }) }) + + describe('updateApp()', () => { + + it('should update an app with payload', async () => { + const mockAppId = 'test_app_id'; + + const payload = { + name: 'UpdatedAppName', + indexes: ['indexId'] + }; + + const metal = new Metal(API_KEY, CLIENT_ID); + + fetchMock.mockResolvedValueOnce( + new Response('', { + status: 200, + }) + ); + + await metal.updateApp(mockAppId, payload); + + expect(fetchMock).toHaveBeenCalledTimes(1); + expect(fetchMock.mock.calls[0][0]).toBe('https://api.getmetal.io/v1/apps/test_app_id'); + expect(fetchMock.mock.calls[0][1]).toEqual({ + method: 'PUT', + body: JSON.stringify(payload), + headers: HEADERS, + }); + }); + + it('should throw an error if indexes array length is not 1', async () => { + const mockAppId = 'test_app_id'; + + const payloadWithInvalidIndexes = { + name: 'UpdatedAppName', + indexes: ['index1', 'index2'] + }; + + const metal = new Metal(API_KEY, CLIENT_ID); + + await expect(metal.updateApp(mockAppId, payloadWithInvalidIndexes)).rejects.toThrow('at this time, only one index can be added to an app'); + }); + + }); }) + + + }) From 01f530edf0ddfc8c55daf0539991bcc9955303bf Mon Sep 17 00:00:00 2001 From: Pablo Rios Date: Mon, 30 Oct 2023 14:32:01 +0000 Subject: [PATCH 3/5] runnin prettier --- src/metal.ts | 38 +++++++++-------------- tests/metal.test.ts | 74 ++++++++++++++++++++++----------------------- 2 files changed, 51 insertions(+), 61 deletions(-) diff --git a/src/metal.ts b/src/metal.ts index 734747c..5e762ed 100644 --- a/src/metal.ts +++ b/src/metal.ts @@ -582,21 +582,20 @@ export class Metal implements Client { async getIndex(indexId: string): Promise { if (!indexId) { - throw new Error('indexId required'); + throw new Error('indexId required') } const data = await request(`${API_URL}/v1/indexes/${indexId}`, { - method: 'GET', - headers: { - 'Content-Type': 'application/json', - 'x-metal-api-key': this.apiKey, - 'x-metal-client-id': this.clientId, - }, - }); - - return data; -} + method: 'GET', + headers: { + 'Content-Type': 'application/json', + 'x-metal-api-key': this.apiKey, + 'x-metal-client-id': this.clientId, + }, + }) + return data + } async updateIndex(indexId: string, payload: UpdateIndexPayload): Promise { if (!indexId) { @@ -691,14 +690,13 @@ export class Metal implements Client { return data } - async updateApp(appId: string, payload: UpdateAppPayload): Promise { if (!appId) { - throw new Error('App id is required'); + throw new Error('App id is required') } if (payload.indexes && payload.indexes.length !== 1) { - throw new Error('at this time, only one index can be added to an app'); + throw new Error('at this time, only one index can be added to an app') } const body: UpdateAppPayload = { @@ -713,21 +711,15 @@ export class Metal implements Client { 'x-metal-api-key': this.apiKey, 'x-metal-client-id': this.clientId, }, - }); + }) if (!data.ok) { - const errorData = await data.json(); - throw new Error(errorData.message || 'Failed to update app'); + const errorData = await data.json() + throw new Error(errorData.message || 'Failed to update app') } return data } - - - } - - - export default Metal diff --git a/tests/metal.test.ts b/tests/metal.test.ts index 1e256d7..c956436 100644 --- a/tests/metal.test.ts +++ b/tests/metal.test.ts @@ -1241,30 +1241,31 @@ describe('Metal', () => { }) }) - describe('getIndex()', () => { - it('should error without `indexId`', async () => { - const metal = new Metal(API_KEY, CLIENT_ID); + const metal = new Metal(API_KEY, CLIENT_ID) // @ts-expect-error testing - const result = metal.getIndex(); - await expect(result).rejects.toThrowError('indexId required'); - }); + const result = metal.getIndex() + await expect(result).rejects.toThrowError('indexId required') + }) it('should get an index by id', async () => { - const mockIndexId = 'test-index'; + const mockIndexId = 'test-index' - fetchMock.mockImplementationOnce(getMockRes({ id: mockIndexId, name: 'Some Index' })); + fetchMock.mockImplementationOnce(getMockRes({ id: mockIndexId, name: 'Some Index' })) - const metal = new Metal(API_KEY, CLIENT_ID); - await metal.getIndex(mockIndexId); + const metal = new Metal(API_KEY, CLIENT_ID) + await metal.getIndex(mockIndexId) - expect(fetchMock).toHaveBeenCalledWith(`https://api.getmetal.io/v1/indexes/${mockIndexId}`, { - method: 'GET', - headers: HEADERS, - }); - }); - }); + expect(fetchMock).toHaveBeenCalledWith( + `https://api.getmetal.io/v1/indexes/${mockIndexId}`, + { + method: 'GET', + headers: HEADERS, + } + ) + }) + }) describe('updateIndex()', () => { it('should update an index with payload', async () => { @@ -1433,50 +1434,47 @@ describe('Metal', () => { }) describe('updateApp()', () => { - it('should update an app with payload', async () => { - const mockAppId = 'test_app_id'; + const mockAppId = 'test_app_id' const payload = { name: 'UpdatedAppName', - indexes: ['indexId'] - }; + indexes: ['indexId'], + } - const metal = new Metal(API_KEY, CLIENT_ID); + const metal = new Metal(API_KEY, CLIENT_ID) fetchMock.mockResolvedValueOnce( new Response('', { status: 200, }) - ); + ) - await metal.updateApp(mockAppId, payload); + await metal.updateApp(mockAppId, payload) - expect(fetchMock).toHaveBeenCalledTimes(1); - expect(fetchMock.mock.calls[0][0]).toBe('https://api.getmetal.io/v1/apps/test_app_id'); + expect(fetchMock).toHaveBeenCalledTimes(1) + expect(fetchMock.mock.calls[0][0]).toBe('https://api.getmetal.io/v1/apps/test_app_id') expect(fetchMock.mock.calls[0][1]).toEqual({ method: 'PUT', body: JSON.stringify(payload), headers: HEADERS, - }); - }); + }) + }) it('should throw an error if indexes array length is not 1', async () => { - const mockAppId = 'test_app_id'; + const mockAppId = 'test_app_id' const payloadWithInvalidIndexes = { name: 'UpdatedAppName', - indexes: ['index1', 'index2'] - }; - - const metal = new Metal(API_KEY, CLIENT_ID); + indexes: ['index1', 'index2'], + } - await expect(metal.updateApp(mockAppId, payloadWithInvalidIndexes)).rejects.toThrow('at this time, only one index can be added to an app'); - }); + const metal = new Metal(API_KEY, CLIENT_ID) - }); + await expect(metal.updateApp(mockAppId, payloadWithInvalidIndexes)).rejects.toThrow( + 'at this time, only one index can be added to an app' + ) + }) + }) }) - - - }) From 3f4a217ce7e23b3e3030fbb088faf383088084ed Mon Sep 17 00:00:00 2001 From: Pablo Rios Date: Mon, 30 Oct 2023 14:40:19 +0000 Subject: [PATCH 4/5] 7.7.0 --- package-lock.json | 4 ++-- package.json | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/package-lock.json b/package-lock.json index 80ceb98..4df754e 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "@getmetal/metal-sdk", - "version": "7.6.0", + "version": "7.7.0", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "@getmetal/metal-sdk", - "version": "7.6.0", + "version": "7.7.0", "license": "Apache 2.0", "dependencies": { "mime-types": "^2.1.35" diff --git a/package.json b/package.json index 1537417..e65d1a1 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@getmetal/metal-sdk", - "version": "7.6.0", + "version": "7.7.0", "description": "Metal SDK", "engineStrict": true, "engines": { From d4ba8184ca2060ef5c50de9b47bace7d2de9b11c Mon Sep 17 00:00:00 2001 From: Pablo Rios Date: Tue, 31 Oct 2023 15:23:12 +0000 Subject: [PATCH 5/5] allowing indexes array of 1 for app creation --- src/types.ts | 1 + tests/metal.test.ts | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 33 insertions(+) diff --git a/src/types.ts b/src/types.ts index 1848586..a4832b0 100644 --- a/src/types.ts +++ b/src/types.ts @@ -181,6 +181,7 @@ export interface UpdateIndexPayload { export interface CreateAppPayload { name: string + indexes?: string[] } export interface UpdateAppPayload { diff --git a/tests/metal.test.ts b/tests/metal.test.ts index c956436..f1633ea 100644 --- a/tests/metal.test.ts +++ b/tests/metal.test.ts @@ -1356,6 +1356,38 @@ describe('Metal', () => { }, }) }) + + it('should add an app with an associated index', async () => { + const mockAppName = 'test_app_with_index' + const mockIndexId = 'indexID' + + const payload = { + name: mockAppName, + indexes: [mockIndexId], + } + + const metal = new Metal(API_KEY, CLIENT_ID) + + fetchMock.mockResolvedValueOnce( + new Response('', { + status: 201, + }) + ) + + await metal.addApp(payload) + + expect(fetchMock).toHaveBeenCalledTimes(1) + expect(fetchMock.mock.calls[0][0]).toBe('https://api.getmetal.io/v1/apps') + expect(fetchMock.mock.calls[0][1]).toEqual({ + method: 'POST', + body: JSON.stringify(payload), + headers: { + 'Content-Type': 'application/json', + 'x-metal-api-key': API_KEY, + 'x-metal-client-id': CLIENT_ID, + }, + }) + }) }) describe('getApps()', () => {