Skip to content

Commit

Permalink
Merge pull request #49 from getmetal/update-app-get-index
Browse files Browse the repository at this point in the history
Update app get index
  • Loading branch information
pariosur authored Oct 31, 2023
2 parents 7963ce4 + d4ba818 commit 1887d5d
Show file tree
Hide file tree
Showing 5 changed files with 160 additions and 3 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@getmetal/metal-sdk",
"version": "7.6.0",
"version": "7.7.0",
"description": "Metal SDK",
"engineStrict": true,
"engines": {
Expand Down
49 changes: 49 additions & 0 deletions src/metal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import {
type AddDataEntityPayload,
type UpdateIndexPayload,
type CreateAppPayload,
type UpdateAppPayload,
} from './types'

export class Metal implements Client {
Expand Down Expand Up @@ -579,6 +580,23 @@ export class Metal implements Client {
return data
}

async getIndex(indexId: string): Promise<object> {
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<object> {
if (!indexId) {
throw new Error('Index id is required')
Expand Down Expand Up @@ -671,6 +689,37 @@ export class Metal implements Client {

return data
}

async updateApp(appId: string, payload: UpdateAppPayload): Promise<object> {
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
6 changes: 6 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -181,4 +181,10 @@ export interface UpdateIndexPayload {

export interface CreateAppPayload {
name: string
indexes?: string[]
}

export interface UpdateAppPayload {
name?: string
indexes?: string[]
}
102 changes: 102 additions & 0 deletions tests/metal.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1241,6 +1241,32 @@ 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'
Expand Down Expand Up @@ -1330,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()', () => {
Expand Down Expand Up @@ -1406,5 +1464,49 @@ 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'
)
})
})
})
})

0 comments on commit 1887d5d

Please sign in to comment.